Revert the size_t modifications from HEAD that had led to more
[openssl.git] / crypto / rsa / rsa_ameth.c
index 287098029c8ed3103d813571797eff5742c770f3..8c3209885eab2f1bf3f4c18d3c5e06cf00cacfa2 100644 (file)
@@ -1,4 +1,5 @@
-/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+/* crypto/rsa/rsa_ameth.c */
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2006.
  */
 /* ====================================================================
 #include <openssl/asn1t.h>
 #include <openssl/x509.h>
 #include <openssl/rsa.h>
+#include <openssl/bn.h>
+#ifndef OPENSSL_NO_CMS
+#include <openssl/cms.h>
+#endif
+#include "asn1_locl.h"
 
 static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
        {
@@ -100,22 +106,24 @@ static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
        return 1;
        }
 
-static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
+static int old_rsa_priv_decode(EVP_PKEY *pkey,
+                                       const unsigned char **pder, int derlen)
        {
-       const unsigned char *p;
-       int pklen;
-       RSA *rsa = NULL;
-       if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
-               return 0;
-       if (!(rsa = d2i_RSAPrivateKey (NULL, &p, pklen)))
+       RSA *rsa;
+       if (!(rsa = d2i_RSAPrivateKey (NULL, pder, derlen)))
                {
-               RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
+               RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
                return 0;
                }
-       EVP_PKEY_assign_RSA (pkey, rsa);
+       EVP_PKEY_assign_RSA(pkey, rsa);
        return 1;
        }
 
+static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
+       {
+       return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
+       }
+
 static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
        {
        unsigned char *rk = NULL;
@@ -138,6 +146,15 @@ static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
        return 1;
        }
 
+static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
+       {
+       const unsigned char *p;
+       int pklen;
+       if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
+               return 0;
+       return old_rsa_priv_decode(pkey, &p, pklen);
+       }
+
 static int int_rsa_size(const EVP_PKEY *pkey)
        {
        return RSA_size(pkey->pkey.rsa);
@@ -156,7 +173,7 @@ static void int_rsa_free(EVP_PKEY *pkey)
 
 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,7 +204,7 @@ static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv)
        m=(unsigned char *)OPENSSL_malloc(buf_len+10);
        if (m == NULL)
                {
-               RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
+               RSAerr(RSA_F_DO_RSA_PRINT,ERR_R_MALLOC_FAILURE);
                goto err;
                }
 
@@ -248,12 +265,61 @@ static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
        return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
        }
 
+
+static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
+       {
+       X509_ALGOR *alg = NULL;
+       switch (op)
+               {
+
+               case ASN1_PKEY_CTRL_PKCS7_SIGN:
+               if (arg1 == 0)
+                       PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
+               break;
+
+               case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
+               if (arg1 == 0)
+                       PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
+               break;
+#ifndef OPENSSL_NO_CMS
+               case ASN1_PKEY_CTRL_CMS_SIGN:
+               if (arg1 == 0)
+                       CMS_SignerInfo_get0_algs(arg2, NULL, NULL, NULL, &alg);
+               break;
+
+               case ASN1_PKEY_CTRL_CMS_ENVELOPE:
+               if (arg1 == 0)
+                       CMS_RecipientInfo_ktri_get0_algs(arg2, NULL, NULL, &alg);
+               break;
+#endif
+
+               case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
+               *(int *)arg2 = NID_sha1;
+               return 1;
+
+               default:
+               return -2;
+
+               }
+
+       if (alg)
+               X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
+                                                       V_ASN1_NULL, 0);
+
+       return 1;
+
+       }
+
+
 const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] = 
        {
                {
                EVP_PKEY_RSA,
                EVP_PKEY_RSA,
-               0,
+               ASN1_PKEY_SIGPARAM_NULL,
+
+               "RSA",
+               "OpenSSL RSA method",
 
                rsa_pub_decode,
                rsa_pub_encode,
@@ -270,7 +336,9 @@ const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] =
                0,0,0,0,0,0,
 
                int_rsa_free,
-               0
+               rsa_pkey_ctrl,
+               old_rsa_priv_decode,
+               old_rsa_priv_encode
                },
 
                {