5
5
/* ====================================================================
6
* Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
6
* Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
8
8
* Redistribution and use in source and binary forms, with or without
9
9
* modification, are permitted provided that the following conditions
55
55
* Hudson (tjh@cryptsoft.com).
58
/* ====================================================================
59
* Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60
* ECDH support in OpenSSL originally developed by
61
* SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
59
64
#ifndef HEADER_ENGINE_H
60
65
#define HEADER_ENGINE_H
76
81
#ifndef OPENSSL_NO_DH
77
82
#include <openssl/dh.h>
84
#ifndef OPENSSL_NO_ECDH
85
#include <openssl/ecdh.h>
87
#ifndef OPENSSL_NO_ECDSA
88
#include <openssl/ecdsa.h>
79
90
#include <openssl/rand.h>
91
#include <openssl/store.h>
80
92
#include <openssl/ui.h>
93
#include <openssl/err.h>
96
#include <openssl/ossl_typ.h>
81
97
#include <openssl/symhacks.h>
82
#include <openssl/err.h>
88
/* Fixups for missing algorithms */
90
typedef void RSA_METHOD;
93
typedef void DSA_METHOD;
96
typedef void DH_METHOD;
99
103
/* These flags are used to control combinations of algorithm (methods)
100
104
* by bitwise "OR"ing. */
101
105
#define ENGINE_METHOD_RSA (unsigned int)0x0001
102
106
#define ENGINE_METHOD_DSA (unsigned int)0x0002
103
107
#define ENGINE_METHOD_DH (unsigned int)0x0004
104
108
#define ENGINE_METHOD_RAND (unsigned int)0x0008
109
#define ENGINE_METHOD_ECDH (unsigned int)0x0010
110
#define ENGINE_METHOD_ECDSA (unsigned int)0x0020
105
111
#define ENGINE_METHOD_CIPHERS (unsigned int)0x0040
106
112
#define ENGINE_METHOD_DIGESTS (unsigned int)0x0080
113
#define ENGINE_METHOD_STORE (unsigned int)0x0100
107
114
/* Obvious all-or-nothing cases. */
108
115
#define ENGINE_METHOD_ALL (unsigned int)0xFFFF
109
116
#define ENGINE_METHOD_NONE (unsigned int)0x0000
173
180
handles/connections etc. */
174
181
#define ENGINE_CTRL_SET_USER_INTERFACE 4 /* Alternative to callback */
175
182
#define ENGINE_CTRL_SET_CALLBACK_DATA 5 /* User-specific data, used
176
when calling the password
177
callback and the user
183
when calling the password
184
callback and the user
186
#define ENGINE_CTRL_LOAD_CONFIGURATION 6 /* Load a configuration, given
187
a string that represents a
189
#define ENGINE_CTRL_LOAD_SECTION 7 /* Load data from a given
190
section in the already loaded
180
193
/* These control commands allow an application to deal with an arbitrary engine
181
194
* in a dynamic way. Warn: Negative return values indicate errors FOR THESE
223
236
/* ENGINE implementations should start the numbering of their own control
224
237
* commands from this value. (ie. ENGINE_CMD_BASE, ENGINE_CMD_BASE + 1, etc). */
225
#define ENGINE_CMD_BASE 200
238
#define ENGINE_CMD_BASE 200
227
240
/* NB: These 2 nCipher "chil" control commands are deprecated, and their
228
241
* functionality is now available through ENGINE-specific control commands
257
270
} ENGINE_CMD_DEFN;
259
272
/* Generic function pointer */
260
typedef int (*ENGINE_GEN_FUNC_PTR)();
273
typedef int (*ENGINE_GEN_FUNC_PTR)(void);
261
274
/* Generic function pointer taking no arguments */
262
275
typedef int (*ENGINE_GEN_INT_FUNC_PTR)(ENGINE *);
263
276
/* Specific control function pointer */
264
typedef int (*ENGINE_CTRL_FUNC_PTR)(ENGINE *, int, long, void *, void (*f)());
277
typedef int (*ENGINE_CTRL_FUNC_PTR)(ENGINE *, int, long, void *, void (*f)(void));
265
278
/* Generic load_key function pointer */
266
279
typedef EVP_PKEY * (*ENGINE_LOAD_KEY_PTR)(ENGINE *, const char *,
267
280
UI_METHOD *ui_method, void *callback_data);
305
318
/* Add all the built-in engines. */
306
319
void ENGINE_load_openssl(void);
307
320
void ENGINE_load_dynamic(void);
321
#ifndef OPENSSL_NO_STATIC_ENGINE
322
void ENGINE_load_4758cca(void);
323
void ENGINE_load_aep(void);
324
void ENGINE_load_atalla(void);
325
void ENGINE_load_chil(void);
308
326
void ENGINE_load_cswift(void);
309
void ENGINE_load_chil(void);
310
void ENGINE_load_atalla(void);
327
#ifndef OPENSSL_NO_GMP
328
void ENGINE_load_gmp(void);
311
330
void ENGINE_load_nuron(void);
331
void ENGINE_load_sureware(void);
312
332
void ENGINE_load_ubsec(void);
313
void ENGINE_load_aep(void);
314
void ENGINE_load_sureware(void);
315
void ENGINE_load_4758cca(void);
316
334
void ENGINE_load_cryptodev(void);
335
void ENGINE_load_padlock(void);
317
336
void ENGINE_load_builtin_engines(void);
319
338
/* Get and set global flags (ENGINE_TABLE_FLAG_***) for the implementation
337
356
void ENGINE_unregister_DSA(ENGINE *e);
338
357
void ENGINE_register_all_DSA(void);
359
int ENGINE_register_ECDH(ENGINE *e);
360
void ENGINE_unregister_ECDH(ENGINE *e);
361
void ENGINE_register_all_ECDH(void);
363
int ENGINE_register_ECDSA(ENGINE *e);
364
void ENGINE_unregister_ECDSA(ENGINE *e);
365
void ENGINE_register_all_ECDSA(void);
340
367
int ENGINE_register_DH(ENGINE *e);
341
368
void ENGINE_unregister_DH(ENGINE *e);
342
369
void ENGINE_register_all_DH(void);
345
372
void ENGINE_unregister_RAND(ENGINE *e);
346
373
void ENGINE_register_all_RAND(void);
375
int ENGINE_register_STORE(ENGINE *e);
376
void ENGINE_unregister_STORE(ENGINE *e);
377
void ENGINE_register_all_STORE(void);
348
379
int ENGINE_register_ciphers(ENGINE *e);
349
380
void ENGINE_unregister_ciphers(ENGINE *e);
350
381
void ENGINE_register_all_ciphers(void);
367
398
* reference to an engine, but many control commands may require the engine be
368
399
* functional. The caller should be aware of trying commands that require an
369
400
* operational ENGINE, and only use functional references in such situations. */
370
int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
401
int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
372
403
/* This function tests if an ENGINE-specific command is usable as a "setting".
373
404
* Eg. in an application's config file that gets processed through
380
411
* See the comment on ENGINE_ctrl_cmd_string() for an explanation on how to
381
412
* use the cmd_name and cmd_optional. */
382
413
int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
383
long i, void *p, void (*f)(), int cmd_optional);
414
long i, void *p, void (*f)(void), int cmd_optional);
385
416
/* This function passes a command-name and argument to an ENGINE. The cmd_name
386
417
* is converted to a command number and the control command is called using
417
448
int ENGINE_set_name(ENGINE *e, const char *name);
418
449
int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
419
450
int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
451
int ENGINE_set_ECDH(ENGINE *e, const ECDH_METHOD *ecdh_meth);
452
int ENGINE_set_ECDSA(ENGINE *e, const ECDSA_METHOD *ecdsa_meth);
420
453
int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
421
454
int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth);
455
int ENGINE_set_STORE(ENGINE *e, const STORE_METHOD *store_meth);
422
456
int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f);
423
457
int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f);
424
458
int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
429
463
int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f);
430
464
int ENGINE_set_flags(ENGINE *e, int flags);
431
465
int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns);
432
/* These functions (and the "get" function lower down) allow control over any
433
* per-structure ENGINE data. */
466
/* These functions allow control over any per-structure ENGINE data. */
434
467
int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
435
468
CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
436
469
int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg);
470
void *ENGINE_get_ex_data(const ENGINE *e, int idx);
438
472
/* This function cleans up anything that needs it. Eg. the ENGINE_add() function
439
473
* automatically ensures the list cleanup function is registered to be called
449
483
const char *ENGINE_get_name(const ENGINE *e);
450
484
const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e);
451
485
const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e);
486
const ECDH_METHOD *ENGINE_get_ECDH(const ENGINE *e);
487
const ECDSA_METHOD *ENGINE_get_ECDSA(const ENGINE *e);
452
488
const DH_METHOD *ENGINE_get_DH(const ENGINE *e);
453
489
const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e);
490
const STORE_METHOD *ENGINE_get_STORE(const ENGINE *e);
454
491
ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e);
455
492
ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e);
456
493
ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e);
463
500
const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid);
464
501
const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e);
465
502
int ENGINE_get_flags(const ENGINE *e);
466
void *ENGINE_get_ex_data(const ENGINE *e, int idx);
468
504
/* FUNCTIONAL functions. These functions deal with ENGINE structures
469
505
* that have (or will) be initialised for use. Broadly speaking, the
501
537
ENGINE *ENGINE_get_default_RSA(void);
502
538
/* Same for the other "methods" */
503
539
ENGINE *ENGINE_get_default_DSA(void);
540
ENGINE *ENGINE_get_default_ECDH(void);
541
ENGINE *ENGINE_get_default_ECDSA(void);
504
542
ENGINE *ENGINE_get_default_DH(void);
505
543
ENGINE *ENGINE_get_default_RAND(void);
506
544
/* These functions can be used to get a functional reference to perform
516
554
int ENGINE_set_default_string(ENGINE *e, const char *def_list);
517
555
/* Same for the other "methods" */
518
556
int ENGINE_set_default_DSA(ENGINE *e);
557
int ENGINE_set_default_ECDH(ENGINE *e);
558
int ENGINE_set_default_ECDSA(ENGINE *e);
519
559
int ENGINE_set_default_DH(ENGINE *e);
520
560
int ENGINE_set_default_RAND(ENGINE *e);
521
561
int ENGINE_set_default_ciphers(ENGINE *e);
538
578
/**************************/
540
580
/* Binary/behaviour compatibility levels */
541
#define OSSL_DYNAMIC_VERSION (unsigned long)0x00010200
581
#define OSSL_DYNAMIC_VERSION (unsigned long)0x00020000
542
582
/* Binary versions older than this are too old for us (whether we're a loader or
544
#define OSSL_DYNAMIC_OLDEST (unsigned long)0x00010200
584
#define OSSL_DYNAMIC_OLDEST (unsigned long)0x00020000
546
586
/* When compiling an ENGINE entirely as an external shared library, loadable by
547
587
* the "dynamic" ENGINE, these types are needed. The 'dynamic_fns' structure
548
588
* type provides the calling application's (or library's) error functionality
549
589
* and memory management function pointers to the loaded library. These should
550
590
* be used/set in the loaded library code so that the loading application's
551
* 'state' will be used/changed in all operations. */
591
* 'state' will be used/changed in all operations. The 'static_state' pointer
592
* allows the loaded library to know if it shares the same static data as the
593
* calling application (or library), and thus whether these callbacks need to be
552
595
typedef void *(*dyn_MEM_malloc_cb)(size_t);
553
596
typedef void *(*dyn_MEM_realloc_cb)(void *, size_t);
554
597
typedef void (*dyn_MEM_free_cb)(void *);
616
660
const dynamic_fns *fns);
617
661
#define IMPLEMENT_DYNAMIC_BIND_FN(fn) \
618
662
int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { \
619
if (ERR_get_implementation() != fns->err_fns) \
621
if(!CRYPTO_set_mem_functions(fns->mem_fns.malloc_cb, \
622
fns->mem_fns.realloc_cb, fns->mem_fns.free_cb)) \
624
CRYPTO_set_locking_callback(fns->lock_fns.lock_locking_cb); \
625
CRYPTO_set_add_lock_callback(fns->lock_fns.lock_add_lock_cb); \
626
CRYPTO_set_dynlock_create_callback(fns->lock_fns.dynlock_create_cb); \
627
CRYPTO_set_dynlock_lock_callback(fns->lock_fns.dynlock_lock_cb); \
628
CRYPTO_set_dynlock_destroy_callback(fns->lock_fns.dynlock_destroy_cb); \
629
if(!CRYPTO_set_ex_data_implementation(fns->ex_data_fns)) \
631
if(!ERR_set_implementation(fns->err_fns)) return 0; \
663
if(ENGINE_get_static_state() == fns->static_state) goto skip_cbs; \
664
if(!CRYPTO_set_mem_functions(fns->mem_fns.malloc_cb, \
665
fns->mem_fns.realloc_cb, fns->mem_fns.free_cb)) \
667
CRYPTO_set_locking_callback(fns->lock_fns.lock_locking_cb); \
668
CRYPTO_set_add_lock_callback(fns->lock_fns.lock_add_lock_cb); \
669
CRYPTO_set_dynlock_create_callback(fns->lock_fns.dynlock_create_cb); \
670
CRYPTO_set_dynlock_lock_callback(fns->lock_fns.dynlock_lock_cb); \
671
CRYPTO_set_dynlock_destroy_callback(fns->lock_fns.dynlock_destroy_cb); \
672
if(!CRYPTO_set_ex_data_implementation(fns->ex_data_fns)) \
674
if(!ERR_set_implementation(fns->err_fns)) return 0; \
633
676
if(!fn(e,id)) return 0; \
679
/* If the loading application (or library) and the loaded ENGINE library share
680
* the same static data (eg. they're both dynamically linked to the same
681
* libcrypto.so) we need a way to avoid trying to set system callbacks - this
682
* would fail, and for the same reason that it's unnecessary to try. If the
683
* loaded ENGINE has (or gets from through the loader) its own copy of the
684
* libcrypto static data, we will need to set the callbacks. The easiest way to
685
* detect this is to have a function that returns a pointer to some static data
686
* and let the loading application and loaded ENGINE compare their respective
688
void *ENGINE_get_static_state(void);
636
690
#if defined(__OpenBSD__) || defined(__FreeBSD__)
637
691
void ENGINE_setup_bsd_cryptodev(void);
649
703
#define ENGINE_F_DYNAMIC_CTRL 180
650
704
#define ENGINE_F_DYNAMIC_GET_DATA_CTX 181
651
705
#define ENGINE_F_DYNAMIC_LOAD 182
706
#define ENGINE_F_DYNAMIC_SET_DATA_CTX 183
652
707
#define ENGINE_F_ENGINE_ADD 105
653
708
#define ENGINE_F_ENGINE_BY_ID 106
654
709
#define ENGINE_F_ENGINE_CMD_IS_EXECUTABLE 170
656
711
#define ENGINE_F_ENGINE_CTRL_CMD 178
657
712
#define ENGINE_F_ENGINE_CTRL_CMD_STRING 171
658
713
#define ENGINE_F_ENGINE_FINISH 107
659
#define ENGINE_F_ENGINE_FREE 108
714
#define ENGINE_F_ENGINE_FREE_UTIL 108
660
715
#define ENGINE_F_ENGINE_GET_CIPHER 185
661
716
#define ENGINE_F_ENGINE_GET_DEFAULT_TYPE 177
662
717
#define ENGINE_F_ENGINE_GET_DIGEST 186
667
722
#define ENGINE_F_ENGINE_LIST_REMOVE 121
668
723
#define ENGINE_F_ENGINE_LOAD_PRIVATE_KEY 150
669
724
#define ENGINE_F_ENGINE_LOAD_PUBLIC_KEY 151
670
#define ENGINE_F_ENGINE_MODULE_INIT 187
671
725
#define ENGINE_F_ENGINE_NEW 122
672
726
#define ENGINE_F_ENGINE_REMOVE 123
673
727
#define ENGINE_F_ENGINE_SET_DEFAULT_STRING 189
676
730
#define ENGINE_F_ENGINE_SET_NAME 130
677
731
#define ENGINE_F_ENGINE_TABLE_REGISTER 184
678
732
#define ENGINE_F_ENGINE_UNLOAD_KEY 152
733
#define ENGINE_F_ENGINE_UNLOCKED_FINISH 191
679
734
#define ENGINE_F_ENGINE_UP_REF 190
680
735
#define ENGINE_F_INT_CTRL_HELPER 172
681
736
#define ENGINE_F_INT_ENGINE_CONFIGURE 188
737
#define ENGINE_F_INT_ENGINE_MODULE_INIT 187
682
738
#define ENGINE_F_LOG_MESSAGE 141
683
#define ENGINE_F_SET_DATA_CTX 183
685
740
/* Reason codes. */
686
741
#define ENGINE_R_ALREADY_LOADED 100