From 0b6f3c66cddf047933b8ff77c4f8a3e706e93aa6 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Thu, 6 Apr 2006 13:02:06 +0000 Subject: [PATCH] Initial definitions and a few functions for EVP_PKEY_METHOD: an extension of the EVP routines to public key algorithms. --- CHANGES | 4 ++ crypto/Makefile | 2 +- crypto/evp/Makefile | 4 +- crypto/evp/evp.h | 1 + crypto/evp/evp_locl.h | 71 ++++++++++++++++++++ crypto/evp/pmeth_lib.c | 146 +++++++++++++++++++++++++++++++++++++++++ crypto/ossl_typ.h | 3 + crypto/rsa/Makefile | 6 +- crypto/rsa/rsa_pmeth.c | 69 +++++++++++++++++++ 9 files changed, 301 insertions(+), 5 deletions(-) create mode 100644 crypto/evp/pmeth_lib.c create mode 100644 crypto/rsa/rsa_pmeth.c diff --git a/CHANGES b/CHANGES index e173a298a6..e00277ef93 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,10 @@ Changes between 0.9.8a and 0.9.9 [xx XXX xxxx] + *) Initial definitions for EVP_PKEY_METHOD. This will be a high level public + key API, doesn't do much yet. + [Steve Henson] + *) New function EVP_PKEY_asn1_get0_info() to retrieve information about public key algorithms. New option to openssl utility: "list-public-key-algorithms" to print out info. diff --git a/crypto/Makefile b/crypto/Makefile index 7c55d5933b..b6e3f9424b 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../asn1 -I../../include +INCLUDES= -I.. -I../.. -I../asn1 -I../evp -I../../include CFLAG= -g MAKEDEPPROG= makedepend MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) diff --git a/crypto/evp/Makefile b/crypto/evp/Makefile index 310fff4257..f6476d88bc 100644 --- a/crypto/evp/Makefile +++ b/crypto/evp/Makefile @@ -28,7 +28,7 @@ LIBSRC= encode.c digest.c evp_enc.c evp_key.c evp_acnf.c \ bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c \ c_all.c c_allc.c c_alld.c evp_lib.c bio_ok.c \ evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c \ - e_old.c + e_old.c pmeth_lib.c LIBOBJ= encode.o digest.o evp_enc.o evp_key.o evp_acnf.o \ e_des.o e_bf.o e_idea.o e_des3.o \ @@ -40,7 +40,7 @@ LIBOBJ= encode.o digest.o evp_enc.o evp_key.o evp_acnf.o \ bio_md.o bio_b64.o bio_enc.o evp_err.o e_null.o \ c_all.o c_allc.o c_alld.o evp_lib.o bio_ok.o \ evp_pkey.o evp_pbe.o p5_crpt.o p5_crpt2.o \ - e_old.o + e_old.o pmeth_lib.o SRC= $(LIBSRC) diff --git a/crypto/evp/evp.h b/crypto/evp/evp.h index d06904b7ec..7d16da8f04 100644 --- a/crypto/evp/evp.h +++ b/crypto/evp/evp.h @@ -129,6 +129,7 @@ struct evp_pkey_st int save_type; int references; const EVP_PKEY_ASN1_METHOD *ameth; + const EVP_PKEY_METHOD *pmeth; union { char *ptr; #ifndef OPENSSL_NO_RSA diff --git a/crypto/evp/evp_locl.h b/crypto/evp/evp_locl.h index 2204e345ad..af02514252 100644 --- a/crypto/evp/evp_locl.h +++ b/crypto/evp/evp_locl.h @@ -234,3 +234,74 @@ const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } EVP_CIPHER_set_asn1_iv, \ EVP_CIPHER_get_asn1_iv, \ NULL) + + +struct evp_pkey_ctx_st + { + /* Method associated with this operation */ + const EVP_PKEY_METHOD *pmeth; + /* Key: may be NULL */ + EVP_PKEY *pkey; + /* Actual operation */ + int operation; + /* Algorithm specific data */ + void *data; + } /* EVP_PKEY_CTX */; + +#define EVP_PKEY_OP_UNDEFINED 0 +#define EVP_PKEY_OP_PARAMGEN 1 +#define EVP_PKEY_OP_KEYGEN 2 +#define EVP_PKEY_OP_SIGN 3 +#define EVP_PKEY_OP_VERIFY 4 +#define EVP_PKEY_OP_VERIFYRECOVER 5 +#define EVP_PKEY_OP_SIGNCTX 6 +#define EVP_PKEY_OP_VERIFYCTX 7 +#define EVP_PKEY_OP_ENCRYPT 8 +#define EVP_PKEY_OP_DECRYPT 9 + +struct evp_pkey_method_st + { + int pkey_id; + int (*paramgen_init)(EVP_PKEY_CTX *ctx); + int (*paramgen)(EVP_PKEY *key, EVP_PKEY_CTX *ctx); + + int (*keygen_init)(EVP_PKEY_CTX *ctx); + int (*keygen)(EVP_PKEY *key, EVP_PKEY_CTX *ctx); + + int (*sign_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); + int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen, + unsigned char *tbs, int tbslen); + + int (*verify_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); + int (*verify)(EVP_PKEY_CTX *ctx, unsigned char *sig, int siglen, + unsigned char *tbs, int tbslen); + + int (*verify_recover_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); + int (*verify_recover)(EVP_PKEY_CTX *ctx, + unsigned char *rout, int *routlen, + unsigned char *sig, int siglen); + + int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx, + EVP_PKEY *pkey); + int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen, + EVP_MD_CTX *mctx); + + int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx, + EVP_PKEY *pkey); + int (*verifyctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, int siglen, + EVP_MD_CTX *mctx); + + int (*encrypt_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); + int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen, + unsigned char *in, int inlen); + + int (*decrypt_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); + int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen, + unsigned char *in, int inlen); + + int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2); + int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value); + + void (*cleanup)(EVP_PKEY_CTX *ctx); + + } /* EVP_PKEY_METHOD */; diff --git a/crypto/evp/pmeth_lib.c b/crypto/evp/pmeth_lib.c new file mode 100644 index 0000000000..1830ea1a6d --- /dev/null +++ b/crypto/evp/pmeth_lib.c @@ -0,0 +1,146 @@ +/* pmeth_lib.c */ +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL + * project 2006. + */ +/* ==================================================================== + * Copyright (c) 2006 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * licensing@OpenSSL.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + +#include +#include +#include +#include "cryptlib.h" +#include "evp_locl.h" + +STACK *app_pkey_methods = NULL; + +extern EVP_PKEY_METHOD rsa_pkey_meth; + +const EVP_PKEY_METHOD *standard_methods[] = + { + &rsa_pkey_meth + }; + +static int pmeth_cmp(const EVP_PKEY_METHOD * const *a, + const EVP_PKEY_METHOD * const *b) + { + return ((*a)->pkey_id - (*b)->pkey_id); + } + +const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type, ENGINE *e) + { + EVP_PKEY_METHOD tmp, *t = &tmp, **ret; + tmp.pkey_id = type; + if (app_pkey_methods) + { + int idx; + idx = sk_find(app_pkey_methods, (char *)&tmp); + if (idx >= 0) + return (EVP_PKEY_METHOD *) + sk_value(app_pkey_methods, idx); + } + ret = (EVP_PKEY_METHOD **) OBJ_bsearch((char *)&t, + (char *)standard_methods, + sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *), + sizeof(EVP_PKEY_METHOD *), + (int (*)(const void *, const void *))pmeth_cmp); + if (!ret || !*ret) + return NULL; + return *ret; + } + +EVP_PKEY_CTX *EVP_PKEY_CTX_new(int ktype, ENGINE *e) + { + EVP_PKEY_CTX *ret; + const EVP_PKEY_METHOD *pmeth; + pmeth = EVP_PKEY_meth_find(ktype, e); + if (pmeth == NULL) + return NULL; + ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX)); + ret->pmeth = pmeth; + ret->operation = EVP_PKEY_OP_UNDEFINED; + ret->pkey = NULL; + ret->data = NULL; + + return ret; + } + +int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, + int cmd, int p1, void *p2) + { + if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) + return -2; + if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) + return -1; + + if (ctx->operation == EVP_PKEY_OP_UNDEFINED) + { + /* Not initialized */ + return -1; + } + + if ((optype != -1) && (ctx->operation != optype)) + { + /* Invalid operation type */ + return -1; + } + + return ctx->pmeth->ctrl(ctx, cmd, p1, p2); + + } + + + + + + diff --git a/crypto/ossl_typ.h b/crypto/ossl_typ.h index ffb624421b..085953208e 100644 --- a/crypto/ossl_typ.h +++ b/crypto/ossl_typ.h @@ -123,6 +123,9 @@ typedef struct evp_pkey_st EVP_PKEY; typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD; +typedef struct evp_pkey_method_st EVP_PKEY_METHOD; +typedef struct evp_pkey_ctx_st EVP_PKEY_CTX; + typedef struct dh_st DH; typedef struct dh_method DH_METHOD; diff --git a/crypto/rsa/Makefile b/crypto/rsa/Makefile index 9f833b09e5..f5b0cde8d2 100644 --- a/crypto/rsa/Makefile +++ b/crypto/rsa/Makefile @@ -19,10 +19,12 @@ APPS= LIB=$(TOP)/libcrypto.a LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c rsa_err.c \ rsa_pk1.c rsa_ssl.c rsa_none.c rsa_oaep.c rsa_chk.c rsa_null.c \ - rsa_pss.c rsa_x931.c rsa_asn1.c rsa_depr.c rsa_ameth.c rsa_prn.c + rsa_pss.c rsa_x931.c rsa_asn1.c rsa_depr.c rsa_ameth.c rsa_prn.c \ + rsa_pmeth.c LIBOBJ= rsa_eay.o rsa_gen.o rsa_lib.o rsa_sign.o rsa_saos.o rsa_err.o \ rsa_pk1.o rsa_ssl.o rsa_none.o rsa_oaep.o rsa_chk.o rsa_null.o \ - rsa_pss.o rsa_x931.o rsa_asn1.o rsa_depr.o rsa_ameth.o rsa_prn.o + rsa_pss.o rsa_x931.o rsa_asn1.o rsa_depr.o rsa_ameth.o rsa_prn.o \ + rsa_pmeth.o SRC= $(LIBSRC) diff --git a/crypto/rsa/rsa_pmeth.c b/crypto/rsa/rsa_pmeth.c new file mode 100644 index 0000000000..aa896091fe --- /dev/null +++ b/crypto/rsa/rsa_pmeth.c @@ -0,0 +1,69 @@ +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL + * project 2006. + */ +/* ==================================================================== + * Copyright (c) 2006 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * licensing@OpenSSL.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + +#include +#include "cryptlib.h" +#include +#include +#include +#include "asn1_locl.h" +#include "evp_locl.h" + +const EVP_PKEY_METHOD rsa_pkey_meth = + { + EVP_PKEY_RSA, + }; -- 2.25.1