Fix things so DH_free() will be no-op when passed NULL, like RSA_free() and
[openssl.git] / crypto / asn1 / x_pubkey.c
index b1a2499d349425345116a2b4bb69f0393850902e..2177ebd6ed41e1954f780e4a537abb7c673416cb 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/asn1/x_pubkey.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
@@ -61,8 +61,8 @@
 #include "asn1_mac.h"
 
 /*
- * ASN1err(ASN1_F_D2I_X509_PUBKEY,ASN1_R_LENGTH_MISMATCH);
- * ASN1err(ASN1_F_X509_PUBKEY_NEW,ASN1_R_LENGTH_MISMATCH);
+ * ASN1err(ASN1_F_D2I_X509_PUBKEY,ERR_R_ASN1_LENGTH_MISMATCH);
+ * ASN1err(ASN1_F_X509_PUBKEY_NEW,ERR_R_ASN1_LENGTH_MISMATCH);
  */
 
 int i2d_X509_PUBKEY(a,pp)
@@ -104,6 +104,7 @@ long length;
 X509_PUBKEY *X509_PUBKEY_new()
        {
        X509_PUBKEY *ret=NULL;
+       ASN1_CTX c;
 
        M_ASN1_New_Malloc(ret,X509_PUBKEY);
        M_ASN1_New(ret->algor,X509_ALGOR_new);
@@ -182,11 +183,15 @@ EVP_PKEY *pkey;
                goto err;
                }
 
-       i=i2d_PublicKey(pkey,NULL);
+       if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
        if ((s=(unsigned char *)Malloc(i+1)) == NULL) goto err;
        p=s;
        i2d_PublicKey(pkey,&p);
        if (!ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
+       /* Set number of unused bits to zero */
+       pk->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
+       pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
+
        Free(s);
 
        CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
@@ -211,12 +216,20 @@ X509_PUBKEY *key;
        long j;
        int type;
        unsigned char *p;
+#ifndef NO_DSA
        X509_ALGOR *a;
+#endif
+
+       if (key == NULL) goto err;
 
        if (key->pkey != NULL)
-               {
-               return(key->pkey);
-               }
+           {
+           CRYPTO_add(&key->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
+           return(key->pkey);
+           }
+
+       if (key->public_key == NULL) goto err;
+
        type=OBJ_obj2nid(key->algor->algorithm);
        p=key->public_key->data;
         j=key->public_key->length;
@@ -243,6 +256,7 @@ X509_PUBKEY *key;
                }
 #endif
        key->pkey=ret;
+       CRYPTO_add(&ret->references,1,CRYPTO_LOCK_EVP_PKEY);
        return(ret);
 err:
        if (ret != NULL)