co-didcomm 0.8.0

COKIT fork of didcomm-rs — DIDComm messaging v2 implementation with modern crypto and WASM support.
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::convert::{TryFrom, TryInto};

use aes_gcm::{aead::generic_array::GenericArray, Aes256Gcm};
use arrayref::array_ref;
use chacha20poly1305::{
    aead::{Aead, KeyInit},
    XChaCha20Poly1305, XNonce,
};
#[cfg(feature = "resolve")]
use ddoresolver_rs::*;
use rand::{prelude::SliceRandom, Rng};
use sha2::{Digest, Sha256};
use x25519_dalek::{PublicKey, StaticSecret};

use crate::crypto::CryptoAlgorithm;
use crate::{Error, Jwe, Jwk, JwmHeader, KeyAlgorithm, Message, Recipient};

/// Decrypts the content encryption key with a key encryption key.
///
/// # Arguments
///
/// * `jwe` - jwe to decrypt content encryption key for
///
/// * `sk` - recipients private key
///
/// * `recipient` - recipient data from JWE
///
/// * `recipient_public_key` - can be provided if key should not be resolved via recipients DID
pub(crate) fn decrypt_cek(
    jwe: &Jwe,
    sk: &[u8],
    recipient: &Recipient,
    recipient_public_key: Option<Vec<u8>>,
) -> Result<Vec<u8>, Error> {
    trace!("decrypting per-recipient JWE value");
    let alg = jwe
        .get_alg()
        .ok_or_else(|| Error::Generic("missing encryption 'alg' in header".to_string()))?;
    trace!("using algorithm {}", &alg);

    let skid = jwe
        .get_skid()
        .ok_or_else(|| Error::Generic("missing 'skid' in header".to_string()))?;

    // zE (temporary secret)
    let epk = recipient
        .header
        .epk
        .as_ref()
        .ok_or_else(|| Error::Generic("JWM header is missing epk".to_string()))?;
    let epk_public_array: [u8; 32] = base64_url::decode(&epk.x)?
        .try_into()
        .map_err(|_err| Error::Generic("failed to decode epk public key".to_string()))?;
    let epk_public = PublicKey::from(epk_public_array);
    let ss = StaticSecret::from(array_ref!(sk, 0, 32).to_owned()).diffie_hellman(&epk_public);
    let ze = *ss.as_bytes();
    trace!("ze: {:?}", &ze.as_ref());

    // key encryption key
    let kek = generate_kek(&skid, sk, ze, &alg, recipient_public_key)?;
    trace!("kek: {:?}", &kek);

    let iv = recipient
        .header
        .other
        .get("iv")
        .ok_or_else(|| Error::Generic("missing iv in header".to_string()))?;
    let iv_bytes = base64_url::decode(&iv)?;

    let tag = recipient
        .header
        .other
        .get("tag")
        .ok_or_else(|| Error::Generic("missing tag in header".to_string()))?;
    let mut ciphertext_and_tag: Vec<u8> = vec![];
    ciphertext_and_tag.extend(base64_url::decode(&recipient.encrypted_key)?);
    ciphertext_and_tag.extend(&base64_url::decode(&tag)?);

    match alg.as_ref() {
        "ECDH-1PU+XC20PKW" => {
            let nonce = XNonce::from_slice(&iv_bytes);
            let kek_key = chacha20poly1305::Key::from_slice(kek.as_slice());
            let crypter = XChaCha20Poly1305::new(kek_key);

            let cek = crypter
                .decrypt(nonce, ciphertext_and_tag.as_ref())
                .map_err(|e| Error::Generic(e.to_string()))?;

            Ok(cek)
        }
        "ECDH-1PU+A256KW" => {
            let nonce = GenericArray::from_slice(&iv_bytes);
            let kek_key = GenericArray::from_slice(kek.as_slice());
            let crypter = Aes256Gcm::new(kek_key);

            let cek = crypter
                .decrypt(nonce, ciphertext_and_tag.as_ref())
                .map_err(|e| Error::Generic(e.to_string()))?;

            Ok(cek)
        }
        _ => Err(Error::Generic(format!(
            "encryption algorithm '{}' not implemented",
            &alg
        ))),
    }
}

/// Encrypts the content encryption key with a key encryption key.
///
/// # Arguments
///
/// * `message` - message the content encryption key should be encrypted for
///
/// * `sk` - senders private key
///
/// * `dest` - recipient to encrypt cek for
///
/// * `cek` - key used to encrypt content with, will be encrypted per recipient
///
/// * `recipient_public_key` - can be provided if key should not be resolved via recipients DID
pub(crate) fn encrypt_cek(
    message: &Message,
    sk: &[u8],
    dest: &str,
    cek: &[u8; 32],
    recipient_public_key: Option<Vec<u8>>,
) -> Result<Recipient, Error> {
    trace!("creating per-recipient JWE value for {}", &dest);
    let alg = message
        .jwm_header
        .alg
        .as_ref()
        .ok_or_else(|| Error::Generic("missing encryption 'alg' in header".to_string()))?;
    trace!("using algorithm {}", &alg);

    // zE (temporary secret)
    let epk = StaticSecret::random_from_rng(rand_core::OsRng);
    let epk_public = PublicKey::from(&epk);
    let ze = generate_shared_for_recipient(epk.to_bytes(), dest, recipient_public_key.clone())?;
    trace!(
        "ze: {:?} epk_public: {:?}, dest: {:?}",
        &ze.as_ref(),
        epk_public,
        dest
    );

    // key encryption key
    let kek = generate_kek(dest, sk, ze, alg, recipient_public_key)?;
    trace!("kek: {:?}", &kek);

    // preparation for initial vector
    let mut rng = rand::thread_rng();
    let mut iv: Vec<u8>;

    // start building jwk
    let mut jwk = Jwk::new();
    jwk.kid = Some(get_did_from_didurl(dest));

    let sealed_cek_and_tag: Vec<u8> = match alg.as_ref() {
        "ECDH-1PU+A256KW" => {
            jwk.alg = KeyAlgorithm::Ecdh1puA256kw;

            // initial vector
            iv = rng.gen::<[u8; 12]>().to_vec();
            iv.shuffle(&mut rng);

            // encrypt jwk for each recipient using shared secret
            let kek_key = GenericArray::from_slice(kek.as_slice());
            let crypter = Aes256Gcm::new(kek_key);
            trace!("iv: {:?}", &iv);
            let nonce = GenericArray::from_slice(iv.as_ref());
            trace!("nonce: {:?}", &nonce);
            crypter
                .encrypt(nonce, cek.as_ref())
                .map_err(|e| Error::Generic(e.to_string()))?
        }
        "ECDH-1PU+XC20PKW" => {
            jwk.alg = KeyAlgorithm::Ecdh1puXc20pkw;

            // initial vector
            iv = rng.gen::<[u8; 24]>().to_vec();
            iv.shuffle(&mut rng);

            // encrypt jwk for each recipient using shared secret
            let kek_key = chacha20poly1305::Key::from_slice(kek.as_slice());
            let crypter = XChaCha20Poly1305::new(kek_key);
            trace!("iv: {:?}", &iv);
            let nonce = XNonce::from_slice(iv.as_ref());
            trace!("nonce: {:?}", &nonce);
            crypter
                .encrypt(nonce, cek.as_ref())
                .map_err(|e| Error::Generic(e.to_string()))?
        }
        _ => {
            return Err(Error::Generic(format!(
                "encryption algorithm '{}' not implemented",
                &alg
            )));
        }
    };

    let (sealed_cek, tag) = sealed_cek_and_tag.split_at(sealed_cek_and_tag.len() - 16);
    jwk.add_other_header("iv".to_string(), base64_url::encode(&iv));
    jwk.add_other_header("tag".to_string(), base64_url::encode(&tag));

    // finish jwk and build result
    let jwk = jwk.ephemeral(
        "OKP".to_string(),
        "X25519".to_string(),
        base64_url::encode(epk_public.as_bytes()),
        None,
    );
    Ok(Recipient {
        header: jwk,
        encrypted_key: base64_url::encode(sealed_cek),
    })
}

/// Create a `CryptoAlgorithm` by using headers `alg` value.
pub(crate) fn get_crypter_from_header(header: &JwmHeader) -> Result<CryptoAlgorithm, Error> {
    match &header.alg {
        None => Err(Error::JweParseError),
        Some(alg) => alg.try_into(),
    }
}

/// Use given key from `signing_sender_public_key` or if `None`, use key from "kid".
/// `kid` is currently "resolved" by hex-decoding it and using it as the public key.
///
/// # Arguments
///
/// * `signing_sender_public_key` - optional senders public to verify signature
///
/// * `kid` - key reference to senders public key to verify signature
pub(crate) fn get_signing_sender_public_key(
    signing_sender_public_key: Option<&[u8]>,
    kid: Option<&String>,
) -> Result<Vec<u8>, Error> {
    if let Some(key) = signing_sender_public_key {
        return Ok(key.to_vec());
    }
    if let Some(kid) = kid {
        return hex::decode(&kid).map_err(|_| Error::JwsParseError);
    }

    Err(Error::JwsParseError)
}

/// Concatenates key derivation function
fn concat_kdf(
    secret: &[u8],
    alg: &str,
    producer_info: Option<&Vec<u8>>,
    consumer_info: Option<&Vec<u8>>,
) -> Result<Vec<u8>, Error> {
    let mut value = get_length_and_input(alg.as_bytes())?;
    if let Some(vector) = producer_info {
        value.extend(get_length_and_input(vector)?);
    } else {
        value.extend(&[0, 0, 0, 0]);
    }
    if let Some(vector) = consumer_info {
        value.extend(get_length_and_input(vector)?);
    } else {
        value.extend(&[0, 0, 0, 0]);
    }
    // only key length 256 is supported
    value.extend(&[0, 0, 1, 0]);

    // since our key length is 256 we only have to do one round
    let mut to_hash: Vec<u8> = vec![0, 0, 0, 1];
    to_hash.extend(secret);
    to_hash.extend(value);

    let mut hasher = Sha256::new();
    hasher.input(&to_hash);
    let hash_result = hasher.result();
    let hashed = hash_result.as_slice();

    Ok(hashed.to_vec())
}

/// Creates a key used to encrypt/decrypt keys (key encryption key).
///
/// # Arguments
///
/// * `did` - recipient of a message (during encryption) or sender of a message (during decryption)
///
/// * `sk` - senders private key (encryption) or recipient private key (decryption)
///
/// * `ze` - temporary secret zE
///
/// * `alg` - encryption algorithm used
///
/// * `recipient_public_key` - can be provided if key should not be resolved via recipients DID
fn generate_kek(
    did: &str,
    sk: &[u8],
    ze: impl AsRef<[u8]>,
    alg: &str,
    recipient_public_key: Option<Vec<u8>>,
) -> Result<Vec<u8>, Error> {
    // zS (shared for recipient)
    let shared = generate_shared_for_recipient(sk, did, recipient_public_key)?;
    trace!(
        "sk: {:?} shared: {:?} dest: {:?}",
        sk,
        &shared.as_ref(),
        did
    );

    // shared secret
    let shared_secret = [ze.as_ref(), shared.as_ref()].concat();
    trace!("shared_secret: {:?}", &shared_secret);

    // key encryption key
    let kek = concat_kdf(&shared_secret, alg, None, None)?;
    trace!("kek: {:?}", &kek);

    Ok(kek)
}

/// Generates shared secret for a message recipient with a senders public key and a recipients
/// private key. Key is taken from `recipient_public_key`, if it contains a value.
///
/// If `recipient_public_key` is set to `None`, the public key is automatically resolved by using
/// `recipient_did`, which is only possible if `resolve` feature is enabled.
///
/// If recipient_public_key` is set to `None`, and `resolve` feature is disabled, function
/// invocation will return an `Error`.
///
/// # Arguments
///
/// * `sender_private_key` - senders private key, used to generate a shared secret for recipient
///
/// * `recipient_did` - if `recipient_public_key` is `None`, used to resolved recipient public key
///                     if `resolve` feature is enabled
///
/// * `recipient_public_key` - public key, allows to skip public key resolving via
///                            via `recipient_did`
///
#[allow(unused_variables)]
fn generate_shared_for_recipient(
    sender_private_key: impl AsRef<[u8]>,
    recipient_did: &str,
    recipient_public_key: Option<Vec<u8>>,
) -> Result<impl AsRef<[u8]>, Error> {
    let recipient_public = match recipient_public_key {
        Some(value) => value.to_vec(),
        None => {
            #[cfg(feature = "resolve")]
            {
                let document = resolve_any(recipient_did).ok_or(Error::DidResolveFailed)?;
                document
                    .find_public_key_for_curve("X25519")
                    .ok_or(Error::DidResolveFailed)?
            }
            #[cfg(not(feature = "resolve"))]
            {
                return Err(Error::DidResolveFailed);
            }
        }
    };
    let ss = StaticSecret::from(array_ref!(sender_private_key.as_ref(), 0, 32).to_owned())
        .diffie_hellman(&PublicKey::from(
            array_ref!(recipient_public, 0, 32).to_owned(),
        ));

    Ok(*ss.as_bytes())
}

/// Combines length of array and its its length into a vector.
fn get_length_and_input(vector: &[u8]) -> Result<Vec<u8>, Error> {
    let mut collected: Vec<u8> = u32::try_from(vector.len())
        .map_err(|err| Error::Generic(err.to_string()))?
        .to_be_bytes()
        .to_vec();
    collected.extend(vector);
    Ok(collected)
}

/// Extracts key did part from a did url (drops path, query, and segment).
fn get_did_from_didurl(url: &str) -> String {
    let re = regex::Regex::new(
        r"(?x)
        ^
        (?P<did>
            did             # scheme
            :
            [a-z]+          # method
            :
            (?:[a-z]*:)*    # optional subdomains, postfixed with a ':'
            [a-zA-Z0-9.\-_%]+    # method specific identifier
        )
        (?:/[^?\#]*)?        # optional path
        (?:\?[^\#]*)?        # optional query
        (?:\#.*)?            # optional fragment
        $
    ",
    )
    .unwrap();
    match re.captures(url) {
        Some(s) => s
            .name("did")
            .map(|v| v.as_str().to_string())
            .unwrap_or_else(String::default),
        None => String::default(),
    }
}