Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

299 строки
12 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@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@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@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@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. #ifndef OPENSSL_HEADER_EVP_INTERNAL_H
  57. #define OPENSSL_HEADER_EVP_INTERNAL_H
  58. #include <openssl/base.h>
  59. #if defined(__cplusplus)
  60. extern "C" {
  61. #endif
  62. /* These values are flags for EVP_PKEY_ASN1_METHOD.flags. */
  63. /* ASN1_PKEY_SIGPARAM_NULL controls whether the default behavior of
  64. * EVP_DigestSignAlgorithm writes an explicit NULL parameter in the
  65. * AlgorithmIdentifier. */
  66. #define ASN1_PKEY_SIGPARAM_NULL 0x1
  67. /* evp_digest_sign_algorithm_result_t is the return value of the
  68. * digest_sign_algorithm function in EVP_PKEY_ASN1_METHOD. */
  69. typedef enum {
  70. /* EVP_DIGEST_SIGN_ALGORITHM_ERROR signals an error. */
  71. EVP_DIGEST_SIGN_ALGORITHM_ERROR = 0,
  72. /* EVP_DIGEST_SIGN_ALGORITHM_SUCCESS signals that the parameters were
  73. * serialized in the AlgorithmIdentifier. */
  74. EVP_DIGEST_SIGN_ALGORITHM_SUCCESS = 1,
  75. /* EVP_DIGEST_SIGN_ALGORITHM_DEFAULT signals that the parameters are
  76. * serialized using the default behavior. */
  77. EVP_DIGEST_SIGN_ALGORITHM_DEFAULT = 2,
  78. } evp_digest_sign_algorithm_result_t;
  79. struct evp_pkey_asn1_method_st {
  80. int pkey_id;
  81. int pkey_base_id;
  82. unsigned long pkey_flags;
  83. char *pem_str;
  84. char *info;
  85. int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub);
  86. int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk);
  87. int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
  88. int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx);
  89. int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf);
  90. int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
  91. int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  92. ASN1_PCTX *pctx);
  93. /* pkey_opaque returns 1 if the |pk| is opaque. Opaque keys are backed by
  94. * custom implementations which do not expose key material and parameters.*/
  95. int (*pkey_opaque)(const EVP_PKEY *pk);
  96. /* pkey_supports_digest returns one if |pkey| supports digests of
  97. * type |md|. This is intended for use with EVP_PKEYs backing custom
  98. * implementations which can't sign all digests. If null, it is
  99. * assumed that all digests are supported. */
  100. int (*pkey_supports_digest)(const EVP_PKEY *pkey, const EVP_MD *md);
  101. int (*pkey_size)(const EVP_PKEY *pk);
  102. int (*pkey_bits)(const EVP_PKEY *pk);
  103. int (*param_decode)(EVP_PKEY *pkey, const unsigned char **pder, int derlen);
  104. int (*param_encode)(const EVP_PKEY *pkey, unsigned char **pder);
  105. int (*param_missing)(const EVP_PKEY *pk);
  106. int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
  107. int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
  108. int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
  109. ASN1_PCTX *pctx);
  110. int (*sig_print)(BIO *out, const X509_ALGOR *sigalg, const ASN1_STRING *sig,
  111. int indent, ASN1_PCTX *pctx);
  112. void (*pkey_free)(EVP_PKEY *pkey);
  113. /* Legacy functions for old PEM */
  114. int (*old_priv_decode)(EVP_PKEY *pkey, const unsigned char **pder,
  115. int derlen);
  116. int (*old_priv_encode)(const EVP_PKEY *pkey, unsigned char **pder);
  117. /* Converting parameters to/from AlgorithmIdentifier (X509_ALGOR). */
  118. int (*digest_verify_init_from_algorithm)(EVP_MD_CTX *ctx,
  119. X509_ALGOR *algor,
  120. EVP_PKEY *pkey);
  121. evp_digest_sign_algorithm_result_t (*digest_sign_algorithm)(
  122. EVP_MD_CTX *ctx,
  123. X509_ALGOR *algor);
  124. } /* EVP_PKEY_ASN1_METHOD */;
  125. typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
  126. #define EVP_PKEY_OP_UNDEFINED 0
  127. #define EVP_PKEY_OP_PARAMGEN (1 << 1)
  128. #define EVP_PKEY_OP_KEYGEN (1 << 2)
  129. #define EVP_PKEY_OP_SIGN (1 << 3)
  130. #define EVP_PKEY_OP_VERIFY (1 << 4)
  131. #define EVP_PKEY_OP_VERIFYRECOVER (1 << 5)
  132. #define EVP_PKEY_OP_SIGNCTX (1 << 6)
  133. #define EVP_PKEY_OP_VERIFYCTX (1 << 7)
  134. #define EVP_PKEY_OP_ENCRYPT (1 << 8)
  135. #define EVP_PKEY_OP_DECRYPT (1 << 9)
  136. #define EVP_PKEY_OP_DERIVE (1 << 10)
  137. #define EVP_PKEY_OP_TYPE_SIG \
  138. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYRECOVER | \
  139. EVP_PKEY_OP_SIGNCTX | EVP_PKEY_OP_VERIFYCTX)
  140. #define EVP_PKEY_OP_TYPE_CRYPT (EVP_PKEY_OP_ENCRYPT | EVP_PKEY_OP_DECRYPT)
  141. #define EVP_PKEY_OP_TYPE_NOGEN \
  142. (EVP_PKEY_OP_SIG | EVP_PKEY_OP_CRYPT | EVP_PKEY_OP_DERIVE)
  143. #define EVP_PKEY_OP_TYPE_GEN (EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN)
  144. /* EVP_PKEY_CTX_ctrl performs |cmd| on |ctx|. The |keytype| and |optype|
  145. * arguments can be -1 to specify that any type and operation are acceptable,
  146. * otherwise |keytype| must match the type of |ctx| and the bits of |optype|
  147. * must intersect the operation flags set on |ctx|.
  148. *
  149. * The |p1| and |p2| arguments depend on the value of |cmd|.
  150. *
  151. * It returns one on success and zero on error. */
  152. OPENSSL_EXPORT int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  153. int cmd, int p1, void *p2);
  154. /* EVP_PKEY_CTRL_DIGESTINIT is an internal value. It's called by
  155. * EVP_DigestInit_ex to signal the |EVP_PKEY| that a digest operation is
  156. * starting.
  157. *
  158. * TODO(davidben): This is only needed to support the deprecated HMAC |EVP_PKEY|
  159. * types. */
  160. #define EVP_PKEY_CTRL_DIGESTINIT 3
  161. /* EVP_PKEY_CTRL_PEER_KEY is called with different values of |p1|:
  162. * 0: Is called from |EVP_PKEY_derive_set_peer| and |p2| contains a peer key.
  163. * If the return value is <= 0, the key is rejected.
  164. * 1: Is called at the end of |EVP_PKEY_derive_set_peer| and |p2| contains a
  165. * peer key. If the return value is <= 0, the key is rejected.
  166. * 2: Is called with |p2| == NULL to test whether the peer's key was used.
  167. * (EC)DH always return one in this case.
  168. * 3: Is called with |p2| == NULL to set whether the peer's key was used.
  169. * (EC)DH always return one in this case. This was only used for GOST. */
  170. #define EVP_PKEY_CTRL_PEER_KEY 4
  171. /* EVP_PKEY_CTRL_SET_MAC_KEY sets a MAC key. For example, this can be done an
  172. * |EVP_PKEY_CTX| prior to calling |EVP_PKEY_keygen| in order to generate an
  173. * HMAC |EVP_PKEY| with the given key. It returns one on success and zero on
  174. * error. */
  175. #define EVP_PKEY_CTRL_SET_MAC_KEY 5
  176. /* EVP_PKEY_ALG_CTRL is the base value from which key-type specific ctrl
  177. * commands are numbered. */
  178. #define EVP_PKEY_ALG_CTRL 0x1000
  179. #define EVP_PKEY_CTRL_MD 1
  180. #define EVP_PKEY_CTRL_GET_MD 2
  181. #define EVP_PKEY_CTRL_RSA_PADDING (EVP_PKEY_ALG_CTRL + 1)
  182. #define EVP_PKEY_CTRL_GET_RSA_PADDING (EVP_PKEY_ALG_CTRL + 2)
  183. #define EVP_PKEY_CTRL_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 3)
  184. #define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 4)
  185. #define EVP_PKEY_CTRL_RSA_KEYGEN_BITS (EVP_PKEY_ALG_CTRL + 5)
  186. #define EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP (EVP_PKEY_ALG_CTRL + 6)
  187. #define EVP_PKEY_CTRL_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 7)
  188. #define EVP_PKEY_CTRL_GET_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 8)
  189. #define EVP_PKEY_CTRL_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 9)
  190. #define EVP_PKEY_CTRL_GET_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 10)
  191. #define EVP_PKEY_CTRL_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 11)
  192. #define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12)
  193. #define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
  194. struct evp_pkey_ctx_st {
  195. /* Method associated with this operation */
  196. const EVP_PKEY_METHOD *pmeth;
  197. /* Engine that implements this method or NULL if builtin */
  198. ENGINE *engine;
  199. /* Key: may be NULL */
  200. EVP_PKEY *pkey;
  201. /* Peer key for key agreement, may be NULL */
  202. EVP_PKEY *peerkey;
  203. /* operation contains one of the |EVP_PKEY_OP_*| values. */
  204. int operation;
  205. /* Algorithm specific data */
  206. void *data;
  207. /* Application specific data */
  208. void *app_data;
  209. } /* EVP_PKEY_CTX */;
  210. struct evp_pkey_method_st {
  211. int pkey_id;
  212. int flags;
  213. int (*init)(EVP_PKEY_CTX *ctx);
  214. int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src);
  215. void (*cleanup)(EVP_PKEY_CTX *ctx);
  216. int (*paramgen_init)(EVP_PKEY_CTX *ctx);
  217. int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  218. int (*keygen_init)(EVP_PKEY_CTX *ctx);
  219. int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  220. int (*sign_init)(EVP_PKEY_CTX *ctx);
  221. int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  222. const unsigned char *tbs, size_t tbslen);
  223. int (*verify_init)(EVP_PKEY_CTX *ctx);
  224. int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
  225. const unsigned char *tbs, size_t tbslen);
  226. int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  227. int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  228. EVP_MD_CTX *mctx);
  229. int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
  230. int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
  231. EVP_MD_CTX *mctx);
  232. int (*encrypt_init)(EVP_PKEY_CTX *ctx);
  233. int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  234. const unsigned char *in, size_t inlen);
  235. int (*decrypt_init)(EVP_PKEY_CTX *ctx);
  236. int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  237. const unsigned char *in, size_t inlen);
  238. int (*derive_init)(EVP_PKEY_CTX *ctx);
  239. int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  240. int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  241. int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value);
  242. } /* EVP_PKEY_METHOD */;
  243. #if defined(__cplusplus)
  244. } /* extern C */
  245. #endif
  246. #endif /* OPENSSL_HEADER_EVP_INTERNAL_H */