/src/gnutls/lib/x509/key_encode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011-2012 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2013-2017 Red Hat |
4 | | * |
5 | | * Author: Nikos Mavrogiannopoulos |
6 | | * |
7 | | * This file is part of GnuTLS. |
8 | | * |
9 | | * The GnuTLS is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public License |
11 | | * as published by the Free Software Foundation; either version 2.1 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, but |
15 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
21 | | * |
22 | | */ |
23 | | |
24 | | #include "gnutls_int.h" |
25 | | #include "errors.h" |
26 | | #include "global.h" |
27 | | #include <libtasn1.h> |
28 | | #include "datum.h" |
29 | | #include "common.h" |
30 | | #include "x509_int.h" |
31 | | #include "num.h" |
32 | | #include "pk.h" |
33 | | #include "mpi.h" |
34 | | #include "ecc.h" |
35 | | |
36 | | static int _gnutls_x509_write_rsa_pubkey(const gnutls_pk_params_st *params, |
37 | | gnutls_datum_t *der); |
38 | | static int _gnutls_x509_write_dsa_params(const gnutls_pk_params_st *params, |
39 | | gnutls_datum_t *der); |
40 | | static int _gnutls_x509_write_dsa_pubkey(const gnutls_pk_params_st *params, |
41 | | gnutls_datum_t *der); |
42 | | static int _gnutls_x509_write_gost_params(const gnutls_pk_params_st *params, |
43 | | gnutls_datum_t *der); |
44 | | static int _gnutls_x509_write_gost_pubkey(const gnutls_pk_params_st *params, |
45 | | gnutls_datum_t *der); |
46 | | |
47 | | /* |
48 | | * some x509 certificate functions that relate to MPI parameter |
49 | | * setting. This writes the BIT STRING subjectPublicKey. |
50 | | * Needs 2 parameters (m,e). |
51 | | * |
52 | | * Allocates the space used to store the DER data. |
53 | | */ |
54 | | static int _gnutls_x509_write_rsa_pubkey(const gnutls_pk_params_st *params, |
55 | | gnutls_datum_t *der) |
56 | 0 | { |
57 | 0 | int result; |
58 | 0 | asn1_node spk = NULL; |
59 | |
|
60 | 0 | der->data = NULL; |
61 | 0 | der->size = 0; |
62 | |
|
63 | 0 | if (params->params_nr < RSA_PUBLIC_PARAMS) { |
64 | 0 | gnutls_assert(); |
65 | 0 | result = GNUTLS_E_INVALID_REQUEST; |
66 | 0 | goto cleanup; |
67 | 0 | } |
68 | | |
69 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
70 | 0 | "GNUTLS.RSAPublicKey", &spk)) != |
71 | 0 | ASN1_SUCCESS) { |
72 | 0 | gnutls_assert(); |
73 | 0 | return _gnutls_asn2err(result); |
74 | 0 | } |
75 | | |
76 | 0 | result = _gnutls_x509_write_int(spk, "modulus", params->params[0], 1); |
77 | 0 | if (result < 0) { |
78 | 0 | gnutls_assert(); |
79 | 0 | goto cleanup; |
80 | 0 | } |
81 | | |
82 | 0 | result = _gnutls_x509_write_int(spk, "publicExponent", |
83 | 0 | params->params[1], 1); |
84 | 0 | if (result < 0) { |
85 | 0 | gnutls_assert(); |
86 | 0 | goto cleanup; |
87 | 0 | } |
88 | | |
89 | 0 | result = _gnutls_x509_der_encode(spk, "", der, 0); |
90 | 0 | if (result < 0) { |
91 | 0 | gnutls_assert(); |
92 | 0 | goto cleanup; |
93 | 0 | } |
94 | | |
95 | 0 | result = 0; |
96 | |
|
97 | 0 | cleanup: |
98 | 0 | asn1_delete_structure(&spk); |
99 | |
|
100 | 0 | return result; |
101 | 0 | } |
102 | | |
103 | | /* |
104 | | * some x509 certificate functions that relate to MPI parameter |
105 | | * setting. This writes an ECPoint. |
106 | | * |
107 | | * Allocates the space used to store the DER data. |
108 | | */ |
109 | | int _gnutls_x509_write_ecc_pubkey(const gnutls_pk_params_st *params, |
110 | | gnutls_datum_t *der) |
111 | 0 | { |
112 | 0 | int result; |
113 | |
|
114 | 0 | der->data = NULL; |
115 | 0 | der->size = 0; |
116 | |
|
117 | 0 | if (params->params_nr < ECC_PUBLIC_PARAMS) |
118 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
119 | | |
120 | 0 | result = _gnutls_ecc_ansi_x962_export(params->curve, |
121 | 0 | params->params[ECC_X], |
122 | 0 | params->params[ECC_Y], /*&out */ |
123 | 0 | der); |
124 | 0 | if (result < 0) |
125 | 0 | return gnutls_assert_val(result); |
126 | | |
127 | 0 | return 0; |
128 | 0 | } |
129 | | |
130 | | /* |
131 | | * some x509 certificate functions that relate to MPI parameter |
132 | | * setting. This writes a raw public key. |
133 | | * |
134 | | * Allocates the space used to store the data. |
135 | | */ |
136 | | int _gnutls_x509_write_eddsa_pubkey(const gnutls_pk_params_st *params, |
137 | | gnutls_datum_t *raw) |
138 | 0 | { |
139 | 0 | int ret; |
140 | |
|
141 | 0 | raw->data = NULL; |
142 | 0 | raw->size = 0; |
143 | |
|
144 | 0 | if (params->raw_pub.size == 0) |
145 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
146 | | |
147 | 0 | if (params->curve != GNUTLS_ECC_CURVE_ED25519 && |
148 | 0 | params->curve != GNUTLS_ECC_CURVE_ED448) |
149 | 0 | return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE); |
150 | | |
151 | 0 | ret = _gnutls_set_datum(raw, params->raw_pub.data, |
152 | 0 | params->raw_pub.size); |
153 | 0 | if (ret < 0) |
154 | 0 | return gnutls_assert_val(ret); |
155 | | |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | /* |
160 | | * some x509 certificate functions that relate to MPI parameter |
161 | | * setting. This writes a raw public key. |
162 | | * |
163 | | * Allocates the space used to store the data. |
164 | | */ |
165 | | static int |
166 | | _gnutls_x509_write_modern_ecdh_pubkey(const gnutls_pk_params_st *params, |
167 | | gnutls_datum_t *raw) |
168 | 0 | { |
169 | 0 | int ret; |
170 | |
|
171 | 0 | raw->data = NULL; |
172 | 0 | raw->size = 0; |
173 | |
|
174 | 0 | if (params->raw_pub.size == 0) |
175 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
176 | | |
177 | 0 | if (params->curve != GNUTLS_ECC_CURVE_X25519 && |
178 | 0 | params->curve != GNUTLS_ECC_CURVE_X448) |
179 | 0 | return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE); |
180 | | |
181 | 0 | ret = _gnutls_set_datum(raw, params->raw_pub.data, |
182 | 0 | params->raw_pub.size); |
183 | 0 | if (ret < 0) |
184 | 0 | return gnutls_assert_val(ret); |
185 | | |
186 | 0 | return 0; |
187 | 0 | } |
188 | | |
189 | | int _gnutls_x509_write_gost_pubkey(const gnutls_pk_params_st *params, |
190 | | gnutls_datum_t *der) |
191 | 0 | { |
192 | 0 | bigint_t x, y; |
193 | 0 | int numlen; |
194 | 0 | int byte_size, ret; |
195 | 0 | size_t size; |
196 | 0 | int pos; |
197 | |
|
198 | 0 | der->data = NULL; |
199 | 0 | der->size = 0; |
200 | |
|
201 | 0 | if (params->params_nr < GOST_PUBLIC_PARAMS) |
202 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
203 | | |
204 | 0 | x = params->params[GOST_X]; |
205 | 0 | y = params->params[GOST_Y]; |
206 | 0 | numlen = gnutls_ecc_curve_get_size(params->curve); |
207 | |
|
208 | 0 | if (numlen == 0) |
209 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
210 | | |
211 | 0 | der->size = 1 + ASN1_MAX_LENGTH_SIZE + 2 * numlen; |
212 | |
|
213 | 0 | der->data = gnutls_malloc(der->size); |
214 | 0 | if (der->data == NULL) |
215 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
216 | | |
217 | 0 | memset(der->data, 0, der->size); |
218 | |
|
219 | 0 | der->data[0] = ASN1_TAG_OCTET_STRING; |
220 | 0 | asn1_length_der(2 * numlen, &der->data[1], &pos); |
221 | 0 | pos += 1; |
222 | | |
223 | | /* pad and store x */ |
224 | 0 | byte_size = (_gnutls_mpi_get_nbits(x) + 7) / 8; |
225 | 0 | if (numlen < byte_size) { |
226 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
227 | 0 | goto cleanup; |
228 | 0 | } |
229 | | |
230 | 0 | size = numlen; |
231 | 0 | ret = _gnutls_mpi_print_le(x, &der->data[pos], &size); |
232 | 0 | if (ret < 0) { |
233 | 0 | gnutls_assert(); |
234 | 0 | goto cleanup; |
235 | 0 | } |
236 | | |
237 | | /* pad and store y */ |
238 | 0 | byte_size = (_gnutls_mpi_get_nbits(y) + 7) / 8; |
239 | 0 | if (numlen < byte_size) { |
240 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
241 | 0 | goto cleanup; |
242 | 0 | } |
243 | | |
244 | 0 | size = numlen; |
245 | 0 | ret = _gnutls_mpi_print_le(y, &der->data[pos + numlen], &size); |
246 | 0 | if (ret < 0) { |
247 | 0 | gnutls_assert(); |
248 | 0 | goto cleanup; |
249 | 0 | } |
250 | | |
251 | 0 | der->size = pos + 2 * numlen; |
252 | |
|
253 | 0 | return 0; |
254 | | |
255 | 0 | cleanup: |
256 | 0 | _gnutls_free_datum(der); |
257 | 0 | return ret; |
258 | 0 | } |
259 | | |
260 | | int _gnutls_x509_write_pubkey_params(const gnutls_pk_params_st *params, |
261 | | gnutls_datum_t *der) |
262 | 0 | { |
263 | 0 | switch (params->algo) { |
264 | 0 | case GNUTLS_PK_DSA: |
265 | 0 | return _gnutls_x509_write_dsa_params(params, der); |
266 | 0 | case GNUTLS_PK_RSA: |
267 | 0 | der->data = gnutls_malloc(ASN1_NULL_SIZE); |
268 | 0 | if (der->data == NULL) |
269 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
270 | | |
271 | 0 | memcpy(der->data, ASN1_NULL, ASN1_NULL_SIZE); |
272 | 0 | der->size = ASN1_NULL_SIZE; |
273 | 0 | return 0; |
274 | 0 | case GNUTLS_PK_RSA_PSS: |
275 | 0 | return _gnutls_x509_write_rsa_pss_params(¶ms->spki, der); |
276 | 0 | case GNUTLS_PK_ECDSA: |
277 | 0 | return _gnutls_x509_write_ecc_params(params->curve, der); |
278 | 0 | case GNUTLS_PK_EDDSA_ED25519: |
279 | 0 | case GNUTLS_PK_EDDSA_ED448: |
280 | 0 | case GNUTLS_PK_ECDH_X25519: |
281 | 0 | case GNUTLS_PK_ECDH_X448: |
282 | 0 | der->data = NULL; |
283 | 0 | der->size = 0; |
284 | |
|
285 | 0 | return 0; |
286 | 0 | case GNUTLS_PK_GOST_01: |
287 | 0 | case GNUTLS_PK_GOST_12_256: |
288 | 0 | case GNUTLS_PK_GOST_12_512: |
289 | 0 | return _gnutls_x509_write_gost_params(params, der); |
290 | 0 | default: |
291 | 0 | return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE); |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | | int _gnutls_x509_write_pubkey(const gnutls_pk_params_st *params, |
296 | | gnutls_datum_t *der) |
297 | 0 | { |
298 | 0 | switch (params->algo) { |
299 | 0 | case GNUTLS_PK_DSA: |
300 | 0 | return _gnutls_x509_write_dsa_pubkey(params, der); |
301 | 0 | case GNUTLS_PK_RSA: |
302 | 0 | case GNUTLS_PK_RSA_PSS: |
303 | 0 | return _gnutls_x509_write_rsa_pubkey(params, der); |
304 | 0 | case GNUTLS_PK_ECDSA: |
305 | 0 | return _gnutls_x509_write_ecc_pubkey(params, der); |
306 | 0 | case GNUTLS_PK_EDDSA_ED25519: |
307 | 0 | case GNUTLS_PK_EDDSA_ED448: |
308 | 0 | return _gnutls_x509_write_eddsa_pubkey(params, der); |
309 | 0 | case GNUTLS_PK_ECDH_X25519: |
310 | 0 | case GNUTLS_PK_ECDH_X448: |
311 | 0 | return _gnutls_x509_write_modern_ecdh_pubkey(params, der); |
312 | 0 | case GNUTLS_PK_GOST_01: |
313 | 0 | case GNUTLS_PK_GOST_12_256: |
314 | 0 | case GNUTLS_PK_GOST_12_512: |
315 | 0 | return _gnutls_x509_write_gost_pubkey(params, der); |
316 | 0 | default: |
317 | 0 | return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE); |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | | /* |
322 | | * This function writes the parameters for DSS keys. |
323 | | * Needs 3 parameters (p,q,g). |
324 | | * |
325 | | * Allocates the space used to store the DER data. |
326 | | */ |
327 | | static int _gnutls_x509_write_dsa_params(const gnutls_pk_params_st *params, |
328 | | gnutls_datum_t *der) |
329 | 0 | { |
330 | 0 | int result; |
331 | 0 | asn1_node spk = NULL; |
332 | |
|
333 | 0 | der->data = NULL; |
334 | 0 | der->size = 0; |
335 | |
|
336 | 0 | if (params->params_nr < DSA_PUBLIC_PARAMS - 1) { |
337 | 0 | gnutls_assert(); |
338 | 0 | result = GNUTLS_E_INVALID_REQUEST; |
339 | 0 | goto cleanup; |
340 | 0 | } |
341 | | |
342 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
343 | 0 | "GNUTLS.DSAParameters", &spk)) != |
344 | 0 | ASN1_SUCCESS) { |
345 | 0 | gnutls_assert(); |
346 | 0 | return _gnutls_asn2err(result); |
347 | 0 | } |
348 | | |
349 | 0 | result = _gnutls_x509_write_int(spk, "p", params->params[0], 1); |
350 | 0 | if (result < 0) { |
351 | 0 | gnutls_assert(); |
352 | 0 | goto cleanup; |
353 | 0 | } |
354 | | |
355 | 0 | result = _gnutls_x509_write_int(spk, "q", params->params[1], 1); |
356 | 0 | if (result < 0) { |
357 | 0 | gnutls_assert(); |
358 | 0 | goto cleanup; |
359 | 0 | } |
360 | | |
361 | 0 | result = _gnutls_x509_write_int(spk, "g", params->params[2], 1); |
362 | 0 | if (result < 0) { |
363 | 0 | gnutls_assert(); |
364 | 0 | goto cleanup; |
365 | 0 | } |
366 | | |
367 | 0 | result = _gnutls_x509_der_encode(spk, "", der, 0); |
368 | 0 | if (result < 0) { |
369 | 0 | gnutls_assert(); |
370 | 0 | goto cleanup; |
371 | 0 | } |
372 | | |
373 | 0 | result = 0; |
374 | |
|
375 | 0 | cleanup: |
376 | 0 | asn1_delete_structure(&spk); |
377 | 0 | return result; |
378 | 0 | } |
379 | | |
380 | | /* |
381 | | * This function writes the parameters for ECC keys. |
382 | | * That is the ECParameters struct. |
383 | | * |
384 | | * Allocates the space used to store the DER data. |
385 | | */ |
386 | | int _gnutls_x509_write_ecc_params(const gnutls_ecc_curve_t curve, |
387 | | gnutls_datum_t *der) |
388 | 0 | { |
389 | 0 | int result; |
390 | 0 | asn1_node spk = NULL; |
391 | 0 | const char *oid; |
392 | |
|
393 | 0 | der->data = NULL; |
394 | 0 | der->size = 0; |
395 | |
|
396 | 0 | oid = gnutls_ecc_curve_get_oid(curve); |
397 | 0 | if (oid == NULL) |
398 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
399 | | |
400 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
401 | 0 | "GNUTLS.ECParameters", &spk)) != |
402 | 0 | ASN1_SUCCESS) { |
403 | 0 | gnutls_assert(); |
404 | 0 | return _gnutls_asn2err(result); |
405 | 0 | } |
406 | | |
407 | 0 | if ((result = asn1_write_value(spk, "", "namedCurve", 1)) != |
408 | 0 | ASN1_SUCCESS) { |
409 | 0 | gnutls_assert(); |
410 | 0 | result = _gnutls_asn2err(result); |
411 | 0 | goto cleanup; |
412 | 0 | } |
413 | | |
414 | 0 | if ((result = asn1_write_value(spk, "namedCurve", oid, 1)) != |
415 | 0 | ASN1_SUCCESS) { |
416 | 0 | gnutls_assert(); |
417 | 0 | result = _gnutls_asn2err(result); |
418 | 0 | goto cleanup; |
419 | 0 | } |
420 | | |
421 | 0 | result = _gnutls_x509_der_encode(spk, "", der, 0); |
422 | 0 | if (result < 0) { |
423 | 0 | gnutls_assert(); |
424 | 0 | goto cleanup; |
425 | 0 | } |
426 | | |
427 | 0 | result = 0; |
428 | |
|
429 | 0 | cleanup: |
430 | 0 | asn1_delete_structure(&spk); |
431 | 0 | return result; |
432 | 0 | } |
433 | | |
434 | | int _gnutls_x509_write_rsa_pss_params(const gnutls_x509_spki_st *params, |
435 | | gnutls_datum_t *der) |
436 | 0 | { |
437 | 0 | int result; |
438 | 0 | asn1_node spk = NULL; |
439 | 0 | asn1_node c2 = NULL; |
440 | 0 | const char *oid; |
441 | 0 | gnutls_datum_t tmp = { NULL, 0 }; |
442 | |
|
443 | 0 | der->data = NULL; |
444 | 0 | der->size = 0; |
445 | |
|
446 | 0 | if (params->pk != GNUTLS_PK_RSA_PSS) |
447 | 0 | return 0; |
448 | | |
449 | | /* refuse to write parameters we cannot read */ |
450 | 0 | if (gnutls_pk_to_sign(GNUTLS_PK_RSA_PSS, params->rsa_pss_dig) == |
451 | 0 | GNUTLS_SIGN_UNKNOWN) |
452 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
453 | | |
454 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
455 | 0 | "GNUTLS.RSAPSSParameters", &spk)) != |
456 | 0 | ASN1_SUCCESS) { |
457 | 0 | gnutls_assert(); |
458 | 0 | result = _gnutls_asn2err(result); |
459 | 0 | goto cleanup; |
460 | 0 | } |
461 | | |
462 | 0 | oid = gnutls_digest_get_oid(params->rsa_pss_dig); |
463 | |
|
464 | 0 | if ((result = asn1_write_value(spk, "hashAlgorithm.algorithm", oid, |
465 | 0 | 1)) != ASN1_SUCCESS) { |
466 | 0 | gnutls_assert(); |
467 | 0 | result = _gnutls_asn2err(result); |
468 | 0 | goto cleanup; |
469 | 0 | } |
470 | | |
471 | 0 | if ((result = asn1_write_value(spk, "hashAlgorithm.parameters", NULL, |
472 | 0 | 0)) != ASN1_SUCCESS) { |
473 | 0 | gnutls_assert(); |
474 | 0 | result = _gnutls_asn2err(result); |
475 | 0 | goto cleanup; |
476 | 0 | } |
477 | | |
478 | 0 | if ((result = asn1_write_value(spk, "maskGenAlgorithm.algorithm", |
479 | 0 | PKIX1_RSA_PSS_MGF1_OID, 1)) != |
480 | 0 | ASN1_SUCCESS) { |
481 | 0 | gnutls_assert(); |
482 | 0 | result = _gnutls_asn2err(result); |
483 | 0 | goto cleanup; |
484 | 0 | } |
485 | | |
486 | 0 | if ((result = asn1_create_element(_gnutls_get_pkix(), |
487 | 0 | "PKIX1.AlgorithmIdentifier", &c2)) != |
488 | 0 | ASN1_SUCCESS) { |
489 | 0 | gnutls_assert(); |
490 | 0 | result = _gnutls_asn2err(result); |
491 | 0 | goto cleanup; |
492 | 0 | } |
493 | | |
494 | 0 | if ((result = asn1_write_value(c2, "algorithm", oid, 1)) != |
495 | 0 | ASN1_SUCCESS) { |
496 | 0 | gnutls_assert(); |
497 | 0 | result = _gnutls_asn2err(result); |
498 | 0 | goto cleanup; |
499 | 0 | } |
500 | | |
501 | 0 | if ((result = asn1_write_value(c2, "parameters", NULL, 0)) != |
502 | 0 | ASN1_SUCCESS) { |
503 | 0 | gnutls_assert(); |
504 | 0 | result = _gnutls_asn2err(result); |
505 | 0 | goto cleanup; |
506 | 0 | } |
507 | | |
508 | 0 | result = _gnutls_x509_der_encode(c2, "", &tmp, 0); |
509 | 0 | if (result < 0) { |
510 | 0 | gnutls_assert(); |
511 | 0 | goto cleanup; |
512 | 0 | } |
513 | | |
514 | 0 | if ((result = asn1_write_value(spk, "maskGenAlgorithm.parameters", |
515 | 0 | tmp.data, tmp.size)) != ASN1_SUCCESS) { |
516 | 0 | gnutls_assert(); |
517 | 0 | result = _gnutls_asn2err(result); |
518 | 0 | goto cleanup; |
519 | 0 | } |
520 | | |
521 | 0 | result = |
522 | 0 | _gnutls_x509_write_uint32(spk, "saltLength", params->salt_size); |
523 | 0 | if (result < 0) { |
524 | 0 | gnutls_assert(); |
525 | 0 | goto cleanup; |
526 | 0 | } |
527 | | |
528 | 0 | result = _gnutls_x509_write_uint32(spk, "trailerField", 1); |
529 | 0 | if (result < 0) { |
530 | 0 | gnutls_assert(); |
531 | 0 | goto cleanup; |
532 | 0 | } |
533 | | |
534 | 0 | result = _gnutls_x509_der_encode(spk, "", der, 0); |
535 | 0 | if (result < 0) { |
536 | 0 | gnutls_assert(); |
537 | 0 | goto cleanup; |
538 | 0 | } |
539 | | |
540 | 0 | result = 0; |
541 | |
|
542 | 0 | cleanup: |
543 | 0 | _gnutls_free_datum(&tmp); |
544 | 0 | asn1_delete_structure(&c2); |
545 | 0 | asn1_delete_structure(&spk); |
546 | 0 | return result; |
547 | 0 | } |
548 | | |
549 | | static int _gnutls_x509_write_gost_params(const gnutls_pk_params_st *params, |
550 | | gnutls_datum_t *der) |
551 | 0 | { |
552 | 0 | int result; |
553 | 0 | asn1_node spk = NULL; |
554 | 0 | const char *oid; |
555 | |
|
556 | 0 | der->data = NULL; |
557 | 0 | der->size = 0; |
558 | |
|
559 | 0 | oid = gnutls_ecc_curve_get_oid(params->curve); |
560 | 0 | if (oid == NULL) |
561 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
562 | | |
563 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
564 | 0 | params->algo == GNUTLS_PK_GOST_01 ? |
565 | 0 | "GNUTLS.GOSTParametersOld" : |
566 | 0 | "GNUTLS.GOSTParameters", |
567 | 0 | &spk)) != ASN1_SUCCESS) { |
568 | 0 | gnutls_assert(); |
569 | 0 | return _gnutls_asn2err(result); |
570 | 0 | } |
571 | | |
572 | 0 | if ((result = asn1_write_value(spk, "publicKeyParamSet", oid, 1)) != |
573 | 0 | ASN1_SUCCESS) { |
574 | 0 | gnutls_assert(); |
575 | 0 | result = _gnutls_asn2err(result); |
576 | 0 | goto cleanup; |
577 | 0 | } |
578 | | |
579 | | /* For compatibility per R 1323565.1.023—2018 provide digest OID only |
580 | | * for GOST-2001 keys or GOST-2012 keys with CryptoPro curves. Do not |
581 | | * set this optional parameter for TC26 curves */ |
582 | 0 | if (params->algo == GNUTLS_PK_GOST_01) |
583 | 0 | oid = HASH_OID_GOST_R_3411_94_CRYPTOPRO_PARAMS; |
584 | 0 | else if (params->algo == GNUTLS_PK_GOST_12_256 && |
585 | 0 | (params->curve == GNUTLS_ECC_CURVE_GOST256CPA || |
586 | 0 | params->curve == GNUTLS_ECC_CURVE_GOST256CPB || |
587 | 0 | params->curve == GNUTLS_ECC_CURVE_GOST256CPC || |
588 | 0 | params->curve == GNUTLS_ECC_CURVE_GOST256CPXA || |
589 | 0 | params->curve == GNUTLS_ECC_CURVE_GOST256CPXB)) |
590 | 0 | oid = HASH_OID_STREEBOG_256; |
591 | 0 | else if (params->algo == GNUTLS_PK_GOST_12_512 && |
592 | 0 | (params->curve == GNUTLS_ECC_CURVE_GOST512A || |
593 | 0 | params->curve == GNUTLS_ECC_CURVE_GOST512B)) |
594 | 0 | oid = HASH_OID_STREEBOG_512; |
595 | 0 | else |
596 | 0 | oid = NULL; |
597 | |
|
598 | 0 | if ((result = asn1_write_value(spk, "digestParamSet", oid, |
599 | 0 | oid ? 1 : 0)) != ASN1_SUCCESS) { |
600 | 0 | gnutls_assert(); |
601 | 0 | result = _gnutls_asn2err(result); |
602 | 0 | goto cleanup; |
603 | 0 | } |
604 | | |
605 | 0 | oid = gnutls_gost_paramset_get_oid(params->gost_params); |
606 | 0 | if (oid == NULL) { |
607 | 0 | gnutls_assert(); |
608 | 0 | result = GNUTLS_E_INVALID_REQUEST; |
609 | 0 | goto cleanup; |
610 | 0 | } |
611 | | |
612 | 0 | if (params->algo == GNUTLS_PK_GOST_01) { |
613 | 0 | if (params->gost_params == |
614 | 0 | _gnutls_gost_paramset_default(params->algo)) |
615 | 0 | oid = NULL; |
616 | |
|
617 | 0 | if ((result = asn1_write_value(spk, "encryptionParamSet", oid, |
618 | 0 | oid ? 1 : 0)) != ASN1_SUCCESS) { |
619 | 0 | gnutls_assert(); |
620 | 0 | result = _gnutls_asn2err(result); |
621 | 0 | goto cleanup; |
622 | 0 | } |
623 | 0 | } |
624 | | |
625 | 0 | result = _gnutls_x509_der_encode(spk, "", der, 0); |
626 | 0 | if (result < 0) { |
627 | 0 | gnutls_assert(); |
628 | 0 | goto cleanup; |
629 | 0 | } |
630 | | |
631 | 0 | result = 0; |
632 | |
|
633 | 0 | cleanup: |
634 | 0 | asn1_delete_structure(&spk); |
635 | 0 | return result; |
636 | 0 | } |
637 | | |
638 | | /* |
639 | | * This function writes the public parameters for DSS keys. |
640 | | * Needs 1 parameter (y). |
641 | | * |
642 | | * Allocates the space used to store the DER data. |
643 | | */ |
644 | | static int _gnutls_x509_write_dsa_pubkey(const gnutls_pk_params_st *params, |
645 | | gnutls_datum_t *der) |
646 | 0 | { |
647 | 0 | int result; |
648 | 0 | asn1_node spk = NULL; |
649 | |
|
650 | 0 | der->data = NULL; |
651 | 0 | der->size = 0; |
652 | |
|
653 | 0 | if (params->params_nr < DSA_PUBLIC_PARAMS) { |
654 | 0 | gnutls_assert(); |
655 | 0 | result = GNUTLS_E_INVALID_REQUEST; |
656 | 0 | goto cleanup; |
657 | 0 | } |
658 | | |
659 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
660 | 0 | "GNUTLS.DSAPublicKey", &spk)) != |
661 | 0 | ASN1_SUCCESS) { |
662 | 0 | gnutls_assert(); |
663 | 0 | return _gnutls_asn2err(result); |
664 | 0 | } |
665 | | |
666 | 0 | result = _gnutls_x509_write_int(spk, "", params->params[3], 1); |
667 | 0 | if (result < 0) { |
668 | 0 | gnutls_assert(); |
669 | 0 | goto cleanup; |
670 | 0 | } |
671 | | |
672 | 0 | result = _gnutls_x509_der_encode(spk, "", der, 0); |
673 | 0 | if (result < 0) { |
674 | 0 | gnutls_assert(); |
675 | 0 | goto cleanup; |
676 | 0 | } |
677 | | |
678 | 0 | result = 0; |
679 | |
|
680 | 0 | cleanup: |
681 | 0 | asn1_delete_structure(&spk); |
682 | 0 | return result; |
683 | 0 | } |
684 | | |
685 | | /* Encodes the RSA parameters into an ASN.1 RSA private key structure. |
686 | | */ |
687 | | static int _gnutls_asn1_encode_rsa(asn1_node *c2, gnutls_pk_params_st *params) |
688 | 0 | { |
689 | 0 | int result, ret; |
690 | 0 | uint8_t null = '\0'; |
691 | 0 | gnutls_pk_params_st pk_params; |
692 | | |
693 | | /* we do copy the parameters into a new structure to run _gnutls_pk_fixup, |
694 | | * i.e., regenerate some parameters in case they were broken */ |
695 | 0 | gnutls_pk_params_init(&pk_params); |
696 | |
|
697 | 0 | ret = _gnutls_pk_params_copy(&pk_params, params); |
698 | 0 | if (ret < 0) { |
699 | 0 | gnutls_assert(); |
700 | 0 | return ret; |
701 | 0 | } |
702 | | |
703 | 0 | ret = _gnutls_pk_fixup(GNUTLS_PK_RSA, GNUTLS_EXPORT, &pk_params); |
704 | 0 | if (ret < 0) { |
705 | 0 | gnutls_assert(); |
706 | 0 | goto cleanup; |
707 | 0 | } |
708 | | |
709 | | /* Ok. Now we have the data. Create the asn1 structures |
710 | | */ |
711 | | |
712 | | /* first make sure that no previously allocated data are leaked */ |
713 | 0 | if (*c2 != NULL) { |
714 | 0 | asn1_delete_structure(c2); |
715 | 0 | *c2 = NULL; |
716 | 0 | } |
717 | |
|
718 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
719 | 0 | "GNUTLS.RSAPrivateKey", c2)) != |
720 | 0 | ASN1_SUCCESS) { |
721 | 0 | gnutls_assert(); |
722 | 0 | ret = _gnutls_asn2err(result); |
723 | 0 | goto cleanup; |
724 | 0 | } |
725 | | |
726 | | /* Write PRIME |
727 | | */ |
728 | 0 | ret = _gnutls_x509_write_int(*c2, "modulus", |
729 | 0 | params->params[RSA_MODULUS], 1); |
730 | 0 | if (ret < 0) { |
731 | 0 | gnutls_assert(); |
732 | 0 | goto cleanup; |
733 | 0 | } |
734 | | |
735 | 0 | ret = _gnutls_x509_write_int(*c2, "publicExponent", |
736 | 0 | params->params[RSA_PUB], 1); |
737 | 0 | if (ret < 0) { |
738 | 0 | gnutls_assert(); |
739 | 0 | goto cleanup; |
740 | 0 | } |
741 | | |
742 | 0 | ret = _gnutls_x509_write_key_int(*c2, "privateExponent", |
743 | 0 | params->params[RSA_PRIV], 1); |
744 | 0 | if (ret < 0) { |
745 | 0 | gnutls_assert(); |
746 | 0 | goto cleanup; |
747 | 0 | } |
748 | | |
749 | 0 | ret = _gnutls_x509_write_key_int(*c2, "prime1", |
750 | 0 | params->params[RSA_PRIME1], 1); |
751 | 0 | if (ret < 0) { |
752 | 0 | gnutls_assert(); |
753 | 0 | goto cleanup; |
754 | 0 | } |
755 | | |
756 | 0 | ret = _gnutls_x509_write_key_int(*c2, "prime2", |
757 | 0 | params->params[RSA_PRIME2], 1); |
758 | 0 | if (ret < 0) { |
759 | 0 | gnutls_assert(); |
760 | 0 | goto cleanup; |
761 | 0 | } |
762 | | |
763 | 0 | ret = _gnutls_x509_write_key_int(*c2, "coefficient", |
764 | 0 | params->params[RSA_COEF], 1); |
765 | 0 | if (ret < 0) { |
766 | 0 | gnutls_assert(); |
767 | 0 | goto cleanup; |
768 | 0 | } |
769 | | |
770 | 0 | ret = _gnutls_x509_write_key_int(*c2, "exponent1", |
771 | 0 | params->params[RSA_E1], 1); |
772 | 0 | if (ret < 0) { |
773 | 0 | gnutls_assert(); |
774 | 0 | goto cleanup; |
775 | 0 | } |
776 | | |
777 | 0 | ret = _gnutls_x509_write_key_int(*c2, "exponent2", |
778 | 0 | params->params[RSA_E2], 1); |
779 | 0 | if (ret < 0) { |
780 | 0 | gnutls_assert(); |
781 | 0 | goto cleanup; |
782 | 0 | } |
783 | | |
784 | 0 | if ((result = asn1_write_value(*c2, "otherPrimeInfos", NULL, 0)) != |
785 | 0 | ASN1_SUCCESS) { |
786 | 0 | gnutls_assert(); |
787 | 0 | ret = _gnutls_asn2err(result); |
788 | 0 | goto cleanup; |
789 | 0 | } |
790 | | |
791 | 0 | if ((result = asn1_write_value(*c2, "version", &null, 1)) != |
792 | 0 | ASN1_SUCCESS) { |
793 | 0 | gnutls_assert(); |
794 | 0 | ret = _gnutls_asn2err(result); |
795 | 0 | goto cleanup; |
796 | 0 | } |
797 | | |
798 | 0 | ret = 0; |
799 | |
|
800 | 0 | cleanup: |
801 | 0 | if (ret < 0) |
802 | 0 | asn1_delete_structure2(c2, ASN1_DELETE_FLAG_ZEROIZE); |
803 | |
|
804 | 0 | gnutls_pk_params_clear(&pk_params); |
805 | 0 | gnutls_pk_params_release(&pk_params); |
806 | 0 | return ret; |
807 | 0 | } |
808 | | |
809 | | /* Encodes the ECC parameters into an ASN.1 ECPrivateKey structure. |
810 | | */ |
811 | | static int _gnutls_asn1_encode_ecc(asn1_node *c2, gnutls_pk_params_st *params) |
812 | 0 | { |
813 | 0 | int ret; |
814 | 0 | uint8_t one = '\x01'; |
815 | 0 | gnutls_datum_t pubkey = { NULL, 0 }; |
816 | 0 | const char *oid; |
817 | |
|
818 | 0 | oid = gnutls_ecc_curve_get_oid(params->curve); |
819 | 0 | if (oid == NULL) |
820 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
821 | | |
822 | | /* first make sure that no previously allocated data are leaked */ |
823 | 0 | if (*c2 != NULL) { |
824 | 0 | asn1_delete_structure(c2); |
825 | 0 | *c2 = NULL; |
826 | 0 | } |
827 | |
|
828 | 0 | if ((ret = asn1_create_element(_gnutls_get_gnutls_asn(), |
829 | 0 | "GNUTLS.ECPrivateKey", c2)) != |
830 | 0 | ASN1_SUCCESS) { |
831 | 0 | gnutls_assert(); |
832 | 0 | ret = _gnutls_asn2err(ret); |
833 | 0 | goto cleanup; |
834 | 0 | } |
835 | | |
836 | 0 | if ((ret = asn1_write_value(*c2, "Version", &one, 1)) != ASN1_SUCCESS) { |
837 | 0 | gnutls_assert(); |
838 | 0 | ret = _gnutls_asn2err(ret); |
839 | 0 | goto cleanup; |
840 | 0 | } |
841 | | |
842 | 0 | if (curve_is_eddsa(params->curve) || |
843 | 0 | curve_is_modern_ecdh(params->curve)) { |
844 | 0 | if (params->raw_pub.size == 0 || params->raw_priv.size == 0) |
845 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
846 | 0 | ret = asn1_write_value(*c2, "privateKey", params->raw_priv.data, |
847 | 0 | params->raw_priv.size); |
848 | 0 | if (ret != ASN1_SUCCESS) { |
849 | 0 | gnutls_assert(); |
850 | 0 | ret = _gnutls_asn2err(ret); |
851 | 0 | goto cleanup; |
852 | 0 | } |
853 | | |
854 | 0 | ret = asn1_write_value(*c2, "publicKey", params->raw_pub.data, |
855 | 0 | params->raw_pub.size * 8); |
856 | 0 | if (ret != ASN1_SUCCESS) { |
857 | 0 | gnutls_assert(); |
858 | 0 | ret = _gnutls_asn2err(ret); |
859 | 0 | goto cleanup; |
860 | 0 | } |
861 | 0 | } else { |
862 | 0 | if (params->params_nr != ECC_PRIVATE_PARAMS) |
863 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
864 | | |
865 | 0 | ret = _gnutls_ecc_ansi_x962_export(params->curve, |
866 | 0 | params->params[ECC_X], |
867 | 0 | params->params[ECC_Y], |
868 | 0 | &pubkey); |
869 | 0 | if (ret < 0) |
870 | 0 | return gnutls_assert_val(ret); |
871 | | |
872 | 0 | ret = _gnutls_x509_write_key_int(*c2, "privateKey", |
873 | 0 | params->params[ECC_K], 1); |
874 | 0 | if (ret < 0) { |
875 | 0 | gnutls_assert(); |
876 | 0 | goto cleanup; |
877 | 0 | } |
878 | | |
879 | 0 | if ((ret = asn1_write_value(*c2, "publicKey", pubkey.data, |
880 | 0 | pubkey.size * 8)) != ASN1_SUCCESS) { |
881 | 0 | gnutls_assert(); |
882 | 0 | ret = _gnutls_asn2err(ret); |
883 | 0 | goto cleanup; |
884 | 0 | } |
885 | 0 | } |
886 | | |
887 | | /* write our choice */ |
888 | 0 | if ((ret = asn1_write_value(*c2, "parameters", "namedCurve", 1)) != |
889 | 0 | ASN1_SUCCESS) { |
890 | 0 | gnutls_assert(); |
891 | 0 | ret = _gnutls_asn2err(ret); |
892 | 0 | goto cleanup; |
893 | 0 | } |
894 | | |
895 | 0 | if ((ret = asn1_write_value(*c2, "parameters.namedCurve", oid, 1)) != |
896 | 0 | ASN1_SUCCESS) { |
897 | 0 | gnutls_assert(); |
898 | 0 | ret = _gnutls_asn2err(ret); |
899 | 0 | goto cleanup; |
900 | 0 | } |
901 | | |
902 | 0 | _gnutls_free_datum(&pubkey); |
903 | 0 | return 0; |
904 | | |
905 | 0 | cleanup: |
906 | 0 | asn1_delete_structure2(c2, ASN1_DELETE_FLAG_ZEROIZE); |
907 | 0 | _gnutls_free_datum(&pubkey); |
908 | |
|
909 | 0 | return ret; |
910 | 0 | } |
911 | | |
912 | | static int _gnutls_asn1_encode_gost(asn1_node *c2, gnutls_pk_params_st *params) |
913 | 0 | { |
914 | 0 | int ret; |
915 | 0 | const char *oid; |
916 | |
|
917 | 0 | oid = gnutls_pk_get_oid(params->algo); |
918 | |
|
919 | 0 | if (params->params_nr != GOST_PRIVATE_PARAMS || oid == NULL) |
920 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
921 | | |
922 | | /* first make sure that no previously allocated data are leaked */ |
923 | 0 | if (*c2 != NULL) { |
924 | 0 | asn1_delete_structure(c2); |
925 | 0 | *c2 = NULL; |
926 | 0 | } |
927 | |
|
928 | 0 | if ((ret = asn1_create_element(_gnutls_get_gnutls_asn(), |
929 | 0 | "GNUTLS.GOSTPrivateKey", c2)) != |
930 | 0 | ASN1_SUCCESS) { |
931 | 0 | gnutls_assert(); |
932 | 0 | ret = _gnutls_asn2err(ret); |
933 | 0 | goto cleanup; |
934 | 0 | } |
935 | | |
936 | 0 | ret = _gnutls_x509_write_key_int_le(*c2, "", params->params[GOST_K]); |
937 | 0 | if (ret < 0) { |
938 | 0 | gnutls_assert(); |
939 | 0 | goto cleanup; |
940 | 0 | } |
941 | | |
942 | 0 | return 0; |
943 | | |
944 | 0 | cleanup: |
945 | 0 | asn1_delete_structure2(c2, ASN1_DELETE_FLAG_ZEROIZE); |
946 | |
|
947 | 0 | return ret; |
948 | 0 | } |
949 | | |
950 | | /* Encodes the DSA parameters into an ASN.1 DSAPrivateKey structure. |
951 | | */ |
952 | | static int _gnutls_asn1_encode_dsa(asn1_node *c2, gnutls_pk_params_st *params) |
953 | 0 | { |
954 | 0 | int result, ret; |
955 | 0 | const uint8_t null = '\0'; |
956 | | |
957 | | /* first make sure that no previously allocated data are leaked */ |
958 | 0 | if (*c2 != NULL) { |
959 | 0 | asn1_delete_structure(c2); |
960 | 0 | *c2 = NULL; |
961 | 0 | } |
962 | |
|
963 | 0 | if ((result = asn1_create_element(_gnutls_get_gnutls_asn(), |
964 | 0 | "GNUTLS.DSAPrivateKey", c2)) != |
965 | 0 | ASN1_SUCCESS) { |
966 | 0 | gnutls_assert(); |
967 | 0 | return _gnutls_asn2err(result); |
968 | 0 | } |
969 | | |
970 | | /* Write PRIME |
971 | | */ |
972 | 0 | ret = _gnutls_x509_write_int(*c2, "p", params->params[DSA_P], 1); |
973 | 0 | if (ret < 0) { |
974 | 0 | gnutls_assert(); |
975 | 0 | goto cleanup; |
976 | 0 | } |
977 | | |
978 | 0 | ret = _gnutls_x509_write_int(*c2, "q", params->params[DSA_Q], 1); |
979 | 0 | if (ret < 0) { |
980 | 0 | gnutls_assert(); |
981 | 0 | goto cleanup; |
982 | 0 | } |
983 | | |
984 | 0 | ret = _gnutls_x509_write_int(*c2, "g", params->params[DSA_G], 1); |
985 | 0 | if (ret < 0) { |
986 | 0 | gnutls_assert(); |
987 | 0 | goto cleanup; |
988 | 0 | } |
989 | | |
990 | 0 | ret = _gnutls_x509_write_int(*c2, "Y", params->params[DSA_Y], 1); |
991 | 0 | if (ret < 0) { |
992 | 0 | gnutls_assert(); |
993 | 0 | goto cleanup; |
994 | 0 | } |
995 | | |
996 | 0 | ret = _gnutls_x509_write_key_int(*c2, "priv", params->params[DSA_X], 1); |
997 | 0 | if (ret < 0) { |
998 | 0 | gnutls_assert(); |
999 | 0 | goto cleanup; |
1000 | 0 | } |
1001 | | |
1002 | 0 | if ((result = asn1_write_value(*c2, "version", &null, 1)) != |
1003 | 0 | ASN1_SUCCESS) { |
1004 | 0 | gnutls_assert(); |
1005 | 0 | ret = _gnutls_asn2err(result); |
1006 | 0 | goto cleanup; |
1007 | 0 | } |
1008 | | |
1009 | 0 | return 0; |
1010 | | |
1011 | 0 | cleanup: |
1012 | 0 | asn1_delete_structure2(c2, ASN1_DELETE_FLAG_ZEROIZE); |
1013 | |
|
1014 | 0 | return ret; |
1015 | 0 | } |
1016 | | |
1017 | | int _gnutls_asn1_encode_privkey(asn1_node *c2, gnutls_pk_params_st *params) |
1018 | 0 | { |
1019 | 0 | switch (params->algo) { |
1020 | 0 | case GNUTLS_PK_RSA: |
1021 | 0 | case GNUTLS_PK_RSA_PSS: |
1022 | 0 | return _gnutls_asn1_encode_rsa(c2, params); |
1023 | 0 | case GNUTLS_PK_DSA: |
1024 | 0 | return _gnutls_asn1_encode_dsa(c2, params); |
1025 | 0 | case GNUTLS_PK_ECDSA: |
1026 | 0 | case GNUTLS_PK_EDDSA_ED25519: |
1027 | 0 | case GNUTLS_PK_EDDSA_ED448: |
1028 | 0 | case GNUTLS_PK_ECDH_X25519: |
1029 | 0 | case GNUTLS_PK_ECDH_X448: |
1030 | 0 | return _gnutls_asn1_encode_ecc(c2, params); |
1031 | 0 | case GNUTLS_PK_GOST_01: |
1032 | 0 | case GNUTLS_PK_GOST_12_256: |
1033 | 0 | case GNUTLS_PK_GOST_12_512: |
1034 | 0 | return _gnutls_asn1_encode_gost(c2, params); |
1035 | 0 | case GNUTLS_PK_DH: |
1036 | | /* DH keys are only exportable in PKCS#8 format */ |
1037 | 0 | return GNUTLS_E_INVALID_REQUEST; |
1038 | 0 | default: |
1039 | 0 | return GNUTLS_E_UNIMPLEMENTED_FEATURE; |
1040 | 0 | } |
1041 | 0 | } |