der2dsa.c 3.82 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
 * Copyright (C) 2014 Niels Möller
10 11 12 13 14 15 16 17 18 19 20 21 22
 *  
 * 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
23 24
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
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)

42
/* If q_bits > 0, q is required to be of exactly this size. */
43
int
44 45
dsa_params_from_der_iterator(struct dsa_params *params,
			     unsigned max_bits, unsigned q_bits,
46
			     struct asn1_der_iterator *i)
47
{
48
  /* Dss-Parms ::= SEQUENCE {
49 50 51 52 53
	 p  INTEGER,
	 q  INTEGER,
	 g  INTEGER
     }
  */
54 55 56 57 58 59 60 61 62 63 64 65 66 67
  if (i->type == ASN1_INTEGER
      && asn1_der_get_bignum(i, params->p, max_bits)
      && mpz_sgn(params->p) > 0)
    {
      unsigned p_bits = mpz_sizeinbase (params->p, 2);
      return (GET(i, params->q, q_bits ? q_bits : p_bits)
	      && (q_bits == 0 || mpz_sizeinbase(params->q, 2) == q_bits)
	      && mpz_cmp (params->q, params->p) < 0
	      && GET(i, params->g, p_bits)
	      && mpz_cmp (params->g, params->p) < 0
	      && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
    }
  else
    return 0;
68 69 70
}

int
71 72
dsa_public_key_from_der_iterator(const struct dsa_params *params,
				 mpz_t pub,
73 74 75 76 77 78
				 struct asn1_der_iterator *i)
{
  /* DSAPublicKey ::= INTEGER
  */

  return (i->type == ASN1_INTEGER
79 80 81 82
	  && asn1_der_get_bignum(i, pub,
				 mpz_sizeinbase (params->p, 2))
	  && mpz_sgn(pub) > 0
	  && mpz_cmp(pub, params->p) < 0);
83 84 85
}

int
86 87 88
dsa_openssl_private_key_from_der_iterator(struct dsa_params *params,
					  mpz_t pub,
					  mpz_t priv,
89
					  unsigned p_max_bits,
90
					  struct asn1_der_iterator *i)
91 92 93 94 95 96 97 98 99 100 101 102
{
  /* DSAPrivateKey ::= SEQUENCE {
         version           Version,
	 p                 INTEGER,
	 q                 INTEGER,
	 g                 INTEGER,
	 pub_key           INTEGER,  -- y
	 priv_key          INTEGER,  -- x
    }
  */

  uint32_t version;
103 104

  if (i->type == ASN1_SEQUENCE
105
	  && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
106 107 108
	  && i->type == ASN1_INTEGER
	  && asn1_der_get_uint32(i, &version)
	  && version == 0
109 110 111 112 113 114 115 116 117 118 119 120 121
      && GET(i, params->p, p_max_bits))
    {
      unsigned p_bits = mpz_sizeinbase (params->p, 2);
      return (GET(i, params->q, DSA_SHA1_Q_BITS)
	      && GET(i, params->g, p_bits)
	      && mpz_cmp (params->g, params->p) < 0
	      && GET(i, pub, p_bits)
	      && mpz_cmp (pub, params->p) < 0
	      && GET(i, priv, DSA_SHA1_Q_BITS)
	      && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
    }
  else
    return 0;
122 123 124
}

int
125 126 127
dsa_openssl_private_key_from_der(struct dsa_params *params,
				 mpz_t pub,
				 mpz_t priv,
Niels Möller's avatar
Niels Möller committed
128 129
				 unsigned p_max_bits,
				 size_t length, const uint8_t *data)
130 131 132 133 134 135
{
  struct asn1_der_iterator i;
  enum asn1_iterator_result res;

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

136
  return (res == ASN1_ITERATOR_CONSTRUCTED
137 138
	  && dsa_openssl_private_key_from_der_iterator(params, pub, priv,
						       p_max_bits, &i));
139
}