您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Implement new SPKI parsers. Many consumers need SPKI support (X.509, TLS, QUIC, WebCrypto), each with different ways to set signature parameters. SPKIs themselves can get complex with id-RSASSA-PSS keys which come with various constraints in the key parameters. This suggests we want a common in-library representation of an SPKI. This adds two new functions EVP_parse_public_key and EVP_marshal_public_key which converts EVP_PKEY to and from SPKI and implements X509_PUBKEY functions with them. EVP_PKEY seems to have been intended to be able to express the supported SPKI types with full-fidelity, so these APIs will continue this. This means future support for id-RSASSA-PSS would *not* repurpose EVP_PKEY_RSA. I'm worried about code assuming EVP_PKEY_RSA implies acting on the RSA* is legal. Instead, it'd add an EVP_PKEY_RSA_PSS and the data pointer would be some (exposed, so the caller may still check key size, etc.) RSA_PSS_KEY struct. Internally, the EVP_PKEY_CTX implementation would enforce the key constraints. If RSA_PSS_KEY would later need its own API, that code would move there, but that seems unlikely. Ideally we'd have a 1:1 correspondence with key OID, although we may have to fudge things if mistakes happen in standardization. (Whether or not X.509 reuses id-ecPublicKey for Ed25519, we'll give it a separate EVP_PKEY type.) DSA parsing hooks are still implemented, missing parameters and all for now. This isn't any worse than before. Decoupling from the giant crypto/obj OID table will be a later task. BUG=522228 Change-Id: I0e3964edf20cb795a18b0991d17e5ca8bce3e28c Reviewed-on: https://boringssl-review.googlesource.com/6861 Reviewed-by: Adam Langley <agl@google.com>
8 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #include <openssl/rsa.h>
  60. #if defined(__cplusplus)
  61. extern "C" {
  62. #endif
  63. struct evp_pkey_asn1_method_st {
  64. int pkey_id;
  65. uint8_t oid[9];
  66. uint8_t oid_len;
  67. /* pub_decode decodes |params| and |key| as a SubjectPublicKeyInfo
  68. * and writes the result into |out|. It returns one on success and zero on
  69. * error. |params| is the AlgorithmIdentifier after the OBJECT IDENTIFIER
  70. * type field, and |key| is the contents of the subjectPublicKey with the
  71. * leading padding byte checked and removed. Although X.509 uses BIT STRINGs
  72. * to represent SubjectPublicKeyInfo, every key type defined encodes the key
  73. * as a byte string with the same conversion to BIT STRING. */
  74. int (*pub_decode)(EVP_PKEY *out, CBS *params, CBS *key);
  75. /* pub_encode encodes |key| as a SubjectPublicKeyInfo and appends the result
  76. * to |out|. It returns one on success and zero on error. */
  77. int (*pub_encode)(CBB *out, const EVP_PKEY *key);
  78. int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
  79. /* priv_decode decodes |params| and |key| as a PrivateKeyInfo and writes the
  80. * result into |out|. It returns one on success and zero on error. |params| is
  81. * the AlgorithmIdentifier after the OBJECT IDENTIFIER type field, and |key|
  82. * is the contents of the OCTET STRING privateKey field. */
  83. int (*priv_decode)(EVP_PKEY *out, CBS *params, CBS *key);
  84. /* priv_encode encodes |key| as a PrivateKeyInfo and appends the result to
  85. * |out|. It returns one on success and zero on error. */
  86. int (*priv_encode)(CBB *out, const EVP_PKEY *key);
  87. /* pkey_opaque returns 1 if the |pk| is opaque. Opaque keys are backed by
  88. * custom implementations which do not expose key material and parameters.*/
  89. int (*pkey_opaque)(const EVP_PKEY *pk);
  90. int (*pkey_size)(const EVP_PKEY *pk);
  91. int (*pkey_bits)(const EVP_PKEY *pk);
  92. int (*param_missing)(const EVP_PKEY *pk);
  93. int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
  94. int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
  95. void (*pkey_free)(EVP_PKEY *pkey);
  96. } /* EVP_PKEY_ASN1_METHOD */;
  97. #define EVP_PKEY_OP_UNDEFINED 0
  98. #define EVP_PKEY_OP_KEYGEN (1 << 2)
  99. #define EVP_PKEY_OP_SIGN (1 << 3)
  100. #define EVP_PKEY_OP_VERIFY (1 << 4)
  101. #define EVP_PKEY_OP_VERIFYRECOVER (1 << 5)
  102. #define EVP_PKEY_OP_ENCRYPT (1 << 6)
  103. #define EVP_PKEY_OP_DECRYPT (1 << 7)
  104. #define EVP_PKEY_OP_DERIVE (1 << 8)
  105. #define EVP_PKEY_OP_TYPE_SIG \
  106. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYRECOVER)
  107. #define EVP_PKEY_OP_TYPE_CRYPT (EVP_PKEY_OP_ENCRYPT | EVP_PKEY_OP_DECRYPT)
  108. #define EVP_PKEY_OP_TYPE_NOGEN \
  109. (EVP_PKEY_OP_SIG | EVP_PKEY_OP_CRYPT | EVP_PKEY_OP_DERIVE)
  110. #define EVP_PKEY_OP_TYPE_GEN EVP_PKEY_OP_KEYGEN
  111. /* EVP_PKEY_CTX_ctrl performs |cmd| on |ctx|. The |keytype| and |optype|
  112. * arguments can be -1 to specify that any type and operation are acceptable,
  113. * otherwise |keytype| must match the type of |ctx| and the bits of |optype|
  114. * must intersect the operation flags set on |ctx|.
  115. *
  116. * The |p1| and |p2| arguments depend on the value of |cmd|.
  117. *
  118. * It returns one on success and zero on error. */
  119. OPENSSL_EXPORT int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  120. int cmd, int p1, void *p2);
  121. #define EVP_PKEY_CTRL_MD 1
  122. #define EVP_PKEY_CTRL_GET_MD 2
  123. /* EVP_PKEY_CTRL_PEER_KEY is called with different values of |p1|:
  124. * 0: Is called from |EVP_PKEY_derive_set_peer| and |p2| contains a peer key.
  125. * If the return value is <= 0, the key is rejected.
  126. * 1: Is called at the end of |EVP_PKEY_derive_set_peer| and |p2| contains a
  127. * peer key. If the return value is <= 0, the key is rejected.
  128. * 2: Is called with |p2| == NULL to test whether the peer's key was used.
  129. * (EC)DH always return one in this case.
  130. * 3: Is called with |p2| == NULL to set whether the peer's key was used.
  131. * (EC)DH always return one in this case. This was only used for GOST. */
  132. #define EVP_PKEY_CTRL_PEER_KEY 3
  133. /* EVP_PKEY_ALG_CTRL is the base value from which key-type specific ctrl
  134. * commands are numbered. */
  135. #define EVP_PKEY_ALG_CTRL 0x1000
  136. #define EVP_PKEY_CTRL_RSA_PADDING (EVP_PKEY_ALG_CTRL + 1)
  137. #define EVP_PKEY_CTRL_GET_RSA_PADDING (EVP_PKEY_ALG_CTRL + 2)
  138. #define EVP_PKEY_CTRL_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 3)
  139. #define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 4)
  140. #define EVP_PKEY_CTRL_RSA_KEYGEN_BITS (EVP_PKEY_ALG_CTRL + 5)
  141. #define EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP (EVP_PKEY_ALG_CTRL + 6)
  142. #define EVP_PKEY_CTRL_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 7)
  143. #define EVP_PKEY_CTRL_GET_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 8)
  144. #define EVP_PKEY_CTRL_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 9)
  145. #define EVP_PKEY_CTRL_GET_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 10)
  146. #define EVP_PKEY_CTRL_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 11)
  147. #define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12)
  148. struct evp_pkey_ctx_st {
  149. /* Method associated with this operation */
  150. const EVP_PKEY_METHOD *pmeth;
  151. /* Engine that implements this method or NULL if builtin */
  152. ENGINE *engine;
  153. /* Key: may be NULL */
  154. EVP_PKEY *pkey;
  155. /* Peer key for key agreement, may be NULL */
  156. EVP_PKEY *peerkey;
  157. /* operation contains one of the |EVP_PKEY_OP_*| values. */
  158. int operation;
  159. /* Algorithm specific data */
  160. void *data;
  161. } /* EVP_PKEY_CTX */;
  162. struct evp_pkey_method_st {
  163. int pkey_id;
  164. int (*init)(EVP_PKEY_CTX *ctx);
  165. int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src);
  166. void (*cleanup)(EVP_PKEY_CTX *ctx);
  167. int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
  168. int (*sign)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  169. const uint8_t *tbs, size_t tbslen);
  170. int (*sign_message)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  171. const uint8_t *tbs, size_t tbslen);
  172. int (*verify)(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
  173. const uint8_t *tbs, size_t tbslen);
  174. int (*verify_message)(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
  175. const uint8_t *tbs, size_t tbslen);
  176. int (*verify_recover)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  177. const uint8_t *sig, size_t sig_len);
  178. int (*encrypt)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  179. const uint8_t *in, size_t inlen);
  180. int (*decrypt)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  181. const uint8_t *in, size_t inlen);
  182. int (*derive)(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *keylen);
  183. int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  184. } /* EVP_PKEY_METHOD */;
  185. typedef struct {
  186. union {
  187. uint8_t priv[64];
  188. struct {
  189. /* Shift the location of the public key to align with where it is in the
  190. * private key representation. */
  191. uint8_t pad[32];
  192. uint8_t value[32];
  193. } pub;
  194. } key;
  195. char has_private;
  196. } ED25519_KEY;
  197. extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth;
  198. extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth;
  199. extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
  200. extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth;
  201. extern const EVP_PKEY_METHOD rsa_pkey_meth;
  202. extern const EVP_PKEY_METHOD ec_pkey_meth;
  203. extern const EVP_PKEY_METHOD ed25519_pkey_meth;
  204. #if defined(__cplusplus)
  205. } /* extern C */
  206. #endif
  207. #endif /* OPENSSL_HEADER_EVP_INTERNAL_H */