der2dsa.c 3.2 KB
Newer Older
1 2 3 4 5 6 7
/* der2dsa.c
 *
 * Decoding of DSA keys in OpenSSL and X509.1 format.
 */

/* nettle, low-level cryptographics library
 *
Niels Möller's avatar
Niels Möller committed
8
 * Copyright (C) 2005, 2009 Niels Möller, Magnus Holmgren
9 10 11 12 13 14 15 16 17 18 19 20 21
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
22 23
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "dsa.h"

#include "bignum.h"
#include "asn1.h"

#define GET(i, x, l)					\
(asn1_der_iterator_next((i)) == ASN1_ITERATOR_PRIMITIVE	\
 && (i)->type == ASN1_INTEGER				\
 && asn1_der_get_bignum((i), (x), (l))			\
 && mpz_sgn((x)) > 0)

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 48 49 50 51 52
	 p  INTEGER,
	 q  INTEGER,
	 g  INTEGER
     }
  */
  return (i->type == ASN1_INTEGER
53
	  && asn1_der_get_bignum(i, pub->p, p_max_bits)
54
	  && mpz_sgn(pub->p) > 0
55 56
	  && GET(i, pub->q, DSA_SHA1_Q_BITS)
	  && GET(i, pub->g, p_max_bits)
57
	  && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
58 59 60 61
}

int
dsa_public_key_from_der_iterator(struct dsa_public_key *pub,
62
				 unsigned p_max_bits,
63 64 65 66 67 68
				 struct asn1_der_iterator *i)
{
  /* DSAPublicKey ::= INTEGER
  */

  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
}

int
74 75
dsa_openssl_private_key_from_der_iterator(struct dsa_public_key *pub,
					  struct dsa_private_key *priv,
76
					  unsigned p_max_bits,
77
					  struct asn1_der_iterator *i)
78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
  /* DSAPrivateKey ::= SEQUENCE {
         version           Version,
	 p                 INTEGER,
	 q                 INTEGER,
	 g                 INTEGER,
	 pub_key           INTEGER,  -- y
	 priv_key          INTEGER,  -- x
    }
  */

  uint32_t version;
  
  return (i->type == ASN1_SEQUENCE
92
	  && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
93 94 95
	  && i->type == ASN1_INTEGER
	  && asn1_der_get_uint32(i, &version)
	  && version == 0
96 97 98 99 100
	  && GET(i, pub->p, p_max_bits)
	  && GET(i, pub->q, DSA_SHA1_Q_BITS)
	  && GET(i, pub->g, p_max_bits)
	  && GET(i, pub->y, p_max_bits)
	  && GET(i, priv->x, DSA_SHA1_Q_BITS)
101 102 103 104
	  && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
}

int
105
dsa_openssl_private_key_from_der(struct dsa_public_key *pub,
106
		     struct dsa_private_key *priv,
107
		     unsigned p_max_bits,
108 109 110 111 112 113 114
		     unsigned length, const uint8_t *data)
{
  struct asn1_der_iterator i;
  enum asn1_iterator_result res;

  res = asn1_der_iterator_first(&i, length, data);

115
  return (res == ASN1_ITERATOR_CONSTRUCTED
116
	  && dsa_openssl_private_key_from_der_iterator(pub, priv, p_max_bits, &i));
117
}