1 /* 2 * Copyright 2021-2022 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/crypto.h> 11 #include <openssl/provider.h> 12 #include <openssl/decoder.h> 13 #include <openssl/encoder.h> 14 #include <openssl/store.h> 15 #include <openssl/rand.h> 16 #include <openssl/core_names.h> 17 #include "testutil.h" 18 19 static int dummy_decoder_decode(void *ctx, OSSL_CORE_BIO *cin, int selection, 20 OSSL_CALLBACK *object_cb, void *object_cbarg, 21 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) 22 { 23 return 0; 24 } 25 26 static const OSSL_DISPATCH dummy_decoder_functions[] = { 27 { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_decoder_decode }, 28 { 0, NULL } 29 }; 30 31 static const OSSL_ALGORITHM dummy_decoders[] = { 32 { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions }, 33 { NULL, NULL, NULL } 34 }; 35 36 static int dummy_encoder_encode(void *ctx, OSSL_CORE_BIO *out, 37 const void *obj_raw, 38 const OSSL_PARAM obj_abstract[], int selection, 39 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) 40 { 41 return 0; 42 } 43 44 static const OSSL_DISPATCH dummy_encoder_functions[] = { 45 { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_encoder_encode }, 46 { 0, NULL } 47 }; 48 49 static const OSSL_ALGORITHM dummy_encoders[] = { 50 { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions }, 51 { NULL, NULL, NULL } 52 }; 53 54 static void *dummy_store_open(void *provctx, const char *uri) 55 { 56 return NULL; 57 } 58 59 static int dummy_store_load(void *loaderctx, OSSL_CALLBACK *object_cb, 60 void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, 61 void *pw_cbarg) 62 { 63 return 0; 64 } 65 66 static int dumm_store_eof(void *loaderctx) 67 { 68 return 0; 69 } 70 71 static int dummy_store_close(void *loaderctx) 72 { 73 return 0; 74 } 75 76 static const OSSL_DISPATCH dummy_store_functions[] = { 77 { OSSL_FUNC_STORE_OPEN, (void (*)(void))dummy_store_open }, 78 { OSSL_FUNC_STORE_LOAD, (void (*)(void))dummy_store_load }, 79 { OSSL_FUNC_STORE_EOF, (void (*)(void))dumm_store_eof }, 80 { OSSL_FUNC_STORE_CLOSE, (void (*)(void))dummy_store_close }, 81 { 0, NULL } 82 }; 83 84 static const OSSL_ALGORITHM dummy_store[] = { 85 { "DUMMY", "provider=dummy", dummy_store_functions }, 86 { NULL, NULL, NULL } 87 }; 88 89 static void *dummy_rand_newctx(void *provctx, void *parent, 90 const OSSL_DISPATCH *parent_calls) 91 { 92 return provctx; 93 } 94 95 static void dummy_rand_freectx(void *vctx) 96 { 97 } 98 99 static int dummy_rand_instantiate(void *vdrbg, unsigned int strength, 100 int prediction_resistance, 101 const unsigned char *pstr, size_t pstr_len, 102 const OSSL_PARAM params[]) 103 { 104 return 1; 105 } 106 107 static int dummy_rand_uninstantiate(void *vdrbg) 108 { 109 return 1; 110 } 111 112 static int dummy_rand_generate(void *vctx, unsigned char *out, size_t outlen, 113 unsigned int strength, int prediction_resistance, 114 const unsigned char *addin, size_t addin_len) 115 { 116 size_t i; 117 118 for (i = 0; i <outlen; i++) 119 out[i] = (unsigned char)(i & 0xff); 120 121 return 1; 122 } 123 124 static const OSSL_PARAM *dummy_rand_gettable_ctx_params(void *vctx, void *provctx) 125 { 126 static const OSSL_PARAM known_gettable_ctx_params[] = { 127 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), 128 OSSL_PARAM_END 129 }; 130 return known_gettable_ctx_params; 131 } 132 133 static int dummy_rand_get_ctx_params(void *vctx, OSSL_PARAM params[]) 134 { 135 OSSL_PARAM *p; 136 137 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST); 138 if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX)) 139 return 0; 140 141 return 1; 142 } 143 144 static int dummy_rand_enable_locking(void *vtest) 145 { 146 return 1; 147 } 148 149 static int dummy_rand_lock(void *vtest) 150 { 151 return 1; 152 } 153 154 static void dummy_rand_unlock(void *vtest) 155 { 156 } 157 158 static const OSSL_DISPATCH dummy_rand_functions[] = { 159 { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))dummy_rand_newctx }, 160 { OSSL_FUNC_RAND_FREECTX, (void (*)(void))dummy_rand_freectx }, 161 { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))dummy_rand_instantiate }, 162 { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))dummy_rand_uninstantiate }, 163 { OSSL_FUNC_RAND_GENERATE, (void (*)(void))dummy_rand_generate }, 164 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, 165 (void(*)(void))dummy_rand_gettable_ctx_params }, 166 { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))dummy_rand_get_ctx_params }, 167 { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))dummy_rand_enable_locking }, 168 { OSSL_FUNC_RAND_LOCK, (void(*)(void))dummy_rand_lock }, 169 { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))dummy_rand_unlock }, 170 { 0, NULL } 171 }; 172 173 static const OSSL_ALGORITHM dummy_rand[] = { 174 { "DUMMY", "provider=dummy", dummy_rand_functions }, 175 { NULL, NULL, NULL } 176 }; 177 178 static const OSSL_ALGORITHM *dummy_query(void *provctx, int operation_id, 179 int *no_cache) 180 { 181 *no_cache = 0; 182 switch (operation_id) { 183 case OSSL_OP_DECODER: 184 return dummy_decoders; 185 case OSSL_OP_ENCODER: 186 return dummy_encoders; 187 case OSSL_OP_STORE: 188 return dummy_store; 189 case OSSL_OP_RAND: 190 return dummy_rand; 191 } 192 return NULL; 193 } 194 195 static const OSSL_DISPATCH dummy_dispatch_table[] = { 196 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))dummy_query }, 197 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free }, 198 { 0, NULL } 199 }; 200 201 static int dummy_provider_init(const OSSL_CORE_HANDLE *handle, 202 const OSSL_DISPATCH *in, 203 const OSSL_DISPATCH **out, 204 void **provctx) 205 { 206 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_child(handle, in); 207 unsigned char buf[32]; 208 209 *provctx = (void *)libctx; 210 *out = dummy_dispatch_table; 211 212 /* 213 * Do some work using the child libctx, to make sure this is possible from 214 * inside the init function. 215 */ 216 if (RAND_bytes_ex(libctx, buf, sizeof(buf), 0) <= 0) 217 return 0; 218 219 return 1; 220 } 221 222 /* 223 * Try fetching and freeing various things. 224 * Test 0: Decoder 225 * Test 1: Encoder 226 * Test 2: Store loader 227 * Test 3: EVP_RAND 228 * Test 4-7: As above, but additionally with a query string 229 */ 230 static int fetch_test(int tst) 231 { 232 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); 233 OSSL_PROVIDER *dummyprov = NULL; 234 OSSL_PROVIDER *nullprov = NULL; 235 OSSL_DECODER *decoder = NULL; 236 OSSL_ENCODER *encoder = NULL; 237 OSSL_STORE_LOADER *loader = NULL; 238 int testresult = 0; 239 unsigned char buf[32]; 240 int query = tst > 3; 241 242 if (!TEST_ptr(libctx)) 243 goto err; 244 245 if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "dummy-prov", 246 dummy_provider_init)) 247 || !TEST_ptr(nullprov = OSSL_PROVIDER_load(libctx, "default")) 248 || !TEST_ptr(dummyprov = OSSL_PROVIDER_load(libctx, "dummy-prov"))) 249 goto err; 250 251 switch (tst % 4) { 252 case 0: 253 decoder = OSSL_DECODER_fetch(libctx, "DUMMY", 254 query ? "provider=dummy" : NULL); 255 if (!TEST_ptr(decoder)) 256 goto err; 257 break; 258 case 1: 259 encoder = OSSL_ENCODER_fetch(libctx, "DUMMY", 260 query ? "provider=dummy" : NULL); 261 if (!TEST_ptr(encoder)) 262 goto err; 263 break; 264 case 2: 265 loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY", 266 query ? "provider=dummy" : NULL); 267 if (!TEST_ptr(loader)) 268 goto err; 269 break; 270 case 3: 271 if (!TEST_true(RAND_set_DRBG_type(libctx, "DUMMY", 272 query ? "provider=dummy" : NULL, 273 NULL, NULL)) 274 || !TEST_int_ge(RAND_bytes_ex(libctx, buf, sizeof(buf), 0), 1)) 275 goto err; 276 break; 277 default: 278 goto err; 279 } 280 281 testresult = 1; 282 err: 283 OSSL_DECODER_free(decoder); 284 OSSL_ENCODER_free(encoder); 285 OSSL_STORE_LOADER_free(loader); 286 OSSL_PROVIDER_unload(dummyprov); 287 OSSL_PROVIDER_unload(nullprov); 288 OSSL_LIB_CTX_free(libctx); 289 return testresult; 290 } 291 292 int setup_tests(void) 293 { 294 ADD_ALL_TESTS(fetch_test, 8); 295 296 return 1; 297 } 298