diff options
Diffstat (limited to 'src/lib/libcrypto/engine/eng_lib.c')
| -rw-r--r-- | src/lib/libcrypto/engine/eng_lib.c | 290 | 
1 files changed, 164 insertions, 126 deletions
| diff --git a/src/lib/libcrypto/engine/eng_lib.c b/src/lib/libcrypto/engine/eng_lib.c index 4288535d72..569b7199ce 100644 --- a/src/lib/libcrypto/engine/eng_lib.c +++ b/src/lib/libcrypto/engine/eng_lib.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: eng_lib.c,v 1.7 2014/06/12 15:49:29 deraadt Exp $ */ | 1 | /* $OpenBSD: eng_lib.c,v 1.8 2014/06/22 12:05:09 jsing Exp $ */ | 
| 2 | /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL | 2 | /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL | 
| 3 | * project 2000. | 3 | * project 2000. | 
| 4 | */ | 4 | */ | 
| @@ -10,7 +10,7 @@ | |||
| 10 | * are met: | 10 | * are met: | 
| 11 | * | 11 | * | 
| 12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | 
| 13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | 
| 14 | * | 14 | * | 
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | 15 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 16 | * notice, this list of conditions and the following disclaimer in | 16 | * notice, this list of conditions and the following disclaimer in | 
| @@ -61,28 +61,29 @@ | |||
| 61 | 61 | ||
| 62 | /* The "new"/"free" stuff first */ | 62 | /* The "new"/"free" stuff first */ | 
| 63 | 63 | ||
| 64 | ENGINE *ENGINE_new(void) | 64 | ENGINE * | 
| 65 | { | 65 | ENGINE_new(void) | 
| 66 | { | ||
| 66 | ENGINE *ret; | 67 | ENGINE *ret; | 
| 67 | 68 | ||
| 68 | ret = malloc(sizeof(ENGINE)); | 69 | ret = malloc(sizeof(ENGINE)); | 
| 69 | if(ret == NULL) | 70 | if (ret == NULL) { | 
| 70 | { | ||
| 71 | ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE); | 71 | ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE); | 
| 72 | return NULL; | 72 | return NULL; | 
| 73 | } | 73 | } | 
| 74 | memset(ret, 0, sizeof(ENGINE)); | 74 | memset(ret, 0, sizeof(ENGINE)); | 
| 75 | ret->struct_ref = 1; | 75 | ret->struct_ref = 1; | 
| 76 | engine_ref_debug(ret, 0, 1) | 76 | engine_ref_debug(ret, 0, 1) | 
| 77 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data); | 77 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data); | 
| 78 | return ret; | 78 | return ret; | 
| 79 | } | 79 | } | 
| 80 | 80 | ||
| 81 | /* Placed here (close proximity to ENGINE_new) so that modifications to the | 81 | /* Placed here (close proximity to ENGINE_new) so that modifications to the | 
| 82 | * elements of the ENGINE structure are more likely to be caught and changed | 82 | * elements of the ENGINE structure are more likely to be caught and changed | 
| 83 | * here. */ | 83 | * here. */ | 
| 84 | void engine_set_all_null(ENGINE *e) | 84 | void | 
| 85 | { | 85 | engine_set_all_null(ENGINE *e) | 
| 86 | { | ||
| 86 | e->id = NULL; | 87 | e->id = NULL; | 
| 87 | e->name = NULL; | 88 | e->name = NULL; | 
| 88 | e->rsa_meth = NULL; | 89 | e->rsa_meth = NULL; | 
| @@ -100,41 +101,43 @@ void engine_set_all_null(ENGINE *e) | |||
| 100 | e->load_pubkey = NULL; | 101 | e->load_pubkey = NULL; | 
| 101 | e->cmd_defns = NULL; | 102 | e->cmd_defns = NULL; | 
| 102 | e->flags = 0; | 103 | e->flags = 0; | 
| 103 | } | 104 | } | 
| 104 | 105 | ||
| 105 | int engine_free_util(ENGINE *e, int locked) | 106 | int | 
| 106 | { | 107 | engine_free_util(ENGINE *e, int locked) | 
| 108 | { | ||
| 107 | int i; | 109 | int i; | 
| 108 | 110 | ||
| 109 | if(e == NULL) | 111 | if (e == NULL) { | 
| 110 | { | ||
| 111 | ENGINEerr(ENGINE_F_ENGINE_FREE_UTIL, | 112 | ENGINEerr(ENGINE_F_ENGINE_FREE_UTIL, | 
| 112 | ERR_R_PASSED_NULL_PARAMETER); | 113 | ERR_R_PASSED_NULL_PARAMETER); | 
| 113 | return 0; | 114 | return 0; | 
| 114 | } | 115 | } | 
| 115 | if(locked) | 116 | if (locked) | 
| 116 | i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE); | 117 | i = CRYPTO_add(&e->struct_ref, -1, CRYPTO_LOCK_ENGINE); | 
| 117 | else | 118 | else | 
| 118 | i = --e->struct_ref; | 119 | i = --e->struct_ref; | 
| 119 | engine_ref_debug(e, 0, -1) | 120 | engine_ref_debug(e, 0, -1) | 
| 120 | if (i > 0) return 1; | 121 | if (i > 0) | 
| 122 | return 1; | ||
| 121 | 123 | ||
| 122 | /* Free up any dynamically allocated public key methods */ | 124 | /* Free up any dynamically allocated public key methods */ | 
| 123 | engine_pkey_meths_free(e); | 125 | engine_pkey_meths_free(e); | 
| 124 | engine_pkey_asn1_meths_free(e); | 126 | engine_pkey_asn1_meths_free(e); | 
| 125 | /* Give the ENGINE a chance to do any structural cleanup corresponding | 127 | /* Give the ENGINE a chance to do any structural cleanup corresponding | 
| 126 | * to allocation it did in its constructor (eg. unload error strings) */ | 128 | * to allocation it did in its constructor (eg. unload error strings) */ | 
| 127 | if(e->destroy) | 129 | if (e->destroy) | 
| 128 | e->destroy(e); | 130 | e->destroy(e); | 
| 129 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data); | 131 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data); | 
| 130 | free(e); | 132 | free(e); | 
| 131 | return 1; | 133 | return 1; | 
| 132 | } | 134 | } | 
| 133 | 135 | ||
| 134 | int ENGINE_free(ENGINE *e) | 136 | int | 
| 135 | { | 137 | ENGINE_free(ENGINE *e) | 
| 138 | { | ||
| 136 | return engine_free_util(e, 1); | 139 | return engine_free_util(e, 1); | 
| 137 | } | 140 | } | 
| 138 | 141 | ||
| 139 | /* Cleanup stuff */ | 142 | /* Cleanup stuff */ | 
| 140 | 143 | ||
| @@ -143,183 +146,218 @@ int ENGINE_free(ENGINE *e) | |||
| 143 | * bloat by referring to all *possible* cleanups, but any linker bloat into code | 146 | * bloat by referring to all *possible* cleanups, but any linker bloat into code | 
| 144 | * "X" will cause X's cleanup function to end up here. */ | 147 | * "X" will cause X's cleanup function to end up here. */ | 
| 145 | static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL; | 148 | static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL; | 
| 146 | static int int_cleanup_check(int create) | 149 | static int | 
| 147 | { | 150 | int_cleanup_check(int create) | 
| 148 | if(cleanup_stack) return 1; | 151 | { | 
| 149 | if(!create) return 0; | 152 | if (cleanup_stack) | 
| 153 | return 1; | ||
| 154 | if (!create) | ||
| 155 | return 0; | ||
| 150 | cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null(); | 156 | cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null(); | 
| 151 | return (cleanup_stack ? 1 : 0); | 157 | return (cleanup_stack ? 1 : 0); | 
| 152 | } | 158 | } | 
| 153 | static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb) | 159 | |
| 154 | { | 160 | static ENGINE_CLEANUP_ITEM * | 
| 161 | int_cleanup_item(ENGINE_CLEANUP_CB *cb) | ||
| 162 | { | ||
| 155 | ENGINE_CLEANUP_ITEM *item = malloc(sizeof(ENGINE_CLEANUP_ITEM)); | 163 | ENGINE_CLEANUP_ITEM *item = malloc(sizeof(ENGINE_CLEANUP_ITEM)); | 
| 156 | if(!item) return NULL; | 164 | |
| 165 | if (!item) | ||
| 166 | return NULL; | ||
| 157 | item->cb = cb; | 167 | item->cb = cb; | 
| 158 | return item; | 168 | return item; | 
| 159 | } | 169 | } | 
| 160 | void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb) | 170 | |
| 161 | { | 171 | void | 
| 172 | engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb) | ||
| 173 | { | ||
| 162 | ENGINE_CLEANUP_ITEM *item; | 174 | ENGINE_CLEANUP_ITEM *item; | 
| 163 | if(!int_cleanup_check(1)) return; | 175 | |
| 176 | if (!int_cleanup_check(1)) | ||
| 177 | return; | ||
| 164 | item = int_cleanup_item(cb); | 178 | item = int_cleanup_item(cb); | 
| 165 | if(item) | 179 | if (item) | 
| 166 | sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0); | 180 | sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0); | 
| 167 | } | 181 | } | 
| 168 | void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb) | 182 | |
| 169 | { | 183 | void | 
| 184 | engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb) | ||
| 185 | { | ||
| 170 | ENGINE_CLEANUP_ITEM *item; | 186 | ENGINE_CLEANUP_ITEM *item; | 
| 171 | if(!int_cleanup_check(1)) return; | 187 | |
| 188 | if (!int_cleanup_check(1)) | ||
| 189 | return; | ||
| 172 | item = int_cleanup_item(cb); | 190 | item = int_cleanup_item(cb); | 
| 173 | if(item) | 191 | if (item) | 
| 174 | sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item); | 192 | sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item); | 
| 175 | } | 193 | } | 
| 176 | /* The API function that performs all cleanup */ | 194 | /* The API function that performs all cleanup */ | 
| 177 | static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item) | 195 | static void | 
| 178 | { | 196 | engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item) | 
| 197 | { | ||
| 179 | (*(item->cb))(); | 198 | (*(item->cb))(); | 
| 180 | free(item); | 199 | free(item); | 
| 181 | } | 200 | } | 
| 182 | void ENGINE_cleanup(void) | 201 | |
| 183 | { | 202 | void | 
| 184 | if(int_cleanup_check(0)) | 203 | ENGINE_cleanup(void) | 
| 185 | { | 204 | { | 
| 205 | if (int_cleanup_check(0)) { | ||
| 186 | sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack, | 206 | sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack, | 
| 187 | engine_cleanup_cb_free); | 207 | engine_cleanup_cb_free); | 
| 188 | cleanup_stack = NULL; | 208 | cleanup_stack = NULL; | 
| 189 | } | 209 | } | 
| 190 | /* FIXME: This should be handled (somehow) through RAND, eg. by it | 210 | /* FIXME: This should be handled (somehow) through RAND, eg. by it | 
| 191 | * registering a cleanup callback. */ | 211 | * registering a cleanup callback. */ | 
| 192 | RAND_set_rand_method(NULL); | 212 | RAND_set_rand_method(NULL); | 
| 193 | } | 213 | } | 
| 194 | 214 | ||
| 195 | /* Now the "ex_data" support */ | 215 | /* Now the "ex_data" support */ | 
| 196 | 216 | ||
| 197 | int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 217 | int | 
| 198 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | 218 | ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 
| 199 | { | 219 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | 
| 220 | { | ||
| 200 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp, | 221 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp, | 
| 201 | new_func, dup_func, free_func); | 222 | new_func, dup_func, free_func); | 
| 202 | } | 223 | } | 
| 203 | 224 | ||
| 204 | int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg) | 225 | int | 
| 205 | { | 226 | ENGINE_set_ex_data(ENGINE *e, int idx, void *arg) | 
| 206 | return(CRYPTO_set_ex_data(&e->ex_data, idx, arg)); | 227 | { | 
| 207 | } | 228 | return (CRYPTO_set_ex_data(&e->ex_data, idx, arg)); | 
| 229 | } | ||
| 208 | 230 | ||
| 209 | void *ENGINE_get_ex_data(const ENGINE *e, int idx) | 231 | void * | 
| 210 | { | 232 | ENGINE_get_ex_data(const ENGINE *e, int idx) | 
| 211 | return(CRYPTO_get_ex_data(&e->ex_data, idx)); | 233 | { | 
| 212 | } | 234 | return (CRYPTO_get_ex_data(&e->ex_data, idx)); | 
| 235 | } | ||
| 213 | 236 | ||
| 214 | /* Functions to get/set an ENGINE's elements - mainly to avoid exposing the | 237 | /* Functions to get/set an ENGINE's elements - mainly to avoid exposing the | 
| 215 | * ENGINE structure itself. */ | 238 | * ENGINE structure itself. */ | 
| 216 | 239 | ||
| 217 | int ENGINE_set_id(ENGINE *e, const char *id) | 240 | int | 
| 218 | { | 241 | ENGINE_set_id(ENGINE *e, const char *id) | 
| 219 | if(id == NULL) | 242 | { | 
| 220 | { | 243 | if (id == NULL) { | 
| 221 | ENGINEerr(ENGINE_F_ENGINE_SET_ID, | 244 | ENGINEerr(ENGINE_F_ENGINE_SET_ID, | 
| 222 | ERR_R_PASSED_NULL_PARAMETER); | 245 | ERR_R_PASSED_NULL_PARAMETER); | 
| 223 | return 0; | 246 | return 0; | 
| 224 | } | 247 | } | 
| 225 | e->id = id; | 248 | e->id = id; | 
| 226 | return 1; | 249 | return 1; | 
| 227 | } | 250 | } | 
| 228 | 251 | ||
| 229 | int ENGINE_set_name(ENGINE *e, const char *name) | 252 | int | 
| 230 | { | 253 | ENGINE_set_name(ENGINE *e, const char *name) | 
| 231 | if(name == NULL) | 254 | { | 
| 232 | { | 255 | if (name == NULL) { | 
| 233 | ENGINEerr(ENGINE_F_ENGINE_SET_NAME, | 256 | ENGINEerr(ENGINE_F_ENGINE_SET_NAME, | 
| 234 | ERR_R_PASSED_NULL_PARAMETER); | 257 | ERR_R_PASSED_NULL_PARAMETER); | 
| 235 | return 0; | 258 | return 0; | 
| 236 | } | 259 | } | 
| 237 | e->name = name; | 260 | e->name = name; | 
| 238 | return 1; | 261 | return 1; | 
| 239 | } | 262 | } | 
| 240 | 263 | ||
| 241 | int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f) | 264 | int | 
| 242 | { | 265 | ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f) | 
| 266 | { | ||
| 243 | e->destroy = destroy_f; | 267 | e->destroy = destroy_f; | 
| 244 | return 1; | 268 | return 1; | 
| 245 | } | 269 | } | 
| 246 | 270 | ||
| 247 | int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f) | 271 | int | 
| 248 | { | 272 | ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f) | 
| 273 | { | ||
| 249 | e->init = init_f; | 274 | e->init = init_f; | 
| 250 | return 1; | 275 | return 1; | 
| 251 | } | 276 | } | 
| 252 | 277 | ||
| 253 | int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f) | 278 | int | 
| 254 | { | 279 | ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f) | 
| 280 | { | ||
| 255 | e->finish = finish_f; | 281 | e->finish = finish_f; | 
| 256 | return 1; | 282 | return 1; | 
| 257 | } | 283 | } | 
| 258 | 284 | ||
| 259 | int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f) | 285 | int | 
| 260 | { | 286 | ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f) | 
| 287 | { | ||
| 261 | e->ctrl = ctrl_f; | 288 | e->ctrl = ctrl_f; | 
| 262 | return 1; | 289 | return 1; | 
| 263 | } | 290 | } | 
| 264 | 291 | ||
| 265 | int ENGINE_set_flags(ENGINE *e, int flags) | 292 | int | 
| 266 | { | 293 | ENGINE_set_flags(ENGINE *e, int flags) | 
| 294 | { | ||
| 267 | e->flags = flags; | 295 | e->flags = flags; | 
| 268 | return 1; | 296 | return 1; | 
| 269 | } | 297 | } | 
| 270 | 298 | ||
| 271 | int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns) | 299 | int | 
| 272 | { | 300 | ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns) | 
| 301 | { | ||
| 273 | e->cmd_defns = defns; | 302 | e->cmd_defns = defns; | 
| 274 | return 1; | 303 | return 1; | 
| 275 | } | 304 | } | 
| 276 | 305 | ||
| 277 | const char *ENGINE_get_id(const ENGINE *e) | 306 | const char * | 
| 278 | { | 307 | ENGINE_get_id(const ENGINE *e) | 
| 308 | { | ||
| 279 | return e->id; | 309 | return e->id; | 
| 280 | } | 310 | } | 
| 281 | 311 | ||
| 282 | const char *ENGINE_get_name(const ENGINE *e) | 312 | const char * | 
| 283 | { | 313 | ENGINE_get_name(const ENGINE *e) | 
| 314 | { | ||
| 284 | return e->name; | 315 | return e->name; | 
| 285 | } | 316 | } | 
| 286 | 317 | ||
| 287 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e) | 318 | ENGINE_GEN_INT_FUNC_PTR | 
| 288 | { | 319 | ENGINE_get_destroy_function(const ENGINE *e) | 
| 320 | { | ||
| 289 | return e->destroy; | 321 | return e->destroy; | 
| 290 | } | 322 | } | 
| 291 | 323 | ||
| 292 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e) | 324 | ENGINE_GEN_INT_FUNC_PTR | 
| 293 | { | 325 | ENGINE_get_init_function(const ENGINE *e) | 
| 326 | { | ||
| 294 | return e->init; | 327 | return e->init; | 
| 295 | } | 328 | } | 
| 296 | 329 | ||
| 297 | ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e) | 330 | ENGINE_GEN_INT_FUNC_PTR | 
| 298 | { | 331 | ENGINE_get_finish_function(const ENGINE *e) | 
| 332 | { | ||
| 299 | return e->finish; | 333 | return e->finish; | 
| 300 | } | 334 | } | 
| 301 | 335 | ||
| 302 | ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e) | 336 | ENGINE_CTRL_FUNC_PTR | 
| 303 | { | 337 | ENGINE_get_ctrl_function(const ENGINE *e) | 
| 338 | { | ||
| 304 | return e->ctrl; | 339 | return e->ctrl; | 
| 305 | } | 340 | } | 
| 306 | 341 | ||
| 307 | int ENGINE_get_flags(const ENGINE *e) | 342 | int | 
| 308 | { | 343 | ENGINE_get_flags(const ENGINE *e) | 
| 344 | { | ||
| 309 | return e->flags; | 345 | return e->flags; | 
| 310 | } | 346 | } | 
| 311 | 347 | ||
| 312 | const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e) | 348 | const ENGINE_CMD_DEFN * | 
| 313 | { | 349 | ENGINE_get_cmd_defns(const ENGINE *e) | 
| 350 | { | ||
| 314 | return e->cmd_defns; | 351 | return e->cmd_defns; | 
| 315 | } | 352 | } | 
| 316 | 353 | ||
| 317 | /* eng_lib.o is pretty much linked into anything that touches ENGINE already, so | 354 | /* eng_lib.o is pretty much linked into anything that touches ENGINE already, so | 
| 318 | * put the "static_state" hack here. */ | 355 | * put the "static_state" hack here. */ | 
| 319 | 356 | ||
| 320 | static int internal_static_hack = 0; | 357 | static int internal_static_hack = 0; | 
| 321 | 358 | ||
| 322 | void *ENGINE_get_static_state(void) | 359 | void * | 
| 323 | { | 360 | ENGINE_get_static_state(void) | 
| 361 | { | ||
| 324 | return &internal_static_hack; | 362 | return &internal_static_hack; | 
| 325 | } | 363 | } | 
