2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 RCSID("$OpenBSD: scard.c,v 1.18 2002/03/21 16:38:06 markus Exp $");
29 #include <openssl/engine.h>
37 #ifdef OPENSSL_VERSION_NUMBER
38 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
39 #define RSA_get_default_openssl_method RSA_get_default_method
40 #define DSA_get_default_openssl_method DSA_get_default_method
41 #define DH_get_default_openssl_method DH_get_default_method
42 #define ENGINE_set_BN_mod_exp(x,y)
47 #define INS_DECRYPT 0x10
48 #define INS_GET_KEYLENGTH 0x20
49 #define INS_GET_PUBKEY 0x30
50 #define INS_GET_RESPONSE 0xc0
52 #define MAX_BUF_SIZE 256
54 static int sc_fd = -1;
55 static char *sc_reader_id = NULL;
56 static int cla = 0x00; /* class */
58 /* interface to libsectok */
68 sc_fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
70 error("sectok_open failed: %s", sectok_get_sw(sw));
71 return SCARD_ERROR_FAIL;
73 if (! sectok_cardpresent(sc_fd)) {
74 debug("smartcard in reader %s not present, skipping",
77 return SCARD_ERROR_NOCARD;
79 if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
80 error("sectok_reset failed: %s", sectok_get_sw(sw));
82 return SCARD_ERROR_FAIL;
84 if ((cla = cyberflex_inq_class(sc_fd)) < 0)
87 debug("sc_open ok %d", sc_fd);
92 sc_enable_applet(void)
94 static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
97 /* select applet id */
98 sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
99 if (!sectok_swOK(sw)) {
100 error("sectok_apdu failed: %s", sectok_get_sw(sw));
113 if (status == SCARD_ERROR_NOCARD) {
114 return SCARD_ERROR_NOCARD;
117 error("sc_open failed");
120 if (sc_enable_applet() < 0) {
121 error("sc_enable_applet failed");
122 return SCARD_ERROR_APPLET;
128 sc_read_pubkey(Key * k)
132 int len, sw, status = -1;
144 sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
145 sizeof(buf), buf, &sw);
146 if (!sectok_swOK(sw)) {
147 error("could not obtain key length: %s", sectok_get_sw(sw));
150 len = (buf[0] << 8) | buf[1];
152 debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw));
156 sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw);
157 if (!sectok_swOK(sw)) {
158 error("could not obtain public key: %s", sectok_get_sw(sw));
162 debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw));
164 if (BN_bin2bn(n, len, k->rsa->n) == NULL) {
165 error("c_read_pubkey: BN_bin2bn failed");
169 /* currently the java applet just stores 'n' */
170 if (!BN_set_word(k->rsa->e, 35)) {
171 error("c_read_pubkey: BN_set_word(e, 35) failed");
176 p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
177 debug("fingerprint %d %s", key_size(k), p);
187 /* private key operations */
190 sc_private_decrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
193 u_char *padded = NULL;
194 int sw, len, olen, status = -1;
196 debug("sc_private_decrypt called");
204 if (padding != RSA_PKCS1_PADDING)
207 len = BN_num_bytes(rsa->n);
208 padded = xmalloc(len);
210 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, (u_char *)from,
212 if (!sectok_swOK(sw)) {
213 error("sc_private_decrypt: INS_DECRYPT failed: %s",
217 sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
219 if (!sectok_swOK(sw)) {
220 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
224 olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
230 return (olen >= 0 ? olen : status);
234 sc_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
237 u_char *padded = NULL;
238 int sw, len, status = -1;
246 if (padding != RSA_PKCS1_PADDING)
249 debug("sc_private_encrypt called");
250 len = BN_num_bytes(rsa->n);
251 padded = xmalloc(len);
253 if (RSA_padding_add_PKCS1_type_1(padded, len, (u_char *)from, flen) <= 0) {
254 error("RSA_padding_add_PKCS1_type_1 failed");
257 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, 0, NULL, &sw);
258 if (!sectok_swOK(sw)) {
259 error("sc_private_decrypt: INS_DECRYPT failed: %s",
263 sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
265 if (!sectok_swOK(sw)) {
266 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
274 return (len >= 0 ? len : status);
279 static int (*orig_finish)(RSA *rsa) = NULL;
291 /* engine for overloading private key operations */
293 static ENGINE *smart_engine = NULL;
294 static RSA_METHOD smart_rsa;
299 const RSA_METHOD *def;
301 def = RSA_get_default_openssl_method();
303 /* use the OpenSSL version */
304 memcpy(&smart_rsa, def, sizeof(smart_rsa));
306 smart_rsa.name = "sectok";
309 smart_rsa.rsa_priv_enc = sc_private_encrypt;
310 smart_rsa.rsa_priv_dec = sc_private_decrypt;
313 orig_finish = def->finish;
314 smart_rsa.finish = sc_finish;
316 if ((smart_engine = ENGINE_new()) == NULL)
317 fatal("ENGINE_new failed");
319 ENGINE_set_id(smart_engine, "sectok");
320 ENGINE_set_name(smart_engine, "libsectok");
322 ENGINE_set_RSA(smart_engine, &smart_rsa);
323 ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
324 ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
325 ENGINE_set_RAND(smart_engine, RAND_SSLeay());
326 ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
341 sc_get_key(const char *id)
346 if (sc_reader_id != NULL)
348 sc_reader_id = xstrdup(id);
350 k = key_new(KEY_RSA);
354 status = sc_read_pubkey(k);
355 if (status == SCARD_ERROR_NOCARD) {
360 error("sc_read_pubkey failed");
366 #endif /* SMARTCARD */