Initial definitions and a few functions for EVP_PKEY_METHOD: an extension

of the EVP routines to public key algorithms.
This commit is contained in:
Dr. Stephen Henson 2006-04-06 13:02:06 +00:00
parent a01d9ac558
commit 0b6f3c66cd
9 changed files with 301 additions and 5 deletions

View file

@ -4,6 +4,10 @@
Changes between 0.9.8a and 0.9.9 [xx XXX xxxx] 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 *) New function EVP_PKEY_asn1_get0_info() to retrieve information about
public key algorithms. New option to openssl utility: public key algorithms. New option to openssl utility:
"list-public-key-algorithms" to print out info. "list-public-key-algorithms" to print out info.

View file

@ -7,7 +7,7 @@ TOP= ..
CC= cc CC= cc
INCLUDE= -I. -I$(TOP) -I../include INCLUDE= -I. -I$(TOP) -I../include
# INCLUDES targets sudbirs! # INCLUDES targets sudbirs!
INCLUDES= -I.. -I../.. -I../asn1 -I../../include INCLUDES= -I.. -I../.. -I../asn1 -I../evp -I../../include
CFLAG= -g CFLAG= -g
MAKEDEPPROG= makedepend MAKEDEPPROG= makedepend
MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG)

View file

@ -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 \ 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 \ 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 \ 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 \ 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 \ 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 \ 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 \ 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 \ evp_pkey.o evp_pbe.o p5_crpt.o p5_crpt2.o \
e_old.o e_old.o pmeth_lib.o
SRC= $(LIBSRC) SRC= $(LIBSRC)

View file

@ -129,6 +129,7 @@ struct evp_pkey_st
int save_type; int save_type;
int references; int references;
const EVP_PKEY_ASN1_METHOD *ameth; const EVP_PKEY_ASN1_METHOD *ameth;
const EVP_PKEY_METHOD *pmeth;
union { union {
char *ptr; char *ptr;
#ifndef OPENSSL_NO_RSA #ifndef OPENSSL_NO_RSA

View file

@ -234,3 +234,74 @@ const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
EVP_CIPHER_set_asn1_iv, \ EVP_CIPHER_set_asn1_iv, \
EVP_CIPHER_get_asn1_iv, \ EVP_CIPHER_get_asn1_iv, \
NULL) 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 */;

146
crypto/evp/pmeth_lib.c Normal file
View file

@ -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 <stdio.h>
#include <stdlib.h>
#include <openssl/objects.h>
#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);
}

View file

@ -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_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_st DH;
typedef struct dh_method DH_METHOD; typedef struct dh_method DH_METHOD;

View file

@ -19,10 +19,12 @@ APPS=
LIB=$(TOP)/libcrypto.a LIB=$(TOP)/libcrypto.a
LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c rsa_err.c \ 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_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 \ 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_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) SRC= $(LIBSRC)

69
crypto/rsa/rsa_pmeth.c Normal file
View file

@ -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 <stdio.h>
#include "cryptlib.h"
#include <openssl/asn1t.h>
#include <openssl/x509.h>
#include <openssl/rsa.h>
#include "asn1_locl.h"
#include "evp_locl.h"
const EVP_PKEY_METHOD rsa_pkey_meth =
{
EVP_PKEY_RSA,
};