1 /* der2dsa.c
2 
3    Decoding of DSA keys in OpenSSL and X.509.1 format.
4 
5    Copyright (C) 2005, 2009 Niels Möller, Magnus Holmgren
6    Copyright (C) 2014 Niels Möller
7 
8    This file is part of GNU Nettle.
9 
10    GNU Nettle is free software: you can redistribute it and/or
11    modify it under the terms of either:
12 
13      * the GNU Lesser General Public License as published by the Free
14        Software Foundation; either version 3 of the License, or (at your
15        option) any later version.
16 
17    or
18 
19      * the GNU General Public License as published by the Free
20        Software Foundation; either version 2 of the License, or (at your
21        option) any later version.
22 
23    or both in parallel, as here.
24 
25    GNU Nettle is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28    General Public License for more details.
29 
30    You should have received copies of the GNU General Public License and
31    the GNU Lesser General Public License along with this program.  If
32    not, see http://www.gnu.org/licenses/.
33 */
34 
35 #if HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38 
39 #include "dsa.h"
40 
41 #include "bignum.h"
42 #include "asn1.h"
43 
44 #define GET(i, x, l)					\
45 (asn1_der_iterator_next((i)) == ASN1_ITERATOR_PRIMITIVE	\
46  && (i)->type == ASN1_INTEGER				\
47  && asn1_der_get_bignum((i), (x), (l))			\
48  && mpz_sgn((x)) > 0)
49 
50 /* If q_bits > 0, q is required to be of exactly this size. */
51 int
dsa_params_from_der_iterator(struct dsa_params * params,unsigned max_bits,unsigned q_bits,struct asn1_der_iterator * i)52 dsa_params_from_der_iterator(struct dsa_params *params,
53 			     unsigned max_bits, unsigned q_bits,
54 			     struct asn1_der_iterator *i)
55 {
56   /* Dss-Parms ::= SEQUENCE {
57 	 p  INTEGER,
58 	 q  INTEGER,
59 	 g  INTEGER
60      }
61   */
62   if (i->type == ASN1_INTEGER
63       && asn1_der_get_bignum(i, params->p, max_bits)
64       && mpz_sgn(params->p) > 0)
65     {
66       unsigned p_bits = mpz_sizeinbase (params->p, 2);
67       return (GET(i, params->q, q_bits ? q_bits : p_bits)
68 	      && (q_bits == 0 || mpz_sizeinbase(params->q, 2) == q_bits)
69 	      && mpz_cmp (params->q, params->p) < 0
70 	      && GET(i, params->g, p_bits)
71 	      && mpz_cmp (params->g, params->p) < 0
72 	      && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
73     }
74   else
75     return 0;
76 }
77 
78 int
dsa_public_key_from_der_iterator(const struct dsa_params * params,mpz_t pub,struct asn1_der_iterator * i)79 dsa_public_key_from_der_iterator(const struct dsa_params *params,
80 				 mpz_t pub,
81 				 struct asn1_der_iterator *i)
82 {
83   /* DSAPublicKey ::= INTEGER
84   */
85 
86   return (i->type == ASN1_INTEGER
87 	  && asn1_der_get_bignum(i, pub,
88 				 mpz_sizeinbase (params->p, 2))
89 	  && mpz_sgn(pub) > 0
90 	  && mpz_cmp(pub, params->p) < 0);
91 }
92 
93 int
dsa_openssl_private_key_from_der_iterator(struct dsa_params * params,mpz_t pub,mpz_t priv,unsigned p_max_bits,struct asn1_der_iterator * i)94 dsa_openssl_private_key_from_der_iterator(struct dsa_params *params,
95 					  mpz_t pub,
96 					  mpz_t priv,
97 					  unsigned p_max_bits,
98 					  struct asn1_der_iterator *i)
99 {
100   /* DSAPrivateKey ::= SEQUENCE {
101          version           Version,
102 	 p                 INTEGER,
103 	 q                 INTEGER,
104 	 g                 INTEGER,
105 	 pub_key           INTEGER,  -- y
106 	 priv_key          INTEGER,  -- x
107     }
108   */
109 
110   uint32_t version;
111 
112   if (i->type == ASN1_SEQUENCE
113 	  && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
114 	  && i->type == ASN1_INTEGER
115 	  && asn1_der_get_uint32(i, &version)
116 	  && version == 0
117       && GET(i, params->p, p_max_bits))
118     {
119       unsigned p_bits = mpz_sizeinbase (params->p, 2);
120       return (GET(i, params->q, DSA_SHA1_Q_BITS)
121 	      && GET(i, params->g, p_bits)
122 	      && mpz_cmp (params->g, params->p) < 0
123 	      && GET(i, pub, p_bits)
124 	      && mpz_cmp (pub, params->p) < 0
125 	      && GET(i, priv, DSA_SHA1_Q_BITS)
126 	      && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
127     }
128   else
129     return 0;
130 }
131 
132 int
dsa_openssl_private_key_from_der(struct dsa_params * params,mpz_t pub,mpz_t priv,unsigned p_max_bits,size_t length,const uint8_t * data)133 dsa_openssl_private_key_from_der(struct dsa_params *params,
134 				 mpz_t pub,
135 				 mpz_t priv,
136 				 unsigned p_max_bits,
137 				 size_t length, const uint8_t *data)
138 {
139   struct asn1_der_iterator i;
140   enum asn1_iterator_result res;
141 
142   res = asn1_der_iterator_first(&i, length, data);
143 
144   return (res == ASN1_ITERATOR_CONSTRUCTED
145 	  && dsa_openssl_private_key_from_der_iterator(params, pub, priv,
146 						       p_max_bits, &i));
147 }
148