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
|
/*
* Copyright (C) 2017 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "signature_params.h"
#include <asn1/oid.h>
#include <asn1/asn1_parser.h>
/*
* Described in header
*/
bool rsa_pss_params_set_salt_len(rsa_pss_params_t *params, size_t modbits)
{
size_t hash_len;
if (params->salt_len < 0)
{
hash_len = hasher_hash_size(params->hash);
if (!hash_len)
{
return FALSE;
}
switch (params->salt_len)
{
case RSA_PSS_SALT_LEN_DEFAULT:
params->salt_len = hash_len;
break;
case RSA_PSS_SALT_LEN_MAX:
if (modbits)
{
/* emBits = modBits - 1 */
modbits -= 1;
/* emLen = ceil(emBits/8) */
modbits = (modbits+7) / BITS_PER_BYTE;
/* account for 0x01 separator in DB, 0xbc trailing byte */
params->salt_len = max(0, (ssize_t)(modbits - hash_len - 2));
break;
}
return FALSE;
default:
return FALSE;
}
}
return TRUE;
}
/**
* Compare two signature schemes and their parameters
*/
static bool compare_params(signature_params_t *a, signature_params_t *b,
bool strict)
{
if (!a && !b)
{
return TRUE;
}
if (!a || !b)
{
return FALSE;
}
if (a->scheme != b->scheme)
{
return FALSE;
}
if (!a->params && !b->params)
{
return TRUE;
}
if (a->params && b->params)
{
switch (a->scheme)
{
case SIGN_RSA_EMSA_PSS:
{
rsa_pss_params_t *pss_a = a->params, *pss_b = b->params;
return pss_a->hash == pss_b->hash &&
pss_a->mgf1_hash == pss_b->mgf1_hash &&
(!strict || pss_a->salt_len == pss_b->salt_len);
}
default:
break;
}
}
return FALSE;
}
/*
* Described in header
*/
bool signature_params_equal(signature_params_t *a, signature_params_t *b)
{
return compare_params(a, b, TRUE);
}
/*
* Described in header
*/
bool signature_params_comply(signature_params_t *c, signature_params_t *s)
{ /* the salt is variable, so it does not necessarily have to be the same */
return compare_params(c, s, FALSE);
}
/*
* Described in header
*/
signature_params_t *signature_params_clone(signature_params_t *this)
{
signature_params_t *clone;
if (!this)
{
return NULL;
}
INIT(clone,
.scheme = this->scheme,
);
if (this->params)
{
switch (this->scheme)
{
case SIGN_RSA_EMSA_PSS:
{
rsa_pss_params_t *pss, *pss_clone;
pss = this->params;
INIT(pss_clone,
.hash = pss->hash,
.mgf1_hash = pss->mgf1_hash,
.salt_len = pss->salt_len,
/* ignore salt as only used for unit tests */
);
clone->params = pss_clone;
break;
}
default:
break;
}
}
return clone;
}
/*
* Described in header
*/
void signature_params_destroy(signature_params_t *this)
{
if (this)
{
free(this->params);
free(this);
}
}
/*
* Described in header
*/
void signature_params_clear(signature_params_t *this)
{
if (this)
{
free(this->params);
this->params = NULL;
this->scheme = SIGN_UNKNOWN;
}
}
/*
* Described in header
*/
bool signature_params_parse(chunk_t asn1, int level0,
signature_params_t *params)
{
chunk_t parameters = chunk_empty;
int oid;
oid = asn1_parse_algorithmIdentifier(asn1, level0, ¶meters);
params->scheme = signature_scheme_from_oid(oid);
switch (params->scheme)
{
case SIGN_UNKNOWN:
return FALSE;
case SIGN_RSA_EMSA_PSS:
{
rsa_pss_params_t *pss = malloc_thing(rsa_pss_params_t);
if (!rsa_pss_params_parse(parameters, level0+1, pss))
{
DBG1(DBG_IKE, "failed parsing RSASSA-PSS parameters");
free(pss);
return FALSE;
}
params->params = pss;
break;
}
default:
params->params = NULL;
break;
}
return TRUE;
}
/*
* Described in header
*/
bool signature_params_build(signature_params_t *params, chunk_t *asn1)
{
chunk_t parameters = chunk_empty;
int oid;
oid = signature_scheme_to_oid(params->scheme);
if (oid == OID_UNKNOWN)
{
return FALSE;
}
if (params->scheme == SIGN_RSA_EMSA_PSS &&
!rsa_pss_params_build(params->params, ¶meters))
{
return FALSE;
}
if (parameters.len)
{
*asn1 = asn1_algorithmIdentifier_params(oid, parameters);
}
else
{
*asn1 = asn1_algorithmIdentifier(oid);
}
return TRUE;
}
/**
* ASN.1 definition of RSASSA-PSS-params
*/
static const asn1Object_t RSASSAPSSParamsObjects[] = {
{ 0, "RSASSA-PSS-params", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
{ 1, "DEFAULT SHA-1", ASN1_CONTEXT_C_0, ASN1_DEF }, /* 1 */
{ 2, "hashAlgorithm", ASN1_EOC, ASN1_RAW }, /* 2 */
{ 1, "DEFAULT MGF1SHA1", ASN1_CONTEXT_C_1, ASN1_DEF }, /* 3 */
{ 2, "maskGenAlgorithm",ASN1_EOC, ASN1_RAW }, /* 4 */
{ 1, "DEFAULT 20", ASN1_CONTEXT_C_2, ASN1_DEF }, /* 5 */
{ 2, "saltLength", ASN1_INTEGER, ASN1_BODY }, /* 6 */
{ 1, "DEFAULT 1", ASN1_CONTEXT_C_3, ASN1_DEF }, /* 7 */
{ 2, "trailerField", ASN1_INTEGER, ASN1_BODY }, /* 8 */
{ 0, "exit", ASN1_EOC, ASN1_EXIT }
};
#define RSASSA_PSS_PARAMS_HASH_ALG 2
#define RSASSA_PSS_PARAMS_MGF_ALG 4
#define RSASSA_PSS_PARAMS_SALT_LEN 6
#define RSASSA_PSS_PARAMS_TRAILER 8
/*
* Described in header
*/
bool rsa_pss_params_parse(chunk_t asn1, int level0, rsa_pss_params_t *params)
{
asn1_parser_t *parser;
chunk_t object;
int objectID, alg;
bool success = FALSE;
params->hash = HASH_SHA1;
params->mgf1_hash = HASH_SHA1;
params->salt_len = HASH_SIZE_SHA1;
parser = asn1_parser_create(RSASSAPSSParamsObjects, asn1);
parser->set_top_level(parser, level0);
while (parser->iterate(parser, &objectID, &object))
{
u_int level = parser->get_level(parser)+1;
switch (objectID)
{
case RSASSA_PSS_PARAMS_HASH_ALG:
if (object.len)
{
alg = asn1_parse_algorithmIdentifier(object, level, NULL);
params->hash = hasher_algorithm_from_oid(alg);
if (params->hash == HASH_UNKNOWN)
{
goto end;
}
}
break;
case RSASSA_PSS_PARAMS_MGF_ALG:
if (object.len)
{
chunk_t hash = chunk_empty;
alg = asn1_parse_algorithmIdentifier(object, level, &hash);
if (alg != OID_MGF1)
{
goto end;
}
if (!hash.len)
{
goto end;
}
alg = asn1_parse_algorithmIdentifier(hash, level+1, NULL);
params->mgf1_hash = hasher_algorithm_from_oid(alg);
if (params->mgf1_hash == HASH_UNKNOWN)
{
goto end;
}
}
break;
case RSASSA_PSS_PARAMS_SALT_LEN:
if (object.len)
{
params->salt_len = (size_t)asn1_parse_integer_uint64(object);
}
break;
case RSASSA_PSS_PARAMS_TRAILER:
if (object.len && (object.len != 1 || *object.ptr != 1))
{
goto end;
}
break;
default:
break;
}
}
success = parser->success(parser);
end:
parser->destroy(parser);
return success;
}
/*
* Described in header
*/
bool rsa_pss_params_build(rsa_pss_params_t *params, chunk_t *asn1)
{
chunk_t hash = chunk_empty, mgf = chunk_empty, slen = chunk_empty;
int alg;
if (params->hash != HASH_SHA1)
{ /* with SHA-1 we MUST omit the field */
alg = hasher_algorithm_to_oid(params->hash);
if (alg == OID_UNKNOWN)
{
return FALSE;
}
hash = asn1_algorithmIdentifier(alg);
}
if (params->mgf1_hash != HASH_SHA1)
{ /* with MGF1-SHA1 we MUST omit the field */
alg = hasher_algorithm_to_oid(params->mgf1_hash);
if (alg == OID_UNKNOWN)
{
chunk_free(&hash);
return FALSE;
}
mgf = asn1_algorithmIdentifier_params(OID_MGF1,
asn1_algorithmIdentifier(alg));
}
if (params->salt_len < 0)
{
chunk_free(&hash);
chunk_free(&mgf);
return FALSE;
}
else if (params->salt_len != HASH_SIZE_SHA1)
{
slen = asn1_integer("m", asn1_integer_from_uint64(params->salt_len));
}
*asn1 = asn1_wrap(ASN1_SEQUENCE, "mmm",
hash.len ? asn1_wrap(ASN1_CONTEXT_C_0, "m", hash) : chunk_empty,
mgf.len ? asn1_wrap(ASN1_CONTEXT_C_1, "m", mgf) : chunk_empty,
slen.len ? asn1_wrap(ASN1_CONTEXT_C_2, "m", slen) : chunk_empty);
return TRUE;
}
|