Home | History | Annotate | Download | only in evp
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) cryptsoft.com).
      6  * The implementation was written so as to conform with Netscapes SSL.
      7  *
      8  * This library is free for commercial and non-commercial use as long as
      9  * the following conditions are aheared to.  The following conditions
     10  * apply to all code found in this distribution, be it the RC4, RSA,
     11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     12  * included with this distribution is covered by the same copyright terms
     13  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     14  *
     15  * Copyright remains Eric Young's, and as such any Copyright notices in
     16  * the code are not to be removed.
     17  * If this package is used in a product, Eric Young should be given attribution
     18  * as the author of the parts of the library used.
     19  * This can be in the form of a textual message at program startup or
     20  * in documentation (online or textual) provided with the package.
     21  *
     22  * Redistribution and use in source and binary forms, with or without
     23  * modification, are permitted provided that the following conditions
     24  * are met:
     25  * 1. Redistributions of source code must retain the copyright
     26  *    notice, this list of conditions and the following disclaimer.
     27  * 2. Redistributions in binary form must reproduce the above copyright
     28  *    notice, this list of conditions and the following disclaimer in the
     29  *    documentation and/or other materials provided with the distribution.
     30  * 3. All advertising materials mentioning features or use of this software
     31  *    must display the following acknowledgement:
     32  *    "This product includes cryptographic software written by
     33  *     Eric Young (eay (at) cryptsoft.com)"
     34  *    The word 'cryptographic' can be left out if the rouines from the library
     35  *    being used are not cryptographic related :-).
     36  * 4. If you include any Windows specific code (or a derivative thereof) from
     37  *    the apps directory (application code) you must include an acknowledgement:
     38  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     39  *
     40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     50  * SUCH DAMAGE.
     51  *
     52  * The licence and distribution terms for any publically available version or
     53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     54  * copied and put under another distribution licence
     55  * [including the GNU Public Licence.] */
     56 
     57 #ifndef OPENSSL_HEADER_EVP_INTERNAL_H
     58 #define OPENSSL_HEADER_EVP_INTERNAL_H
     59 
     60 #include <openssl/base.h>
     61 
     62 #if defined(__cplusplus)
     63 extern "C" {
     64 #endif
     65 
     66 
     67 /* These values are flags for EVP_PKEY_ASN1_METHOD.flags. */
     68 #define ASN1_PKEY_ALIAS 0x1
     69 #define ASN1_PKEY_DYNAMIC 0x2
     70 #define ASN1_PKEY_SIGPARAM_NULL 0x4
     71 
     72 struct evp_pkey_asn1_method_st {
     73   int pkey_id;
     74   int pkey_base_id;
     75   unsigned long pkey_flags;
     76 
     77   char *pem_str;
     78   char *info;
     79 
     80   int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub);
     81   int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk);
     82   int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
     83   int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx);
     84 
     85   int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf);
     86   int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
     87   int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
     88                     ASN1_PCTX *pctx);
     89 
     90   /* pkey_opaque returns 1 if the |pk| is opaque. Opaque keys are backed by
     91    * custom implementations which do not expose key material and parameters.*/
     92   int (*pkey_opaque)(const EVP_PKEY *pk);
     93 
     94   int (*pkey_size)(const EVP_PKEY *pk);
     95   int (*pkey_bits)(const EVP_PKEY *pk);
     96 
     97   int (*param_decode)(EVP_PKEY *pkey, const unsigned char **pder, int derlen);
     98   int (*param_encode)(const EVP_PKEY *pkey, unsigned char **pder);
     99   int (*param_missing)(const EVP_PKEY *pk);
    100   int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
    101   int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
    102   int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
    103                      ASN1_PCTX *pctx);
    104   int (*sig_print)(BIO *out, const X509_ALGOR *sigalg, const ASN1_STRING *sig,
    105                    int indent, ASN1_PCTX *pctx);
    106 
    107 
    108   void (*pkey_free)(EVP_PKEY *pkey);
    109   int (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2);
    110 
    111   /* Legacy functions for old PEM */
    112 
    113   int (*old_priv_decode)(EVP_PKEY *pkey, const unsigned char **pder,
    114                          int derlen);
    115   int (*old_priv_encode)(const EVP_PKEY *pkey, unsigned char **pder);
    116   /* Custom ASN1 signature verification */
    117   int (*item_verify)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
    118                      X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey);
    119   int (*item_sign)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
    120                    X509_ALGOR *alg1, X509_ALGOR *alg2, ASN1_BIT_STRING *sig);
    121 
    122 } /* EVP_PKEY_ASN1_METHOD */;
    123 
    124 
    125 typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
    126 
    127 #define EVP_PKEY_OP_UNDEFINED 0
    128 #define EVP_PKEY_OP_PARAMGEN (1 << 1)
    129 #define EVP_PKEY_OP_KEYGEN (1 << 2)
    130 #define EVP_PKEY_OP_SIGN (1 << 3)
    131 #define EVP_PKEY_OP_VERIFY (1 << 4)
    132 #define EVP_PKEY_OP_VERIFYRECOVER (1 << 5)
    133 #define EVP_PKEY_OP_SIGNCTX (1 << 6)
    134 #define EVP_PKEY_OP_VERIFYCTX (1 << 7)
    135 #define EVP_PKEY_OP_ENCRYPT (1 << 8)
    136 #define EVP_PKEY_OP_DECRYPT (1 << 9)
    137 #define EVP_PKEY_OP_DERIVE (1 << 10)
    138 
    139 #define EVP_PKEY_OP_TYPE_SIG                                           \
    140   (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYRECOVER | \
    141    EVP_PKEY_OP_SIGNCTX | EVP_PKEY_OP_VERIFYCTX)
    142 
    143 #define EVP_PKEY_OP_TYPE_CRYPT (EVP_PKEY_OP_ENCRYPT | EVP_PKEY_OP_DECRYPT)
    144 
    145 #define EVP_PKEY_OP_TYPE_NOGEN \
    146   (EVP_PKEY_OP_SIG | EVP_PKEY_OP_CRYPT | EVP_PKEY_OP_DERIVE)
    147 
    148 #define EVP_PKEY_OP_TYPE_GEN (EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN)
    149 
    150 #define EVP_PKEY_CTRL_MD 1
    151 #define EVP_PKEY_CTRL_GET_MD 2
    152 #define EVP_PKEY_CTRL_RSA_PADDING (EVP_PKEY_ALG_CTRL + 1)
    153 #define EVP_PKEY_CTRL_GET_RSA_PADDING (EVP_PKEY_ALG_CTRL + 2)
    154 #define EVP_PKEY_CTRL_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 3)
    155 #define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 4)
    156 #define EVP_PKEY_CTRL_RSA_KEYGEN_BITS (EVP_PKEY_ALG_CTRL + 5)
    157 #define EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP	(EVP_PKEY_ALG_CTRL + 6)
    158 #define EVP_PKEY_CTRL_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 7)
    159 #define EVP_PKEY_CTRL_GET_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 8)
    160 #define EVP_PKEY_CTRL_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 9)
    161 #define EVP_PKEY_CTRL_GET_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 10)
    162 #define EVP_PKEY_CTRL_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 11)
    163 #define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12)
    164 
    165 struct evp_pkey_ctx_st {
    166   /* Method associated with this operation */
    167   const EVP_PKEY_METHOD *pmeth;
    168   /* Engine that implements this method or NULL if builtin */
    169   ENGINE *engine;
    170   /* Key: may be NULL */
    171   EVP_PKEY *pkey;
    172   /* Peer key for key agreement, may be NULL */
    173   EVP_PKEY *peerkey;
    174   /* operation contains one of the |EVP_PKEY_OP_*| values. */
    175   int operation;
    176   /* Algorithm specific data */
    177   void *data;
    178   /* Application specific data */
    179   void *app_data;
    180 } /* EVP_PKEY_CTX */;
    181 
    182 struct evp_pkey_method_st {
    183   int pkey_id;
    184   int flags;
    185 
    186   int (*init)(EVP_PKEY_CTX *ctx);
    187   int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src);
    188   void (*cleanup)(EVP_PKEY_CTX *ctx);
    189 
    190   int (*paramgen_init)(EVP_PKEY_CTX *ctx);
    191   int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
    192 
    193   int (*keygen_init)(EVP_PKEY_CTX *ctx);
    194   int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
    195 
    196   int (*sign_init)(EVP_PKEY_CTX *ctx);
    197   int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
    198               const unsigned char *tbs, size_t tbslen);
    199 
    200   int (*verify_init)(EVP_PKEY_CTX *ctx);
    201   int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
    202                 const unsigned char *tbs, size_t tbslen);
    203 
    204   int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
    205   int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
    206                  EVP_MD_CTX *mctx);
    207 
    208   int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
    209   int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
    210                    EVP_MD_CTX *mctx);
    211 
    212   int (*encrypt_init)(EVP_PKEY_CTX *ctx);
    213   int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
    214                  const unsigned char *in, size_t inlen);
    215 
    216   int (*decrypt_init)(EVP_PKEY_CTX *ctx);
    217   int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
    218                  const unsigned char *in, size_t inlen);
    219 
    220   int (*derive_init)(EVP_PKEY_CTX *ctx);
    221   int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
    222 
    223   int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
    224   int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value);
    225 } /* EVP_PKEY_METHOD */;
    226 
    227 
    228 #if defined(__cplusplus)
    229 }  /* extern C */
    230 #endif
    231 
    232 #endif  /* OPENSSL_HEADER_EVP_INTERNAL_H */
    233