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
|
/*
* Copyright (C) 2018 Tobias Brunner
* Copyright (C) 2018 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2018 René Korthaus
* 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_rsa_public_key.h"
#include <botan/build.h>
#ifdef BOTAN_HAS_RSA
#include "botan_util.h"
#include <asn1/oid.h>
#include <asn1/asn1.h>
#include <asn1/asn1_parser.h>
#include <utils/debug.h>
#include <botan/ffi.h>
typedef struct private_botan_rsa_public_key_t private_botan_rsa_public_key_t;
/**
* Private data structure with signing context.
*/
struct private_botan_rsa_public_key_t {
/**
* Public interface for this signer
*/
botan_rsa_public_key_t public;
/**
* Botan public key
*/
botan_pubkey_t key;
/**
* Reference counter
*/
refcount_t ref;
};
/**
* Defined in botan_rsa_private_key.c
*/
bool botan_emsa_pss_identifier(rsa_pss_params_t *params, char *id, size_t len);
/**
* Verification of an EMSA PSS signature described in PKCS#1
*/
static bool verify_emsa_pss_signature(private_botan_rsa_public_key_t *this,
rsa_pss_params_t *params, chunk_t data,
chunk_t signature)
{
char hash_and_padding[BUF_LEN];
if (!botan_emsa_pss_identifier(params, hash_and_padding,
sizeof(hash_and_padding)))
{
return FALSE;
}
return botan_verify_signature(this->key, hash_and_padding, data, signature);
}
METHOD(public_key_t, get_type, key_type_t,
private_botan_rsa_public_key_t *this)
{
return KEY_RSA;
}
METHOD(public_key_t, verify, bool,
private_botan_rsa_public_key_t *this, signature_scheme_t scheme,
void *params, chunk_t data, chunk_t signature)
{
switch (scheme)
{
case SIGN_RSA_EMSA_PKCS1_NULL:
return botan_verify_signature(this->key, "EMSA_PKCS1(Raw)", data,
signature);
case SIGN_RSA_EMSA_PKCS1_SHA1:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-1)", data,
signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_224:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-224)",
data, signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_256:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-256)",
data, signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_384:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-384)",
data, signature);
case SIGN_RSA_EMSA_PKCS1_SHA2_512:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-512)",
data, signature);
case SIGN_RSA_EMSA_PKCS1_SHA3_224:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-3(224)",
data, signature);
case SIGN_RSA_EMSA_PKCS1_SHA3_256:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-3(256))",
data, signature);
case SIGN_RSA_EMSA_PKCS1_SHA3_384:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-3(384))",
data, signature);
case SIGN_RSA_EMSA_PKCS1_SHA3_512:
return botan_verify_signature(this->key, "EMSA_PKCS1(SHA-3(512))",
data, signature);
case SIGN_RSA_EMSA_PSS:
return verify_emsa_pss_signature(this, params, data, signature);
default:
DBG1(DBG_LIB, "signature scheme %N not supported via botan",
signature_scheme_names, scheme);
return FALSE;
}
}
METHOD(public_key_t, encrypt, bool,
private_botan_rsa_public_key_t *this, encryption_scheme_t scheme,
chunk_t plain, chunk_t *crypto)
{
botan_pk_op_encrypt_t encrypt_op;
botan_rng_t rng;
const char* padding;
switch (scheme)
{
case ENCRYPT_RSA_PKCS1:
padding = "PKCS1v15";
break;
case ENCRYPT_RSA_OAEP_SHA1:
padding = "OAEP(SHA-1)";
break;
case ENCRYPT_RSA_OAEP_SHA224:
padding = "OAEP(SHA-224)";
break;
case ENCRYPT_RSA_OAEP_SHA256:
padding = "OAEP(SHA-256)";
break;
case ENCRYPT_RSA_OAEP_SHA384:
padding = "OAEP(SHA-384)";
break;
case ENCRYPT_RSA_OAEP_SHA512:
padding = "OAEP(SHA-512)";
break;
default:
DBG1(DBG_LIB, "encryption scheme %N not supported via botan",
encryption_scheme_names, scheme);
return FALSE;
}
if (botan_rng_init(&rng, "user"))
{
return FALSE;
}
if (botan_pk_op_encrypt_create(&encrypt_op, this->key, padding, 0))
{
botan_rng_destroy(rng);
return FALSE;
}
crypto->len = 0;
if (botan_pk_op_encrypt_output_length(encrypt_op, plain.len, &crypto->len))
{
botan_rng_destroy(rng);
botan_pk_op_encrypt_destroy(encrypt_op);
return FALSE;
}
*crypto = chunk_alloc(crypto->len);
if (botan_pk_op_encrypt(encrypt_op, rng, crypto->ptr, &crypto->len,
plain.ptr, plain.len))
{
chunk_free(crypto);
botan_rng_destroy(rng);
botan_pk_op_encrypt_destroy(encrypt_op);
return FALSE;
}
botan_rng_destroy(rng);
botan_pk_op_encrypt_destroy(encrypt_op);
return TRUE;
}
METHOD(public_key_t, get_keysize, int,
private_botan_rsa_public_key_t *this)
{
botan_mp_t n;
size_t bits = 0;
if (botan_mp_init(&n))
{
return 0;
}
if (botan_pubkey_rsa_get_n(n, this->key) ||
botan_mp_num_bits(n, &bits))
{
botan_mp_destroy(n);
return 0;
}
botan_mp_destroy(n);
return bits;
}
METHOD(public_key_t, get_fingerprint, bool,
private_botan_rsa_public_key_t *this, cred_encoding_type_t type,
chunk_t *fp)
{
return botan_get_fingerprint(this->key, this, type, fp);
}
METHOD(public_key_t, get_encoding, bool,
private_botan_rsa_public_key_t *this, cred_encoding_type_t type,
chunk_t *encoding)
{
return botan_get_encoding(this->key, type, encoding);
}
METHOD(public_key_t, get_ref, public_key_t*,
private_botan_rsa_public_key_t *this)
{
ref_get(&this->ref);
return &this->public.key;
}
METHOD(public_key_t, destroy, void,
private_botan_rsa_public_key_t *this)
{
if (ref_put(&this->ref))
{
lib->encoding->clear_cache(lib->encoding, this);
botan_pubkey_destroy(this->key);
free(this);
}
}
/**
* Internal generic constructor
*/
static private_botan_rsa_public_key_t *create_empty()
{
private_botan_rsa_public_key_t *this;
INIT(this,
.public = {
.key = {
.get_type = _get_type,
.verify = _verify,
.encrypt = _encrypt,
.equals = public_key_equals,
.get_keysize = _get_keysize,
.get_fingerprint = _get_fingerprint,
.has_fingerprint = public_key_has_fingerprint,
.get_encoding = _get_encoding,
.get_ref = _get_ref,
.destroy = _destroy,
},
},
.ref = 1,
);
return this;
}
/*
* Described in header
*/
botan_rsa_public_key_t *botan_rsa_public_key_adopt(botan_pubkey_t key)
{
private_botan_rsa_public_key_t *this;
this = create_empty();
this->key = key;
return &this->public;
}
/*
* Described in header
*/
botan_rsa_public_key_t *botan_rsa_public_key_load(key_type_t type,
va_list args)
{
private_botan_rsa_public_key_t *this = NULL;
chunk_t n, e;
n = e = chunk_empty;
while (TRUE)
{
switch (va_arg(args, builder_part_t))
{
case BUILD_RSA_MODULUS:
n = va_arg(args, chunk_t);
continue;
case BUILD_RSA_PUB_EXP:
e = va_arg(args, chunk_t);
continue;
case BUILD_END:
break;
default:
return NULL;
}
break;
}
if (n.ptr && e.ptr && type == KEY_RSA)
{
botan_mp_t mp_n, mp_e;
if (!chunk_to_botan_mp(n, &mp_n))
{
return NULL;
}
if (!chunk_to_botan_mp(e, &mp_e))
{
botan_mp_destroy(mp_n);
return NULL;
}
this = create_empty();
if (botan_pubkey_load_rsa(&this->key, mp_n, mp_e))
{
botan_mp_destroy(mp_n);
botan_mp_destroy(mp_e);
free(this);
return NULL;
}
botan_mp_destroy(mp_n);
botan_mp_destroy(mp_e);
}
return &this->public;
}
#endif
|