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   STACK_OF(ASN1_TYPE) *ndsa = NULL;
172   DSA *dsa = NULL;
173 
174   if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) {
175     return 0;
176   }
177   X509_ALGOR_get0(NULL, &ptype, &pval, palg);
178 
179   /* Check for broken DSA PKCS#8, UGH! */
180   if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
181     ASN1_TYPE *t1, *t2;
182     ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen);
183     if (ndsa == NULL) {
184       goto decerr;
185     }
186     if (sk_ASN1_TYPE_num(ndsa) != 2) {
187       goto decerr;
188     }
189 
190     /* Handle Two broken types:
191      * SEQUENCE {parameters, priv_key}
192      * SEQUENCE {pub_key, priv_key}. */
193 
194     t1 = sk_ASN1_TYPE_value(ndsa, 0);
195     t2 = sk_ASN1_TYPE_value(ndsa, 1);
196     if (t1->type == V_ASN1_SEQUENCE) {
197       p8->broken = PKCS8_EMBEDDED_PARAM;
198       pval = t1->value.ptr;
199     } else if (ptype == V_ASN1_SEQUENCE) {
200       p8->broken = PKCS8_NS_DB;
201     } else {
202       goto decerr;
203     }
204 
205     if (t2->type != V_ASN1_INTEGER) {
206       goto decerr;
207     }
208 
209     privkey = t2->value.integer;
210   } else {
211     const uint8_t *q = p;
212     privkey = d2i_ASN1_INTEGER(NULL, &p, pklen);
213     if (privkey == NULL) {
214       goto decerr;
215     }
216     if (privkey->type == V_ASN1_NEG_INTEGER) {
217       p8->broken = PKCS8_NEG_PRIVKEY;
218       ASN1_INTEGER_free(privkey);
219       privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen);
220       if (privkey == NULL) {
221         goto decerr;
222       }
223     }
224     if (ptype != V_ASN1_SEQUENCE) {
225       goto decerr;
226     }
227   }
228 
229   pstr = pval;
230   pm = pstr->data;
231   pmlen = pstr->length;
232   dsa = d2i_DSAparams(NULL, &pm, pmlen);
233   if (dsa == NULL) {
234     goto decerr;
235   }
236   /* We have parameters. Now set private key */
237   dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL);
238   if (dsa->priv_key == NULL) {
239     OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_LIB_BN);
240     goto dsaerr;
241   }
242   /* Calculate public key. */
243   dsa->pub_key = BN_new();
244   if (dsa->pub_key == NULL) {
245     OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_R_MALLOC_FAILURE);
246     goto dsaerr;
247   }
248   ctx = BN_CTX_new();
249   if (ctx == NULL) {
250     OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_R_MALLOC_FAILURE);
251     goto dsaerr;
252   }
253 
254   if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
255     OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, ERR_LIB_BN);
256     goto dsaerr;
257   }
258 
259   EVP_PKEY_assign_DSA(pkey, dsa);
260   BN_CTX_free(ctx);
261   sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
262   ASN1_INTEGER_free(privkey);
263 
264   return 1;
265 
266 decerr:
267   OPENSSL_PUT_ERROR(EVP, dsa_priv_decode, EVP_R_DECODE_ERROR);
268 
269 dsaerr:
270   BN_CTX_free(ctx);
271   ASN1_INTEGER_free(privkey);
272   sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
273   DSA_free(dsa);
274   return 0;
275 }
276 
dsa_priv_encode(PKCS8_PRIV_KEY_INFO * p8,const EVP_PKEY * pkey)277 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) {
278   ASN1_STRING *params = NULL;
279   ASN1_INTEGER *prkey = NULL;
280   uint8_t *dp = NULL;
281   int dplen;
282 
283   if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
284     OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, EVP_R_MISSING_PARAMETERS);
285     goto err;
286   }
287 
288   params = ASN1_STRING_new();
289   if (!params) {
290     OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, ERR_R_MALLOC_FAILURE);
291     goto err;
292   }
293 
294   params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
295   if (params->length <= 0) {
296     OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, ERR_R_MALLOC_FAILURE);
297     goto err;
298   }
299   params->type = V_ASN1_SEQUENCE;
300 
301   /* Get private key into integer. */
302   prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
303 
304   if (!prkey) {
305     OPENSSL_PUT_ERROR(EVP, dsa_priv_encode, ERR_LIB_BN);
306     goto err;
307   }
308 
309   dplen = i2d_ASN1_INTEGER(prkey, &dp);
310 
311   ASN1_INTEGER_free(prkey);
312 
313   if (!PKCS8_pkey_set0(p8, (ASN1_OBJECT *)OBJ_nid2obj(NID_dsa), 0,
314                        V_ASN1_SEQUENCE, params, dp, dplen)) {
315     goto err;
316   }
317 
318   return 1;
319 
320 err:
321   OPENSSL_free(dp);
322   ASN1_STRING_free(params);
323   ASN1_INTEGER_free(prkey);
324   return 0;
325 }
326 
int_dsa_size(const EVP_PKEY * pkey)327 static int int_dsa_size(const EVP_PKEY *pkey) {
328   return DSA_size(pkey->pkey.dsa);
329 }
330 
dsa_bits(const EVP_PKEY * pkey)331 static int dsa_bits(const EVP_PKEY *pkey) {
332   return BN_num_bits(pkey->pkey.dsa->p);
333 }
334 
dsa_missing_parameters(const EVP_PKEY * pkey)335 static int dsa_missing_parameters(const EVP_PKEY *pkey) {
336   DSA *dsa;
337   dsa = pkey->pkey.dsa;
338   if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
339     return 1;
340   }
341   return 0;
342 }
343 
dup_bn_into(BIGNUM ** out,BIGNUM * src)344 static int dup_bn_into(BIGNUM **out, BIGNUM *src) {
345   BIGNUM *a;
346 
347   a = BN_dup(src);
348   if (a == NULL) {
349     return 0;
350   }
351   BN_free(*out);
352   *out = a;
353 
354   return 1;
355 }
356 
dsa_copy_parameters(EVP_PKEY * to,const EVP_PKEY * from)357 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
358   if (!dup_bn_into(&to->pkey.dsa->p, from->pkey.dsa->p) ||
359       !dup_bn_into(&to->pkey.dsa->q, from->pkey.dsa->q) ||
360       !dup_bn_into(&to->pkey.dsa->g, from->pkey.dsa->g)) {
361     return 0;
362   }
363 
364   return 1;
365 }
366 
dsa_cmp_parameters(const EVP_PKEY * a,const EVP_PKEY * b)367 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
368   return BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) == 0 &&
369          BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) == 0 &&
370          BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g) == 0;
371 }
372 
dsa_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)373 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
374   return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
375 }
376 
int_dsa_free(EVP_PKEY * pkey)377 static void int_dsa_free(EVP_PKEY *pkey) { DSA_free(pkey->pkey.dsa); }
378 
update_buflen(const BIGNUM * b,size_t * pbuflen)379 static void update_buflen(const BIGNUM *b, size_t *pbuflen) {
380   size_t i;
381 
382   if (!b) {
383     return;
384   }
385   i = BN_num_bytes(b);
386   if (*pbuflen < i) {
387     *pbuflen = i;
388   }
389 }
390 
do_dsa_print(BIO * bp,const DSA * x,int off,int ptype)391 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
392   uint8_t *m = NULL;
393   int ret = 0;
394   size_t buf_len = 0;
395   const char *ktype = NULL;
396 
397   const BIGNUM *priv_key, *pub_key;
398 
399   priv_key = NULL;
400   if (ptype == 2) {
401     priv_key = x->priv_key;
402   }
403 
404   pub_key = NULL;
405   if (ptype > 0) {
406     pub_key = x->pub_key;
407   }
408 
409   ktype = "DSA-Parameters";
410   if (ptype == 2) {
411     ktype = "Private-Key";
412   } else if (ptype == 1) {
413     ktype = "Public-Key";
414   }
415 
416   update_buflen(x->p, &buf_len);
417   update_buflen(x->q, &buf_len);
418   update_buflen(x->g, &buf_len);
419   update_buflen(priv_key, &buf_len);
420   update_buflen(pub_key, &buf_len);
421 
422   m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
423   if (m == NULL) {
424     OPENSSL_PUT_ERROR(EVP, do_dsa_print, ERR_R_MALLOC_FAILURE);
425     goto err;
426   }
427 
428   if (priv_key) {
429     if (!BIO_indent(bp, off, 128) ||
430         BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
431       goto err;
432     }
433   }
434 
435   if (!ASN1_bn_print(bp, "priv:", priv_key, m, off) ||
436       !ASN1_bn_print(bp, "pub: ", pub_key, m, off) ||
437       !ASN1_bn_print(bp, "P:   ", x->p, m, off) ||
438       !ASN1_bn_print(bp, "Q:   ", x->q, m, off) ||
439       !ASN1_bn_print(bp, "G:   ", x->g, m, off)) {
440     goto err;
441   }
442   ret = 1;
443 
444 err:
445   OPENSSL_free(m);
446   return ret;
447 }
448 
dsa_param_decode(EVP_PKEY * pkey,const uint8_t ** pder,int derlen)449 static int dsa_param_decode(EVP_PKEY *pkey, const uint8_t **pder, int derlen) {
450   DSA *dsa;
451   dsa = d2i_DSAparams(NULL, pder, derlen);
452   if (dsa == NULL) {
453     OPENSSL_PUT_ERROR(EVP, dsa_param_decode, ERR_R_DSA_LIB);
454     return 0;
455   }
456   EVP_PKEY_assign_DSA(pkey, dsa);
457   return 1;
458 }
459 
dsa_param_encode(const EVP_PKEY * pkey,uint8_t ** pder)460 static int dsa_param_encode(const EVP_PKEY *pkey, uint8_t **pder) {
461   return i2d_DSAparams(pkey->pkey.dsa, pder);
462 }
463 
dsa_param_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)464 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
465                            ASN1_PCTX *ctx) {
466   return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
467 }
468 
dsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)469 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
470                          ASN1_PCTX *ctx) {
471   return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
472 }
473 
dsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent,ASN1_PCTX * ctx)474 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
475                           ASN1_PCTX *ctx) {
476   return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
477 }
478 
old_dsa_priv_decode(EVP_PKEY * pkey,const uint8_t ** pder,int derlen)479 static int old_dsa_priv_decode(EVP_PKEY *pkey, const uint8_t **pder,
480                                int derlen) {
481   DSA *dsa;
482   dsa = d2i_DSAPrivateKey(NULL, pder, derlen);
483   if (dsa == NULL) {
484     OPENSSL_PUT_ERROR(EVP, old_dsa_priv_decode, ERR_R_DSA_LIB);
485     return 0;
486   }
487   EVP_PKEY_assign_DSA(pkey, dsa);
488   return 1;
489 }
490 
old_dsa_priv_encode(const EVP_PKEY * pkey,uint8_t ** pder)491 static int old_dsa_priv_encode(const EVP_PKEY *pkey, uint8_t **pder) {
492   return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
493 }
494 
dsa_sig_print(BIO * bp,const X509_ALGOR * sigalg,const ASN1_STRING * sig,int indent,ASN1_PCTX * pctx)495 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
496                          const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) {
497   DSA_SIG *dsa_sig;
498   const uint8_t *p;
499 
500   if (!sig) {
501     return BIO_puts(bp, "\n") > 0;
502   }
503 
504   p = sig->data;
505   dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
506   if (dsa_sig == NULL) {
507     return X509_signature_dump(bp, sig, indent);
508   }
509 
510   int rv = 0;
511   size_t buf_len = 0;
512   uint8_t *m = NULL;
513 
514   update_buflen(dsa_sig->r, &buf_len);
515   update_buflen(dsa_sig->s, &buf_len);
516   m = OPENSSL_malloc(buf_len + 10);
517   if (m == NULL) {
518     OPENSSL_PUT_ERROR(EVP, dsa_sig_print, ERR_R_MALLOC_FAILURE);
519     goto err;
520   }
521 
522   if (BIO_write(bp, "\n", 1) != 1 ||
523       !ASN1_bn_print(bp, "r:   ", dsa_sig->r, m, indent) ||
524       !ASN1_bn_print(bp, "s:   ", dsa_sig->s, m, indent)) {
525     goto err;
526   }
527   rv = 1;
528 
529 err:
530   OPENSSL_free(m);
531   DSA_SIG_free(dsa_sig);
532   return rv;
533 }
534 
535 const EVP_PKEY_ASN1_METHOD dsa_asn1_meth = {
536   EVP_PKEY_DSA,
537   EVP_PKEY_DSA,
538   0,
539 
540   "DSA",
541   "OpenSSL DSA method",
542 
543   dsa_pub_decode,
544   dsa_pub_encode,
545   dsa_pub_cmp,
546   dsa_pub_print,
547 
548   dsa_priv_decode,
549   dsa_priv_encode,
550   dsa_priv_print,
551 
552   NULL /* pkey_opaque */,
553   NULL /* pkey_supports_digest */,
554 
555   int_dsa_size,
556   dsa_bits,
557 
558   dsa_param_decode,
559   dsa_param_encode,
560   dsa_missing_parameters,
561   dsa_copy_parameters,
562   dsa_cmp_parameters,
563   dsa_param_print,
564   dsa_sig_print,
565 
566   int_dsa_free,
567   old_dsa_priv_decode,
568   old_dsa_priv_encode,
569 };
570