gstsodium/
lib.rs

1// Copyright 2019 Jordan Petridis <jordan@centricular.com>
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20//
21// SPDX-License-Identifier: MIT
22#![allow(clippy::non_send_fields_in_send_ty, unused_doc_comments)]
23
24/**
25 * plugin-sodium:
26 *
27 * Since: plugins-rs-0.5.0
28 */
29use gst::glib;
30
31const TYPEFIND_HEADER: &[u8; 12] = b"gst-sodium10";
32// `core::slice::<impl [T]>::len` is not yet stable as a const fn
33// const TYPEFIND_HEADER_SIZE: usize = TYPEFIND_HEADER.len();
34const TYPEFIND_HEADER_SIZE: usize = 12;
35/// Encryted steams use an offset at the start to store the block_size
36/// and the nonce that was used.
37const HEADERS_SIZE: usize =
38    TYPEFIND_HEADER_SIZE + sodiumoxide::crypto::box_::NONCEBYTES + std::mem::size_of::<u32>();
39
40mod decrypter;
41mod encrypter;
42
43fn typefind_register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
44    use gst::{Caps, TypeFind, TypeFindProbability};
45
46    TypeFind::register(
47        Some(plugin),
48        "sodium_encrypted_typefind",
49        gst::Rank::NONE,
50        None,
51        Some(&Caps::builder("application/x-sodium-encrypted").build()),
52        |typefind| {
53            if let Some(data) = typefind.peek(0, TYPEFIND_HEADER_SIZE as u32) {
54                if data == TYPEFIND_HEADER {
55                    typefind.suggest(
56                        TypeFindProbability::Maximum,
57                        &Caps::builder("application/x-sodium-encrypted").build(),
58                    );
59                }
60            }
61        },
62    )
63}
64
65fn plugin_init(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
66    encrypter::register(plugin)?;
67    decrypter::register(plugin)?;
68    typefind_register(plugin)?;
69    Ok(())
70}
71
72gst::plugin_define!(
73    sodium,
74    env!("CARGO_PKG_DESCRIPTION"),
75    plugin_init,
76    concat!(env!("CARGO_PKG_VERSION"), "-", env!("COMMIT_ID")),
77    "MIT/X11",
78    env!("CARGO_PKG_NAME"),
79    env!("CARGO_PKG_NAME"),
80    env!("CARGO_PKG_REPOSITORY"),
81    env!("BUILD_REL_DATE")
82);