1 /*
2  * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <openssl/core_dispatch.h>
11 #include "internal/refcount.h"
12 
13 #define EVP_CTRL_RET_UNSUPPORTED -1
14 
15 
16 struct evp_md_ctx_st {
17     const EVP_MD *reqdigest;    /* The original requested digest */
18     const EVP_MD *digest;
19     ENGINE *engine;             /* functional reference if 'digest' is
20                                  * ENGINE-provided */
21     unsigned long flags;
22     void *md_data;
23     /* Public key context for sign/verify */
24     EVP_PKEY_CTX *pctx;
25     /* Update function: usually copied from EVP_MD */
26     int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
27 
28     /*
29      * Opaque ctx returned from a providers digest algorithm implementation
30      * OSSL_FUNC_digest_newctx()
31      */
32     void *algctx;
33     EVP_MD *fetched_digest;
34 } /* EVP_MD_CTX */ ;
35 
36 struct evp_cipher_ctx_st {
37     const EVP_CIPHER *cipher;
38     ENGINE *engine;             /* functional reference if 'cipher' is
39                                  * ENGINE-provided */
40     int encrypt;                /* encrypt or decrypt */
41     int buf_len;                /* number we have left */
42     unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
43     unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
44     unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
45     int num;                    /* used by cfb/ofb/ctr mode */
46     /* FIXME: Should this even exist? It appears unused */
47     void *app_data;             /* application stuff */
48     int key_len;                /* May change for variable length cipher */
49     unsigned long flags;        /* Various flags */
50     void *cipher_data;          /* per EVP data */
51     int final_used;
52     int block_mask;
53     unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
54 
55     /*
56      * Opaque ctx returned from a providers cipher algorithm implementation
57      * OSSL_FUNC_cipher_newctx()
58      */
59     void *algctx;
60     EVP_CIPHER *fetched_cipher;
61 } /* EVP_CIPHER_CTX */ ;
62 
63 struct evp_mac_ctx_st {
64     EVP_MAC *meth;               /* Method structure */
65     /*
66      * Opaque ctx returned from a providers MAC algorithm implementation
67      * OSSL_FUNC_mac_newctx()
68      */
69     void *algctx;
70 } /* EVP_MAC_CTX */;
71 
72 struct evp_kdf_ctx_st {
73     EVP_KDF *meth;              /* Method structure */
74     /*
75      * Opaque ctx returned from a providers KDF algorithm implementation
76      * OSSL_FUNC_kdf_newctx()
77      */
78     void *algctx;
79 } /* EVP_KDF_CTX */ ;
80 
81 struct evp_rand_ctx_st {
82     EVP_RAND *meth;             /* Method structure */
83     /*
84      * Opaque ctx returned from a providers rand algorithm implementation
85      * OSSL_FUNC_rand_newctx()
86      */
87     void *algctx;
88     EVP_RAND_CTX *parent;       /* Parent EVP_RAND or NULL if none */
89     CRYPTO_REF_COUNT refcnt;    /* Context reference count */
90     CRYPTO_RWLOCK *refcnt_lock;
91 } /* EVP_RAND_CTX */ ;
92 
93 struct evp_keymgmt_st {
94     int id;                      /* libcrypto internal */
95 
96     int name_id;
97     char *type_name;
98     const char *description;
99     OSSL_PROVIDER *prov;
100     CRYPTO_REF_COUNT refcnt;
101     CRYPTO_RWLOCK *lock;
102 
103     /* Constructor(s), destructor, information */
104     OSSL_FUNC_keymgmt_new_fn *new;
105     OSSL_FUNC_keymgmt_free_fn *free;
106     OSSL_FUNC_keymgmt_get_params_fn *get_params;
107     OSSL_FUNC_keymgmt_gettable_params_fn *gettable_params;
108     OSSL_FUNC_keymgmt_set_params_fn *set_params;
109     OSSL_FUNC_keymgmt_settable_params_fn *settable_params;
110 
111     /* Generation, a complex constructor */
112     OSSL_FUNC_keymgmt_gen_init_fn *gen_init;
113     OSSL_FUNC_keymgmt_gen_set_template_fn *gen_set_template;
114     OSSL_FUNC_keymgmt_gen_set_params_fn *gen_set_params;
115     OSSL_FUNC_keymgmt_gen_settable_params_fn *gen_settable_params;
116     OSSL_FUNC_keymgmt_gen_fn *gen;
117     OSSL_FUNC_keymgmt_gen_cleanup_fn *gen_cleanup;
118 
119     OSSL_FUNC_keymgmt_load_fn *load;
120 
121     /* Key object checking */
122     OSSL_FUNC_keymgmt_query_operation_name_fn *query_operation_name;
123     OSSL_FUNC_keymgmt_has_fn *has;
124     OSSL_FUNC_keymgmt_validate_fn *validate;
125     OSSL_FUNC_keymgmt_match_fn *match;
126 
127     /* Import and export routines */
128     OSSL_FUNC_keymgmt_import_fn *import;
129     OSSL_FUNC_keymgmt_import_types_fn *import_types;
130     OSSL_FUNC_keymgmt_export_fn *export;
131     OSSL_FUNC_keymgmt_export_types_fn *export_types;
132     OSSL_FUNC_keymgmt_dup_fn *dup;
133 } /* EVP_KEYMGMT */ ;
134 
135 struct evp_keyexch_st {
136     int name_id;
137     char *type_name;
138     const char *description;
139     OSSL_PROVIDER *prov;
140     CRYPTO_REF_COUNT refcnt;
141     CRYPTO_RWLOCK *lock;
142 
143     OSSL_FUNC_keyexch_newctx_fn *newctx;
144     OSSL_FUNC_keyexch_init_fn *init;
145     OSSL_FUNC_keyexch_set_peer_fn *set_peer;
146     OSSL_FUNC_keyexch_derive_fn *derive;
147     OSSL_FUNC_keyexch_freectx_fn *freectx;
148     OSSL_FUNC_keyexch_dupctx_fn *dupctx;
149     OSSL_FUNC_keyexch_set_ctx_params_fn *set_ctx_params;
150     OSSL_FUNC_keyexch_settable_ctx_params_fn *settable_ctx_params;
151     OSSL_FUNC_keyexch_get_ctx_params_fn *get_ctx_params;
152     OSSL_FUNC_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
153 } /* EVP_KEYEXCH */;
154 
155 struct evp_signature_st {
156     int name_id;
157     char *type_name;
158     const char *description;
159     OSSL_PROVIDER *prov;
160     CRYPTO_REF_COUNT refcnt;
161     CRYPTO_RWLOCK *lock;
162 
163     OSSL_FUNC_signature_newctx_fn *newctx;
164     OSSL_FUNC_signature_sign_init_fn *sign_init;
165     OSSL_FUNC_signature_sign_fn *sign;
166     OSSL_FUNC_signature_verify_init_fn *verify_init;
167     OSSL_FUNC_signature_verify_fn *verify;
168     OSSL_FUNC_signature_verify_recover_init_fn *verify_recover_init;
169     OSSL_FUNC_signature_verify_recover_fn *verify_recover;
170     OSSL_FUNC_signature_digest_sign_init_fn *digest_sign_init;
171     OSSL_FUNC_signature_digest_sign_update_fn *digest_sign_update;
172     OSSL_FUNC_signature_digest_sign_final_fn *digest_sign_final;
173     OSSL_FUNC_signature_digest_sign_fn *digest_sign;
174     OSSL_FUNC_signature_digest_verify_init_fn *digest_verify_init;
175     OSSL_FUNC_signature_digest_verify_update_fn *digest_verify_update;
176     OSSL_FUNC_signature_digest_verify_final_fn *digest_verify_final;
177     OSSL_FUNC_signature_digest_verify_fn *digest_verify;
178     OSSL_FUNC_signature_freectx_fn *freectx;
179     OSSL_FUNC_signature_dupctx_fn *dupctx;
180     OSSL_FUNC_signature_get_ctx_params_fn *get_ctx_params;
181     OSSL_FUNC_signature_gettable_ctx_params_fn *gettable_ctx_params;
182     OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params;
183     OSSL_FUNC_signature_settable_ctx_params_fn *settable_ctx_params;
184     OSSL_FUNC_signature_get_ctx_md_params_fn *get_ctx_md_params;
185     OSSL_FUNC_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
186     OSSL_FUNC_signature_set_ctx_md_params_fn *set_ctx_md_params;
187     OSSL_FUNC_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
188 } /* EVP_SIGNATURE */;
189 
190 struct evp_asym_cipher_st {
191     int name_id;
192     char *type_name;
193     const char *description;
194     OSSL_PROVIDER *prov;
195     CRYPTO_REF_COUNT refcnt;
196     CRYPTO_RWLOCK *lock;
197 
198     OSSL_FUNC_asym_cipher_newctx_fn *newctx;
199     OSSL_FUNC_asym_cipher_encrypt_init_fn *encrypt_init;
200     OSSL_FUNC_asym_cipher_encrypt_fn *encrypt;
201     OSSL_FUNC_asym_cipher_decrypt_init_fn *decrypt_init;
202     OSSL_FUNC_asym_cipher_decrypt_fn *decrypt;
203     OSSL_FUNC_asym_cipher_freectx_fn *freectx;
204     OSSL_FUNC_asym_cipher_dupctx_fn *dupctx;
205     OSSL_FUNC_asym_cipher_get_ctx_params_fn *get_ctx_params;
206     OSSL_FUNC_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
207     OSSL_FUNC_asym_cipher_set_ctx_params_fn *set_ctx_params;
208     OSSL_FUNC_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
209 } /* EVP_ASYM_CIPHER */;
210 
211 struct evp_kem_st {
212     int name_id;
213     char *type_name;
214     const char *description;
215     OSSL_PROVIDER *prov;
216     CRYPTO_REF_COUNT refcnt;
217     CRYPTO_RWLOCK *lock;
218 
219     OSSL_FUNC_kem_newctx_fn *newctx;
220     OSSL_FUNC_kem_encapsulate_init_fn *encapsulate_init;
221     OSSL_FUNC_kem_encapsulate_fn *encapsulate;
222     OSSL_FUNC_kem_decapsulate_init_fn *decapsulate_init;
223     OSSL_FUNC_kem_decapsulate_fn *decapsulate;
224     OSSL_FUNC_kem_freectx_fn *freectx;
225     OSSL_FUNC_kem_dupctx_fn *dupctx;
226     OSSL_FUNC_kem_get_ctx_params_fn *get_ctx_params;
227     OSSL_FUNC_kem_gettable_ctx_params_fn *gettable_ctx_params;
228     OSSL_FUNC_kem_set_ctx_params_fn *set_ctx_params;
229     OSSL_FUNC_kem_settable_ctx_params_fn *settable_ctx_params;
230 } /* EVP_KEM */;
231 
232 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
233                              int passlen, ASN1_TYPE *param,
234                              const EVP_CIPHER *c, const EVP_MD *md,
235                              int en_de);
236 int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
237                                 int passlen, ASN1_TYPE *param,
238                                 const EVP_CIPHER *c, const EVP_MD *md,
239                                 int en_de, OSSL_LIB_CTX *libctx, const char *propq);
240 
241 struct evp_Encode_Ctx_st {
242     /* number saved in a partial encode/decode */
243     int num;
244     /*
245      * The length is either the output line length (in input bytes) or the
246      * shortest input line length that is ok.  Once decoding begins, the
247      * length is adjusted up each time a longer line is decoded
248      */
249     int length;
250     /* data to encode */
251     unsigned char enc_data[80];
252     /* number read on current line */
253     int line_num;
254     unsigned int flags;
255 };
256 
257 typedef struct evp_pbe_st EVP_PBE_CTL;
258 DEFINE_STACK_OF(EVP_PBE_CTL)
259 
260 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
261 
262 #include <openssl/types.h>
263 #include <openssl/core.h>
264 
265 void *evp_generic_fetch(OSSL_LIB_CTX *ctx, int operation_id,
266                         const char *name, const char *properties,
267                         void *(*new_method)(int name_id,
268                                             const OSSL_ALGORITHM *algodef,
269                                             OSSL_PROVIDER *prov),
270                         int (*up_ref_method)(void *),
271                         void (*free_method)(void *));
272 void *evp_generic_fetch_by_number(OSSL_LIB_CTX *ctx, int operation_id,
273                                   int name_id, const char *properties,
274                                   void *(*new_method)(int name_id,
275                                                       const OSSL_ALGORITHM *algodef,
276                                                       OSSL_PROVIDER *prov),
277                                   int (*up_ref_method)(void *),
278                                   void (*free_method)(void *));
279 void evp_generic_do_all_prefetched(OSSL_LIB_CTX *libctx, int operation_id,
280                                    void (*user_fn)(void *method, void *arg),
281                                    void *user_arg);
282 void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
283                         void (*user_fn)(void *method, void *arg),
284                         void *user_arg,
285                         void *(*new_method)(int name_id,
286                                             const OSSL_ALGORITHM *algodef,
287                                             OSSL_PROVIDER *prov),
288                         int (*up_ref_method)(void *),
289                         void (*free_method)(void *));
290 
291 /* Internal fetchers for method types that are to be combined with others */
292 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
293                                          const char *properties);
294 
295 /* Internal structure constructors for fetched methods */
296 EVP_MD *evp_md_new(void);
297 EVP_CIPHER *evp_cipher_new(void);
298 
299 int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
300                                     evp_cipher_aead_asn1_params *asn1_params);
301 int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
302                                     evp_cipher_aead_asn1_params *asn1_params);
303 
304 /* Helper functions to avoid duplicating code */
305 
306 /*
307  * These methods implement different ways to pass a params array to the
308  * provider.  They will return one of these values:
309  *
310  * -2 if the method doesn't come from a provider
311  *    (evp_do_param will return this to the called)
312  * -1 if the provider doesn't offer the desired function
313  *    (evp_do_param will raise an error and return 0)
314  * or the return value from the desired function
315  *    (evp_do_param will return it to the caller)
316  */
317 int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
318 int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
319                               OSSL_PARAM params[]);
320 int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
321                               OSSL_PARAM params[]);
322 int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
323 int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
324                             OSSL_PARAM params[]);
325 int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
326                             OSSL_PARAM params[]);
327 
328 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
329 
330 #define M_check_autoarg(ctx, arg, arglen, err) \
331     if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) {           \
332         size_t pksize = (size_t)EVP_PKEY_get_size(ctx->pkey);         \
333                                                                   \
334         if (pksize == 0) {                                        \
335             ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
336             return 0;                                             \
337         }                                                         \
338         if (arg == NULL) {                                        \
339             *arglen = pksize;                                     \
340             return 1;                                             \
341         }                                                         \
342         if (*arglen < pksize) {                                   \
343             ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
344             return 0;                                             \
345         }                                                         \
346     }
347 
348 void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
349 void evp_cipher_free_int(EVP_CIPHER *md);
350 void evp_md_free_int(EVP_MD *md);
351 
352 /* OSSL_PROVIDER * is only used to get the library context */
353 int evp_is_a(OSSL_PROVIDER *prov, int number,
354              const char *legacy_name, const char *name);
355 int evp_names_do_all(OSSL_PROVIDER *prov, int number,
356                      void (*fn)(const char *name, void *data),
357                      void *data);
358 int evp_cipher_cache_constants(EVP_CIPHER *cipher);
359