17574SJan.Pechanec@Sun.COM /* 27574SJan.Pechanec@Sun.COM * CDDL HEADER START 37574SJan.Pechanec@Sun.COM * 47574SJan.Pechanec@Sun.COM * The contents of this file are subject to the terms of the 57574SJan.Pechanec@Sun.COM * Common Development and Distribution License (the "License"). 67574SJan.Pechanec@Sun.COM * You may not use this file except in compliance with the License. 77574SJan.Pechanec@Sun.COM * 87574SJan.Pechanec@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97574SJan.Pechanec@Sun.COM * or http://www.opensolaris.org/os/licensing. 107574SJan.Pechanec@Sun.COM * See the License for the specific language governing permissions 117574SJan.Pechanec@Sun.COM * and limitations under the License. 127574SJan.Pechanec@Sun.COM * 137574SJan.Pechanec@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 147574SJan.Pechanec@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157574SJan.Pechanec@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 167574SJan.Pechanec@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 177574SJan.Pechanec@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 187574SJan.Pechanec@Sun.COM * 197574SJan.Pechanec@Sun.COM * CDDL HEADER END 207574SJan.Pechanec@Sun.COM */ 217574SJan.Pechanec@Sun.COM /* 227574SJan.Pechanec@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237574SJan.Pechanec@Sun.COM * Use is subject to license terms. 247574SJan.Pechanec@Sun.COM */ 257574SJan.Pechanec@Sun.COM 267574SJan.Pechanec@Sun.COM #include "includes.h" 277574SJan.Pechanec@Sun.COM #include "log.h" 287574SJan.Pechanec@Sun.COM #include "engine.h" 297574SJan.Pechanec@Sun.COM 307574SJan.Pechanec@Sun.COM #define PKCS11_ENGINE "pkcs11" 317574SJan.Pechanec@Sun.COM 327574SJan.Pechanec@Sun.COM /* 337574SJan.Pechanec@Sun.COM * Loads the PKCS#11 engine if the UseOpenSSLEngine is set to yes which is the 347574SJan.Pechanec@Sun.COM * default value. 357574SJan.Pechanec@Sun.COM */ 367574SJan.Pechanec@Sun.COM ENGINE * 377574SJan.Pechanec@Sun.COM pkcs11_engine_load(int use_engine) 387574SJan.Pechanec@Sun.COM { 397574SJan.Pechanec@Sun.COM ENGINE *e = NULL; 407574SJan.Pechanec@Sun.COM 417574SJan.Pechanec@Sun.COM debug("use_engine is '%s'", use_engine == 1 ? "yes" : "no"); 427574SJan.Pechanec@Sun.COM if (use_engine == 0) 437574SJan.Pechanec@Sun.COM return (NULL); 447574SJan.Pechanec@Sun.COM 457574SJan.Pechanec@Sun.COM ENGINE_load_pk11(); 467574SJan.Pechanec@Sun.COM /* get structural reference */ 477574SJan.Pechanec@Sun.COM if ((e = ENGINE_by_id(PKCS11_ENGINE)) == NULL) { 48*7733SJan.Pechanec@Sun.COM error("%s engine does not exist", PKCS11_ENGINE); 49*7733SJan.Pechanec@Sun.COM return (NULL); 507574SJan.Pechanec@Sun.COM } 517574SJan.Pechanec@Sun.COM 527574SJan.Pechanec@Sun.COM /* get functional reference */ 537574SJan.Pechanec@Sun.COM if (ENGINE_init(e) == 0) { 54*7733SJan.Pechanec@Sun.COM error("can't initialize %s engine", PKCS11_ENGINE); 55*7733SJan.Pechanec@Sun.COM return (NULL); 567574SJan.Pechanec@Sun.COM } 577574SJan.Pechanec@Sun.COM 587574SJan.Pechanec@Sun.COM debug("%s engine initialized, now setting it as default for " 597574SJan.Pechanec@Sun.COM "RSA, DSA, and symmetric ciphers", PKCS11_ENGINE); 607574SJan.Pechanec@Sun.COM 617574SJan.Pechanec@Sun.COM /* 627574SJan.Pechanec@Sun.COM * Offloading RSA, DSA and symmetric ciphers to the engine is all we 637574SJan.Pechanec@Sun.COM * want. We don't offload Diffie-Helmann since we use longer DH keys 647574SJan.Pechanec@Sun.COM * than supported in ncp/n2cp (2048 bits). And, we don't offload digest 657574SJan.Pechanec@Sun.COM * operations since that would be beneficial if only big packets were 667574SJan.Pechanec@Sun.COM * processed (~8K). However, that's not the case. For example, 677574SJan.Pechanec@Sun.COM * SSH_MSG_CHANNEL_WINDOW_ADJUST messages are always small. Given the 687574SJan.Pechanec@Sun.COM * fact that digest operations are fast in software and the inherent 697574SJan.Pechanec@Sun.COM * overhead of offloading anything to HW is quite big, not offloading 707574SJan.Pechanec@Sun.COM * digests to HW actually makes SSH data transfer faster. 717574SJan.Pechanec@Sun.COM */ 727574SJan.Pechanec@Sun.COM if (!ENGINE_set_default_RSA(e)) { 73*7733SJan.Pechanec@Sun.COM error("can't use %s engine for RSA", PKCS11_ENGINE); 74*7733SJan.Pechanec@Sun.COM return (NULL); 757574SJan.Pechanec@Sun.COM } 767574SJan.Pechanec@Sun.COM if (!ENGINE_set_default_DSA(e)) { 77*7733SJan.Pechanec@Sun.COM error("can't use %s engine for DSA", PKCS11_ENGINE); 78*7733SJan.Pechanec@Sun.COM return (NULL); 797574SJan.Pechanec@Sun.COM } 807574SJan.Pechanec@Sun.COM if (!ENGINE_set_default_ciphers(e)) { 81*7733SJan.Pechanec@Sun.COM error("can't use %s engine for symmetric ciphers", 82*7733SJan.Pechanec@Sun.COM PKCS11_ENGINE); 83*7733SJan.Pechanec@Sun.COM return (NULL); 847574SJan.Pechanec@Sun.COM } 857574SJan.Pechanec@Sun.COM 867574SJan.Pechanec@Sun.COM debug("%s engine initialization complete", PKCS11_ENGINE); 877574SJan.Pechanec@Sun.COM return (e); 887574SJan.Pechanec@Sun.COM } 897574SJan.Pechanec@Sun.COM 907574SJan.Pechanec@Sun.COM /* 917574SJan.Pechanec@Sun.COM * Finishes the PKCS#11 engine after all remaining structural and functional 927574SJan.Pechanec@Sun.COM * references to the ENGINE structure are freed. 937574SJan.Pechanec@Sun.COM */ 947574SJan.Pechanec@Sun.COM void 957574SJan.Pechanec@Sun.COM pkcs11_engine_finish(void *engine) 967574SJan.Pechanec@Sun.COM { 977574SJan.Pechanec@Sun.COM ENGINE *e = (ENGINE *)engine; 987574SJan.Pechanec@Sun.COM 997574SJan.Pechanec@Sun.COM debug("in pkcs11_engine_finish(), engine pointer is %p", e); 1007574SJan.Pechanec@Sun.COM /* UseOpenSSLEngine was 'no' */ 1017574SJan.Pechanec@Sun.COM if (engine == NULL) 1027574SJan.Pechanec@Sun.COM return; 1037574SJan.Pechanec@Sun.COM 1047574SJan.Pechanec@Sun.COM debug("unregistering RSA"); 1057574SJan.Pechanec@Sun.COM ENGINE_unregister_RSA(e); 1067574SJan.Pechanec@Sun.COM debug("unregistering DSA"); 1077574SJan.Pechanec@Sun.COM ENGINE_unregister_DSA(e); 1087574SJan.Pechanec@Sun.COM debug("unregistering ciphers"); 1097574SJan.Pechanec@Sun.COM ENGINE_unregister_ciphers(e); 1107574SJan.Pechanec@Sun.COM 1117574SJan.Pechanec@Sun.COM debug("calling ENGINE_finish()"); 1127574SJan.Pechanec@Sun.COM ENGINE_finish(engine); 1137574SJan.Pechanec@Sun.COM debug("calling ENGINE_remove()"); 1147574SJan.Pechanec@Sun.COM ENGINE_remove(engine); 1157574SJan.Pechanec@Sun.COM debug("calling ENGINE_free()"); 1167574SJan.Pechanec@Sun.COM ENGINE_free(engine); 1177574SJan.Pechanec@Sun.COM debug("%s engine finished", PKCS11_ENGINE); 1187574SJan.Pechanec@Sun.COM } 119