2019-03-20 14:36:10 +00:00
|
|
|
// decrypt_example.rs
|
|
|
|
//
|
|
|
|
// Copyright 2019 Jordan Petridis <jordan@centricular.com>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2019-03-20 14:36:10 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
use sodiumoxide::crypto::box_;
|
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fs::File;
|
2021-03-25 18:14:26 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-03-20 14:36:10 +00:00
|
|
|
|
2022-01-02 09:12:41 +00:00
|
|
|
use clap::Parser;
|
2019-03-20 14:36:10 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-01-02 09:12:41 +00:00
|
|
|
#[derive(Parser, Debug)]
|
2022-09-29 06:48:53 +00:00
|
|
|
#[clap(version, author, about = "Decrypt a gstsodium10 file")]
|
2022-01-02 09:12:41 +00:00
|
|
|
struct Args {
|
|
|
|
/// File to encrypt
|
2022-09-29 06:48:53 +00:00
|
|
|
#[clap(short, long)]
|
2022-01-02 09:12:41 +00:00
|
|
|
input: String,
|
|
|
|
|
|
|
|
/// File to decrypt
|
2022-09-29 06:48:53 +00:00
|
|
|
#[clap(short, long)]
|
2022-01-02 09:12:41 +00:00
|
|
|
output: String,
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:36:10 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct Keys {
|
|
|
|
public: box_::PublicKey,
|
|
|
|
private: box_::SecretKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keys {
|
2021-03-25 18:14:26 +00:00
|
|
|
fn from_file(file: &Path) -> Result<Self, Box<dyn Error>> {
|
2022-11-01 08:27:48 +00:00
|
|
|
let f = File::open(file)?;
|
2019-03-20 14:36:10 +00:00
|
|
|
serde_json::from_reader(f).map_err(From::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2022-01-02 09:12:41 +00:00
|
|
|
let args = Args::parse();
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
gst::init()?;
|
2019-07-04 11:59:29 +00:00
|
|
|
gstsodium::plugin_register_static().expect("Failed to register sodium plugin");
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
let receiver_keys = {
|
|
|
|
let mut r = PathBuf::new();
|
|
|
|
r.push(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
r.push("examples");
|
|
|
|
r.push("receiver_sample");
|
|
|
|
r.set_extension("json");
|
|
|
|
r
|
|
|
|
};
|
|
|
|
|
|
|
|
let sender_keys = {
|
|
|
|
let mut s = PathBuf::new();
|
|
|
|
s.push(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
s.push("examples");
|
|
|
|
s.push("sender_sample");
|
|
|
|
s.set_extension("json");
|
|
|
|
s
|
|
|
|
};
|
|
|
|
|
|
|
|
let receiver = &Keys::from_file(&receiver_keys)?;
|
|
|
|
let sender = &Keys::from_file(&sender_keys)?;
|
|
|
|
|
2022-10-19 16:18:43 +00:00
|
|
|
let filesrc = gst::ElementFactory::make("filesrc")
|
|
|
|
.property("location", &args.input)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let decrypter = gst::ElementFactory::make("sodiumdecrypter")
|
|
|
|
.property("receiver-key", glib::Bytes::from_owned(receiver.private.0))
|
|
|
|
.property("sender-key", glib::Bytes::from_owned(sender.public))
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let typefind = gst::ElementFactory::make("typefind").build().unwrap();
|
|
|
|
let filesink = gst::ElementFactory::make("filesink")
|
|
|
|
.property("location", &args.output)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2019-03-20 14:36:10 +00:00
|
|
|
|
2023-02-25 17:30:26 +00:00
|
|
|
let pipeline = gst::Pipeline::with_name("test-pipeline");
|
2019-03-20 14:36:10 +00:00
|
|
|
pipeline
|
2023-03-09 15:30:57 +00:00
|
|
|
.add_many([&filesrc, &decrypter, &typefind, &filesink])
|
2019-03-20 14:36:10 +00:00
|
|
|
.expect("failed to add elements to the pipeline");
|
2023-03-09 15:30:57 +00:00
|
|
|
gst::Element::link_many([&filesrc, &decrypter, &typefind, &filesink])
|
2019-03-20 14:36:10 +00:00
|
|
|
.expect("failed to link the elements");
|
|
|
|
|
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Playing)
|
|
|
|
.expect("Unable to set the pipeline to the `Playing` state");
|
|
|
|
|
2021-04-12 12:49:54 +00:00
|
|
|
let bus = pipeline.bus().unwrap();
|
2021-05-26 09:54:34 +00:00
|
|
|
for msg in bus.iter_timed(gst::ClockTime::NONE) {
|
2019-03-20 14:36:10 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
match msg.view() {
|
|
|
|
MessageView::Error(err) => {
|
|
|
|
eprintln!(
|
|
|
|
"Error received from element {:?}: {}",
|
2021-04-12 12:49:54 +00:00
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error()
|
2019-03-20 14:36:10 +00:00
|
|
|
);
|
2021-04-12 12:49:54 +00:00
|
|
|
eprintln!("Debugging information: {:?}", err.debug());
|
2019-03-20 14:36:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
MessageView::Eos(..) => break,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Null)
|
|
|
|
.expect("Unable to set the pipeline to the `Playing` state");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|