| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #ifndef OPENSSL_HEADER_CRYPTO_EVP_INTERNAL_H |
| 16 | #define OPENSSL_HEADER_CRYPTO_EVP_INTERNAL_H |
| 17 |
| 18 | #include <openssl/base.h> |
| 19 |
| 20 | #include <openssl/rsa.h> |
| 21 |
| 22 | #if defined(__cplusplus) |
| 23 | extern "C" { |
| 24 | #endif |
| 25 |
| 26 |
| 27 | typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD; |
| 28 | typedef struct evp_pkey_method_st EVP_PKEY_METHOD; |
| 29 |
| 30 | struct evp_pkey_asn1_method_st { |
| 31 | int pkey_id; |
| 32 | uint8_t oid[9]; |
| 33 | uint8_t oid_len; |
| 34 |
| 35 | const EVP_PKEY_METHOD *pkey_method; |
| 36 |
| 37 | // pub_decode decodes |params| and |key| as a SubjectPublicKeyInfo |
| 38 | // and writes the result into |out|. It returns one on success and zero on |
| 39 | // error. |params| is the AlgorithmIdentifier after the OBJECT IDENTIFIER |
| 40 | // type field, and |key| is the contents of the subjectPublicKey with the |
| 41 | // leading padding byte checked and removed. Although X.509 uses BIT STRINGs |
| 42 | // to represent SubjectPublicKeyInfo, every key type defined encodes the key |
| 43 | // as a byte string with the same conversion to BIT STRING. |
| 44 | int (*pub_decode)(EVP_PKEY *out, CBS *params, CBS *key); |
| 45 |
| 46 | // pub_encode encodes |key| as a SubjectPublicKeyInfo and appends the result |
| 47 | // to |out|. It returns one on success and zero on error. |
| 48 | int (*pub_encode)(CBB *out, const EVP_PKEY *key); |
| 49 |
| 50 | int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); |
| 51 |
| 52 | // priv_decode decodes |params| and |key| as a PrivateKeyInfo and writes the |
| 53 | // result into |out|. It returns one on success and zero on error. |params| is |
| 54 | // the AlgorithmIdentifier after the OBJECT IDENTIFIER type field, and |key| |
| 55 | // is the contents of the OCTET STRING privateKey field. |
| 56 | int (*priv_decode)(EVP_PKEY *out, CBS *params, CBS *key); |
| 57 |
| 58 | // priv_encode encodes |key| as a PrivateKeyInfo and appends the result to |
| 59 | // |out|. It returns one on success and zero on error. |
| 60 | int (*priv_encode)(CBB *out, const EVP_PKEY *key); |
| 61 |
| 62 | int (*set_priv_raw)(EVP_PKEY *pkey, const uint8_t *in, size_t len); |
| 63 | int (*set_pub_raw)(EVP_PKEY *pkey, const uint8_t *in, size_t len); |
| 64 | int (*get_priv_raw)(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len); |
| 65 | int (*get_pub_raw)(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len); |
| 66 |
| 67 | // TODO(davidben): Can these be merged with the functions above? OpenSSL does |
| 68 | // not implement |EVP_PKEY_get_raw_public_key|, etc., for |EVP_PKEY_EC|, but |
| 69 | // the distinction seems unimportant. OpenSSL 3.0 has since renamed |
| 70 | // |EVP_PKEY_get1_tls_encodedpoint| to |EVP_PKEY_get1_encoded_public_key|, and |
| 71 | // what is the difference between "raw" and an "encoded" public key. |
| 72 | // |
| 73 | // One nuisance is the notion of "raw" is slightly ambiguous for EC keys. Is |
| 74 | // it a DER ECPrivateKey or just the scalar? |
| 75 | int (*set1_tls_encodedpoint)(EVP_PKEY *pkey, const uint8_t *in, size_t len); |
| 76 | size_t (*get1_tls_encodedpoint)(const EVP_PKEY *pkey, uint8_t **out_ptr); |
| 77 |
| 78 | // pkey_opaque returns 1 if the |pk| is opaque. Opaque keys are backed by |
| 79 | // custom implementations which do not expose key material and parameters. |
| 80 | int (*pkey_opaque)(const EVP_PKEY *pk); |
| 81 |
| 82 | int (*pkey_size)(const EVP_PKEY *pk); |
| 83 | int (*pkey_bits)(const EVP_PKEY *pk); |
| 84 |
| 85 | int (*param_missing)(const EVP_PKEY *pk); |
| 86 | int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from); |
| 87 | int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); |
| 88 |
| 89 | void (*pkey_free)(EVP_PKEY *pkey); |
| 90 | } /* EVP_PKEY_ASN1_METHOD */; |
| 91 |
| 92 | struct evp_pkey_st { |
| 93 | CRYPTO_refcount_t references; |
| 94 |
| 95 | // type contains one of the EVP_PKEY_* values or NID_undef and determines |
| 96 | // the type of |pkey|. |
| 97 | int type; |
| 98 |
| 99 | // pkey contains a pointer to a structure dependent on |type|. |
| 100 | void *pkey; |
| 101 |
| 102 | // ameth contains a pointer to a method table that contains many ASN.1 |
| 103 | // methods for the key type. |
| 104 | const EVP_PKEY_ASN1_METHOD *ameth; |
| 105 | } /* EVP_PKEY */; |
| 106 |
| 107 | #define EVP_PKEY_OP_UNDEFINED 0 |
| 108 | #define EVP_PKEY_OP_KEYGEN (1 << 2) |
| 109 | #define EVP_PKEY_OP_SIGN (1 << 3) |
| 110 | #define EVP_PKEY_OP_VERIFY (1 << 4) |
| 111 | #define EVP_PKEY_OP_VERIFYRECOVER (1 << 5) |
| 112 | #define EVP_PKEY_OP_ENCRYPT (1 << 6) |
| 113 | #define EVP_PKEY_OP_DECRYPT (1 << 7) |
| 114 | #define EVP_PKEY_OP_DERIVE (1 << 8) |
| 115 | #define EVP_PKEY_OP_PARAMGEN (1 << 9) |
| 116 |
| 117 | #define EVP_PKEY_OP_TYPE_SIG \ |
| 118 | (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYRECOVER) |
| 119 |
| 120 | #define EVP_PKEY_OP_TYPE_CRYPT (EVP_PKEY_OP_ENCRYPT | EVP_PKEY_OP_DECRYPT) |
| 121 |
| 122 | #define EVP_PKEY_OP_TYPE_NOGEN \ |
| 123 | (EVP_PKEY_OP_SIG | EVP_PKEY_OP_CRYPT | EVP_PKEY_OP_DERIVE) |
| 124 |
| 125 | #define EVP_PKEY_OP_TYPE_GEN (EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_PARAMGEN) |
| 126 |
| 127 | // EVP_PKEY_CTX_ctrl performs |cmd| on |ctx|. The |keytype| and |optype| |
| 128 | // arguments can be -1 to specify that any type and operation are acceptable, |
| 129 | // otherwise |keytype| must match the type of |ctx| and the bits of |optype| |
| 130 | // must intersect the operation flags set on |ctx|. |
| 131 | // |
| 132 | // The |p1| and |p2| arguments depend on the value of |cmd|. |
| 133 | // |
| 134 | // It returns one on success and zero on error. |
| 135 | OPENSSL_EXPORT int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
| 136 | int cmd, int p1, void *p2); |
| 137 |
| 138 | #define EVP_PKEY_CTRL_MD 1 |
| 139 | #define EVP_PKEY_CTRL_GET_MD 2 |
| 140 |
| 141 | // EVP_PKEY_CTRL_PEER_KEY is called with different values of |p1|: |
| 142 | // 0: Is called from |EVP_PKEY_derive_set_peer| and |p2| contains a peer key. |
| 143 | // If the return value is <= 0, the key is rejected. |
| 144 | // 1: Is called at the end of |EVP_PKEY_derive_set_peer| and |p2| contains a |
| 145 | // peer key. If the return value is <= 0, the key is rejected. |
| 146 | // 2: Is called with |p2| == NULL to test whether the peer's key was used. |
| 147 | // (EC)DH always return one in this case. |
| 148 | // 3: Is called with |p2| == NULL to set whether the peer's key was used. |
| 149 | // (EC)DH always return one in this case. This was only used for GOST. |
| 150 | #define EVP_PKEY_CTRL_PEER_KEY 3 |
| 151 |
| 152 | // EVP_PKEY_ALG_CTRL is the base value from which key-type specific ctrl |
| 153 | // commands are numbered. |
| 154 | #define EVP_PKEY_ALG_CTRL 0x1000 |
| 155 |
| 156 | #define EVP_PKEY_CTRL_RSA_PADDING (EVP_PKEY_ALG_CTRL + 1) |
| 157 | #define EVP_PKEY_CTRL_GET_RSA_PADDING (EVP_PKEY_ALG_CTRL + 2) |
| 158 | #define EVP_PKEY_CTRL_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 3) |
| 159 | #define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 4) |
| 160 | #define EVP_PKEY_CTRL_RSA_KEYGEN_BITS (EVP_PKEY_ALG_CTRL + 5) |
| 161 | #define EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP (EVP_PKEY_ALG_CTRL + 6) |
| 162 | #define EVP_PKEY_CTRL_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 7) |
| 163 | #define EVP_PKEY_CTRL_GET_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 8) |
| 164 | #define EVP_PKEY_CTRL_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 9) |
| 165 | #define EVP_PKEY_CTRL_GET_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 10) |
| 166 | #define EVP_PKEY_CTRL_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 11) |
| 167 | #define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12) |
| 168 | #define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 13) |
| 169 | #define EVP_PKEY_CTRL_HKDF_MODE (EVP_PKEY_ALG_CTRL + 14) |
| 170 | #define EVP_PKEY_CTRL_HKDF_MD (EVP_PKEY_ALG_CTRL + 15) |
| 171 | #define EVP_PKEY_CTRL_HKDF_KEY (EVP_PKEY_ALG_CTRL + 16) |
| 172 | #define EVP_PKEY_CTRL_HKDF_SALT (EVP_PKEY_ALG_CTRL + 17) |
| 173 | #define EVP_PKEY_CTRL_HKDF_INFO (EVP_PKEY_ALG_CTRL + 18) |
| 174 | #define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 19) |
| 175 |
| 176 | struct evp_pkey_ctx_st { |
| 177 | // Method associated with this operation |
| 178 | const EVP_PKEY_METHOD *pmeth; |
| 179 | // Engine that implements this method or NULL if builtin |
| 180 | ENGINE *engine; |
| 181 | // Key: may be NULL |
| 182 | EVP_PKEY *pkey; |
| 183 | // Peer key for key agreement, may be NULL |
| 184 | EVP_PKEY *peerkey; |
| 185 | // operation contains one of the |EVP_PKEY_OP_*| values. |
| 186 | int operation; |
| 187 | // Algorithm specific data |
| 188 | void *data; |
| 189 | } /* EVP_PKEY_CTX */; |
| 190 |
| 191 | struct evp_pkey_method_st { |
| 192 | int pkey_id; |
| 193 |
| 194 | int (*init)(EVP_PKEY_CTX *ctx); |
| 195 | int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src); |
| 196 | void (*cleanup)(EVP_PKEY_CTX *ctx); |
| 197 |
| 198 | int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); |
| 199 |
| 200 | int (*sign)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen, |
| 201 | const uint8_t *tbs, size_t tbslen); |
| 202 |
| 203 | int (*sign_message)(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen, |
| 204 | const uint8_t *tbs, size_t tbslen); |
| 205 |
| 206 | int (*verify)(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen, |
| 207 | const uint8_t *tbs, size_t tbslen); |
| 208 |
| 209 | int (*verify_message)(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen, |
| 210 | const uint8_t *tbs, size_t tbslen); |
| 211 |
| 212 | int (*verify_recover)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, |
| 213 | const uint8_t *sig, size_t sig_len); |
| 214 |
| 215 | int (*encrypt)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen, |
| 216 | const uint8_t *in, size_t inlen); |
| 217 |
| 218 | int (*decrypt)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen, |
| 219 | const uint8_t *in, size_t inlen); |
| 220 |
| 221 | int (*derive)(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *keylen); |
| 222 |
| 223 | int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); |
| 224 |
| 225 | int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2); |
| 226 | } /* EVP_PKEY_METHOD */; |
| 227 |
| 228 | typedef struct { |
| 229 | // key is the concatenation of the private seed and public key. It is stored |
| 230 | // as a single 64-bit array to allow passing to |ED25519_sign|. If |
| 231 | // |has_private| is false, the first 32 bytes are uninitialized and the public |
| 232 | // key is in the last 32 bytes. |
| 233 | uint8_t key[64]; |
| 234 | char has_private; |
| 235 | } ED25519_KEY; |
| 236 |
| 237 | #define ED25519_PUBLIC_KEY_OFFSET 32 |
| 238 |
| 239 | typedef struct { |
| 240 | uint8_t pub[32]; |
| 241 | uint8_t priv[32]; |
| 242 | char has_private; |
| 243 | } X25519_KEY; |
| 244 |
| 245 | extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth; |
| 246 | extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth; |
| 247 | extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth; |
| 248 | extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth; |
| 249 | extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth; |
| 250 | extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth; |
| 251 |
| 252 | extern const EVP_PKEY_METHOD rsa_pkey_meth; |
| 253 | extern const EVP_PKEY_METHOD ec_pkey_meth; |
| 254 | extern const EVP_PKEY_METHOD ed25519_pkey_meth; |
| 255 | extern const EVP_PKEY_METHOD x25519_pkey_meth; |
| 256 | extern const EVP_PKEY_METHOD hkdf_pkey_meth; |
| 257 | extern const EVP_PKEY_METHOD dh_pkey_meth; |
| 258 |
| 259 | // evp_pkey_set_method behaves like |EVP_PKEY_set_type|, but takes a pointer to |
| 260 | // a method table. This avoids depending on every |EVP_PKEY_ASN1_METHOD|. |
| 261 | void evp_pkey_set_method(EVP_PKEY *pkey, const EVP_PKEY_ASN1_METHOD *method); |
| 262 |
| 263 |
| 264 | #if defined(__cplusplus) |
| 265 | } // extern C |
| 266 | #endif |
| 267 |
| 268 | #endif // OPENSSL_HEADER_CRYPTO_EVP_INTERNAL_H |
| 269 |