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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use super::*;
use crypto::encryption::EncryptionKeypair;

use sodiumoxide;

trait Export<I, O> {
    fn export(&self, id: &I) -> SdaClientResult<Option<O>>;
}

pub trait SignExport<I, O>
    where O: Clone + ::std::fmt::Debug + PartialEq + ::serde::Serialize + ::serde::Deserialize
{
    fn sign_export(&self, signer: &Agent, id: &I) -> SdaClientResult<Option<Signed<O>>>;
}

pub trait SignatureVerification<O> {
    fn signature_is_valid(&self, object: &O) -> SdaClientResult<bool>;
}

/// Description of a signature keypair.
#[derive(Debug, Serialize, Deserialize)]
pub struct SignatureKeypair {
    pub vk: VerificationKey,
    pub sk: SigningKey,
}


impl KeyGeneration<VerificationKeyId> for CryptoModule {
    fn new_key(&self) -> SdaClientResult<VerificationKeyId> {
        // generate
        let (vk, sk) = sodiumoxide::crypto::sign::gen_keypair();
        let wrapped_vk = VerificationKey::Sodium(vk.0.into());
        let wrapped_sk = SigningKey::Sodium(sk.0.into());

        // save
        let keypair = SignatureKeypair { vk: wrapped_vk, sk: wrapped_sk };
        let id = VerificationKeyId::random();
        self.keystore.put(&id, &keypair)?;

        Ok(id)
    }
}


impl KeyGeneration<Labelled<VerificationKeyId, VerificationKey>> for CryptoModule {
    fn new_key(&self) -> SdaClientResult<Labelled<VerificationKeyId, VerificationKey>> {
        // generate key
        let key_id: VerificationKeyId = self.new_key()?;

        // export public part, assuming that it is there since we just created it and haven't failed
        let key: VerificationKey = self.export(&key_id)?.expect("Recently created key not found");

        Ok(Labelled {
            id: key_id,
            body: key,
        })
    }
}


impl Export<VerificationKeyId, VerificationKey> for CryptoModule {
    fn export(&self, id: &VerificationKeyId) -> SdaClientResult<Option<VerificationKey>> {
        let keypair: Option<SignatureKeypair> = self.keystore.get(id)?;
        match keypair {
            None => Ok(None),
            Some(keypair) => Ok(Some(keypair.vk))
        }
    }
}


impl SignExport<EncryptionKeyId, Labelled<EncryptionKeyId, EncryptionKey>> for CryptoModule {
    fn sign_export(&self, signer: &Agent, id: &EncryptionKeyId) -> SdaClientResult<Option<Signed<Labelled<EncryptionKeyId, EncryptionKey>>>> {
        // message
        let encryption_keypair: Option<EncryptionKeypair> = self.keystore.get(id)?;
        let message_to_be_signed = match encryption_keypair {
            None => { return Ok(None) },
            Some(encryption_keypair) => {
                Labelled {
                    id: id.clone(),
                    body: encryption_keypair.ek,
                }
            }
        };
        // signature
        let signature_keypair: Option<SignatureKeypair> = self.keystore.get(&signer.verification_key.id)?;
        let signature = match signature_keypair {
            None => { return Ok(None) },
            Some(SignatureKeypair{ sk: SigningKey::Sodium(raw_sk), .. }) => {
                let sk = sodiumoxide::crypto::sign::SecretKey(*raw_sk);
                let msg = &message_to_be_signed.canonical()?;
                let signature = sodiumoxide::crypto::sign::sign_detached(msg, &sk);
                Signature::Sodium(signature.0.into())
            }
        };
        // wrapper
        Ok(Some(Signed {
            signature: signature,
            signer: signer.id().clone(),
            body: message_to_be_signed,
        }))
    }
}


impl<M> SignatureVerification<Signed<M>> for Agent
    where M: Clone + ::std::fmt::Debug + PartialEq + ::serde::Serialize + ::serde::Deserialize
{
    fn signature_is_valid(&self, signed: &Signed<M>) -> SdaClientResult<bool> {

        // FIXME remember result to avoid running verification more than once

        if signed.signer != self.id {
            Err("Agent differs from claimed signer")?
        }

        let wrapped_sig = &signed.signature;
        let wrapped_vk = &self.verification_key.body;

        match (wrapped_vk, wrapped_sig) {

            (&VerificationKey::Sodium(raw_vk), &Signature::Sodium(raw_sig)) => {
                let sig = sodiumoxide::crypto::sign::Signature(*raw_sig);
                let vk = sodiumoxide::crypto::sign::PublicKey(*raw_vk);
                let msg = signed.body.canonical()?;
                let is_valid = sodiumoxide::crypto::sign::verify_detached(&sig, &*msg, &vk);
                Ok(is_valid)
            }

        }
    }
}