Use the generated error code files.
[openssl.git] / demos / engines / rsaref / rsaref.c
index bc68a711e8fc7cf61cd2c4d1b4cd850d1b4cf131..f013d59e1bacd4f1e463b30de57302db213a4071 100644 (file)
@@ -3,15 +3,16 @@
    be found a little here and there. */
 
 #include <stdio.h>
-#if 0
 #include "./source/global.h"
 #include "./source/rsaref.h"
-#endif
-#include "rsaref.h"
+#include "./source/rsa.h"
 #include <openssl/err.h>
 #include <openssl/bn.h>
 #include <openssl/engine.h>
 
+#define RSAREF_LIB_NAME "rsaref engine"
+#include "rsaref_err.c"
+
 /* Constants used when creating the ENGINE */
 static const char *engine_rsaref_id = "rsaref";
 static const char *engine_rsaref_name = "RSAref engine support";
@@ -56,95 +57,6 @@ static RSA_METHOD rsaref_rsa =
   NULL
 };
 
-#ifndef OPENSSL_NO_ERR
-/* Error function codes for use in rsaref operation */
-#define RSAREF_F_BNREF_MOD_EXP                          100
-#define RSAREF_F_RSAREF_BN2BIN                          101
-#define RSAREF_F_RSA_BN2BIN                             102
-#define RSAREF_F_RSA_PRIVATE_DECRYPT                    103
-#define RSAREF_F_RSA_PRIVATE_ENCRYPT                    104
-#define RSAREF_F_RSA_PUBLIC_DECRYPT                     105
-#define RSAREF_F_RSA_PUBLIC_ENCRYPT                     106
-#define RSAREF_F_RSAREF_MOD_EXP                                 108
-#define RSAREF_F_RSAREF_PRIVATE_DECRYPT                         109
-#define RSAREF_F_RSAREF_PRIVATE_ENCRYPT                         110
-#define RSAREF_F_RSAREF_PUBLIC_DECRYPT                  111
-#define RSAREF_F_RSAREF_PUBLIC_ENCRYPT                  112
-/* Error reason codes */
-#define RSAREF_R_CONTENT_ENCODING                       0x0400
-#define RSAREF_R_DATA                                   0x0401
-#define RSAREF_R_DIGEST_ALGORITHM                       0x0402
-#define RSAREF_R_ENCODING                               0x0403
-#define RSAREF_R_ENCRYPTION_ALGORITHM                   0x040d
-#define RSAREF_R_KEY                                    0x0404
-#define RSAREF_R_KEY_ENCODING                           0x0405
-#define RSAREF_R_LEN                                    0x0406
-#define RSAREF_R_MODULUS_LEN                            0x0407
-#define RSAREF_R_NEED_RANDOM                            0x0408
-#define RSAREF_R_PRIVATE_KEY                            0x0409
-#define RSAREF_R_PUBLIC_KEY                             0x040a
-#define RSAREF_R_SIGNATURE                              0x040b
-#define RSAREF_R_SIGNATURE_ENCODING                     0x040c
-
-static ERR_STRING_DATA rsaref_str_functs[] =
-       {
-       /* This first element is changed to match the dynamic 'lib' number */
-{ERR_PACK(0,0,0),                              "rsaref engine code"},
-{ERR_PACK(0,RSAREF_F_BNREF_MOD_EXP,0), "BN_REF_MOD_EXP"},
-{ERR_PACK(0,RSAREF_F_RSAREF_BN2BIN,0), "RSAREF_BN2BIN"},
-{ERR_PACK(0,RSAREF_F_RSA_BN2BIN,0),    "RSA_BN2BIN"},
-{ERR_PACK(0,RSAREF_F_RSA_PRIVATE_DECRYPT,0),   "RSA_private_decrypt"},
-{ERR_PACK(0,RSAREF_F_RSA_PRIVATE_ENCRYPT,0),   "RSA_private_encrypt"},
-{ERR_PACK(0,RSAREF_F_RSA_PUBLIC_DECRYPT,0),    "RSA_public_decrypt"},
-{ERR_PACK(0,RSAREF_F_RSA_PUBLIC_ENCRYPT,0),    "RSA_public_encrypt"},
-{ERR_PACK(0,RSAREF_F_RSAREF_MOD_EXP,0),        "RSA_REF_MOD_EXP"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PRIVATE_DECRYPT,0),        "RSA_REF_PRIVATE_DECRYPT"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PRIVATE_ENCRYPT,0),        "RSA_REF_PRIVATE_ENCRYPT"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PUBLIC_DECRYPT,0), "RSA_REF_PUBLIC_DECRYPT"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PUBLIC_ENCRYPT,0), "RSA_REF_PUBLIC_ENCRYPT"},
-{RSAREF_R_CONTENT_ENCODING               ,"content encoding"},
-{RSAREF_R_DATA                           ,"data"},
-{RSAREF_R_DIGEST_ALGORITHM               ,"digest algorithm"},
-{RSAREF_R_ENCODING                       ,"encoding"},
-{RSAREF_R_ENCRYPTION_ALGORITHM           ,"encryption algorithm"},
-{RSAREF_R_KEY                            ,"key"},
-{RSAREF_R_KEY_ENCODING                   ,"key encoding"},
-{RSAREF_R_LEN                            ,"len"},
-{RSAREF_R_MODULUS_LEN                    ,"modulus len"},
-{RSAREF_R_NEED_RANDOM                    ,"need random"},
-{RSAREF_R_PRIVATE_KEY                    ,"private key"},
-{RSAREF_R_PUBLIC_KEY                     ,"public key"},
-{RSAREF_R_SIGNATURE                      ,"signature"},
-{RSAREF_R_SIGNATURE_ENCODING             ,"signature encoding"},
-{0,NULL}
-       };
-/* The library number we obtain dynamically from the ERR code */
-static int rsaref_err_lib = -1;
-#define RSAREFerr(f,r) ERR_PUT_error(rsaref_err_lib,(f),(r),__FILE__,__LINE__)
-static void rsaref_load_error_strings(void)
-       {
-       if(rsaref_err_lib < 0)
-               {
-               if((rsaref_err_lib = ERR_get_next_error_library()) <= 0)
-                       return;
-               rsaref_str_functs[0].error = ERR_PACK(rsaref_err_lib,0,0);
-               ERR_load_strings(rsaref_err_lib, rsaref_str_functs);
-               }
-       }
-static void rsaref_unload_error_strings(void)
-       {
-       if(rsaref_err_lib >= 0)
-               {
-               ERR_unload_strings(rsaref_err_lib, rsaref_str_functs);
-               rsaref_err_lib = -1;
-               }
-       }
-#else
-#define RSAREFerr(f,r)                                 /* NOP */
-static void rsaref_load_error_strings(void) { }                /* NOP */
-static void rsaref_unload_error_strings(void) { }      /* NOP */
-#endif
-
 /* Now, to our own code */
 
 static int bind_rsaref(ENGINE *e)
@@ -161,7 +73,7 @@ static int bind_rsaref(ENGINE *e)
                return 0;
 
        /* Ensure the rsaref error handling is set up */
-       rsaref_load_error_strings();
+       ERR_load_RSAREF_strings();
        return 1;
        }
 
@@ -216,7 +128,7 @@ static int rsaref_finish(ENGINE *e)
 /* Destructor (complements the "ENGINE_ncipher()" constructor) */
 static int rsaref_destroy(ENGINE *e)
        {
-       rsaref_unload_error_strings();
+       ERR_unload_RSAREF_strings();
        return 1;
        }
 
@@ -274,11 +186,11 @@ static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
        }
 #endif
 
-static int RSAref_Public_eay2ref(RSA *from, RSArefPublicKey *to)
+static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY *to)
        {
        to->bits=BN_num_bits(from->n);
-       if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
+       if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->e,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
        return(1);
        }
 
@@ -307,17 +219,17 @@ static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
        }
 #endif
 
-static int RSAref_Private_eay2ref(RSA *from, RSArefPrivateKey *to)
+static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY *to)
        {
        to->bits=BN_num_bits(from->n);
-       if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->d,to->d,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->p,to->prime[0],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->q,to->prime[1],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->dmp1,to->pexp[0],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->dmq1,to->pexp[1],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->iqmp,to->coef,RSAref_MAX_PLEN)) return(0);
+       if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->e,to->publicExponent,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->d,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->p,to->prime[0],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->q,to->prime[1],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->dmp1,to->primeExponent[0],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->dmq1,to->primeExponent[1],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->iqmp,to->coefficient,MAX_RSA_PRIME_LEN)) return(0);
        return(1);
        }
 
@@ -325,13 +237,13 @@ static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned c
             RSA *rsa, int padding)
        {
        int i,outlen= -1;
-       RSArefPrivateKey RSAkey;
+       R_RSA_PRIVATE_KEY RSAkey;
 
        if (!RSAref_Private_eay2ref(rsa,&RSAkey))
                goto err;
        if ((i=RSAPrivateDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_DECRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT,i);
                outlen= -1;
                }
 err:
@@ -343,18 +255,18 @@ static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned c
             RSA *rsa, int padding)
        {
        int i,outlen= -1;
-       RSArefPrivateKey RSAkey;
+       R_RSA_PRIVATE_KEY RSAkey;
 
        if (padding != RSA_PKCS1_PADDING)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
+               RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
                goto err;
        }
        if (!RSAref_Private_eay2ref(rsa,&RSAkey))
                goto err;
        if ((i=RSAPrivateEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,i);
                outlen= -1;
                }
 err:
@@ -366,13 +278,13 @@ static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned ch
             RSA *rsa, int padding)
        {
        int i,outlen= -1;
-       RSArefPublicKey RSAkey;
+       R_RSA_PUBLIC_KEY RSAkey;
 
        if (!RSAref_Public_eay2ref(rsa,&RSAkey))
                goto err;
        if ((i=RSAPublicDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_DECRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT,i);
                outlen= -1;
                }
 err:
@@ -385,13 +297,13 @@ static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned ch
        {
        int outlen= -1;
        int i;
-       RSArefPublicKey RSAkey;
-       RSARandomState rnd;
+       R_RSA_PUBLIC_KEY RSAkey;
+       R_RANDOM_STRUCT rnd;
        unsigned char buf[16];
 
        if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) 
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
+               RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
                goto err;
                }
        
@@ -409,7 +321,7 @@ static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned ch
                goto err;
        if ((i=RSAPublicEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT,i);
                outlen= -1;
                goto err;
                }