[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / der2dsa.c
1 /* der2dsa.c
2  *
3  * Decoding of DSA keys in OpenSSL and X509.1 format.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2005, 2009 Niels Möller, Magnus Holmgren
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "dsa.h"
31
32 #include "bignum.h"
33 #include "asn1.h"
34
35 #define GET(i, x, l)                                    \
36 (asn1_der_iterator_next((i)) == ASN1_ITERATOR_PRIMITIVE \
37  && (i)->type == ASN1_INTEGER                           \
38  && asn1_der_get_bignum((i), (x), (l))                  \
39  && mpz_sgn((x)) > 0)
40
41 int
42 dsa_params_from_der_iterator(struct dsa_public_key *pub,
43                              unsigned p_max_bits,
44                              struct asn1_der_iterator *i)
45 {
46   /* Dss-Parms ::= SEQUENCE {
47          p  INTEGER,
48          q  INTEGER,
49          g  INTEGER
50      }
51   */
52   return (i->type == ASN1_INTEGER
53           && asn1_der_get_bignum(i, pub->p, p_max_bits)
54           && mpz_sgn(pub->p) > 0
55           && GET(i, pub->q, DSA_SHA1_Q_BITS)
56           && GET(i, pub->g, p_max_bits)
57           && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
58 }
59
60 int
61 dsa_public_key_from_der_iterator(struct dsa_public_key *pub,
62                                  unsigned p_max_bits,
63                                  struct asn1_der_iterator *i)
64 {
65   /* DSAPublicKey ::= INTEGER
66   */
67
68   return (i->type == ASN1_INTEGER
69           && asn1_der_get_bignum(i, pub->y, p_max_bits)
70           && mpz_sgn(pub->y) > 0);
71 }
72
73 /* FIXME: Rename this and the next function to something
74    openssl-specific? */
75 int
76 dsa_openssl_private_key_from_der_iterator(struct dsa_public_key *pub,
77                                           struct dsa_private_key *priv,
78                                           unsigned p_max_bits,
79                                           struct asn1_der_iterator *i)
80 {
81   /* DSAPrivateKey ::= SEQUENCE {
82          version           Version,
83          p                 INTEGER,
84          q                 INTEGER,
85          g                 INTEGER,
86          pub_key           INTEGER,  -- y
87          priv_key          INTEGER,  -- x
88     }
89   */
90
91   uint32_t version;
92   
93   return (i->type == ASN1_SEQUENCE
94           && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
95           && i->type == ASN1_INTEGER
96           && asn1_der_get_uint32(i, &version)
97           && version == 0
98           && GET(i, pub->p, p_max_bits)
99           && GET(i, pub->q, DSA_SHA1_Q_BITS)
100           && GET(i, pub->g, p_max_bits)
101           && GET(i, pub->y, p_max_bits)
102           && GET(i, priv->x, DSA_SHA1_Q_BITS)
103           && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
104 }
105
106 int
107 dsa_openssl_private_key_from_der(struct dsa_public_key *pub,
108                      struct dsa_private_key *priv,
109                      unsigned p_max_bits,
110                      unsigned length, const uint8_t *data)
111 {
112   struct asn1_der_iterator i;
113   enum asn1_iterator_result res;
114
115   res = asn1_der_iterator_first(&i, length, data);
116
117   return (res == ASN1_ITERATOR_CONSTRUCTED
118           && dsa_openssl_private_key_from_der_iterator(pub, priv, p_max_bits, &i));
119 }