1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
2 * 2006.
3 */
4 /* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com). */
55
56 #include <openssl/evp.h>
57
58 #include <openssl/asn1.h>
59 #include <openssl/asn1t.h>
60 #include <openssl/digest.h>
61 #include <openssl/dsa.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/x509.h>
66
67 #include "../dsa/internal.h"
68 #include "internal.h"
69
70
dsa_pub_decode(EVP_PKEY * pkey,X509_PUBKEY * pubkey)71 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) {
72 const uint8_t *p, *pm;
73 int pklen, pmlen;
74 int ptype;
75 void *pval;
76 ASN1_STRING *pstr;
77 X509_ALGOR *palg;
78 ASN1_INTEGER *public_key = NULL;
79
80 DSA *dsa = NULL;
81
82 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) {
83 return 0;
84 }
85 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
86
87 if (ptype == V_ASN1_SEQUENCE) {
88 pstr = pval;
89 pm = pstr->data;
90 pmlen = pstr->length;
91
92 dsa = d2i_DSAparams(NULL, &pm, pmlen);
93 if (dsa == NULL) {
94 OPENSSL_PUT_ERROR(EVP, dsa_pub_decode, EVP_R_DECODE_ERROR);
95 goto err;
96 }
97 } else if (ptype == V_ASN1_NULL || ptype == V_ASN1_UNDEF) {
98 dsa = DSA_new();
99 if (dsa == NULL) {
100 OPENSSL_PUT_ERROR(EVP, dsa_pub_decode, ERR_R_MALLOC_FAILURE);
101 goto err;
102 }
103 } else {
104 OPENSSL_PUT_ERROR(EVP, dsa_pub_decode, EVP_R_PARAMETER_ENCODING_ERROR);
105 goto err;
106 }
107
108 public_key = d2i_ASN1_INTEGER(NULL, &p, pklen);
109 if (public_key == NULL) {
110 OPENSSL_PUT_ERROR(EVP, dsa_pub_decode, EVP_R_DECODE_ERROR);
111 goto err;
112 }
113
114 dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL);
115 if (dsa->pub_key == NULL) {
116 OPENSSL_PUT_ERROR(EVP, dsa_pub_decode, EVP_R_BN_DECODE_ERROR);
117 goto err;
118 }
119
120 ASN1_INTEGER_free(public_key);
121 EVP_PKEY_assign_DSA(pkey, dsa);
122 return 1;
123
124 err:
125 ASN1_INTEGER_free(public_key);
126 DSA_free(dsa);
127 return 0;
128 }
129
dsa_pub_encode(X509_PUBKEY * pk,const EVP_PKEY * pkey)130 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) {
131 DSA *dsa;
132 void *pval = NULL;
133 uint8_t *penc = NULL;
134 int penclen;
135
136 dsa = pkey->pkey.dsa;
137 dsa->write_params = 0;
138
139 penclen = i2d_DSAPublicKey(dsa, &penc);
140
141 if (penclen <= 0) {
142 OPENSSL_PUT_ERROR(EVP, dsa_pub_encode, ERR_R_MALLOC_FAILURE);
143 goto err;
144 }
145
146 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA), V_ASN1_UNDEF, pval,
147 penc, penclen)) {
148 return 1;
149 }
150
151 err:
152 OPENSSL_free(penc);
153 ASN1_STRING_free(pval);
154
155 return 0;
156 }
157
dsa_priv_decode(EVP_PKEY * pkey,PKCS8_PRIV_KEY_INFO * p8)158 static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) {
159 const uint8_t *p, *pm;
160 int pklen, pmlen;
161 int ptype;
162 void *pval;
163 ASN1_STRING *pstr;
164 X509_ALGOR *palg;
165 ASN1_INTEGER *privkey = NULL;
166 BN_CTX *ctx = NULL;
167
168 /* In PKCS#8 DSA: you just get a private key integer and parameters in the
169 * AlgorithmIdentifier the pubkey must be recalculated. */
170
171 DSA *dsa = NULL;
172
173 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) {
174 return 0;
175 }
176 privkey = d2i_ASN1_INTEGER(NULL, &p, pklen);
177 if (privkey == NULL || privkey->type == V_ASN1_NEG_INTEGER) {
178 goto decerr;
179 }
180
181 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
182 if (ptype != V_ASN1_SEQUENCE) {
183 goto decerr;
184 }
185 pstr = pval;
186 pm = pstr->data;
187 pmlen = pstr->length;
188 dsa = d2i_DSAparams(NULL, &pm, pmlen);
189 if (dsa == NULL) {
190 goto decerr;
191 }
192 /* We have parameters. Now set private key */
193 dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL);
194 if (dsa->priv_key == NULL) {
195 OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_LIB_BN);
196 goto dsaerr;
197 }
198 /* Calculate public key. */
199 dsa->pub_key = BN_new();
200 if (dsa->pub_key == NULL) {
201 OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_R_MALLOC_FAILURE);
202 goto dsaerr;
203 }
204 ctx = BN_CTX_new();
205 if (ctx == NULL) {
206 OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_R_MALLOC_FAILURE);
207 goto dsaerr;
208 }
209
210 if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
211 OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_LIB_BN);
212 goto dsaerr;
213 }
214
215 EVP_PKEY_assign_DSA(pkey, dsa);
216 BN_CTX_free(ctx);
217 ASN1_INTEGER_free(privkey);
218
219 return 1;
220
221 decerr:
222 OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, EVP_R_DECODE_ERROR);
223
224 dsaerr:
225 BN_CTX_free(ctx);
226 ASN1_INTEGER_free(privkey);
227 DSA_free(dsa);
228 return 0;
229 }
230
dsa_priv_encode(PKCS8_PRIV_KEY_INFO * p8,const EVP_PKEY * pkey)231 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) {
232 ASN1_STRING *params = NULL;
233 ASN1_INTEGER *prkey = NULL;
234 uint8_t *dp = NULL;
235 int dplen;
236
237 if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
238 OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, EVP_R_MISSING_PARAMETERS);
239 goto err;
240 }
241
242 params = ASN1_STRING_new();
243 if (!params) {
244 OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, ERR_R_MALLOC_FAILURE);
245 goto err;
246 }
247
248 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
249 if (params->length <= 0) {
250 OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, ERR_R_MALLOC_FAILURE);
251 goto err;
252 }
253 params->type = V_ASN1_SEQUENCE;
254
255 /* Get private key into integer. */
256 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
257
258 if (!prkey) {
259 OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, ERR_LIB_BN);
260 goto err;
261 }
262
263 dplen = i2d_ASN1_INTEGER(prkey, &dp);
264
265 ASN1_INTEGER_free(prkey);
266
267 if (!PKCS8_pkey_set0(p8, (ASN1_OBJECT *)OBJ_nid2obj(NID_dsa), 0,
268 V_ASN1_SEQUENCE, params, dp, dplen)) {
269 goto err;
270 }
271
272 return 1;
273
274 err:
275 OPENSSL_free(dp);
276 ASN1_STRING_free(params);
277 ASN1_INTEGER_free(prkey);
278 return 0;
279 }
280
int_dsa_size(const EVP_PKEY * pkey)281 static int int_dsa_size(const EVP_PKEY *pkey) {
282 return DSA_size(pkey->pkey.dsa);
283 }
284
dsa_bits(const EVP_PKEY * pkey)285 static int dsa_bits(const EVP_PKEY *pkey) {
286 return BN_num_bits(pkey->pkey.dsa->p);
287 }
288
dsa_missing_parameters(const EVP_PKEY * pkey)289 static int dsa_missing_parameters(const EVP_PKEY *pkey) {
290 DSA *dsa;
291 dsa = pkey->pkey.dsa;
292 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
293 return 1;
294 }
295 return 0;
296 }
297
dup_bn_into(BIGNUM ** out,BIGNUM * src)298 static int dup_bn_into(BIGNUM **out, BIGNUM *src) {
299 BIGNUM *a;
300
301 a = BN_dup(src);
302 if (a == NULL) {
303 return 0;
304 }
305 BN_free(*out);
306 *out = a;
307
308 return 1;
309 }
310
dsa_copy_parameters(EVP_PKEY * to,const EVP_PKEY * from)311 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
312 if (!dup_bn_into(&to->pkey.dsa->p, from->pkey.dsa->p) ||
313 !dup_bn_into(&to->pkey.dsa->q, from->pkey.dsa->q) ||
314 !dup_bn_into(&to->pkey.dsa->g, from->pkey.dsa->g)) {
315 return 0;
316 }
317
318 return 1;
319 }
320
dsa_cmp_parameters(const EVP_PKEY * a,const EVP_PKEY * b)321 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
322 return BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) == 0 &&
323 BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) == 0 &&
324 BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g) == 0;
325 }
326
dsa_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)327 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
328 return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
329 }
330
int_dsa_free(EVP_PKEY * pkey)331 static void int_dsa_free(EVP_PKEY *pkey) { DSA_free(pkey->pkey.dsa); }
332
update_buflen(const BIGNUM * b,size_t * pbuflen)333 static void update_buflen(const BIGNUM *b, size_t *pbuflen) {
334 size_t i;
335
336 if (!b) {
337 return;
338 }
339 i = BN_num_bytes(b);
340 if (*pbuflen < i) {
341 *pbuflen = i;
342 }
343 }
344
do_dsa_print(BIO * bp,const DSA * x,int off,int ptype)345 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
346 uint8_t *m = NULL;
347 int ret = 0;
348 size_t buf_len = 0;
349 const char *ktype = NULL;
350
351 const BIGNUM *priv_key, *pub_key;
352
353 priv_key = NULL;
354 if (ptype == 2) {
355 priv_key = x->priv_key;
356 }
357
358 pub_key = NULL;
359 if (ptype > 0) {
360 pub_key = x->pub_key;
361 }
362
363 ktype = "DSA-Parameters";
364 if (ptype == 2) {
365 ktype = "Private-Key";
366 } else if (ptype == 1) {
367 ktype = "Public-Key";
368 }
369
370 update_buflen(x->p, &buf_len);
371 update_buflen(x->q, &buf_len);
372 update_buflen(x->g, &buf_len);
373 update_buflen(priv_key, &buf_len);
374 update_buflen(pub_key, &buf_len);
375
376 m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
377 if (m == NULL) {
378 OPENSSL_PUT_ERROR(EVP, do_dsa_print, ERR_R_MALLOC_FAILURE);
379 goto err;
380 }
381
382 if (priv_key) {
383 if (!BIO_indent(bp, off, 128) ||
384 BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
385 goto err;
386 }
387 }
388
389 if (!ASN1_bn_print(bp, "priv:", priv_key, m, off) ||
390 !ASN1_bn_print(bp, "pub: ", pub_key, m, off) ||
391 !ASN1_bn_print(bp, "P: ", x->p, m, off) ||
392 !ASN1_bn_print(bp, "Q: ", x->q, m, off) ||
393 !ASN1_bn_print(bp, "G: ", x->g, m, off)) {
394 goto err;
395 }
396 ret = 1;
397
398 err:
399 OPENSSL_free(m);
400 return ret;
401 }
402
dsa_param_decode(EVP_PKEY * pkey,const uint8_t ** pder,int derlen)403 static int dsa_param_decode(EVP_PKEY *pkey, const uint8_t **pder, int derlen) {
404 DSA *dsa;
405 dsa = d2i_DSAparams(NULL, pder, derlen);
406 if (dsa == NULL) {
407 OPENSSL_PUT_ERROR(EVP, dsa_param_decode, ERR_R_DSA_LIB);
408 return 0;
409 }
410 EVP_PKEY_assign_DSA(pkey, dsa);
411 return 1;
412 }
413
dsa_param_encode(const EVP_PKEY * pkey,uint8_t ** pder)414 static int dsa_param_encode(const EVP_PKEY *pkey, uint8_t **pder) {
415 return i2d_DSAparams(pkey->pkey.dsa, pder);
416 }
417
dsa_param_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)418 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
419 ASN1_PCTX *ctx) {
420 return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
421 }
422
dsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)423 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
424 ASN1_PCTX *ctx) {
425 return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
426 }
427
dsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)428 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
429 ASN1_PCTX *ctx) {
430 return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
431 }
432
old_dsa_priv_decode(EVP_PKEY * pkey,const uint8_t ** pder,int derlen)433 static int old_dsa_priv_decode(EVP_PKEY *pkey, const uint8_t **pder,
434 int derlen) {
435 DSA *dsa;
436 dsa = d2i_DSAPrivateKey(NULL, pder, derlen);
437 if (dsa == NULL) {
438 OPENSSL_PUT_ERROR(EVP, old_dsa_priv_decode, ERR_R_DSA_LIB);
439 return 0;
440 }
441 EVP_PKEY_assign_DSA(pkey, dsa);
442 return 1;
443 }
444
old_dsa_priv_encode(const EVP_PKEY * pkey,uint8_t ** pder)445 static int old_dsa_priv_encode(const EVP_PKEY *pkey, uint8_t **pder) {
446 return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
447 }
448
dsa_sig_print(BIO * bp,const X509_ALGOR * sigalg,const ASN1_STRING * sig,int indent,ASN1_PCTX * pctx)449 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
450 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) {
451 DSA_SIG *dsa_sig;
452 const uint8_t *p;
453
454 if (!sig) {
455 return BIO_puts(bp, "\n") > 0;
456 }
457
458 p = sig->data;
459 dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
460 if (dsa_sig == NULL) {
461 return X509_signature_dump(bp, sig, indent);
462 }
463
464 int rv = 0;
465 size_t buf_len = 0;
466 uint8_t *m = NULL;
467
468 update_buflen(dsa_sig->r, &buf_len);
469 update_buflen(dsa_sig->s, &buf_len);
470 m = OPENSSL_malloc(buf_len + 10);
471 if (m == NULL) {
472 OPENSSL_PUT_ERROR(EVP, dsa_sig_print, ERR_R_MALLOC_FAILURE);
473 goto err;
474 }
475
476 if (BIO_write(bp, "\n", 1) != 1 ||
477 !ASN1_bn_print(bp, "r: ", dsa_sig->r, m, indent) ||
478 !ASN1_bn_print(bp, "s: ", dsa_sig->s, m, indent)) {
479 goto err;
480 }
481 rv = 1;
482
483 err:
484 OPENSSL_free(m);
485 DSA_SIG_free(dsa_sig);
486 return rv;
487 }
488
489 const EVP_PKEY_ASN1_METHOD dsa_asn1_meth = {
490 EVP_PKEY_DSA,
491 EVP_PKEY_DSA,
492 0,
493
494 "DSA",
495 "OpenSSL DSA method",
496
497 dsa_pub_decode,
498 dsa_pub_encode,
499 dsa_pub_cmp,
500 dsa_pub_print,
501
502 dsa_priv_decode,
503 dsa_priv_encode,
504 dsa_priv_print,
505
506 NULL /* pkey_opaque */,
507 NULL /* pkey_supports_digest */,
508
509 int_dsa_size,
510 dsa_bits,
511
512 dsa_param_decode,
513 dsa_param_encode,
514 dsa_missing_parameters,
515 dsa_copy_parameters,
516 dsa_cmp_parameters,
517 dsa_param_print,
518 dsa_sig_print,
519
520 int_dsa_free,
521 old_dsa_priv_decode,
522 old_dsa_priv_encode,
523 };
524