1cd7d5fafSJan Pechanec /*
2cd7d5fafSJan Pechanec * CDDL HEADER START
3cd7d5fafSJan Pechanec *
4cd7d5fafSJan Pechanec * The contents of this file are subject to the terms of the
5cd7d5fafSJan Pechanec * Common Development and Distribution License (the "License").
6cd7d5fafSJan Pechanec * You may not use this file except in compliance with the License.
7cd7d5fafSJan Pechanec *
8cd7d5fafSJan Pechanec * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9cd7d5fafSJan Pechanec * or http://www.opensolaris.org/os/licensing.
10cd7d5fafSJan Pechanec * See the License for the specific language governing permissions
11cd7d5fafSJan Pechanec * and limitations under the License.
12cd7d5fafSJan Pechanec *
13cd7d5fafSJan Pechanec * When distributing Covered Code, include this CDDL HEADER in each
14cd7d5fafSJan Pechanec * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15cd7d5fafSJan Pechanec * If applicable, add the following below this CDDL HEADER, with the
16cd7d5fafSJan Pechanec * fields enclosed by brackets "[]" replaced with your own identifying
17cd7d5fafSJan Pechanec * information: Portions Copyright [yyyy] [name of copyright owner]
18cd7d5fafSJan Pechanec *
19cd7d5fafSJan Pechanec * CDDL HEADER END
20cd7d5fafSJan Pechanec */
21cd7d5fafSJan Pechanec /*
22cd7d5fafSJan Pechanec * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23cd7d5fafSJan Pechanec * Use is subject to license terms.
24cd7d5fafSJan Pechanec */
25cd7d5fafSJan Pechanec
26cd7d5fafSJan Pechanec #include "includes.h"
27cd7d5fafSJan Pechanec #include "log.h"
28cd7d5fafSJan Pechanec #include "engine.h"
29cd7d5fafSJan Pechanec
30cd7d5fafSJan Pechanec #define PKCS11_ENGINE "pkcs11"
31cd7d5fafSJan Pechanec
32cd7d5fafSJan Pechanec /*
33cd7d5fafSJan Pechanec * Loads the PKCS#11 engine if the UseOpenSSLEngine is set to yes which is the
34cd7d5fafSJan Pechanec * default value.
35cd7d5fafSJan Pechanec */
36cd7d5fafSJan Pechanec ENGINE *
pkcs11_engine_load(int use_engine)37cd7d5fafSJan Pechanec pkcs11_engine_load(int use_engine)
38cd7d5fafSJan Pechanec {
39cd7d5fafSJan Pechanec ENGINE *e = NULL;
40cd7d5fafSJan Pechanec
41cd7d5fafSJan Pechanec debug("use_engine is '%s'", use_engine == 1 ? "yes" : "no");
42cd7d5fafSJan Pechanec if (use_engine == 0)
43cd7d5fafSJan Pechanec return (NULL);
44cd7d5fafSJan Pechanec
45cd7d5fafSJan Pechanec ENGINE_load_pk11();
46cd7d5fafSJan Pechanec /* get structural reference */
47cd7d5fafSJan Pechanec if ((e = ENGINE_by_id(PKCS11_ENGINE)) == NULL) {
48*23b4d00cSJan Pechanec error("%s engine does not exist", PKCS11_ENGINE);
49*23b4d00cSJan Pechanec return (NULL);
50cd7d5fafSJan Pechanec }
51cd7d5fafSJan Pechanec
52cd7d5fafSJan Pechanec /* get functional reference */
53cd7d5fafSJan Pechanec if (ENGINE_init(e) == 0) {
54*23b4d00cSJan Pechanec error("can't initialize %s engine", PKCS11_ENGINE);
55*23b4d00cSJan Pechanec return (NULL);
56cd7d5fafSJan Pechanec }
57cd7d5fafSJan Pechanec
58cd7d5fafSJan Pechanec debug("%s engine initialized, now setting it as default for "
59cd7d5fafSJan Pechanec "RSA, DSA, and symmetric ciphers", PKCS11_ENGINE);
60cd7d5fafSJan Pechanec
61cd7d5fafSJan Pechanec /*
62cd7d5fafSJan Pechanec * Offloading RSA, DSA and symmetric ciphers to the engine is all we
63cd7d5fafSJan Pechanec * want. We don't offload Diffie-Helmann since we use longer DH keys
64cd7d5fafSJan Pechanec * than supported in ncp/n2cp (2048 bits). And, we don't offload digest
65cd7d5fafSJan Pechanec * operations since that would be beneficial if only big packets were
66cd7d5fafSJan Pechanec * processed (~8K). However, that's not the case. For example,
67cd7d5fafSJan Pechanec * SSH_MSG_CHANNEL_WINDOW_ADJUST messages are always small. Given the
68cd7d5fafSJan Pechanec * fact that digest operations are fast in software and the inherent
69cd7d5fafSJan Pechanec * overhead of offloading anything to HW is quite big, not offloading
70cd7d5fafSJan Pechanec * digests to HW actually makes SSH data transfer faster.
71cd7d5fafSJan Pechanec */
72cd7d5fafSJan Pechanec if (!ENGINE_set_default_RSA(e)) {
73*23b4d00cSJan Pechanec error("can't use %s engine for RSA", PKCS11_ENGINE);
74*23b4d00cSJan Pechanec return (NULL);
75cd7d5fafSJan Pechanec }
76cd7d5fafSJan Pechanec if (!ENGINE_set_default_DSA(e)) {
77*23b4d00cSJan Pechanec error("can't use %s engine for DSA", PKCS11_ENGINE);
78*23b4d00cSJan Pechanec return (NULL);
79cd7d5fafSJan Pechanec }
80cd7d5fafSJan Pechanec if (!ENGINE_set_default_ciphers(e)) {
81*23b4d00cSJan Pechanec error("can't use %s engine for symmetric ciphers",
82*23b4d00cSJan Pechanec PKCS11_ENGINE);
83*23b4d00cSJan Pechanec return (NULL);
84cd7d5fafSJan Pechanec }
85cd7d5fafSJan Pechanec
86cd7d5fafSJan Pechanec debug("%s engine initialization complete", PKCS11_ENGINE);
87cd7d5fafSJan Pechanec return (e);
88cd7d5fafSJan Pechanec }
89cd7d5fafSJan Pechanec
90cd7d5fafSJan Pechanec /*
91cd7d5fafSJan Pechanec * Finishes the PKCS#11 engine after all remaining structural and functional
92cd7d5fafSJan Pechanec * references to the ENGINE structure are freed.
93cd7d5fafSJan Pechanec */
94cd7d5fafSJan Pechanec void
pkcs11_engine_finish(void * engine)95cd7d5fafSJan Pechanec pkcs11_engine_finish(void *engine)
96cd7d5fafSJan Pechanec {
97cd7d5fafSJan Pechanec ENGINE *e = (ENGINE *)engine;
98cd7d5fafSJan Pechanec
99cd7d5fafSJan Pechanec debug("in pkcs11_engine_finish(), engine pointer is %p", e);
100cd7d5fafSJan Pechanec /* UseOpenSSLEngine was 'no' */
101cd7d5fafSJan Pechanec if (engine == NULL)
102cd7d5fafSJan Pechanec return;
103cd7d5fafSJan Pechanec
104cd7d5fafSJan Pechanec debug("unregistering RSA");
105cd7d5fafSJan Pechanec ENGINE_unregister_RSA(e);
106cd7d5fafSJan Pechanec debug("unregistering DSA");
107cd7d5fafSJan Pechanec ENGINE_unregister_DSA(e);
108cd7d5fafSJan Pechanec debug("unregistering ciphers");
109cd7d5fafSJan Pechanec ENGINE_unregister_ciphers(e);
110cd7d5fafSJan Pechanec
111cd7d5fafSJan Pechanec debug("calling ENGINE_finish()");
112cd7d5fafSJan Pechanec ENGINE_finish(engine);
113cd7d5fafSJan Pechanec debug("calling ENGINE_remove()");
114cd7d5fafSJan Pechanec ENGINE_remove(engine);
115cd7d5fafSJan Pechanec debug("calling ENGINE_free()");
116cd7d5fafSJan Pechanec ENGINE_free(engine);
117cd7d5fafSJan Pechanec debug("%s engine finished", PKCS11_ENGINE);
118cd7d5fafSJan Pechanec }
119