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
|
/*
* Copyright (C) 2018 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2018 René Korthaus
* Copyright (C) 2018 Konstantinos Kolelis
* Rohde & Schwarz Cybersecurity GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "botan_ec_private_key.h"
#include "botan_ec_public_key.h"
#include "botan_util.h"
#include <botan/build.h>
#ifdef BOTAN_HAS_ECDSA
#include <asn1/asn1.h>
#include <asn1/oid.h>
#include <utils/debug.h>
#include <botan/ffi.h>
typedef struct private_botan_ec_private_key_t private_botan_ec_private_key_t;
/**
* Private data of a botan_ec_private_key_t object.
*/
struct private_botan_ec_private_key_t {
/**
* Public interface
*/
botan_ec_private_key_t public;
/**
* Botan ec private key
*/
botan_privkey_t key;
/**
* OID of the curve
*/
int oid;
/**
* Reference count
*/
refcount_t ref;
};
#define SIG_FORMAT_IEEE_1363 0
#define SIG_FORMAT_DER_SEQUENCE 1
/**
* Build a DER encoded signature as in RFC 3279 or as in RFC 4754
*/
static bool build_signature(botan_privkey_t key, const char *hash_and_padding,
int signature_format, chunk_t data,
chunk_t *signature)
{
if (!botan_get_signature(key, hash_and_padding, data, signature))
{
return FALSE;
}
if (signature_format == SIG_FORMAT_DER_SEQUENCE)
{
/* format as ASN.1 sequence of two integers r,s */
chunk_t r = chunk_empty, s = chunk_empty;
chunk_split(*signature, "aa", signature->len / 2, &r,
signature->len / 2, &s);
chunk_free(signature);
*signature = asn1_wrap(ASN1_SEQUENCE, "mm", asn1_integer("m", r),
asn1_integer("m", s));
}
return TRUE;
}
METHOD(private_key_t, sign, bool,
private_botan_ec_private_key_t *this, signature_scheme_t scheme,
void *params, chunk_t data, chunk_t *signature)
{
switch (scheme)
{
/* r||s -> Botan::IEEE_1363, data is the hash already */
case SIGN_ECDSA_WITH_NULL:
return build_signature(this->key, "Raw",
SIG_FORMAT_IEEE_1363, data, signature);
/* DER SEQUENCE of two INTEGERS r,s -> Botan::DER_SEQUENCE */
case SIGN_ECDSA_WITH_SHA1_DER:
return build_signature(this->key, "EMSA1(SHA-1)",
SIG_FORMAT_DER_SEQUENCE, data, signature);
case SIGN_ECDSA_WITH_SHA256_DER:
return build_signature(this->key, "EMSA1(SHA-256)",
SIG_FORMAT_DER_SEQUENCE, data, signature);
case SIGN_ECDSA_WITH_SHA384_DER:
return build_signature(this->key, "EMSA1(SHA-384)",
SIG_FORMAT_DER_SEQUENCE, data, signature);
case SIGN_ECDSA_WITH_SHA512_DER:
return build_signature(this->key, "EMSA1(SHA-512)",
SIG_FORMAT_DER_SEQUENCE, data, signature);
/* r||s -> Botan::IEEE_1363 */
case SIGN_ECDSA_256:
return build_signature(this->key, "EMSA1(SHA-256)",
SIG_FORMAT_IEEE_1363, data, signature);
case SIGN_ECDSA_384:
return build_signature(this->key, "EMSA1(SHA-384)",
SIG_FORMAT_IEEE_1363, data, signature);
case SIGN_ECDSA_521:
return build_signature(this->key, "EMSA1(SHA-512)",
SIG_FORMAT_IEEE_1363, data, signature);
default:
DBG1(DBG_LIB, "signature scheme %N not supported via botan",
signature_scheme_names, scheme);
return FALSE;
}
}
METHOD(private_key_t, decrypt, bool,
private_botan_ec_private_key_t *this, encryption_scheme_t scheme,
chunk_t crypto, chunk_t *plain)
{
DBG1(DBG_LIB, "EC private key decryption not implemented");
return FALSE;
}
METHOD(private_key_t, get_keysize, int,
private_botan_ec_private_key_t *this)
{
botan_mp_t p;
size_t bits = 0;
if (botan_mp_init(&p))
{
return 0;
}
if (botan_privkey_get_field(p, this->key, "p") ||
botan_mp_num_bits(p, &bits))
{
botan_mp_destroy(p);
return 0;
}
botan_mp_destroy(p);
return bits;
}
METHOD(private_key_t, get_type, key_type_t,
private_botan_ec_private_key_t *this)
{
return KEY_ECDSA;
}
METHOD(private_key_t, get_public_key, public_key_t*,
private_botan_ec_private_key_t *this)
{
botan_pubkey_t pubkey;
if (botan_privkey_export_pubkey(&pubkey, this->key))
{
return NULL;
}
return (public_key_t*)botan_ec_public_key_adopt(pubkey);
}
METHOD(private_key_t, get_fingerprint, bool,
private_botan_ec_private_key_t *this, cred_encoding_type_t type,
chunk_t *fingerprint)
{
botan_pubkey_t pubkey;
bool success = FALSE;
/* check the cache before doing the export */
if (lib->encoding->get_cache(lib->encoding, type, this, fingerprint))
{
return TRUE;
}
if (botan_privkey_export_pubkey(&pubkey, this->key))
{
return FALSE;
}
success = botan_get_fingerprint(pubkey, this, type, fingerprint);
botan_pubkey_destroy(pubkey);
return success;
}
METHOD(private_key_t, get_encoding, bool,
private_botan_ec_private_key_t *this, cred_encoding_type_t type,
chunk_t *encoding)
{
return botan_get_privkey_encoding(this->key, type, encoding);
}
METHOD(private_key_t, get_ref, private_key_t*,
private_botan_ec_private_key_t *this)
{
ref_get(&this->ref);
return &this->public.key;
}
METHOD(private_key_t, destroy, void,
private_botan_ec_private_key_t *this)
{
if (ref_put(&this->ref))
{
lib->encoding->clear_cache(lib->encoding, this);
botan_privkey_destroy(this->key);
free(this);
}
}
/**
* Internal generic constructor
*/
static private_botan_ec_private_key_t *create_empty(int oid)
{
private_botan_ec_private_key_t *this;
INIT(this,
.public = {
.key = {
.get_type = _get_type,
.sign = _sign,
.decrypt = _decrypt,
.get_keysize = _get_keysize,
.get_public_key = _get_public_key,
.equals = private_key_equals,
.belongs_to = private_key_belongs_to,
.get_fingerprint = _get_fingerprint,
.has_fingerprint = private_key_has_fingerprint,
.get_encoding = _get_encoding,
.get_ref = _get_ref,
.destroy = _destroy,
},
},
.oid = oid,
.ref = 1,
);
return this;
}
/*
* Described in header
*/
botan_ec_private_key_t *botan_ec_private_key_adopt(botan_privkey_t key, int oid)
{
private_botan_ec_private_key_t *this;
this = create_empty(oid);
this->key = key;
return &this->public;
}
/*
* Described in header
*/
botan_ec_private_key_t *botan_ec_private_key_gen(key_type_t type, va_list args)
{
private_botan_ec_private_key_t *this;
botan_rng_t rng;
u_int key_size = 0;
int oid;
const char *curve;
while (TRUE)
{
switch (va_arg(args, builder_part_t))
{
case BUILD_KEY_SIZE:
key_size = va_arg(args, u_int);
continue;
case BUILD_END:
break;
default:
return NULL;
}
break;
}
if (!key_size)
{
return NULL;
}
switch (key_size)
{
case 256:
curve = "secp256r1";
oid = OID_PRIME256V1;
break;
case 384:
curve = "secp384r1";
oid = OID_SECT384R1;
break;
case 521:
curve = "secp521r1";
oid = OID_SECT521R1;
break;
default:
DBG1(DBG_LIB, "EC private key size %d not supported via botan",
key_size);
return NULL;
}
if (botan_rng_init(&rng, "system"))
{
return NULL;
}
this = create_empty(oid);
if (botan_privkey_create_ecdsa(&this->key, rng, curve))
{
DBG1(DBG_LIB, "EC private key generation failed");
botan_rng_destroy(rng);
free(this);
return NULL;
}
botan_rng_destroy(rng);
return &this->public;
}
/*
* Described in header
*/
botan_ec_private_key_t *botan_ec_private_key_load(key_type_t type, va_list args)
{
private_botan_ec_private_key_t *this;
chunk_t params = chunk_empty, key = chunk_empty;
chunk_t alg_id = chunk_empty, pkcs8 = chunk_empty;
botan_rng_t rng;
int oid = OID_UNKNOWN;
while (TRUE)
{
switch (va_arg(args, builder_part_t))
{
case BUILD_BLOB_ALGID_PARAMS:
params = va_arg(args, chunk_t);
continue;
case BUILD_BLOB_ASN1_DER:
key = va_arg(args, chunk_t);
continue;
case BUILD_END:
break;
default:
return NULL;
}
break;
}
/*
* Botan expects a PKCS#8 private key, so we build one, if necessary.
* RFC 5480 mandates ECParameters as part of the algorithmIdentifier, which
* we should get from e.g. the pkcs8 plugin.
*/
if (params.len != 0 && type == KEY_ECDSA)
{
/* if ECParameters is passed, just use it */
alg_id = asn1_algorithmIdentifier_params(OID_EC_PUBLICKEY,
chunk_clone(params));
if (asn1_unwrap(¶ms, ¶ms) == ASN1_OID)
{
oid = asn1_known_oid(params);
}
}
else
{
/*
* no explicit ECParameters passed, try to extract them from the
* ECPrivateKey structure and create an algorithmIdentifier
*/
chunk_t unwrap = key, inner;
if (asn1_unwrap(&unwrap, &unwrap) == ASN1_SEQUENCE &&
asn1_unwrap(&unwrap, &inner) == ASN1_INTEGER &&
asn1_parse_integer_uint64(inner) == 1 &&
asn1_unwrap(&unwrap, &inner) == ASN1_OCTET_STRING &&
asn1_unwrap(&unwrap, &inner) == ASN1_CONTEXT_C_0 &&
asn1_unwrap(&inner, &inner) == ASN1_OID)
{
oid = asn1_known_oid(inner);
if (oid != OID_UNKNOWN)
{
alg_id = asn1_algorithmIdentifier_params(OID_EC_PUBLICKEY,
asn1_simple_object(ASN1_OID, inner));
}
}
}
if (oid == OID_UNKNOWN)
{
chunk_free(&alg_id);
return NULL;
}
pkcs8 = asn1_wrap(ASN1_SEQUENCE, "mms",
asn1_integer("c", chunk_from_chars(0x00)),
alg_id,
asn1_wrap(ASN1_OCTET_STRING, "c", key));
this = create_empty(oid);
if (botan_rng_init(&rng, "user"))
{
chunk_clear(&pkcs8);
free(this);
return NULL;
}
if (botan_privkey_load(&this->key, rng, pkcs8.ptr, pkcs8.len, NULL))
{
chunk_clear(&pkcs8);
botan_rng_destroy(rng);
free(this);
return NULL;
}
chunk_clear(&pkcs8);
botan_rng_destroy(rng);
return &this->public;
}
#endif
|