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.10 2001/07/31 12:53:34 jakob Exp $");
29 #include <openssl/engine.h>
38 #define INS_DECRYPT 0x10
39 #define INS_GET_KEYLENGTH 0x20
40 #define INS_GET_PUBKEY 0x30
41 #define INS_GET_RESPONSE 0xc0
43 #define MAX_BUF_SIZE 256
45 static int sc_fd = -1;
46 static int sc_reader_num = -1;
47 static int cla = 0x00; /* class */
49 /* interface to libsectok */
59 sc_fd = sectok_open(sc_reader_num, STONOWAIT, &sw);
61 error("sectok_open failed: %s", sectok_get_sw(sw));
62 return SCARD_ERROR_FAIL;
64 if (! sectok_cardpresent(sc_fd)) {
65 debug("smartcard in reader %d not present, skipping",
68 return SCARD_ERROR_NOCARD;
70 if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
71 error("sectok_reset failed: %s", sectok_get_sw(sw));
73 return SCARD_ERROR_FAIL;
75 if ((cla = cyberflex_inq_class(sc_fd)) < 0)
78 debug("sc_open ok %d", sc_fd);
83 sc_enable_applet(void)
85 static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
88 /* select applet id */
89 sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
90 if (!sectok_swOK(sw)) {
91 error("sectok_apdu failed: %s", sectok_get_sw(sw));
104 if (status == SCARD_ERROR_NOCARD) {
105 return SCARD_ERROR_NOCARD;
108 error("sc_open failed");
111 if (sc_enable_applet() < 0) {
112 error("sc_enable_applet failed");
113 return SCARD_ERROR_APPLET;
119 sc_read_pubkey(Key * k)
134 sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
135 sizeof(buf), buf, &sw);
136 if (!sectok_swOK(sw)) {
137 error("could not obtain key length: %s", sectok_get_sw(sw));
141 len = (buf[0] << 8) | buf[1];
143 debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw));
147 sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw);
148 if (!sectok_swOK(sw)) {
149 error("could not obtain public key: %s", sectok_get_sw(sw));
153 debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw));
155 if (BN_bin2bn(n, len, k->rsa->n) == NULL) {
156 error("c_read_pubkey: BN_bin2bn failed");
163 /* currently the java applet just stores 'n' */
164 if (!BN_set_word(k->rsa->e, 35)) {
165 error("c_read_pubkey: BN_set_word(e, 35) failed");
169 p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
170 debug("fingerprint %d %s", key_size(k), p);
176 /* private key operations */
179 sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
181 u_char *padded = NULL;
182 int sw, len, olen, status;
184 debug("sc_private_decrypt called");
192 if (padding != RSA_PKCS1_PADDING)
195 len = BN_num_bytes(rsa->n);
196 padded = xmalloc(len);
198 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, 0, NULL, &sw);
199 if (!sectok_swOK(sw)) {
200 error("sc_private_decrypt: INS_DECRYPT failed: %s",
205 sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
207 if (!sectok_swOK(sw)) {
208 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
213 olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
218 return (olen >= 0 ? olen : status);
222 sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
224 u_char *padded = NULL;
233 if (padding != RSA_PKCS1_PADDING)
236 debug("sc_private_encrypt called");
237 len = BN_num_bytes(rsa->n);
238 padded = xmalloc(len);
240 if (RSA_padding_add_PKCS1_type_1(padded, len, from, flen) <= 0) {
241 error("RSA_padding_add_PKCS1_type_1 failed");
244 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, 0, NULL, &sw);
245 if (!sectok_swOK(sw)) {
246 error("sc_private_decrypt: INS_DECRYPT failed: %s",
251 sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
253 if (!sectok_swOK(sw)) {
254 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
262 return (len >= 0 ? len : status);
265 /* engine for overloading private key operations */
267 static ENGINE *smart_engine = NULL;
268 static RSA_METHOD smart_rsa =
288 def = RSA_get_default_openssl_method();
291 smart_rsa.rsa_priv_enc = sc_private_encrypt;
292 smart_rsa.rsa_priv_dec = sc_private_decrypt;
294 /* just use the OpenSSL version */
295 smart_rsa.rsa_pub_enc = def->rsa_pub_enc;
296 smart_rsa.rsa_pub_dec = def->rsa_pub_dec;
297 smart_rsa.rsa_mod_exp = def->rsa_mod_exp;
298 smart_rsa.bn_mod_exp = def->bn_mod_exp;
299 smart_rsa.init = def->init;
300 smart_rsa.finish = def->finish;
301 smart_rsa.flags = def->flags;
302 smart_rsa.app_data = def->app_data;
303 smart_rsa.rsa_sign = def->rsa_sign;
304 smart_rsa.rsa_verify = def->rsa_verify;
306 smart_engine = ENGINE_new();
308 ENGINE_set_id(smart_engine, "sectok");
309 ENGINE_set_name(smart_engine, "libsectok");
310 ENGINE_set_RSA(smart_engine, &smart_rsa);
311 ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
312 ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
313 ENGINE_set_RAND(smart_engine, RAND_SSLeay());
314 ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
335 k = key_new(KEY_RSA);
339 status = sc_read_pubkey(k);
340 if (status == SCARD_ERROR_NOCARD) {
345 error("sc_read_pubkey failed");