summaryrefslogtreecommitdiffstats
path: root/adenosine-pds/src/crypto.rs
blob: 1fa6f4c0dc5f044ff66cf90b19a4c074741e7dd0 (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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::P256KeyMaterial;
use anyhow::{anyhow, ensure, Result};
use k256;
use multibase;
use p256;
use p256::ecdsa::signature::{Signer, Verifier};
use std::str::FromStr;
use ucan::builder::UcanBuilder;

// Need to:
//
// - generate new random keypair
// - generate keypair from seed
// - read/write secret keypair (eg, for PDS config loading)
// - sign bytes (and ipld?) using keypair
// - verify signature bytes (and ipld?) using pubkey

const MULTICODE_P256_BYTES: [u8; 2] = [0x80, 0x24];
const MULTICODE_K256_BYTES: [u8; 2] = [0xe7, 0x01];

#[derive(Clone, PartialEq, Eq)]
pub struct KeyPair {
    public: p256::ecdsa::VerifyingKey,
    secret: p256::ecdsa::SigningKey,
}

#[derive(Clone, PartialEq, Eq)]
pub enum PubKey {
    P256(p256::ecdsa::VerifyingKey),
    K256(k256::ecdsa::VerifyingKey),
}

impl KeyPair {
    pub fn new_random() -> Self {
        let signing = p256::ecdsa::SigningKey::random(&mut p256::elliptic_curve::rand_core::OsRng);
        KeyPair {
            public: signing.verifying_key(),
            secret: signing,
        }
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<KeyPair> {
        let signing = p256::ecdsa::SigningKey::from_bytes(bytes)?;
        Ok(KeyPair {
            public: signing.verifying_key(),
            secret: signing,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.secret.to_bytes().to_vec()
    }

    pub fn pubkey(&self) -> PubKey {
        PubKey::P256(self.public.clone())
    }

    pub fn sign_bytes(&self, data: &[u8]) -> String {
        let sig = self.secret.sign(data);
        data_encoding::BASE64URL_NOPAD.encode(&sig.to_vec())
    }

    fn ucan_keymaterial(&self) -> P256KeyMaterial {
        P256KeyMaterial(self.public, Some(self.secret.clone()))
    }

    /// This is currently just an un-validated token; we don't actually verify these.
    pub fn ucan(&self, did: &str) -> Result<String> {
        let key_material = self.ucan_keymaterial();
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()?;
        rt.block_on(build_ucan(key_material, did))
    }

    pub fn to_hex(&self) -> String {
        data_encoding::HEXUPPER.encode(&self.to_bytes())
    }

    pub fn from_hex(hex: &str) -> Result<Self> {
        Ok(Self::from_bytes(
            &data_encoding::HEXUPPER.decode(&hex.as_bytes())?,
        )?)
    }
}

async fn build_ucan(key_material: P256KeyMaterial, did: &str) -> Result<String> {
    let token_string = UcanBuilder::default()
        .issued_by(&key_material)
        .for_audience(did)
        .with_nonce()
        .with_lifetime(60 * 60 * 24 * 90)
        .build()?
        .sign()
        .await?
        .encode()?;
    Ok(token_string)
}

impl PubKey {
    pub fn verify_bytes(&self, data: &[u8], sig: &str) -> Result<()> {
        let sig_bytes = data_encoding::BASE64URL_NOPAD.decode(sig.as_bytes())?;
        // TODO: better way other than this re-encoding?
        let sig_hex = data_encoding::HEXUPPER.encode(&sig_bytes);
        match self {
            PubKey::P256(key) => {
                let sig = p256::ecdsa::Signature::from_str(&sig_hex)?;
                Ok(key.verify(data, &sig)?)
            }
            PubKey::K256(key) => {
                let sig = k256::ecdsa::Signature::from_str(&sig_hex)?;
                Ok(key.verify(data, &sig)?)
            }
        }
    }

    pub fn key_type(&self) -> String {
        match self {
            PubKey::P256(_) => "EcdsaSecp256r1VerificationKey2019",
            PubKey::K256(_) => "EcdsaSecp256k1VerificationKey2019",
        }
        .to_string()
    }

    /// This public verification key encoded as base58btc multibase string
    pub fn to_multibase(&self) -> String {
        let mut bytes: Vec<u8> = vec![];
        match self {
            PubKey::P256(key) => {
                bytes.extend_from_slice(&MULTICODE_P256_BYTES);
                bytes.extend_from_slice(&key.to_encoded_point(true).to_bytes());
            }
            PubKey::K256(key) => {
                bytes.extend_from_slice(&MULTICODE_K256_BYTES);
                bytes.extend_from_slice(&key.to_bytes());
            }
        }
        format!("{}", multibase::encode(multibase::Base::Base58Btc, &bytes))
    }

    pub fn to_did_key(&self) -> String {
        format!("did:key:{}", self.to_multibase())
    }

    pub fn from_did_key(did_key: &str) -> Result<Self> {
        if !did_key.starts_with("did:key:") || did_key.len() < 20 {
            return Err(anyhow!("does not look like a did:key: {}", did_key));
        }
        let (key_type, bytes) = multibase::decode(&did_key[8..])?;
        ensure!(
            key_type == multibase::Base::Base58Btc,
            "base58btc-encoded key"
        );
        // prefix bytes
        let prefix: [u8; 2] = [bytes[0], bytes[1]];
        match prefix {
            MULTICODE_K256_BYTES => Ok(PubKey::K256(k256::ecdsa::VerifyingKey::from_sec1_bytes(
                &bytes[2..],
            )?)),
            MULTICODE_P256_BYTES => Ok(PubKey::P256(p256::ecdsa::VerifyingKey::from_sec1_bytes(
                &bytes[2..],
            )?)),
            _ => Err(anyhow!(
                "key type (multicodec) not handled when parsing DID key: {}",
                did_key
            )),
        }
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            PubKey::P256(key) => key.to_encoded_point(true).to_bytes().to_vec(),
            PubKey::K256(key) => key.to_bytes().to_vec(),
        }
    }

    pub fn ucan_keymaterial(&self) -> P256KeyMaterial {
        match self {
            PubKey::P256(key) => P256KeyMaterial(*key, None),
            PubKey::K256(_key) => unimplemented!(),
        }
    }
}

impl std::fmt::Display for PubKey {
    // TODO: what should this actually be, instead of multibase? the did:key representation?
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.to_multibase())
    }
}

#[test]
fn test_did_secp256k1_p256() {
    // did:key secp256k1 test vectors from W3C
    // https://github.com/w3c-ccg/did-method-key/blob/main/test-vectors/secp256k1.json
    // via atproto repo
    let pairs = vec![
        (
            "9085d2bef69286a6cbb51623c8fa258629945cd55ca705cc4e66700396894e0c",
            "did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme",
        ),
        (
            "f0f4df55a2b3ff13051ea814a8f24ad00f2e469af73c363ac7e9fb999a9072ed",
            "did:key:zQ3shtxV1FrJfhqE1dvxYRcCknWNjHc3c5X1y3ZSoPDi2aur2",
        ),
        (
            "6b0b91287ae3348f8c2f2552d766f30e3604867e34adc37ccbb74a8e6b893e02",
            "did:key:zQ3shZc2QzApp2oymGvQbzP8eKheVshBHbU4ZYjeXqwSKEn6N",
        ),
        (
            "c0a6a7c560d37d7ba81ecee9543721ff48fea3e0fb827d42c1868226540fac15",
            "did:key:zQ3shadCps5JLAHcZiuX5YUtWHHL8ysBJqFLWvjZDKAWUBGzy",
        ),
        (
            "175a232d440be1e0788f25488a73d9416c04b6f924bea6354bf05dd2f1a75133",
            "did:key:zQ3shptjE6JwdkeKN4fcpnYQY3m9Cet3NiHdAfpvSUZBFoKBj",
        ),
    ];

    // test decode/encode did:key
    for (hex, did) in pairs.iter() {
        assert_eq!(did, &PubKey::from_did_key(did).unwrap().to_did_key());
    }

    let p256_dids = vec![
        "did:key:zDnaerx9CtbPJ1q36T5Ln5wYt3MQYeGRG5ehnPAmxcf5mDZpv",
        "did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169",
    ];
    for did in p256_dids {
        assert_eq!(did, &PubKey::from_did_key(did).unwrap().to_did_key());
    }
}

#[test]
fn test_did_plc_examples() {
    // https://atproto.com/specs/did-plc
    let example_dids = vec![
        "did:key:zDnaejYFhgFiVF89LhJ4UipACLKuqo6PteZf8eKDVKeExXUPk",
        "did:key:zDnaeSezF2TgCD71b5DiiFyhHQwKAfsBVqTTHRMvP597Z5Ztn",
        "did:key:zDnaeh9v2RmcMo13Du2d6pjUf5bZwtauYxj3n9dYjw4EZUAR7",
        "did:key:zDnaedvvAsDE6H3BDdBejpx9ve2Tz95cymyCAKF66JbyMh1Lt",
    ];

    for did in example_dids {
        assert_eq!(did, &PubKey::from_did_key(did).unwrap().to_did_key());
    }
}

#[test]
fn test_signing() {
    let msg = b"you have found the secret message";
    let keypair = KeyPair::new_random();
    let sig_str = keypair.sign_bytes(msg);
    keypair.pubkey().verify_bytes(msg, &sig_str).unwrap();

    // and with pubkey that has been serialized/deserialized
    let did_key = keypair.pubkey().to_did_key();
    let pubkey = PubKey::from_did_key(&did_key).unwrap();
    pubkey.verify_bytes(msg, &sig_str).unwrap();
}

#[test]
fn test_keypair_hex() {
    let before = KeyPair::new_random();
    let after = KeyPair::from_hex(&before.to_hex()).unwrap();
    assert!(before == after);
}