Keep a not of original encoding in certificate requests.
[oweals/openssl.git] / crypto / asn1 / x_pubkey.c
index 8adaeba82ced98f180bf035a85010411a042cb22..b2e2a514777094311ebfb93e2013e1c705b3793a 100644 (file)
@@ -112,7 +112,7 @@ void X509_PUBKEY_free(X509_PUBKEY *a)
        X509_ALGOR_free(a->algor);
        M_ASN1_BIT_STRING_free(a->public_key);
        if (a->pkey != NULL) EVP_PKEY_free(a->pkey);
-       Free((char *)a);
+       OPENSSL_free(a);
        }
 
 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
@@ -156,14 +156,14 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
                dsa->write_params=0;
                ASN1_TYPE_free(a->parameter);
                i=i2d_DSAparams(dsa,NULL);
-               p=(unsigned char *)Malloc(i);
+               p=(unsigned char *)OPENSSL_malloc(i);
                pp=p;
                i2d_DSAparams(dsa,&pp);
                a->parameter=ASN1_TYPE_new();
                a->parameter->type=V_ASN1_SEQUENCE;
                a->parameter->value.sequence=ASN1_STRING_new();
                ASN1_STRING_set(a->parameter->value.sequence,p,i);
-               Free(p);
+               OPENSSL_free(p);
                }
        else
 #endif
@@ -173,7 +173,7 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
                }
 
        if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
-       if ((s=(unsigned char *)Malloc(i+1)) == NULL) goto err;
+       if ((s=(unsigned char *)OPENSSL_malloc(i+1)) == NULL) goto err;
        p=s;
        i2d_PublicKey(pkey,&p);
        if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
@@ -181,10 +181,12 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
        pk->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
        pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
 
-       Free(s);
+       OPENSSL_free(s);
 
+#if 0
        CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
        pk->pkey=pkey;
+#endif
 
        if (*x != NULL)
                X509_PUBKEY_free(*x);
@@ -252,3 +254,113 @@ err:
        return(NULL);
        }
 
+/* Now two pseudo ASN1 routines that take an EVP_PKEY structure
+ * and encode or decode as X509_PUBKEY
+ */
+
+EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, unsigned char **pp,
+            long length)
+{
+       X509_PUBKEY *xpk;
+       EVP_PKEY *pktmp;
+       xpk = d2i_X509_PUBKEY(NULL, pp, length);
+       if(!xpk) return NULL;
+       pktmp = X509_PUBKEY_get(xpk);
+       X509_PUBKEY_free(xpk);
+       if(!pktmp) return NULL;
+       if(a) {
+               EVP_PKEY_free(*a);
+               *a = pktmp;
+       }
+       return pktmp;
+}
+
+int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
+{
+       X509_PUBKEY *xpk=NULL;
+       int ret;
+       if(!a) return 0;
+       if(!X509_PUBKEY_set(&xpk, a)) return 0;
+       ret = i2d_X509_PUBKEY(xpk, pp);
+       X509_PUBKEY_free(xpk);
+       return ret;
+}
+
+/* The following are equivalents but which return RSA and DSA
+ * keys
+ */
+#ifndef NO_RSA
+RSA *d2i_RSA_PUBKEY(RSA **a, unsigned char **pp,
+            long length)
+{
+       EVP_PKEY *pkey;
+       RSA *key;
+       unsigned char *q;
+       q = *pp;
+       pkey = d2i_PUBKEY(NULL, &q, length);
+       if(!pkey) return NULL;
+       key = EVP_PKEY_get1_RSA(pkey);
+       EVP_PKEY_free(pkey);
+       if(!key) return NULL;
+       *pp = q;
+       if(a) {
+               RSA_free(*a);
+               *a = key;
+       }
+       return key;
+}
+
+int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
+{
+       EVP_PKEY *pktmp;
+       int ret;
+       if(!a) return 0;
+       pktmp = EVP_PKEY_new();
+       if(!pktmp) {
+               ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
+               return 0;
+       }
+       EVP_PKEY_set1_RSA(pktmp, a);
+       ret = i2d_PUBKEY(pktmp, pp);
+       EVP_PKEY_free(pktmp);
+       return ret;
+}
+#endif
+
+#ifndef NO_DSA
+DSA *d2i_DSA_PUBKEY(DSA **a, unsigned char **pp,
+            long length)
+{
+       EVP_PKEY *pkey;
+       DSA *key;
+       unsigned char *q;
+       q = *pp;
+       pkey = d2i_PUBKEY(NULL, &q, length);
+       if(!pkey) return NULL;
+       key = EVP_PKEY_get1_DSA(pkey);
+       EVP_PKEY_free(pkey);
+       if(!key) return NULL;
+       *pp = q;
+       if(a) {
+               DSA_free(*a);
+               *a = key;
+       }
+       return key;
+}
+
+int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
+{
+       EVP_PKEY *pktmp;
+       int ret;
+       if(!a) return 0;
+       pktmp = EVP_PKEY_new();
+       if(!pktmp) {
+               ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
+               return 0;
+       }
+       EVP_PKEY_set1_DSA(pktmp, a);
+       ret = i2d_PUBKEY(pktmp, pp);
+       EVP_PKEY_free(pktmp);
+       return ret;
+}
+#endif