From 18e377b4ffa6d15572d7283f1eb1743ce5875804 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Wed, 22 Mar 2006 17:59:49 +0000 Subject: [PATCH] Make EVP_PKEY_ASN1_METHOD opaque. Add application level functions to initialize it. Initial support for application added public key ASN1. --- crypto/Makefile | 2 +- crypto/asn1/ameth_lib.c | 128 +++++++++++++++++++++++++++++++++++++++- crypto/asn1/asn1.h | 36 ----------- crypto/asn1/asn1_locl.h | 37 ++++++++++++ crypto/asn1/d2i_pr.c | 2 +- crypto/asn1/x_pubkey.c | 5 +- crypto/dh/dh_ameth.c | 1 + crypto/dsa/dsa_ameth.c | 1 + crypto/ec/ec_ameth.c | 1 + crypto/evp/evp.h | 2 +- crypto/evp/evp_pkey.c | 12 +--- crypto/evp/p_lib.c | 6 +- crypto/rsa/rsa_ameth.c | 1 + 13 files changed, 179 insertions(+), 55 deletions(-) diff --git a/crypto/Makefile b/crypto/Makefile index c9e90d3931..7c55d5933b 100644 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -7,7 +7,7 @@ TOP= .. CC= cc INCLUDE= -I. -I$(TOP) -I../include # INCLUDES targets sudbirs! -INCLUDES= -I.. -I../.. -I../../include +INCLUDES= -I.. -I../.. -I../asn1 -I../../include CFLAG= -g MAKEDEPPROG= makedepend MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) diff --git a/crypto/asn1/ameth_lib.c b/crypto/asn1/ameth_lib.c index f2c4166f1d..aeadaf7e6a 100644 --- a/crypto/asn1/ameth_lib.c +++ b/crypto/asn1/ameth_lib.c @@ -81,6 +81,11 @@ const EVP_PKEY_ASN1_METHOD *standard_methods[] = &eckey_asn1_meth }; +typedef int sk_cmp_fn_type(const char * const *a, const char * const *b); +static STACK *app_methods = NULL; + + + #ifdef TEST void main() { @@ -97,14 +102,21 @@ void main() static int ameth_cmp(const EVP_PKEY_ASN1_METHOD * const *a, const EVP_PKEY_ASN1_METHOD * const *b) { -/*fprintf(stderr, "Comparing %d with %d\n", (*a)->pkey_id, (*b)->pkey_id);*/ return ((*a)->pkey_id - (*b)->pkey_id); } -const EVP_PKEY_ASN1_METHOD *EVP_PKEY_ASN1_find(int type) +const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(int type) { EVP_PKEY_ASN1_METHOD tmp, *t = &tmp, **ret; tmp.pkey_id = type; + if (app_methods) + { + int idx; + idx = sk_find(app_methods, (char *)&t); + if (idx > 0) + return (EVP_PKEY_ASN1_METHOD *) + sk_value(app_methods, idx); + } ret = (EVP_PKEY_ASN1_METHOD **) OBJ_bsearch((char *)&t, (char *)standard_methods, sizeof(standard_methods)/sizeof(EVP_PKEY_ASN1_METHOD *), @@ -113,7 +125,117 @@ const EVP_PKEY_ASN1_METHOD *EVP_PKEY_ASN1_find(int type) if (!ret || !*ret) return NULL; if ((*ret)->pkey_flags & ASN1_PKEY_ALIAS) - return EVP_PKEY_ASN1_find((*ret)->pkey_base_id); + return EVP_PKEY_asn1_find((*ret)->pkey_base_id); return *ret; } +int EVP_PKEY_asn1_add(const EVP_PKEY_ASN1_METHOD *ameth) + { + if (app_methods == NULL) + { + app_methods = sk_new((sk_cmp_fn_type *)ameth_cmp); + if (!app_methods) + return 0; + } + if (!sk_push(app_methods, (char *)ameth)) + return 0; + sk_sort(app_methods); + return 1; + } + +EVP_PKEY_ASN1_METHOD* EVP_PKEY_asn1_new(int id) + { + EVP_PKEY_ASN1_METHOD *ameth; + ameth = OPENSSL_malloc(sizeof(EVP_PKEY_ASN1_METHOD)); + if (!ameth) + return NULL; + + ameth->pkey_id = id; + ameth->pkey_base_id = id; + ameth->pkey_flags = ASN1_PKEY_DYNAMIC; + + ameth->pub_decode = 0; + ameth->pub_encode = 0; + ameth->pub_cmp = 0; + ameth->pub_print = 0; + + + ameth->priv_decode = 0; + ameth->priv_encode = 0; + ameth->priv_print = 0; + + + ameth->pkey_size = 0; + ameth->pkey_bits = 0; + + ameth->param_decode = 0; + ameth->param_encode = 0; + ameth->param_missing = 0; + ameth->param_copy = 0; + ameth->param_cmp = 0; + ameth->param_print = 0; + + + ameth->pkey_free = 0; + ameth->pkey_ctrl = 0; + + return ameth; + } + +void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth, + int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub), + int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk), + int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b), + int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, + ASN1_PCTX *pctx), + int (*pkey_size)(const EVP_PKEY *pk), + int (*pkey_bits)(const EVP_PKEY *pk)) + { + ameth->pub_decode = pub_decode; + ameth->pub_encode = pub_encode; + ameth->pub_cmp = pub_cmp; + ameth->pub_print = pub_print; + ameth->pkey_size = pkey_size; + ameth->pkey_bits = pkey_bits; + } + +void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth, + int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf), + int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk), + int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent, + ASN1_PCTX *pctx)) + { + ameth->priv_decode = priv_decode; + ameth->priv_encode = priv_encode; + ameth->priv_print = priv_print; + } + +void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth, + int (*param_decode)(const EVP_PKEY *pk, X509_PUBKEY *pub), + int (*param_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk), + int (*param_missing)(const EVP_PKEY *pk), + int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from), + int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b), + int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent, + ASN1_PCTX *pctx)) + { + ameth->param_decode = param_decode; + ameth->param_encode = param_encode; + ameth->param_missing = param_missing; + ameth->param_copy = param_copy; + ameth->param_cmp = param_cmp; + ameth->param_print = param_print; + } + +void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth, + void (*pkey_free)(EVP_PKEY *pkey)) + { + ameth->pkey_free = pkey_free; + } + +void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth, + void (*pkey_ctrl)(EVP_PKEY *pkey, int op, + long arg1, void *arg2)) + { + ameth->pkey_ctrl = pkey_ctrl; + } diff --git a/crypto/asn1/asn1.h b/crypto/asn1/asn1.h index b366fa483e..8aa7d3b09d 100644 --- a/crypto/asn1/asn1.h +++ b/crypto/asn1/asn1.h @@ -282,42 +282,6 @@ typedef struct ASN1_TLC_st ASN1_TLC; /* This is just an opaque pointer */ typedef struct ASN1_VALUE_st ASN1_VALUE; -/* ASN1 public key method structure */ - -#define ASN1_PKEY_ALIAS 0x1 - -struct evp_pkey_asn1_method_st - { - int pkey_id; - int pkey_base_id; - unsigned long pkey_flags; - - int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub); - int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk); - int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); - int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *pctx); - - int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf); - int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk); - int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *pctx); - - int (*pkey_size)(const EVP_PKEY *pk); - int (*pkey_bits)(const EVP_PKEY *pk); - - int (*param_decode)(const EVP_PKEY *pk, X509_PUBKEY *pub); - int (*param_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk); - int (*param_missing)(const EVP_PKEY *pk); - int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from); - int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); - int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *pctx); - - void (*pkey_free)(EVP_PKEY *pkey); - void (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2); - } /* EVP_PKEY_ASN1_METHOD */; - /* Declare ASN1 functions: the implement macro in in asn1t.h */ #define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type) diff --git a/crypto/asn1/asn1_locl.h b/crypto/asn1/asn1_locl.h index 3f24f1bd48..1328cdb106 100644 --- a/crypto/asn1/asn1_locl.h +++ b/crypto/asn1/asn1_locl.h @@ -68,3 +68,40 @@ struct asn1_pctx_st unsigned long oid_flags; unsigned long str_flags; } /* ASN1_PCTX */; + +/* ASN1 public key method structure */ + +#define ASN1_PKEY_ALIAS 0x1 +#define ASN1_PKEY_DYNAMIC 0x2 + +struct evp_pkey_asn1_method_st + { + int pkey_id; + int pkey_base_id; + unsigned long pkey_flags; + + int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub); + int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk); + int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); + int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, + ASN1_PCTX *pctx); + + int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf); + int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk); + int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent, + ASN1_PCTX *pctx); + + int (*pkey_size)(const EVP_PKEY *pk); + int (*pkey_bits)(const EVP_PKEY *pk); + + int (*param_decode)(const EVP_PKEY *pk, X509_PUBKEY *pub); + int (*param_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk); + int (*param_missing)(const EVP_PKEY *pk); + int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from); + int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); + int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent, + ASN1_PCTX *pctx); + + void (*pkey_free)(EVP_PKEY *pkey); + void (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2); + } /* EVP_PKEY_ASN1_METHOD */; diff --git a/crypto/asn1/d2i_pr.c b/crypto/asn1/d2i_pr.c index 512d11d6f6..bb6582b7cf 100644 --- a/crypto/asn1/d2i_pr.c +++ b/crypto/asn1/d2i_pr.c @@ -89,7 +89,7 @@ EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, ret->save_type=type; ret->type=EVP_PKEY_type(type); - ret->ameth = EVP_PKEY_ASN1_find(type); + ret->ameth = EVP_PKEY_asn1_find(type); switch (ret->type) { #ifndef OPENSSL_NO_RSA diff --git a/crypto/asn1/x_pubkey.c b/crypto/asn1/x_pubkey.c index 311e3bf30c..34f0af0f59 100644 --- a/crypto/asn1/x_pubkey.c +++ b/crypto/asn1/x_pubkey.c @@ -60,6 +60,7 @@ #include "cryptlib.h" #include #include +#include "asn1_locl.h" #ifndef OPENSSL_NO_RSA #include #endif @@ -95,7 +96,7 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) if ((pk=X509_PUBKEY_new()) == NULL) goto error; - meth = EVP_PKEY_ASN1_find(pkey->type); + meth = EVP_PKEY_asn1_find(pkey->type); if (meth) { @@ -153,7 +154,7 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) goto error; } - meth = EVP_PKEY_ASN1_find(OBJ_obj2nid(key->algor->algorithm)); + meth = EVP_PKEY_asn1_find(OBJ_obj2nid(key->algor->algorithm)); if (meth) { diff --git a/crypto/dh/dh_ameth.c b/crypto/dh/dh_ameth.c index 0a862b73d2..d82a1c91c1 100644 --- a/crypto/dh/dh_ameth.c +++ b/crypto/dh/dh_ameth.c @@ -60,6 +60,7 @@ #include #include #include +#include "asn1_locl.h" static void int_dh_free(EVP_PKEY *pkey) { diff --git a/crypto/dsa/dsa_ameth.c b/crypto/dsa/dsa_ameth.c index 1b39713c62..7ef6aaa8d4 100644 --- a/crypto/dsa/dsa_ameth.c +++ b/crypto/dsa/dsa_ameth.c @@ -60,6 +60,7 @@ #include #include #include +#include "asn1_locl.h" static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) { diff --git a/crypto/ec/ec_ameth.c b/crypto/ec/ec_ameth.c index 98003b1b0e..e21221868f 100644 --- a/crypto/ec/ec_ameth.c +++ b/crypto/ec/ec_ameth.c @@ -59,6 +59,7 @@ #include "cryptlib.h" #include #include +#include "asn1_locl.h" static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key) { diff --git a/crypto/evp/evp.h b/crypto/evp/evp.h index a2214c9406..ab2a1e593f 100644 --- a/crypto/evp/evp.h +++ b/crypto/evp/evp.h @@ -855,7 +855,7 @@ int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md, EVP_PBE_KEYGEN *keygen); void EVP_PBE_cleanup(void); -const EVP_PKEY_ASN1_METHOD *EVP_PKEY_ASN1_find(int type); +const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(int type); /* BEGIN ERROR CODES */ /* The following lines are auto generated by the script mkerr.pl. Any changes diff --git a/crypto/evp/evp_pkey.c b/crypto/evp/evp_pkey.c index 938541fc71..e81c4fedb1 100644 --- a/crypto/evp/evp_pkey.c +++ b/crypto/evp/evp_pkey.c @@ -61,13 +61,7 @@ #include "cryptlib.h" #include #include -#ifndef OPENSSL_NO_RSA -#include -#endif -#ifndef OPENSSL_NO_DSA -#include -#endif -#include +#include "asn1_locl.h" /* Extract a private key from a PKCS8 structure */ @@ -86,7 +80,7 @@ EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) return NULL; } - meth = EVP_PKEY_ASN1_find(OBJ_obj2nid(algoid)); + meth = EVP_PKEY_asn1_find(OBJ_obj2nid(algoid)); if (meth) { @@ -138,7 +132,7 @@ PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken) } p8->broken = broken; - meth = EVP_PKEY_ASN1_find(pkey->type); + meth = EVP_PKEY_asn1_find(pkey->type); if (meth) { diff --git a/crypto/evp/p_lib.c b/crypto/evp/p_lib.c index 7fd31b00a4..f18ac62a0f 100644 --- a/crypto/evp/p_lib.c +++ b/crypto/evp/p_lib.c @@ -74,6 +74,8 @@ #include #endif +#include "asn1_locl.h" + static void EVP_PKEY_free_it(EVP_PKEY *x); int EVP_PKEY_bits(EVP_PKEY *pkey) @@ -189,7 +191,7 @@ int EVP_PKEY_assign(EVP_PKEY *pkey, int type, char *key) if (pkey == NULL) return(0); if (pkey->pkey.ptr != NULL) EVP_PKEY_free_it(pkey); - ameth = EVP_PKEY_ASN1_find(type); + ameth = EVP_PKEY_asn1_find(type); pkey->ameth = ameth; pkey->type = ameth->pkey_id; pkey->save_type=type; @@ -284,7 +286,7 @@ DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) int EVP_PKEY_type(int type) { const EVP_PKEY_ASN1_METHOD *ameth; - ameth = EVP_PKEY_ASN1_find(type); + ameth = EVP_PKEY_asn1_find(type); if (ameth) return ameth->pkey_id; return NID_undef; diff --git a/crypto/rsa/rsa_ameth.c b/crypto/rsa/rsa_ameth.c index 287098029c..dc32a5cd31 100644 --- a/crypto/rsa/rsa_ameth.c +++ b/crypto/rsa/rsa_ameth.c @@ -60,6 +60,7 @@ #include #include #include +#include "asn1_locl.h" static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) { -- 2.25.1