1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
|
.\" $OpenBSD: engine.3,v 1.15 2018/04/15 01:43:45 schwarze Exp $
.\" full merge up to: OpenSSL crypto/engine e6390aca Jul 21 10:06:03 2015 -0400
.\" selective merge up to: man3/ENGINE_add 1f13ad31 Dec 25 17:50:39 2017 +0800
.\"
.\" This file was written by Geoff Thorpe <geoff@openssl.org>
.\" with contributions from Paul Yang <yang.yang@baishancloud.com>.
.\" Copyright (c) 2002, 2004, 2007, 2015, 2017 The OpenSSL Project.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" 3. All advertising materials mentioning features or use of this
.\" software must display the following acknowledgment:
.\" "This product includes software developed by the OpenSSL Project
.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
.\"
.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
.\" endorse or promote products derived from this software without
.\" prior written permission. For written permission, please contact
.\" openssl-core@openssl.org.
.\"
.\" 5. Products derived from this software may not be called "OpenSSL"
.\" nor may "OpenSSL" appear in their names without prior written
.\" permission of the OpenSSL Project.
.\"
.\" 6. Redistributions of any form whatsoever must retain the following
.\" acknowledgment:
.\" "This product includes software developed by the OpenSSL Project
.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: April 15 2018 $
.Dt ENGINE 3
.Os
.Sh NAME
.Nm ENGINE_load_openssl ,
.Nm ENGINE_load_dynamic ,
.Nm ENGINE_load_builtin_engines ,
.Nm ENGINE_cleanup ,
.Nm ENGINE_new ,
.Nm ENGINE_free ,
.Nm ENGINE_up_ref ,
.Nm ENGINE_set_id ,
.Nm ENGINE_set_name ,
.Nm ENGINE_set_RSA ,
.Nm ENGINE_set_DSA ,
.Nm ENGINE_set_ECDH ,
.Nm ENGINE_set_ECDSA ,
.Nm ENGINE_set_DH ,
.Nm ENGINE_set_RAND ,
.Nm ENGINE_set_STORE ,
.Nm ENGINE_set_destroy_function ,
.Nm ENGINE_set_load_privkey_function ,
.Nm ENGINE_set_load_pubkey_function ,
.Nm ENGINE_set_ciphers ,
.Nm ENGINE_set_digests ,
.Nm ENGINE_set_flags ,
.Nm ENGINE_get_id ,
.Nm ENGINE_get_name ,
.Nm ENGINE_get_RSA ,
.Nm ENGINE_get_DSA ,
.Nm ENGINE_get_ECDH ,
.Nm ENGINE_get_ECDSA ,
.Nm ENGINE_get_DH ,
.Nm ENGINE_get_RAND ,
.Nm ENGINE_get_STORE ,
.Nm ENGINE_get_destroy_function ,
.Nm ENGINE_get_load_privkey_function ,
.Nm ENGINE_get_load_pubkey_function ,
.Nm ENGINE_get_ciphers ,
.Nm ENGINE_get_digests ,
.Nm ENGINE_get_cipher ,
.Nm ENGINE_get_digest ,
.Nm ENGINE_get_flags ,
.Nm ENGINE_load_private_key ,
.Nm ENGINE_load_public_key
.Nd ENGINE cryptographic module support
.Sh SYNOPSIS
.In openssl/engine.h
.Ft void
.Fn ENGINE_load_openssl void
.Ft void
.Fn ENGINE_load_dynamic void
.Ft void
.Fn ENGINE_load_builtin_engines void
.Ft void
.Fn ENGINE_cleanup void
.Ft ENGINE *
.Fn ENGINE_new void
.Ft int
.Fo ENGINE_free
.Fa "ENGINE *e"
.Fc
.Ft int
.Fo ENGINE_up_ref
.Fa "ENGINE *e"
.Fc
.Ft int
.Fo ENGINE_set_id
.Fa "ENGINE *e"
.Fa "const char *id"
.Fc
.Ft int
.Fo ENGINE_set_name
.Fa "ENGINE *e"
.Fa "const char *name"
.Fc
.Ft int
.Fo ENGINE_set_RSA
.Fa "ENGINE *e"
.Fa "const RSA_METHOD *rsa_meth"
.Fc
.Ft int
.Fo ENGINE_set_DSA
.Fa "ENGINE *e"
.Fa "const DSA_METHOD *dsa_meth"
.Fc
.Ft int
.Fo ENGINE_set_ECDH
.Fa "ENGINE *e"
.Fa "const ECDH_METHOD *dh_meth"
.Fc
.Ft int
.Fo ENGINE_set_ECDSA
.Fa "ENGINE *e"
.Fa "const ECDSA_METHOD *dh_meth"
.Fc
.Ft int
.Fo ENGINE_set_DH
.Fa "ENGINE *e"
.Fa "const DH_METHOD *dh_meth"
.Fc
.Ft int
.Fo ENGINE_set_RAND
.Fa "ENGINE *e"
.Fa "const RAND_METHOD *rand_meth"
.Fc
.Ft int
.Fo ENGINE_set_STORE
.Fa "ENGINE *e"
.Fa "const STORE_METHOD *rand_meth"
.Fc
.Ft int
.Fo ENGINE_set_destroy_function
.Fa "ENGINE *e"
.Fa "ENGINE_GEN_INT_FUNC_PTR destroy_f"
.Fc
.Ft int
.Fo ENGINE_set_load_privkey_function
.Fa "ENGINE *e"
.Fa "ENGINE_LOAD_KEY_PTR loadpriv_f"
.Fc
.Ft int
.Fo ENGINE_set_load_pubkey_function
.Fa "ENGINE *e"
.Fa "ENGINE_LOAD_KEY_PTR loadpub_f"
.Fc
.Ft int
.Fo ENGINE_set_ciphers
.Fa "ENGINE *e"
.Fa "ENGINE_CIPHERS_PTR f"
.Fc
.Ft int
.Fo ENGINE_set_digests
.Fa "ENGINE *e"
.Fa "ENGINE_DIGESTS_PTR f"
.Fc
.Ft int
.Fo ENGINE_set_flags
.Fa "ENGINE *e"
.Fa "int flags"
.Fc
.Ft const char *
.Fo ENGINE_get_id
.Fa "const ENGINE *e"
.Fc
.Ft const char *
.Fo ENGINE_get_name
.Fa "const ENGINE *e"
.Fc
.Ft const RSA_METHOD *
.Fo ENGINE_get_RSA
.Fa "const ENGINE *e"
.Fc
.Ft const DSA_METHOD *
.Fo ENGINE_get_DSA
.Fa "const ENGINE *e"
.Fc
.Ft const ECDH_METHOD *
.Fo ENGINE_get_ECDH
.Fa "const ENGINE *e"
.Fc
.Ft const ECDSA_METHOD *
.Fo ENGINE_get_ECDSA
.Fa "const ENGINE *e"
.Fc
.Ft const DH_METHOD *
.Fo ENGINE_get_DH
.Fa "const ENGINE *e"
.Fc
.Ft const RAND_METHOD *
.Fo ENGINE_get_RAND
.Fa "const ENGINE *e"
.Fc
.Ft const STORE_METHOD *
.Fo ENGINE_get_STORE
.Fa "const ENGINE *e"
.Fc
.Ft ENGINE_GEN_INT_FUNC_PTR
.Fo ENGINE_get_destroy_function
.Fa "const ENGINE *e"
.Fc
.Ft ENGINE_LOAD_KEY_PTR
.Fo ENGINE_get_load_privkey_function
.Fa "const ENGINE *e"
.Fc
.Ft ENGINE_LOAD_KEY_PTR
.Fo ENGINE_get_load_pubkey_function
.Fa "const ENGINE *e"
.Fc
.Ft ENGINE_CIPHERS_PTR
.Fo ENGINE_get_ciphers
.Fa "const ENGINE *e"
.Fc
.Ft ENGINE_DIGESTS_PTR
.Fo ENGINE_get_digests
.Fa "const ENGINE *e"
.Fc
.Ft const EVP_CIPHER *
.Fo ENGINE_get_cipher
.Fa "ENGINE *e"
.Fa "int nid"
.Fc
.Ft const EVP_MD *
.Fo ENGINE_get_digest
.Fa "ENGINE *e"
.Fa "int nid"
.Fc
.Ft int
.Fo ENGINE_get_flags
.Fa "const ENGINE *e"
.Fc
.Ft EVP_PKEY *
.Fo ENGINE_load_private_key
.Fa "ENGINE *e"
.Fa "const char *key_id"
.Fa "UI_METHOD *ui_method"
.Fa "void *callback_data"
.Fc
.Ft EVP_PKEY *
.Fo ENGINE_load_public_key
.Fa "ENGINE *e"
.Fa "const char *key_id"
.Fa "UI_METHOD *ui_method"
.Fa "void *callback_data"
.Fc
.Sh DESCRIPTION
These functions create, manipulate, and use cryptographic modules
in the form of
.Vt ENGINE
objects.
These objects act as containers for implementations of cryptographic
algorithms, and support a reference-counted mechanism to allow them to
be dynamically loaded in and out of the running application.
.Pp
The cryptographic functionality that can be provided by an
.Vt ENGINE
implementation includes the following abstractions:
.Pp
.Bl -bullet -compact
.It
.Vt RSA_METHOD :
for providing alternative RSA implementations
.It
.Vt DSA_METHOD , DH_METHOD , RAND_METHOD , ECDH_METHOD ,
.Vt ECDSA_METHOD , STORE_METHOD :
similarly for other OpenSSL APIs
.It
.Vt EVP_CIPHER :
potentially multiple cipher algorithms (indexed by 'nid')
.It
.Vt EVP_DIGEST :
potentially multiple hash algorithms (indexed by 'nid')
.It
key-loading: loading public and/or private EVP_PKEY keys
.El
.Ss Reference counting and handles
Due to the modular nature of the
.Nm engine
API, pointers to
.Vt ENGINE Ns s
need to be treated as handles - i.e. not only as pointers, but also
as references to the underlying
.Vt ENGINE
object.
One should obtain a new reference when making copies of an
.Vt ENGINE
pointer if the copies will be used (and released) independently.
.Pp
.Vt ENGINE
objects have two levels of reference-counting to match the way in
which the objects are used.
At the most basic level, each
.Vt ENGINE
pointer is inherently a
.Sy structural
reference - a structural reference is required to use the pointer value
at all, as this kind of reference is a guarantee that the structure cannot
be deallocated until the reference is released.
.Pp
However, a structural reference provides no guarantee that the
.Vt ENGINE
is initialised and able to use any of its cryptographic implementations.
Indeed it's quite possible that most
.Vt ENGINE Ns s
will not initialise at all in typical environments, as
.Vt ENGINE Ns s
are typically used to support specialised hardware.
To use an
.Vt ENGINE Ap s
functionality, you need a
.Sy functional
reference.
This kind of reference can be considered a specialised form of
structural reference, because each functional reference implicitly
contains a structural reference as well - however to avoid
difficult-to-find programming bugs, it is recommended to treat the two
kinds of reference independently.
If you have a functional reference to an
.Vt ENGINE ,
you have a guarantee that the
.Vt ENGINE
has been initialised and is ready to perform cryptographic operations and
will remain uninitialised until after you have released your
reference.
.Pp
.Em Structural references
.Pp
This basic type of reference is used for instantiating new
.Vt ENGINE Ns s ,
iterating across OpenSSL's internal linked-list of loaded
.Vt ENGINE Ns s ,
reading information about an
.Vt ENGINE ,
etc.
Essentially a structural reference is sufficient if you only need to
query or manipulate the data of an
.Vt ENGINE
implementation rather than use its functionality.
.Pp
.Fn ENGINE_new
allocates and initializes an empty
.Vt ENGINE
object and sets its structural reference count to 1
and its functional reference count to 0.
Many functions increment the structural reference count by 1
when successful.
Some of them, including
.Xr ENGINE_by_id 3 ,
.Xr ENGINE_get_first 3 ,
.Xr ENGINE_get_last 3 ,
.Xr ENGINE_get_next 3 ,
and
.Xr ENGINE_get_prev 3 ,
do so because they return a structural reference to the user.
Other functions, including
.Xr ENGINE_add 3 ,
.Xr ENGINE_init 3 ,
.Xr ENGINE_get_cipher_engine 3 ,
.Xr ENGINE_get_digest_engine 3 ,
and the
.Xr ENGINE_get_default_RSA 3
and
.Xr ENGINE_set_default 3
families of functions
do so because they store a structural refence internally.
.Fn ENGINE_up_ref
explicitly increment the structural reference count by 1.
.Pp
.Fn ENGINE_free
decrements the structural reference count by 1,
and if it reaches 0, the cleanup function associated with
.Fa e
is called, and both the memory used internally by
.Fa e
and
.Fa e
itself are freed.
If
.Fa e
is a
.Dv NULL
pointer, no action occurs.
Many functions internally call the equivalent of
.Fn ENGINE_free .
Some of them, including
.Xr ENGINE_get_next 3
and
.Xr ENGINE_get_prev 3 ,
thus invalidate the structural reference passed in by the user.
Other functions, including
.Xr ENGINE_finish 3 ,
.Xr ENGINE_remove 3 ,
and the
.Xr ENGINE_set_default 3
family of functions
do so when an internally stored structural reference is no longer needed.
.Ss Application requirements
This section will explain the basic things an application programmer
should support to make the most useful elements of the
.Nm engine
functionality available to the user.
The first thing to consider is whether the programmer wishes to make
alternative
.Vt ENGINE
modules available to the application and user.
OpenSSL maintains an internal linked list of "visible"
.Vt ENGINE Ns s
from which it has to operate.
At start-up, this list is empty, and in fact if an application does
not call any
.Nm engine
API calls and it uses static
linking against openssl, then the resulting application binary will
not contain any alternative
.Nm engine
code at all.
So the first consideration is whether any/all available
.Vt ENGINE
implementations should be made visible to OpenSSL.
This is controlled by calling the various "load" functions, e.g.
.Fn ENGINE_load_builtin_engines
to make all
.Vt ENGINE
implementations bundled with OpenSSL available.
.Pp
Note that
.Fn ENGINE_load_dynamic
is a placeholder and does not enable dynamic engine loading support.
.Pp
Having called any of these functions,
.Vt ENGINE
objects would have been dynamically allocated and populated with
these implementations and linked into OpenSSL's internal linked
list.
.Pp
If no
.Nm engine
API functions are called at all in an application, then there are
no inherent memory leaks to worry about from the
.Nm engine
functionality, however if any
.Vt ENGINE Ns s
are loaded, even if they are never registered or used, it is necessary
to use the
.Fn ENGINE_cleanup
function to correspondingly cleanup before program exit, if the caller
wishes to avoid memory leaks.
This mechanism uses an internal callback registration table so that any
.Nm engine
API functionality that knows it requires cleanup can register its
cleanup details to be called during
.Fn ENGINE_cleanup .
This approach allows
.Fn ENGINE_cleanup
to clean up after any
.Nm engine
functionality at all that your program uses, yet doesn't automatically
create linker dependencies to all possible
.Nm engine
functionality - only the cleanup callbacks required by the functionality
you do use will be required by the linker.
.Pp
The fact that
.Vt ENGINE Ns s
are made visible to OpenSSL (and thus are linked into the program
and loaded into memory at run-time) does not mean they are "registered"
or called into use by OpenSSL automatically - that behaviour is
something for the application to control.
Some applications will want to allow the user to specify exactly which
.Vt ENGINE
they want used if any is to be used at all.
Others may prefer to load all support and have OpenSSL automatically use
at run-time any
.Vt ENGINE
that is able to successfully initialised - i.e. to assume that this
corresponds to acceleration hardware attached to the machine or
some such thing.
There are probably numerous other ways in which applications may prefer
to handle things, so we will simply illustrate the consequences as they
apply to a couple of simple cases and leave developers to consider these
and the source code to openssl's builtin utilities as guides.
.Pp
.Em Using a specific ENGINE implementation
.Pp
Here we'll assume an application has been configured by its user or
admin to want to use the "ACME"
.Vt ENGINE
if it is available in the version of OpenSSL the application was
compiled with.
If it is available, it should be used by default for all RSA, DSA, and
symmetric cipher operations, otherwise OpenSSL should use its builtin
software as usual.
The following code illustrates how to approach this:
.Bd -literal
ENGINE *e;
const char *engine_id = "ACME";
ENGINE_load_builtin_engines();
e = ENGINE_by_id(engine_id);
if (!e)
/* the engine isn't available */
return;
if (!ENGINE_init(e)) {
/* the engine couldn't initialise, release 'e' */
ENGINE_free(e);
return;
}
if (!ENGINE_set_default_RSA(e))
/* This should only happen when 'e' can't initialise, but the previous
* statement suggests it did. */
abort();
ENGINE_set_default_DSA(e);
ENGINE_set_default_ciphers(e);
/* Release the functional reference from ENGINE_init() */
ENGINE_finish(e);
/* Release the structural reference from ENGINE_by_id() */
ENGINE_free(e);
.Ed
.Pp
.Em Automatically using builtin ENGINE implementations
.Pp
Here we'll assume we want to load and register all
.Vt ENGINE
implementations bundled with OpenSSL, such that for any cryptographic
algorithm required by OpenSSL - if there is an
.Vt ENGINE
that implements it and can be initialised, it should be used.
The following code illustrates how this can work;
.Bd -literal
/* Load all bundled ENGINEs into memory and make them visible */
ENGINE_load_builtin_engines();
/* Register all of them for every algorithm they collectively implement */
ENGINE_register_all_complete();
.Ed
.Pp
That's all that's required.
For example, the next time OpenSSL tries to set up an RSA key, any bundled
.Vt ENGINE Ns s
that implement
.Vt RSA_METHOD
will be passed to
.Xr ENGINE_init 3
and if any of those succeed, that
.Vt ENGINE
will be set as the default for RSA use from then on.
.Ss Advanced configuration support
There is a mechanism supported by the
.Nm engine
framework that allows each
.Vt ENGINE
implementation to define an arbitrary set of configuration
"commands" and expose them to OpenSSL and any applications based on
OpenSSL.
This mechanism is entirely based on the use of name-value pairs
and assumes ASCII input (no unicode or UTF for now!), so it is ideal if
applications want to provide a transparent way for users to provide
arbitrary configuration "directives" directly to such
.Vt ENGINE Ns s .
It is also possible for the application to dynamically interrogate the
loaded
.Vt ENGINE
implementations for the names, descriptions, and input flags of
their available "control commands", providing a more flexible
configuration scheme.
However, if the user is expected to know which
.Vt ENGINE
device he/she is using (in the case of specialised hardware, this
goes without saying) then applications may not need to concern
themselves with discovering the supported control commands and
simply prefer to pass settings into
.Vt ENGINE s
exactly as they are provided by the user.
.Pp
Before illustrating how control commands work, it is worth mentioning
what they are typically used for.
Broadly speaking there are two uses for control commands; the first is
to provide the necessary details to the implementation (which may know
nothing at all specific to the host system) so that it can be
initialised for use.
This could include the path to any driver or config files it needs to
load, required network addresses, smart-card identifiers, passwords to
initialise protected devices, logging information, etc.
This class of commands typically needs to be passed to an
.Vt ENGINE
.Sy before
attempting to initialise it, i.e. before calling
.Xr ENGINE_init 3 .
The other class of commands consist of settings or operations that tweak
certain behaviour or cause certain operations to take place, and these
commands may work either before or after
.Xr ENGINE_init 3 ,
or in some cases both.
.Vt ENGINE
implementations should provide indications of this in the descriptions
attached to builtin control commands and/or in external product
documentation.
.Pp
.Em Issuing control commands to an ENGINE
.Pp
Let's illustrate by example; a function for which the caller supplies
the name of the
.Vt ENGINE
it wishes to use, a table of string-pairs for use before initialisation,
and another table for use after initialisation.
Note that the string-pairs used for control commands consist of a
command "name" followed by the command "parameter" - the parameter
could be
.Dv NULL
in some cases but the name cannot.
This function should initialise the
.Vt ENGINE
(issuing the "pre" commands beforehand and the "post" commands
afterwards) and set it as the default for everything except RAND
and then return a boolean success or failure.
.Bd -literal
int
generic_load_engine_fn(const char *engine_id,
const char **pre_cmds, int pre_num,
const char **post_cmds, int post_num)
{
ENGINE *e = ENGINE_by_id(engine_id);
if (!e)
return 0;
while (pre_num--) {
if (!ENGINE_ctrl_cmd_string(e,
pre_cmds[0], pre_cmds[1], 0)) {
fprintf(stderr,
"Failed command (%s - %s:%s)\en",
engine_id, pre_cmds[0],
pre_cmds[1] ? pre_cmds[1] : "(NULL)");
ENGINE_free(e);
return 0;
}
pre_cmds += 2;
}
if (!ENGINE_init(e)) {
fprintf(stderr, "Failed initialisation\en");
ENGINE_free(e);
return 0;
}
/*
* ENGINE_init() returned a functional reference,
* so free the structural reference from
* ENGINE_by_id().
*/
ENGINE_free(e);
while (post_num--) {
if (!ENGINE_ctrl_cmd_string(e,
post_cmds[0], post_cmds[1], 0)) {
fprintf(stderr,
"Failed command (%s - %s:%s)\en",
engine_id, post_cmds[0],
post_cmds[1] ? post_cmds[1] : "(NULL)");
ENGINE_finish(e);
return 0;
}
post_cmds += 2;
}
ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
/* Success */
return 1;
}
.Ed
.Pp
Note that
.Fn ENGINE_ctrl_cmd_string
accepts a boolean argument that can relax the semantics of the function.
If set to non-zero it will only return failure if the
.Vt ENGINE
supported the given command name but failed while executing it, if the
.Vt ENGINE
doesn't support the command name it will simply return success without
doing anything.
In this case we assume the user is only supplying commands specific to
the given
.Vt ENGINE
so we set this to FALSE.
.Pp
.Em Discovering supported control commands
.Pp
It is possible to discover at run-time the names, numerical-ids,
descriptions and input parameters of the control commands supported by an
.Vt ENGINE
using a structural reference.
Note that some control commands are defined by OpenSSL itself and it
will intercept and handle these control commands on behalf of the
.Vt ENGINE ,
i.e. the
.Vt ENGINE Ap s
ctrl() handler is not used for the control command.
.In openssl/engine.h
defines an index,
.Dv ENGINE_CMD_BASE ,
that all control commands implemented by
.Vt ENGINE Ns s
should be numbered from.
Any command value lower than this symbol is considered a "generic"
command is handled directly by the OpenSSL core routines.
.Pp
It is using these "core" control commands that one can discover the
control commands implemented by a given
.Vt ENGINE ,
specifically the commands:
.Bd -literal
#define ENGINE_HAS_CTRL_FUNCTION 10
#define ENGINE_CTRL_GET_FIRST_CMD_TYPE 11
#define ENGINE_CTRL_GET_NEXT_CMD_TYPE 12
#define ENGINE_CTRL_GET_CMD_FROM_NAME 13
#define ENGINE_CTRL_GET_NAME_LEN_FROM_CMD 14
#define ENGINE_CTRL_GET_NAME_FROM_CMD 15
#define ENGINE_CTRL_GET_DESC_LEN_FROM_CMD 16
#define ENGINE_CTRL_GET_DESC_FROM_CMD 17
#define ENGINE_CTRL_GET_CMD_FLAGS 18
.Ed
.Pp
Whilst these commands are automatically processed by the OpenSSL
framework code, they use various properties exposed by each
.Vt ENGINE
to process these queries.
An
.Vt ENGINE
has 3 properties it exposes that can affect how this behaves;
it can supply a ctrl() handler, it can specify
.Dv ENGINE_FLAGS_MANUAL_CMD_CTRL
in the
.Vt ENGINE Ap s
flags, and it can expose an array of control command descriptions.
If an
.Vt ENGINE
specifies the
.Dv ENGINE_FLAGS_MANUAL_CMD_CTRL
flag, then it will simply pass all these "core" control commands
directly to the
.Vt ENGINE Ap s
ctrl() handler (and thus, it must have supplied one), so it is up
to the
.Vt ENGINE
to reply to these "discovery" commands itself.
If that flag is not set, then the OpenSSL framework code will work with
the following rules;
.Bl -tag -width Ds
.It If no ctrl() handler is supplied:
.Dv ENGINE_HAS_CTRL_FUNCTION
returns FALSE (zero), all other commands fail.
.It If a ctrl() handler was supplied but no array of control commands:
.Dv ENGINE_HAS_CTRL_FUNCTION
returns TRUE, all other commands fail.
.It If a ctrl() handler and array of control commands was supplied:
.Dv ENGINE_HAS_CTRL_FUNCTION
returns TRUE, all other commands proceed processing...
.El
.Pp
If the
.Vt ENGINE Ns s
array of control commands is empty, then all other commands will fail.
Otherwise
.Dv ENGINE_CTRL_GET_FIRST_CMD_TYPE
returns the identifier of the first command supported by the
.Vt ENGINE ,
.Dv ENGINE_GET_NEXT_CMD_TYPE
takes the identifier of a command supported by the
.Vt ENGINE
and returns the next command identifier or fails if there are no more,
.Dv ENGINE_CMD_FROM_NAME
takes a string name for a command and returns the corresponding
identifier or fails if no such command name exists, and the remaining
commands take a command identifier and return properties of the
corresponding commands.
All except
.Dv ENGINE_CTRL_GET_FLAGS
return the string length of a command name or description, or
populate a supplied character buffer with a copy of the command
name or description.
.Dv ENGINE_CTRL_GET_FLAGS
returns a bitwise-OR'd mask of the following possible values:
.Bd -literal
#define ENGINE_CMD_FLAG_NUMERIC (unsigned int)0x0001
#define ENGINE_CMD_FLAG_STRING (unsigned int)0x0002
#define ENGINE_CMD_FLAG_NO_INPUT (unsigned int)0x0004
#define ENGINE_CMD_FLAG_INTERNAL (unsigned int)0x0008
.Ed
.Pp
If the
.Dv ENGINE_CMD_FLAG_INTERNAL
flag is set, then any other flags are purely informational to the caller.
This flag will prevent the command being usable for any higher-level
.Vt ENGINE
functions such as
.Fn ENGINE_ctrl_cmd_string .
"INTERNAL" commands are not intended to be exposed to text-based
configuration by applications, administrations, users, etc.
These can support arbitrary operations via
.Fn ENGINE_ctrl ,
including passing to and/or from the control commands data of any
arbitrary type.
These commands are supported in the discovery mechanisms simply allow
applications to determine if an
.Vt ENGINE
supports certain specific commands it might want to use (e.g.
application "foo" might query various
.Vt ENGINE Ns s
to see if they implement "FOO_GET_VENDOR_LOGO_GIF" - and
.Vt ENGINE
could therefore decide whether or not to support this "foo"-specific
extension).
.Sh RETURN VALUES
.Fn ENGINE_get_cipher_engine ,
.Fn ENGINE_get_digest_engine ,
and
.Fn ENGINE_new
return a valid
.Vt ENGINE
structure or
.Dv NULL
if an error occurred.
.Pp
.Fn ENGINE_free ,
.Fn ENGINE_up_ref ,
and all
.Fn ENGINE_set_*
functions return 1 on success or 0 on error.
.Pp
.Fn ENGINE_get_id
and
.Fn ENGINE_get_name
return a pointer to an internal string representing the identifier
and the name of
.Fa e ,
respectively.
.Pp
.Fn ENGINE_get_RSA ,
.Fn ENGINE_get_DSA ,
.Fn ENGINE_get_DH ,
.Fn ENGINE_get_RAND ,
and
.Fn ENGINE_get_STORE
return a method structure for the respective algorithm.
.Pp
.Fn ENGINE_get_destroy_function ,
.Fn ENGINE_get_load_privkey_function ,
.Fn ENGINE_get_load_pubkey_function ,
.Fn ENGINE_get_ciphers ,
and
.Fn ENGINE_get_digests
return a function pointer to the respective callback.
.Pp
.Fn ENGINE_get_cipher
returns a valid
.Vt EVP_CIPHER
structure on success or
.Dv NULL
if an error occurred.
.Pp
.Fn ENGINE_get_digest
returns a valid
.Vt EVP_MD
structure on success or
.Dv NULL
if an error occurred.
.Pp
.Fn ENGINE_get_flags
returns an integer representing the flags
which are used to control various behaviours of an
.Vt ENGINE .
.Pp
.Fn ENGINE_load_private_key
and
.Fn ENGINE_load_public_key
return a valid
.Vt EVP_PKEY
structure on success or
.Dv NULL
if an error occurred.
.Sh SEE ALSO
.Xr DH_new 3 ,
.Xr DSA_new 3 ,
.Xr ENGINE_add_conf_module 3 ,
.Xr ENGINE_set_ex_data 3 ,
.Xr RSA_new 3
.Sh HISTORY
The engine API first appeared in OpenSSL 0.9.7
and has been available since
.Ox 3.2 .
|