summaryrefslogtreecommitdiff
path: root/src/bin/roughenough-kms.rs
blob: a5eaac11dbecc27df317b8ccd7e90b52781b0672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2017-2019 int08h LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//!
//! CLI used to encrypt the Roughenough long-term key using one of the KMS implementations
//!

#[macro_use]
extern crate log;

use clap::{App, Arg};
use roughenough::roughenough_version;

#[cfg(feature = "awskms")]
fn aws_kms(kms_key: &str, plaintext_seed: &[u8]) {
    use roughenough::kms::{AwsKms, EnvelopeEncryption};

    let client = AwsKms::from_arn(kms_key).unwrap();

    match EnvelopeEncryption::encrypt_seed(&client, &plaintext_seed) {
        Ok(encrypted_blob) => {
            println!("kms_protection: \"{}\"", kms_key);
            println!("seed: {}", hex::encode(&encrypted_blob));
        }
        Err(e) => {
            error!("Error: {:?}", e);
        }
    }
}

#[cfg(feature = "gcpkms")]
fn gcp_kms(kms_key: &str, plaintext_seed: &[u8]) {
    use roughenough::kms::{EnvelopeEncryption, GcpKms};

    let client = GcpKms::from_resource_id(kms_key).unwrap();

    match EnvelopeEncryption::encrypt_seed(&client, &plaintext_seed) {
        Ok(encrypted_blob) => {
            println!("kms_protection: \"{}\"", kms_key);
            println!("seed: {}", hex::encode(&encrypted_blob));
        }
        Err(e) => {
            error!("Error: {:?}", e);
        }
    }
}

#[allow(unused_variables)]
pub fn main() {
    use log::Level;

    simple_logger::init_with_level(Level::Info).unwrap();

    let matches = App::new("roughenough-kms")
        .version(roughenough_version().as_ref())
        .long_about("Encrypt a Roughenough server's long-term seed using a KMS")
        .arg(
            Arg::with_name("KEY_ID")
                .short("k")
                .long("kms-key")
                .takes_value(true)
                .required(true)
                .help("Identity of the KMS key to be used"),
        ).arg(
            Arg::with_name("SEED")
                .short("s")
                .long("seed")
                .takes_value(true)
                .required(true)
                .help("32 byte hex seed for the server's long-term identity"),
        ).get_matches();

    let kms_key = matches.value_of("KEY_ID").unwrap();
    let plaintext_seed = matches
        .value_of("SEED")
        .map(|seed| hex::decode(seed).expect("Error parsing seed value"))
        .unwrap();

    if plaintext_seed.len() != 32 {
        error!(
            "Seed must be 32 bytes long; provided seed is {}",
            plaintext_seed.len()
        );
        return;
    }

    if cfg!(feature = "awskms") {
        #[cfg(feature = "awskms")]
        aws_kms(kms_key, &plaintext_seed);
    } else if cfg!(feature = "gcpkms") {
        #[cfg(feature = "gcpkms")]
        gcp_kms(kms_key, &plaintext_seed);
    } else {
        warn!("KMS support was not compiled, nothing to do.");
        warn!("For information on KMS support see the Roughenough documentation.");
    }
}