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
use std::borrow::Cow;
use anyhow::Context;
use sequoia_openpgp as openpgp;
use openpgp::crypto::mpi;
use openpgp::crypto;
use openpgp::packet::key::PublicParts;
use openpgp::packet::key::UnspecifiedRole;
use openpgp::packet;
use openpgp::types::HashAlgorithm;
use openpgp::types::PublicKeyAlgorithm;
use cryptoki::mechanism::Mechanism;
use cryptoki::mechanism::eddsa::EddsaParams;
use cryptoki::mechanism::eddsa::EddsaSignatureScheme;
use cryptoki::mechanism::dsa;
use cryptoki::object::KeyType;
use cryptoki::session::UserType;
use cryptoki::types::AuthPin;
use crate::Result;
use crate::Prompt;
use crate::key_info::KeyInfo;
use crate::objects::Key;
/// A signer for a PKCS#11 key.
///
/// This implements [`sequoia_openpgp::crypto::Signer`] and hence can
/// be used wherever a Sequoia method needs to create a signature.
///
/// [`sequoia_openpgp::crypto::Signer`]: https://docs.rs/sequoia-openpgp/latest/sequoia_openpgp/crypto/trait.Signer.html
pub struct Signer<'key, 'prompt> {
key: Cow<'key, Key>,
openpgp_key: packet::Key<PublicParts, UnspecifiedRole>,
user_pin: Option<AuthPin>,
prompt: Box<dyn Prompt + 'prompt>,
}
impl<'key, 'prompt> Signer<'key, 'prompt> {
/// Creates a `Signer`.
///
/// This is a bit ugly, because we can't stash a PKCS#11
/// `Session`: a struct that implements `Signer` has to implement
/// `Sync`, but a `Session` is not `Sync`.
///
/// If the user PIN is known, pass it through to avoid prompting
/// the user. (If `user_pin` is wrong, we don't prompt the user,
/// but immediately return an error.) Otherwise, the user is
/// prompted.
pub fn new<P>(prompt: P, key: &'key Key, user_pin: Option<AuthPin>)
-> Result<Self>
where
P: Prompt + 'prompt
{
// Before we prompt for the PIN, make sure we can convert the
// public key to an OpenPGP key.
let openpgp_key = key.to_openpgp_key()
.context("Converting public key to OpenPGP format")?
.role_into_unspecified();
Ok(Self {
key: Cow::Borrowed(key),
openpgp_key,
user_pin,
prompt: Box::new(prompt),
})
}
/// Creates a `Signer`.
///
/// Like [`Signer::new`], but takes ownership of `key`.
pub fn new_owned<P>(prompt: P, key: Key, user_pin: Option<AuthPin>)
-> Result<Self>
where
P: Prompt + 'prompt
{
// Before we prompt for the PIN, make sure we can convert the
// public key to an OpenPGP key.
let openpgp_key = key.to_openpgp_key()
.context("Converting public key to OpenPGP format")?
.role_into_unspecified();
Ok(Self {
key: Cow::Owned(key),
openpgp_key,
user_pin,
prompt: Box::new(prompt),
})
}
}
impl crypto::Signer for Signer<'_, '_> {
fn public(&self) -> &packet::Key<PublicParts, UnspecifiedRole> {
&self.openpgp_key
}
fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
-> Result<mpi::Signature>
{
let (session, _object) = self.key.open_ro_session()?;
// Login as the user.
if let Some(user_pin) = self.user_pin.as_ref() {
// PIN was supplied.
session.login(UserType::User, Some(&user_pin))
.context("Logging in to token")?;
} else {
// Prompt the user for the PIN.
loop {
let user_pin = self.prompt.prompt_to_unlock("the user PIN")?;
let user_pin = String::from_utf8(user_pin.map(|p| p.to_vec()))
.context("Parsing user PIN")?;
let user_pin = AuthPin::new(user_pin.into());
match session.login(UserType::User, Some(&user_pin)) {
Ok(_) => {
self.user_pin = Some(user_pin);
break;
}
Err(err) => {
eprintln!("Error logging in: {}", err);
}
}
}
}
let key_info = KeyInfo::new(&session, &self.key)?;
let composite_info = if let Some(composite)
= self.key.composite_key().as_ref()
{
Some(KeyInfo::new(&session, composite)?)
} else {
None
};
match self.openpgp_key.pk_algo() {
PublicKeyAlgorithm::Ed25519 => {
let key_info = key_info.check_standalone(
&self.openpgp_key, composite_info, KeyType::EC_EDWARDS)?;
// RFC 9580, Section 12.7:
//
// [W]hile [RFC8032] describes different variants of EdDSA,
// OpenPGP uses the "pure" variant (PureEdDSA). The hashing
// that happens with OpenPGP is done as part of the standard
// OpenPGP signature process, and that hash itself is fed as
// the input message to the PureEdDSA algorithm.
//
// https://www.rfc-editor.org/rfc/rfc9580.html#section-12.7
let eddsa_params = EddsaParams::new(EddsaSignatureScheme::Pure);
let raw = session.sign(&Mechanism::Eddsa(eddsa_params),
key_info.private_key.clone(), digest)
.with_context(|| {
format!("Signing data")
})?;
if raw.len() != 64 {
return Err(anyhow::anyhow!(
"Token returned an invalid signature: expected \
64 bytes of signature data, got {} bytes",
raw.len()));
}
Ok(mpi::Signature::Ed25519 {
s: raw.try_into().expect("checked length above"),
})
},
PublicKeyAlgorithm::Ed448 => {
let key_info = key_info.check_standalone(
&self.openpgp_key, composite_info, KeyType::EC_EDWARDS)?;
// RFC 9580, Section 12.7:
//
// [W]hile [RFC8032] describes different variants of EdDSA,
// OpenPGP uses the "pure" variant (PureEdDSA). The hashing
// that happens with OpenPGP is done as part of the standard
// OpenPGP signature process, and that hash itself is fed as
// the input message to the PureEdDSA algorithm.
//
// As specified in [RFC8032], Ed448 also expects a "context
// string". In OpenPGP, Ed448 is used with the empty string as
// a context string.
//
// https://www.rfc-editor.org/rfc/rfc9580.html#section-12.7
let eddsa_params =
EddsaParams::new(EddsaSignatureScheme::Ed448(&[]));
let raw = session.sign(&Mechanism::Eddsa(eddsa_params),
key_info.private_key.clone(), digest)
.with_context(|| {
format!("Signing data")
})?;
if raw.len() != 114 {
return Err(anyhow::anyhow!(
"Token returned an invalid signature: expected \
114 bytes of signature data, got {} bytes",
raw.len()));
}
Ok(mpi::Signature::Ed448 {
s: raw.try_into().expect("checked length above"),
})
},
// MLDSA65_Ed25519.
PublicKeyAlgorithm::MLDSA65_Ed25519 => {
let (mldsa, eddsa) = key_info.check_composite(
&self.openpgp_key, composite_info,
KeyType::ML_DSA, KeyType::EC_EDWARDS)?;
let eddsa = session.sign(
&Mechanism::Eddsa(EddsaParams::new(
EddsaSignatureScheme::Pure)),
eddsa.private_key, digest)
.context("Generating the Ed25519 signature")?
.try_into()
.map_err(|_| anyhow::anyhow!("signature has wrong size"))?;
let mldsa = session.sign(
&Mechanism::MlDsa(dsa::SignAdditionalContext::new(
dsa::HedgeType::Required, None)),
mldsa.private_key, digest)
.context("Generating the ML-DSA signature")?
.try_into()
.map_err(|_| anyhow::anyhow!("signature has wrong size"))?;
Ok(mpi::Signature::MLDSA65_Ed25519 {
eddsa: Box::new(eddsa),
mldsa: Box::new(mldsa),
})
},
// MLDSA87_Ed448.
PublicKeyAlgorithm::MLDSA87_Ed448 => {
let (mldsa, eddsa) = key_info.check_composite(
&self.openpgp_key, composite_info,
KeyType::ML_DSA, KeyType::EC_EDWARDS)?;
let eddsa = session.sign(
&Mechanism::Eddsa(EddsaParams::new(
EddsaSignatureScheme::Ed448(&[]))),
eddsa.private_key, digest)
.context("Generating the Ed448 signature")?
.try_into()
.map_err(|_| anyhow::anyhow!("signature has wrong size"))?;
let mldsa = session.sign(
&Mechanism::MlDsa(dsa::SignAdditionalContext::new(
dsa::HedgeType::Required, None)),
mldsa.private_key, digest)
.context("Generating the ML-DSA signature")?
.try_into()
.map_err(|_| anyhow::anyhow!("signature has wrong size"))?;
Ok(mpi::Signature::MLDSA87_Ed448 {
eddsa: Box::new(eddsa),
mldsa: Box::new(mldsa),
})
},
// SLH-DSA-128s.
PublicKeyAlgorithm::SLHDSA128s => {
let key_info = key_info.check_standalone(
&self.openpgp_key, composite_info, KeyType::SLH_DSA)?;
let sig = session.sign(
&Mechanism::SlhDsa(dsa::SignAdditionalContext::new(
dsa::HedgeType::Required, None)),
key_info.private_key, digest)?
.try_into()
.map_err(|_| anyhow::anyhow!("signature has wrong size"))?;
Ok(mpi::Signature::SLHDSA128s {
sig,
})
},
// SLH-DSA-128f.
PublicKeyAlgorithm::SLHDSA128f => {
let key_info = key_info.check_standalone(
&self.openpgp_key, composite_info, KeyType::SLH_DSA)?;
let sig = session.sign(
&Mechanism::SlhDsa(dsa::SignAdditionalContext::new(
dsa::HedgeType::Required, None)),
key_info.private_key, digest)?
.try_into()
.map_err(|_| anyhow::anyhow!("signature has wrong size"))?;
Ok(mpi::Signature::SLHDSA128f {
sig,
})
},
// SLH-DSA-256s.
PublicKeyAlgorithm::SLHDSA256s => {
let key_info = key_info.check_standalone(
&self.openpgp_key, composite_info, KeyType::SLH_DSA)?;
let sig = session.sign(
&Mechanism::SlhDsa(dsa::SignAdditionalContext::new(
dsa::HedgeType::Required, None)),
key_info.private_key, digest)?
.try_into()
.map_err(|_| anyhow::anyhow!("signature has wrong size"))?;
Ok(mpi::Signature::SLHDSA256s {
sig,
})
},
// Ed25519Legacy.
PublicKeyAlgorithm::EdDSA => {
let key_info = key_info.check_standalone(
&self.openpgp_key, composite_info, KeyType::EC_EDWARDS)?;
// RFC 9580, Section 12.7:
//
// [W]hile [RFC8032] describes different variants of EdDSA,
// OpenPGP uses the "pure" variant (PureEdDSA). The hashing
// that happens with OpenPGP is done as part of the standard
// OpenPGP signature process, and that hash itself is fed as
// the input message to the PureEdDSA algorithm.
//
// https://www.rfc-editor.org/rfc/rfc9580.html#section-12.7
let eddsa_params = EddsaParams::new(EddsaSignatureScheme::Pure);
let raw = session.sign(&Mechanism::Eddsa(eddsa_params),
key_info.private_key.clone(), digest)
.with_context(|| {
format!("Signing data")
})?;
if raw.len() != 64 {
return Err(anyhow::anyhow!(
"Token returned an invalid signature: expected \
64 bytes of signature data, got {} bytes",
raw.len()));
}
let r = mpi::MPI::new(&raw[..32]);
let s = mpi::MPI::new(&raw[32..]);
Ok(mpi::Signature::EdDSA { r, s, })
},
#[allow(deprecated)]
PublicKeyAlgorithm::RSASign | PublicKeyAlgorithm::RSAEncryptSign => {
let key_info = key_info.check_standalone(
&self.openpgp_key, composite_info, KeyType::RSA)?;
// We use the raw RSA signing mechanism because we
// handle the hashing in the OpenPGP layer. But, that
// means we have to do part of EMSA-PKCS1-v1_5
// ourselves.
// Form the DigestInfo sequence.
const MAX_OID_SIZE: usize = 20;
let mut v = Vec::with_capacity(MAX_OID_SIZE + digest.len());
v.extend(hash_algo.oid()?);
v.extend(digest);
// Then sign that.
let raw = session.sign(&Mechanism::RsaPkcs,
key_info.private_key.clone(),
&v)?;
Ok(mpi::Signature::RSA { s: mpi::MPI::new(&raw[..]) })
},
algo => {
Err(anyhow::anyhow!("{} keys are not supported", algo))
},
}
}
}