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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::{proof, proof::ContentExt, Result, Url};
use crev_common::{
    self,
    serde::{as_base64, from_base64},
};
use derive_builder::Builder;
use ed25519_dalek::{self, PublicKey, SecretKey};
use failure::format_err;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum IdType {
    #[serde(rename = "crev")]
    Crev,
}

impl fmt::Display for IdType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use self::IdType::*;
        f.write_str(match self {
            Crev => "crev",
        })
    }
}

/// An Id supported by `crev` system
///
/// Right now it's only native CrevID, but in future at least GPG
/// should be supported.
#[derive(Clone, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[serde(tag = "id-type")]
pub enum Id {
    #[serde(rename = "crev")]
    Crev {
        #[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
        id: Vec<u8>,
    },
}

impl fmt::Debug for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Id::Crev { id } => f.write_str(&crev_common::base64_encode(id)),
        }
    }
}

impl fmt::Display for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Id::Crev { id } => f.write_str(&crev_common::base64_encode(id)),
        }
    }
}

impl Id {
    pub fn crevid_from_str(s: &str) -> Result<Self> {
        let bytes = crev_common::base64_decode(s)?;

        Ok(Id::Crev { id: bytes })
    }

    pub fn verify_signature(&self, content: &[u8], sig_str: &str) -> Result<()> {
        match self {
            Id::Crev { id } => {
                let pubkey = ed25519_dalek::PublicKey::from_bytes(&id)?;

                let sig_bytes = crev_common::base64_decode(sig_str)?;
                let signature = ed25519_dalek::Signature::from_bytes(&sig_bytes)?;
                pubkey.verify(&content, &signature)?;
            }
        }

        Ok(())
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            Id::Crev { id } => id.clone(),
        }
    }
}

#[derive(Clone, Debug, Builder, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct PubId {
    #[serde(flatten)]
    pub id: Id,
    #[serde(flatten)]
    pub url: Url,
}

impl PubId {
    pub fn new(id: Id, url: Url) -> Self {
        PubId { id, url }
    }

    pub fn new_from_pubkey(v: Vec<u8>, url: Url) -> Self {
        PubId {
            id: Id::Crev { id: v },
            url,
        }
    }

    pub fn new_crevid_from_base64(s: &str, url: Url) -> Result<Self> {
        let v = crev_common::base64_decode(s)?;
        Ok(PubId {
            id: Id::Crev { id: v },
            url,
        })
    }

    pub fn create_trust_proof<'a>(
        &self,
        ids: impl IntoIterator<Item = &'a PubId>,
        trust_level: proof::trust::TrustLevel,
    ) -> Result<proof::Trust> {
        Ok(proof::TrustBuilder::default()
            .from(self.clone())
            .trust(trust_level)
            .ids(ids.into_iter().cloned().collect())
            .build()
            .map_err(|e| format_err!("{}", e))?)
    }

    pub fn create_package_review_proof(
        &self,
        package: proof::PackageInfo,
        review: proof::review::Review,
        comment: String,
    ) -> Result<proof::review::Package> {
        Ok(proof::review::PackageBuilder::default()
            .from(self.clone())
            .package(package)
            .review(review)
            .comment(comment)
            .build()
            .map_err(|e| format_err!("{}", e))?)
    }
}

/// A `PubId` with the corresponding secret key
#[derive(Debug)]
pub struct OwnId {
    pub id: PubId,
    pub keypair: ed25519_dalek::Keypair,
}

impl AsRef<Id> for OwnId {
    fn as_ref(&self) -> &Id {
        &self.id.id
    }
}

impl AsRef<PubId> for OwnId {
    fn as_ref(&self) -> &PubId {
        &self.id
    }
}

impl OwnId {
    #[allow(clippy::new_ret_no_self)]
    pub fn new(url: Url, sec_key: Vec<u8>) -> Result<Self> {
        let sec_key = SecretKey::from_bytes(&sec_key)?;
        let calculated_pub_key: PublicKey = PublicKey::from(&sec_key);

        Ok(Self {
            id: crate::PubId::new_from_pubkey(calculated_pub_key.as_bytes().to_vec(), url),
            keypair: ed25519_dalek::Keypair {
                secret: sec_key,
                public: calculated_pub_key,
            },
        })
    }

    pub fn sign(&self, msg: &[u8]) -> Vec<u8> {
        self.keypair.sign(msg).to_bytes().to_vec()
    }

    pub fn type_as_string(&self) -> String {
        "crev".into()
    }

    pub fn as_pubid(&self) -> &PubId {
        &self.id
    }

    pub fn generate_for_git_url(url: &str) -> Self {
        Self::generate(Url::new_git(url.to_owned()))
    }

    pub fn generate(url: Url) -> Self {
        let keypair = ed25519_dalek::Keypair::generate(&mut OsRng);
        Self {
            id: PubId::new_from_pubkey(keypair.public.as_bytes().to_vec(), url),
            keypair,
        }
    }

    pub fn create_signed_trust_proof<'a>(
        &self,
        ids: impl IntoIterator<Item = &'a PubId>,
        trust_level: proof::trust::TrustLevel,
    ) -> Result<proof::Proof> {
        self.id.create_trust_proof(ids, trust_level)?.sign_by(&self)
    }
}