jose 0.0.2

A JSON Object Signing and Encryption implementation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! The primitives for working with EdDSA algorithms (`kty`
//! parameter = `OKP`).
//!
//! If you are looking for the other curve types, see the
//! [`ec`](crate::crypto::ec) module, which contains all curves, that do not
//! require an octet key pair as a key.

use alloc::{borrow::Cow, format, string::String, vec::Vec};
use core::{fmt, marker::PhantomData};

use serde::{de::Error as _, Deserialize, Serialize};

use super::backend::{
    interface::{
        self,
        okp::{PrivateKey as _, PublicKey as _},
    },
    Backend,
};
use crate::{
    base64_url::{Base64UrlBytes, SecretBase64UrlBytes},
    crypto::Result,
    jwa, jwk, jws, Base64UrlString,
};

const KTY: &str = "OKP";

type BackendPublicKey = <Backend as interface::Backend>::EdPublicKey;
type BackendPrivateKey = <Backend as interface::Backend>::EdPrivateKey;

/// The curve trait marks all possible curves for EdDSA keys.
///
/// Technically, the implementors of this trait (e.g. [`Ed25519`]) are not
/// curves, but rather the curve + algorithm combination. However, the JWK
/// specification uses the term "curve" for this combination, so we will
/// follow that convention here.
pub trait Curve: sealed::Sealed {
    /// The name of the curve, that is also used in the `crv` parameter of a
    /// JWK.
    const NAME: &'static str;
}

/// The public key using the Ed25519 curve.
pub type Ed25519PublicKey = PublicKey<Ed25519>;

/// The private key using the Ed25519 curve.
pub type Ed25519PrivateKey = PrivateKey<Ed25519>;

/// The signer type using the Ed25519 curve.
pub type Ed25519Signer = Signer<Ed25519>;

/// The verifier type using the Ed25519 curve.
pub type Ed25519Verifier = Verifier<Ed25519>;

/// The public key using the Ed448 curve.
pub type Ed448PublicKey = PublicKey<Ed448>;

/// The private key using the Ed448 curve.
pub type Ed448PrivateKey = PrivateKey<Ed448>;

/// The signer type using the Ed448 curve.
pub type Ed448Signer = Signer<Ed448>;

/// The verifier type using the Ed448 curve.
pub type Ed448Verifier = Verifier<Ed448>;

/// The returned signature from a sign operation.
#[repr(transparent)]
pub struct Signature {
    inner: <BackendPrivateKey as interface::okp::PrivateKey>::Signature,
}

impl From<Signature> for Vec<u8> {
    fn from(value: Signature) -> Self {
        value.as_ref().to_vec()
    }
}

impl AsRef<[u8]> for Signature {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_ref()
    }
}

impl fmt::Debug for Signature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(AsRef::<[u8]>::as_ref(&self.inner), f)
    }
}

/// A public key for EdDSA algorithms.
///
/// The type of algorithm and curve is determined by the
/// `C` type parameter.
#[derive(Clone)]
pub struct PublicKey<C> {
    inner: BackendPublicKey,
    _curve: PhantomData<C>,
}

impl<C: Curve> Eq for PublicKey<C> {}
impl<C: Curve> PartialEq for PublicKey<C> {
    fn eq(&self, other: &Self) -> bool {
        interface::okp::PublicKey::to_bytes(&self.inner)
            == interface::okp::PublicKey::to_bytes(&other.inner)
    }
}

impl<C: Curve> fmt::Debug for PublicKey<C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let bytes = Base64UrlString::encode(interface::okp::PublicKey::to_bytes(&self.inner));
        f.debug_struct("PublicKey")
            .field("curve", &C::NAME)
            .field("x", &bytes)
            .finish()
    }
}

impl<C: Curve> From<PublicKey<C>> for jwk::JsonWebKeyType {
    fn from(value: PublicKey<C>) -> Self {
        C::public_to_jwk_type(value)
    }
}

impl<C: Curve> crate::sealed::Sealed for PublicKey<C> {}
impl<C: Curve> jwk::IntoJsonWebKey for PublicKey<C> {
    type Algorithm = ();
    type Error = crate::crypto::Error;

    fn into_jwk(
        self,
        alg: Option<impl Into<Self::Algorithm>>,
    ) -> Result<jwk::JsonWebKey, Self::Error> {
        let key = C::public_to_jwk_type(self);
        let key = jwk::JsonWebKey::new_with_algorithm(
            key,
            alg.map(|_| jwa::JsonWebSigningAlgorithm::EdDSA.into()),
        );
        Ok(key)
    }
}

impl<C: Curve> jwk::Thumbprint for PublicKey<C> {
    fn thumbprint_prehashed(&self) -> String {
        jwk::thumbprint::serialize_key_thumbprint(self)
    }
}

#[derive(Serialize, Deserialize)]
struct PublicRepr<'a> {
    crv: Cow<'a, str>,
    kty: Cow<'a, str>,
    x: Base64UrlBytes,
}

impl<'de, C: Curve> Deserialize<'de> for PublicKey<C> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = PublicRepr::deserialize(deserializer)?;

        if repr.crv != C::NAME {
            return Err(D::Error::custom(format!(
                "Invalid curve type `{}`. Expected `{}`",
                repr.crv,
                C::NAME
            )));
        }

        if repr.kty != KTY {
            return Err(D::Error::custom(format!(
                "Invalid key type `{}`. Expected `{KTY}`",
                repr.kty,
            )));
        }

        let key =
            interface::okp::PublicKey::new(C::ALGORITHM, repr.x.0).map_err(D::Error::custom)?;
        Ok(Self {
            inner: key,
            _curve: PhantomData,
        })
    }
}

impl<C: Curve> Serialize for PublicKey<C> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let repr = PublicRepr {
            crv: C::NAME.into(),
            kty: KTY.into(),
            x: Base64UrlBytes(interface::okp::PublicKey::to_bytes(&self.inner)),
        };

        repr.serialize(serializer)
    }
}

/// A private key for EdDSA algorithms.
///
/// The type of algorithm and curve is determined by the
/// `C` type parameter.
#[derive(Clone)]
pub struct PrivateKey<C> {
    inner: BackendPrivateKey,
    _curve: PhantomData<C>,
}

impl<C: Curve> PrivateKey<C> {
    /// Generate a new random key pair.
    ///
    /// # Errors
    ///
    /// Returns an [`Err`] if the key generation fails.
    pub fn generate() -> Result<Self> {
        Ok(Self {
            inner: BackendPrivateKey::generate(C::ALGORITHM)?,
            _curve: PhantomData,
        })
    }

    /// Returns the public key of this private key.
    pub fn to_public_key(&self) -> PublicKey<C> {
        PublicKey {
            inner: self.inner.to_public_key(),
            _curve: PhantomData,
        }
    }
}

impl<C: Curve> Eq for PrivateKey<C> {}
impl<C: Curve> PartialEq for PrivateKey<C> {
    fn eq(&self, other: &Self) -> bool {
        self.to_public_key() == other.to_public_key()
    }
}

impl<C: Curve> fmt::Debug for PrivateKey<C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let pub_key = interface::okp::PrivateKey::to_public_key(&self.inner);
        let bytes = Base64UrlString::encode(interface::okp::PublicKey::to_bytes(&pub_key));

        f.debug_struct("PrivateKey")
            .field("curve", &C::NAME)
            .field("x", &bytes)
            .field("d", &"[REDACTED]")
            .finish()
    }
}

impl<C: Curve> From<PrivateKey<C>> for jwk::JsonWebKeyType {
    fn from(value: PrivateKey<C>) -> Self {
        C::private_to_jwk_type(value)
    }
}

impl<C: Curve> crate::sealed::Sealed for PrivateKey<C> {}
impl<C: Curve> jwk::IntoJsonWebKey for PrivateKey<C> {
    type Algorithm = ();
    type Error = crate::crypto::Error;

    fn into_jwk(
        self,
        alg: Option<impl Into<Self::Algorithm>>,
    ) -> Result<jwk::JsonWebKey, Self::Error> {
        let key = C::private_to_jwk_type(self);
        let key = jwk::JsonWebKey::new_with_algorithm(
            key,
            alg.map(|_| jwa::JsonWebSigningAlgorithm::EdDSA.into()),
        );
        Ok(key)
    }
}

impl<C: Curve> jwk::Thumbprint for PrivateKey<C> {
    fn thumbprint_prehashed(&self) -> String {
        self.to_public_key().thumbprint_prehashed()
    }
}

#[derive(Serialize, Deserialize)]
struct PrivateRepr<'a> {
    #[serde(flatten)]
    public: PublicRepr<'a>,
    d: SecretBase64UrlBytes,
}

impl<'de, C: Curve> Deserialize<'de> for PrivateKey<C> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let repr = PrivateRepr::deserialize(deserializer)?;

        let key = interface::okp::PrivateKey::new(C::ALGORITHM, repr.public.x.0, repr.d.0)
            .map_err(D::Error::custom)?;

        Ok(Self {
            inner: key,
            _curve: PhantomData,
        })
    }
}

impl<C: Curve> Serialize for PrivateKey<C> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let pub_key = self.inner.to_public_key();
        let repr = PrivateRepr {
            public: PublicRepr {
                crv: C::NAME.into(),
                kty: KTY.into(),
                x: Base64UrlBytes(interface::okp::PublicKey::to_bytes(&pub_key)),
            },
            d: SecretBase64UrlBytes(interface::okp::PrivateKey::to_bytes(&self.inner)),
        };

        repr.serialize(serializer)
    }
}

/// The [`Signer`](jws::Signer) for EC keys.
pub struct Signer<C> {
    inner: PrivateKey<C>,
}

impl<C: Curve> fmt::Debug for Signer<C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Signer").field("key", &self.inner).finish()
    }
}

impl<C: Curve> jws::Signer<Signature> for Signer<C> {
    fn sign(&mut self, msg: &[u8]) -> Result<Signature> {
        let sig = self.inner.inner.sign(msg)?;
        Ok(Signature { inner: sig })
    }

    fn algorithm(&self) -> jwa::JsonWebSigningAlgorithm {
        jwa::JsonWebSigningAlgorithm::EdDSA
    }
}

impl<C: Curve> jwk::FromKey<PrivateKey<C>> for Signer<C> {
    type Error = jws::InvalidSigningAlgorithmError;

    fn from_key(value: PrivateKey<C>, alg: jwa::JsonWebAlgorithm) -> Result<Self, Self::Error> {
        match alg {
            jwa::JsonWebAlgorithm::Signing(jwa::JsonWebSigningAlgorithm::EdDSA) => {
                Ok(Self { inner: value })
            }
            _ => Err(jws::InvalidSigningAlgorithmError),
        }
    }
}

/// The [`Verifier`](jws::Verifier) for EC keys.
pub struct Verifier<C> {
    inner: PublicKey<C>,
}

impl<C: Curve> fmt::Debug for Verifier<C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Verifier")
            .field("key", &self.inner)
            .finish()
    }
}

impl<C: Curve> jws::Verifier for Verifier<C> {
    fn verify(&mut self, msg: &[u8], signature: &[u8]) -> Result<(), jws::VerifyError> {
        match self.inner.inner.verify(msg, signature) {
            Ok(true) => Ok(()),
            Ok(false) => Err(jws::VerifyError::InvalidSignature),
            Err(e) => Err(jws::VerifyError::CryptoBackend(e)),
        }
    }
}

impl<C: Curve> jwk::FromKey<PublicKey<C>> for Verifier<C> {
    type Error = jws::InvalidSigningAlgorithmError;

    fn from_key(value: PublicKey<C>, alg: jwa::JsonWebAlgorithm) -> Result<Self, Self::Error> {
        match alg {
            jwa::JsonWebAlgorithm::Signing(jwa::JsonWebSigningAlgorithm::EdDSA) => {
                Ok(Self { inner: value })
            }
            _ => Err(jws::InvalidSigningAlgorithmError),
        }
    }
}

impl<C: Curve> jwk::FromKey<PrivateKey<C>> for Verifier<C> {
    type Error = jws::InvalidSigningAlgorithmError;

    #[inline]
    fn from_key(value: PrivateKey<C>, alg: jwa::JsonWebAlgorithm) -> Result<Self, Self::Error> {
        Self::from_key(value.to_public_key(), alg)
    }
}

macro_rules! impl_curve {
    ($(
        $(#[$doc:meta])*
        $curve:ident {
            name: $curve_name:literal,
        }
    ),*$(,)?) => { $(
        $(#[$doc])*
        #[derive(Debug, Clone, Copy)]
        pub enum $curve {}

        impl Curve for $curve {
            const NAME: &'static str = $curve_name;
        }

        impl sealed::Sealed for $curve {
            #[expect(private_interfaces)]
            const ALGORITHM: interface::okp::CurveAlgorithm = interface::okp::CurveAlgorithm::$curve;

            fn public_to_jwk_type(key: PublicKey<Self>) -> jwk::JsonWebKeyType {
                jwk::JsonWebKeyType::Asymmetric(alloc::boxed::Box::new(jwk::AsymmetricJsonWebKey::Public(
                    jwk::Public::Okp(jwk::OkpPublic::$curve(key)),
                )))
            }

            fn private_to_jwk_type(key: PrivateKey<Self>) -> jwk::JsonWebKeyType {
                jwk::JsonWebKeyType::Asymmetric(alloc::boxed::Box::new(jwk::AsymmetricJsonWebKey::Private(
                    jwk::Private::Okp(jwk::OkpPrivate::$curve(key)),
                )))
            }
        }
    )* };
}

impl_curve!(
    /// The Ed25519 curve.
    Ed25519 {
        name: "Ed25519",
    },

    /// The Ed448 curve.
    Ed448 {
        name: "Ed448",
    },
);

mod sealed {
    use crate::{crypto::backend::interface, jwk::JsonWebKeyType};

    pub trait Sealed: Sized {
        #[expect(private_interfaces)]
        const ALGORITHM: interface::okp::CurveAlgorithm;

        fn public_to_jwk_type(key: super::PublicKey<Self>) -> JsonWebKeyType;

        fn private_to_jwk_type(key: super::PrivateKey<Self>) -> JsonWebKeyType;
    }
}