Initial experimental support for X9.42 DH parameter format to handle
[openssl.git] / crypto / dh / dh_ameth.c
index ba22f34d403552ff7db16e059cef631221d6512d..7d1254ecd86b354adae7be05ad3247f1fe74b14d 100644 (file)
@@ -1,4 +1,4 @@
-/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2006.
  */
 /* ====================================================================
 #include <openssl/x509.h>
 #include <openssl/asn1.h>
 #include <openssl/dh.h>
+#include <openssl/bn.h>
 #include "asn1_locl.h"
 
+extern const EVP_PKEY_ASN1_METHOD dhx_asn1_meth;
+
+/* i2d/d2i like DH parameter functions which use the appropriate routine
+ * for PKCS#3 DH or X9.42 DH.
+ */
+
+static DH * d2i_dhp(const EVP_PKEY *pkey, const unsigned char **pp, long length)
+       {
+       if (pkey->ameth == &dhx_asn1_meth)
+               return d2i_DHxparams(NULL, pp, length);
+       return d2i_DHparams(NULL, pp, length);
+       }
+
+static int i2d_dhp(const EVP_PKEY *pkey, const DH *a, unsigned char **pp)
+       {
+       if (pkey->ameth == &dhx_asn1_meth)
+               return i2d_DHxparams(a, pp);
+       return i2d_DHparams(a, pp);
+       }
+
 static void int_dh_free(EVP_PKEY *pkey)
        {
        DH_free(pkey->pkey.dh);
@@ -93,7 +114,7 @@ static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
        pm = pstr->data;
        pmlen = pstr->length;
 
-       if (!(dh = d2i_DHparams(NULL, &pm, pmlen)))
+       if (!(dh = d2i_dhp(pkey, &pm, pmlen)))
                {
                DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
                goto err;
@@ -113,11 +134,11 @@ static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
                }
 
        ASN1_INTEGER_free(public_key);
-       EVP_PKEY_assign_DH(pkey, dh);
+       EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
        return 1;
 
        err:
-       if (pubkey)
+       if (public_key)
                ASN1_INTEGER_free(public_key);
        if (dh)
                DH_free(dh);
@@ -138,7 +159,7 @@ static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
        dh=pkey->pkey.dh;
 
        str = ASN1_STRING_new();
-       str->length = i2d_DHparams(dh, &str->data);
+       str->length = i2d_dhp(pkey, dh, &str->data);
        if (str->length <= 0)
                {
                DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
@@ -161,7 +182,7 @@ static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
                goto err;
                }
 
-       if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DH),
+       if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
                                ptype, pval, penc, penclen))
                return 1;
 
@@ -174,9 +195,120 @@ static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
        return 0;
        }
 
+
+/* PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in
+ * that the AlgorithmIdentifier contains the paramaters, the private key
+ * is explcitly included and the pubkey must be recalculated.
+ */
+       
+static int dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
+       {
+       const unsigned char *p, *pm;
+       int pklen, pmlen;
+       int ptype;
+       void *pval;
+       ASN1_STRING *pstr;
+       X509_ALGOR *palg;
+       ASN1_INTEGER *privkey = NULL;
+
+       DH *dh = NULL;
+
+       if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
+               return 0;
+
+       X509_ALGOR_get0(NULL, &ptype, &pval, palg);
+
+       if (ptype != V_ASN1_SEQUENCE)
+                       goto decerr;
+
+       if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen)))
+               goto decerr;
+
+
+       pstr = pval;    
+       pm = pstr->data;
+       pmlen = pstr->length;
+       if (!(dh = d2i_dhp(pkey, &pm, pmlen)))
+               goto decerr;
+       /* We have parameters now set private key */
+       if (!(dh->priv_key = ASN1_INTEGER_to_BN(privkey, NULL)))
+               {
+               DHerr(DH_F_DH_PRIV_DECODE,DH_R_BN_ERROR);
+               goto dherr;
+               }
+       /* Calculate public key */
+       if (!DH_generate_key(dh))
+               goto dherr;
+
+       EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
+
+       ASN1_INTEGER_free(privkey);
+
+       return 1;
+
+       decerr:
+       DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR);
+       dherr:
+       DH_free(dh);
+       return 0;
+       }
+
+static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
+{
+       ASN1_STRING *params = NULL;
+       ASN1_INTEGER *prkey = NULL;
+       unsigned char *dp = NULL;
+       int dplen;
+
+       params = ASN1_STRING_new();
+
+       if (!params)
+               {
+               DHerr(DH_F_DH_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
+               goto err;
+               }
+
+       params->length = i2d_dhp(pkey, pkey->pkey.dh, &params->data);
+       if (params->length <= 0)
+               {
+               DHerr(DH_F_DH_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
+               goto err;
+               }
+       params->type = V_ASN1_SEQUENCE;
+
+       /* Get private key into integer */
+       prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL);
+
+       if (!prkey)
+               {
+               DHerr(DH_F_DH_PRIV_ENCODE,DH_R_BN_ERROR);
+               goto err;
+               }
+
+       dplen = i2d_ASN1_INTEGER(prkey, &dp);
+
+       ASN1_INTEGER_free(prkey);
+
+       if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
+                               V_ASN1_SEQUENCE, params, dp, dplen))
+               goto err;
+
+       return 1;
+
+err:
+       if (dp != NULL)
+               OPENSSL_free(dp);
+       if (params != NULL)
+               ASN1_STRING_free(params);
+       if (prkey != NULL)
+               ASN1_INTEGER_free(prkey);
+       return 0;
+}
+
+
 static void update_buflen(const BIGNUM *b, size_t *pbuflen)
        {
-       int i;
+       size_t i;
        if (!b)
                return;
        if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
@@ -187,18 +319,18 @@ static int dh_param_decode(EVP_PKEY *pkey,
                                        const unsigned char **pder, int derlen)
        {
        DH *dh;
-       if (!(dh = d2i_DHparams(NULL, pder, derlen)))
+       if (!(dh = d2i_dhp(pkey, pder, derlen)))
                {
                DHerr(DH_F_DH_PARAM_DECODE, ERR_R_DH_LIB);
                return 0;
                }
-       EVP_PKEY_assign_DH(pkey, dh);
+       EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
        return 1;
        }
 
 static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
        {
-       return i2d_DHparams(pkey->pkey.dh, pder);
+       return i2d_dhp(pkey, pkey->pkey.dh, pder);
        }
 
 static int do_dh_print(BIO *bp, const DH *x, int indent,
@@ -235,11 +367,11 @@ static int do_dh_print(BIO *bp, const DH *x, int indent,
        update_buflen(priv_key, &buf_len);
 
        if (ptype == 2)
-               ktype = "PKCS#3 DH Private-Key";
+               ktype = "DH Private-Key";
        else if (ptype == 1)
-               ktype = "PKCS#3 DH Public-Key";
+               ktype = "DH Public-Key";
        else
-               ktype = "PKCS#3 DH Parameters";
+               ktype = "DH Parameters";
 
        m= OPENSSL_malloc(buf_len+10);
        if (m == NULL)
@@ -270,7 +402,7 @@ static int do_dh_print(BIO *bp, const DH *x, int indent,
        if (0)
                {
 err:
-               DHerr(DH_F_DHPARAMS_PRINT,reason);
+               DHerr(DH_F_DO_DH_PRINT,reason);
                }
        if (m != NULL) OPENSSL_free(m);
        return(ret);
@@ -286,32 +418,52 @@ static int dh_bits(const EVP_PKEY *pkey)
        return BN_num_bits(pkey->pkey.dh->p);
        }
 
+static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
+       {
+       if (    BN_cmp(a->pkey.dh->p,b->pkey.dh->p) ||
+               BN_cmp(a->pkey.dh->g,b->pkey.dh->g))
+               return 0;
+       else if (a->ameth == &dhx_asn1_meth)
+               {
+               if (BN_cmp(a->pkey.dh->q,b->pkey.dh->q))
+                       return 0;
+               }
+       return 1;
+       }
+
 static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
        {
        BIGNUM *a;
 
        if ((a=BN_dup(from->pkey.dh->p)) == NULL)
                return 0;
-       if (to->pkey.dsa->p != NULL)
+       if (to->pkey.dh->p != NULL)
                BN_free(to->pkey.dh->p);
-       to->pkey.dsa->p=a;
+       to->pkey.dh->p=a;
 
        if ((a=BN_dup(from->pkey.dh->g)) == NULL)
                return 0;
-       if (to->pkey.dsa->g != NULL)
+       if (to->pkey.dh->g != NULL)
                BN_free(to->pkey.dh->g);
        to->pkey.dh->g=a;
+       if (from->ameth == &dhx_asn1_meth)
+               {
+               a = BN_dup(from->pkey.dh->q);
+               if (!a)
+                       return 0;
+               if (to->pkey.dh->q)
+                       BN_free(to->pkey.dh->q);
+               to->pkey.dh->q = a;
+               }
 
        return 1;
        }
 
-static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
+static int dh_missing_parameters(const EVP_PKEY *a)
        {
-       if (    BN_cmp(a->pkey.dh->p,b->pkey.dsa->p) ||
-               BN_cmp(a->pkey.dh->g,b->pkey.dsa->g))
-               return 0;
-       else
+       if (!a->pkey.dh->p || !a->pkey.dh->g)
                return 1;
+       return 0;
        }
 
 static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
@@ -361,8 +513,41 @@ const EVP_PKEY_ASN1_METHOD dh_asn1_meth =
        dh_pub_cmp,
        dh_public_print,
 
+       dh_priv_decode,
+       dh_priv_encode,
+       dh_private_print,
+
+       int_dh_size,
+       dh_bits,
+
+       dh_param_decode,
+       dh_param_encode,
+       dh_missing_parameters,
+       dh_copy_parameters,
+       dh_cmp_parameters,
+       dh_param_print,
        0,
+
+       int_dh_free,
+       0
+       };
+
+const EVP_PKEY_ASN1_METHOD dhx_asn1_meth = 
+       {
+       EVP_PKEY_DHX,
+       EVP_PKEY_DHX,
        0,
+
+       "X9.42 DH",
+       "OpenSSL X9.42 DH method",
+
+       dh_pub_decode,
+       dh_pub_encode,
+       dh_pub_cmp,
+       dh_public_print,
+
+       dh_priv_decode,
+       dh_priv_encode,
        dh_private_print,
 
        int_dh_size,
@@ -370,10 +555,11 @@ const EVP_PKEY_ASN1_METHOD dh_asn1_meth =
 
        dh_param_decode,
        dh_param_encode,
-       0,
+       dh_missing_parameters,
        dh_copy_parameters,
        dh_cmp_parameters,
        dh_param_print,
+       0,
 
        int_dh_free,
        0