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
pub mod der;
mod error;
mod positive;
pub use error::{KeyRejected, Unspecified};
pub use positive::Positive;
use untrusted;
const ELEM_MAX_BITS: usize = 384;
pub const ELEM_MAX_BYTES: usize = (ELEM_MAX_BITS + 7) / 8;
pub const SCALAR_MAX_BYTES: usize = ELEM_MAX_BYTES;
pub const SEED_MAX_BYTES: usize = ELEM_MAX_BYTES;
pub const PUBLIC_KEY_MAX_LEN: usize = 1 + (2 * ELEM_MAX_BYTES);
pub const ED25519_PKCS8_TEMPLATE: Template = Template {
bytes: include_bytes!("ed25519_pkcs8_v2_template.der"),
alg_id_range: core::ops::Range { start: 7, end: 12 },
curve_id_index: 0,
private_key_index: 0x10,
};
pub const EC_PUBLIC_KEY_P256_PKCS8_V1_TEMPLATE: Template = Template {
bytes: include_bytes!("ecPublicKey_p256_pkcs8_v1_template.der"),
alg_id_range: core::ops::Range { start: 8, end: 27 },
curve_id_index: 9,
private_key_index: 0x24,
};
pub const EC_PUBLIC_KEY_P384_PKCS8_V1_TEMPLATE: Template = Template {
bytes: include_bytes!("ecPublicKey_p384_pkcs8_v1_template.der"),
alg_id_range: core::ops::Range { start: 8, end: 24 },
curve_id_index: 9,
private_key_index: 0x23,
};
pub const PKCS8_DOCUMENT_MAX_LEN: usize = 40 + SCALAR_MAX_BYTES + PUBLIC_KEY_MAX_LEN;
pub enum Version {
V1Only,
V1OrV2,
V2Only,
}
pub struct Template {
pub bytes: &'static [u8],
pub alg_id_range: core::ops::Range<usize>,
pub curve_id_index: usize,
pub private_key_index: usize,
}
impl Template {
#[inline]
fn alg_id_value(&self) -> untrusted::Input {
untrusted::Input::from(self.alg_id_value_())
}
fn alg_id_value_(&self) -> &[u8] {
&self.bytes[self.alg_id_range.start..self.alg_id_range.end]
}
#[inline]
pub fn curve_oid(&self) -> untrusted::Input {
untrusted::Input::from(&self.alg_id_value_()[self.curve_id_index..])
}
}
pub fn unwrap_key<'a>(
template: &Template,
version: Version,
input: untrusted::Input<'a>,
) -> Result<(untrusted::Input<'a>, Option<untrusted::Input<'a>>), error::KeyRejected> {
unwrap_key_(template.alg_id_value(), version, input)
}
pub fn unwrap_key_<'a>(
alg_id: untrusted::Input,
version: Version,
input: untrusted::Input<'a>,
) -> Result<(untrusted::Input<'a>, Option<untrusted::Input<'a>>), error::KeyRejected> {
input.read_all(error::KeyRejected::invalid_encoding(), |input| {
der::nested(
input,
der::Tag::Sequence,
error::KeyRejected::invalid_encoding(),
|input| unwrap_key__(alg_id, version, input),
)
})
}
fn unwrap_key__<'a>(
alg_id: untrusted::Input,
version: Version,
input: &mut untrusted::Reader<'a>,
) -> Result<(untrusted::Input<'a>, Option<untrusted::Input<'a>>), error::KeyRejected> {
let actual_version = der::small_nonnegative_integer(input)
.map_err(|error::Unspecified| error::KeyRejected::invalid_encoding())?;
if actual_version > 1 {
return Err(error::KeyRejected::version_not_supported());
};
let actual_alg_id = der::expect_tag_and_get_value(input, der::Tag::Sequence)
.map_err(|error::Unspecified| error::KeyRejected::invalid_encoding())?;
if actual_alg_id != alg_id {
return Err(error::KeyRejected::wrong_algorithm());
}
let require_public_key = match (actual_version, version) {
(0, Version::V1Only) => false,
(0, Version::V1OrV2) => false,
(1, Version::V1OrV2) | (1, Version::V2Only) => true,
_ => {
return Err(error::KeyRejected::version_not_supported());
}
};
let private_key = der::expect_tag_and_get_value(input, der::Tag::OctetString)
.map_err(|error::Unspecified| error::KeyRejected::invalid_encoding())?;
if input.peek(der::Tag::ContextSpecificConstructed0 as u8) {
let _ = der::expect_tag_and_get_value(input, der::Tag::ContextSpecificConstructed0)
.map_err(|error::Unspecified| error::KeyRejected::invalid_encoding())?;
}
let public_key = if require_public_key {
if input.at_end() {
return Err(error::KeyRejected::public_key_is_missing());
}
let public_key = der::nested(
input,
der::Tag::ContextSpecificConstructed1,
error::Unspecified,
der::bit_string_with_no_unused_bits,
)
.map_err(|error::Unspecified| error::KeyRejected::invalid_encoding())?;
Some(public_key)
} else {
None
};
Ok((private_key, public_key))
}
pub struct Document {
bytes: [u8; PKCS8_DOCUMENT_MAX_LEN],
len: usize,
}
impl AsRef<[u8]> for Document {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.bytes[..self.len]
}
}
pub fn wrap_key(template: &Template, private_key: &[u8], public_key: &[u8]) -> Document {
let mut result = Document {
bytes: [0; PKCS8_DOCUMENT_MAX_LEN],
len: template.bytes.len() + private_key.len() + public_key.len(),
};
wrap_key_(
template,
private_key,
public_key,
&mut result.bytes[..result.len],
);
result
}
pub fn wrap_sk(template: &Template, private_key: &[u8]) -> Document {
let mut result = Document {
bytes: [0; PKCS8_DOCUMENT_MAX_LEN],
len: template.bytes.len() + private_key.len(),
};
wrap_sk_(template, private_key, &mut result.bytes[..result.len]);
result
}
fn wrap_key_(template: &Template, private_key: &[u8], public_key: &[u8], bytes: &mut [u8]) {
let (before_private_key, after_private_key) =
template.bytes.split_at(template.private_key_index);
let private_key_end_index = template.private_key_index + private_key.len();
bytes[..template.private_key_index].copy_from_slice(before_private_key);
bytes[template.private_key_index..private_key_end_index].copy_from_slice(&private_key);
bytes[private_key_end_index..(private_key_end_index + after_private_key.len())]
.copy_from_slice(after_private_key);
bytes[(private_key_end_index + after_private_key.len())..].copy_from_slice(public_key);
}
fn wrap_sk_(template: &Template, private_key: &[u8], bytes: &mut [u8]) {
let (before_private_key, after_private_key) =
template.bytes.split_at(template.private_key_index);
let private_key_end_index = template.private_key_index + private_key.len();
bytes[..template.private_key_index].copy_from_slice(before_private_key);
bytes[template.private_key_index..private_key_end_index].copy_from_slice(&private_key);
bytes[private_key_end_index..(private_key_end_index + after_private_key.len())]
.copy_from_slice(after_private_key);
}