Esempio n. 1
0
int
rsa_private_key_from_der_iterator(struct rsa_public_key *pub,
				  struct rsa_private_key *priv,
				  unsigned limit,
				  struct asn1_der_iterator *i)
{
  /* RSAPrivateKey ::= SEQUENCE {
         version           Version,
	 modulus           INTEGER,  -- n
	 publicExponent    INTEGER,  -- e
	 privateExponent   INTEGER,  -- d
	 prime1            INTEGER,  -- p
	 prime2            INTEGER,  -- q
	 exponent1         INTEGER,  -- d mod (p-1)
	 exponent2         INTEGER,  -- d mod (q-1)
	 coefficient       INTEGER,  -- (inverse of q) mod p
	 otherPrimeInfos   OtherPrimeInfos OPTIONAL
    }
  */

  uint32_t version;
  
  if (i->type != ASN1_SEQUENCE)
    return 0;

  if (asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
      && i->type == ASN1_INTEGER
      && asn1_der_get_uint32(i, &version)
      && version <= 1
      && GET(i, pub->n, limit)
      && GET(i, pub->e, limit)
      && rsa_public_key_prepare(pub)
      && GET(i, priv->d, limit)
      && GET(i, priv->p, limit)
      && GET(i, priv->q, limit)
      && GET(i, priv->a, limit)
      && GET(i, priv->b, limit)
      && GET(i, priv->c, limit)
      && rsa_private_key_prepare(priv))
    {
      if (version == 1)
	{
	  /* otherPrimeInfos must be present. We ignore the contents */
	  if (!(asn1_der_iterator_next(i) == ASN1_ITERATOR_CONSTRUCTED
		&& i->type == ASN1_SEQUENCE))
	    return 0;
	}

      return (asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
    }
  
  return 0;
}
Esempio n. 2
0
int
rsa_public_key_from_der_iterator(struct rsa_public_key *pub,
				 unsigned limit,
				 struct asn1_der_iterator *i)
{
  /* RSAPublicKey ::= SEQUENCE {
         modulus           INTEGER,  -- n
	 publicExponent    INTEGER   -- e
      }
  */

  return (i->type == ASN1_SEQUENCE
	  && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
	  && asn1_der_get_bignum(i, pub->n, limit) 
	  && mpz_sgn(pub->n) > 0
	  && GET(i, pub->e, limit)
	  && asn1_der_iterator_next(i) == ASN1_ITERATOR_END
	  && rsa_public_key_prepare(pub));
}
/* Returns 1 on success, 0 on error, and -1 for unsupported algorithms. */
static int
convert_public_key(struct nettle_buffer *buffer, unsigned length, const uint8_t *data)
{
  /* SubjectPublicKeyInfo ::= SEQUENCE {
         algorithm		AlgorithmIdentifier,
	 subjectPublicKey 	BIT STRING
     }

     AlgorithmIdentifier ::= SEQUENCE {
         algorithm  	OBJECT IDENTIFIER,
	 parameters 	OPTIONAL
     }
  */
  struct asn1_der_iterator i;
  struct asn1_der_iterator j;
  int res = 0;

  if (asn1_der_iterator_first(&i, length, data) == ASN1_ITERATOR_CONSTRUCTED
      && i.type == ASN1_SEQUENCE
      && asn1_der_decode_constructed_last(&i) == ASN1_ITERATOR_CONSTRUCTED
      && i.type == ASN1_SEQUENCE

      /* Use the j iterator to parse the algorithm identifier */
      && asn1_der_decode_constructed(&i, &j) == ASN1_ITERATOR_PRIMITIVE
      && j.type == ASN1_IDENTIFIER
      && asn1_der_iterator_next(&i) == ASN1_ITERATOR_PRIMITIVE
      && i.type == ASN1_BITSTRING

      /* Use i to parse the object wrapped in the bit string.*/
      && asn1_der_decode_bitstring_last(&i))
    {
      /* pkcs-1 {
	     iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-1(1)
	     modules(0) pkcs-1(1)
	 }

	 --
	 -- When rsaEncryption is used in an AlgorithmIdentifier the
	 -- parameters MUST be present and MUST be NULL.
	 --
	 rsaEncryption    OBJECT IDENTIFIER ::= { pkcs-1 1 }
      */
      static const uint8_t id_rsaEncryption[9] =
	{ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };
      /*
	 --
	 -- When dsa is used in an AlgorithmIdentifier the
	 -- parameters MUST be present and MUST NOT be NULL.
	 --
	 dsa    OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) x9-57(10040) x9algorithm(4) 1 }
      */
      static const uint8_t id_dsa[7] =
	{ 0x2A, 0x86, 0x48, 0xCE, 0x38, 0x04, 0x01 };

      switch (j.length)
	{
	unknown:
	default:
	  werror("SubjectPublicKeyInfo: Unsupported algorithm.\n");
	  res = -1;
	  break;
	  
	case 7:
	  if (memcmp(j.data, id_dsa, 7) == 0)
	    {
	      if (asn1_der_iterator_next(&j) == ASN1_ITERATOR_CONSTRUCTED
		  && asn1_der_decode_constructed_last(&j) == ASN1_ITERATOR_PRIMITIVE)
		{
		  struct dsa_public_key pub;

		  dsa_public_key_init(&pub);

		  if (dsa_params_from_der_iterator(&pub, 0, &i)
		      && dsa_public_key_from_der_iterator(&pub, 0, &j))
		    {
		      nettle_buffer_reset(buffer);
		      res = dsa_keypair_to_sexp(buffer, NULL, &pub, NULL) > 0;
		    }
		}
	      if (!res)
		werror("SubjectPublicKeyInfo: Invalid DSA key.\n");
	      break;
	    }
	  else goto unknown;
	case 9:
	  if (memcmp(j.data, id_rsaEncryption, 9) == 0)
	    {
	      if (asn1_der_iterator_next(&j) == ASN1_ITERATOR_PRIMITIVE
		  && j.type == ASN1_NULL
		  && j.length == 0
		  && asn1_der_iterator_next(&j) == ASN1_ITERATOR_END)
		{
		  struct rsa_public_key pub;

		  rsa_public_key_init(&pub);

		  if (rsa_public_key_from_der_iterator(&pub, 0, &i))
		    {
		      nettle_buffer_reset(buffer);
		      res = rsa_keypair_to_sexp(buffer, NULL, &pub, NULL) > 0;
		    }
		}
	      if (!res)
		werror("SubjectPublicKeyInfo: Invalid RSA key.\n");
	      break;
	    }
	  else goto unknown;
	}
    }
  else
    werror("SubjectPublicKeyInfo: Invalid object.\n");
  
  return res;
}