Annotation of src/usr.bin/ssh/scard.c, Revision 1.23
1.1 markus 1: /*
2: * Copyright (c) 2001 Markus Friedl. All rights reserved.
3: *
4: * Redistribution and use in source and binary forms, with or without
5: * modification, are permitted provided that the following conditions
6: * are met:
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.
12: *
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.
23: */
24:
25: #ifdef SMARTCARD
26: #include "includes.h"
1.23 ! markus 27: RCSID("$OpenBSD: scard.c,v 1.22 2002/03/21 21:54:34 rees Exp $");
1.1 markus 28:
29: #include <openssl/engine.h>
1.19 markus 30: #include <openssl/evp.h>
1.1 markus 31: #include <sectok.h>
32:
33: #include "key.h"
34: #include "log.h"
35: #include "xmalloc.h"
1.22 rees 36: #include "readpass.h"
1.1 markus 37: #include "scard.h"
38:
1.18 markus 39: #ifdef OPENSSL_VERSION_NUMBER
40: #if OPENSSL_VERSION_NUMBER >= 0x00907000L
41: #define RSA_get_default_openssl_method RSA_get_default_method
42: #define DSA_get_default_openssl_method DSA_get_default_method
43: #define DH_get_default_openssl_method DH_get_default_method
44: #define ENGINE_set_BN_mod_exp(x,y)
45: #endif
46: #endif
47:
1.1 markus 48: #define CLA_SSH 0x05
49: #define INS_DECRYPT 0x10
50: #define INS_GET_KEYLENGTH 0x20
51: #define INS_GET_PUBKEY 0x30
52: #define INS_GET_RESPONSE 0xc0
53:
54: #define MAX_BUF_SIZE 256
55:
1.22 rees 56: u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
57:
1.1 markus 58: static int sc_fd = -1;
1.11 markus 59: static char *sc_reader_id = NULL;
1.22 rees 60: static char *sc_pin = NULL;
1.1 markus 61: static int cla = 0x00; /* class */
62:
1.22 rees 63: static void sc_mk_digest(const char *pin, u_char *digest);
64: static int get_AUT0(u_char *aut0);
65:
1.1 markus 66: /* interface to libsectok */
67:
1.16 deraadt 68: static int
1.5 markus 69: sc_open(void)
1.1 markus 70: {
1.4 markus 71: int sw;
1.1 markus 72:
73: if (sc_fd >= 0)
74: return sc_fd;
75:
1.11 markus 76: sc_fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
1.1 markus 77: if (sc_fd < 0) {
1.5 markus 78: error("sectok_open failed: %s", sectok_get_sw(sw));
1.8 jakob 79: return SCARD_ERROR_FAIL;
80: }
81: if (! sectok_cardpresent(sc_fd)) {
1.11 markus 82: debug("smartcard in reader %s not present, skipping",
83: sc_reader_id);
1.10 jakob 84: sc_close();
1.8 jakob 85: return SCARD_ERROR_NOCARD;
1.1 markus 86: }
1.7 rees 87: if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
1.4 markus 88: error("sectok_reset failed: %s", sectok_get_sw(sw));
1.1 markus 89: sc_fd = -1;
1.8 jakob 90: return SCARD_ERROR_FAIL;
1.1 markus 91: }
1.7 rees 92: if ((cla = cyberflex_inq_class(sc_fd)) < 0)
93: cla = 0;
1.5 markus 94:
1.4 markus 95: debug("sc_open ok %d", sc_fd);
1.1 markus 96: return sc_fd;
97: }
98:
1.16 deraadt 99: static int
1.4 markus 100: sc_enable_applet(void)
1.1 markus 101: {
1.7 rees 102: static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
103: int sw = 0;
1.1 markus 104:
1.7 rees 105: /* select applet id */
106: sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
1.4 markus 107: if (!sectok_swOK(sw)) {
108: error("sectok_apdu failed: %s", sectok_get_sw(sw));
1.5 markus 109: sc_close();
110: return -1;
111: }
112: return 0;
113: }
114:
1.16 deraadt 115: static int
1.5 markus 116: sc_init(void)
117: {
1.8 jakob 118: int status;
119:
120: status = sc_open();
121: if (status == SCARD_ERROR_NOCARD) {
122: return SCARD_ERROR_NOCARD;
123: }
124: if (status < 0 ) {
1.5 markus 125: error("sc_open failed");
1.8 jakob 126: return status;
1.5 markus 127: }
128: if (sc_enable_applet() < 0) {
129: error("sc_enable_applet failed");
1.8 jakob 130: return SCARD_ERROR_APPLET;
1.1 markus 131: }
132: return 0;
133: }
134:
1.16 deraadt 135: static int
1.1 markus 136: sc_read_pubkey(Key * k)
137: {
1.4 markus 138: u_char buf[2], *n;
139: char *p;
1.14 markus 140: int len, sw, status = -1;
1.1 markus 141:
1.4 markus 142: len = sw = 0;
1.15 djm 143: n = NULL;
1.1 markus 144:
1.8 jakob 145: if (sc_fd < 0) {
146: status = sc_init();
147: if (status < 0 )
1.14 markus 148: goto err;
1.8 jakob 149: }
1.5 markus 150:
1.1 markus 151: /* get key size */
1.4 markus 152: sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
1.16 deraadt 153: sizeof(buf), buf, &sw);
1.4 markus 154: if (!sectok_swOK(sw)) {
155: error("could not obtain key length: %s", sectok_get_sw(sw));
1.14 markus 156: goto err;
1.1 markus 157: }
158: len = (buf[0] << 8) | buf[1];
159: len /= 8;
1.4 markus 160: debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw));
1.1 markus 161:
1.4 markus 162: n = xmalloc(len);
1.1 markus 163: /* get n */
1.4 markus 164: sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw);
165: if (!sectok_swOK(sw)) {
166: error("could not obtain public key: %s", sectok_get_sw(sw));
1.14 markus 167: goto err;
1.1 markus 168: }
1.14 markus 169:
1.4 markus 170: debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw));
171:
172: if (BN_bin2bn(n, len, k->rsa->n) == NULL) {
173: error("c_read_pubkey: BN_bin2bn failed");
1.14 markus 174: goto err;
1.4 markus 175: }
1.1 markus 176:
177: /* currently the java applet just stores 'n' */
1.4 markus 178: if (!BN_set_word(k->rsa->e, 35)) {
179: error("c_read_pubkey: BN_set_word(e, 35) failed");
1.14 markus 180: goto err;
1.4 markus 181: }
1.1 markus 182:
1.14 markus 183: status = 0;
1.1 markus 184: p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
185: debug("fingerprint %d %s", key_size(k), p);
186: xfree(p);
187:
1.14 markus 188: err:
189: if (n != NULL)
190: xfree(n);
191: sc_close();
192: return status;
1.1 markus 193: }
194:
1.23 ! markus 195: static int
! 196: try_AUT0(void)
! 197: {
! 198: u_char aut0[EVP_MAX_MD_SIZE];
! 199:
! 200: /* permission denied; try PIN if provided */
! 201: if (sc_pin && strlen(sc_pin) > 0) {
! 202: sc_mk_digest(sc_pin, aut0);
! 203: if (cyberflex_verify_AUT0(sc_fd, cla, aut0, 8) < 0) {
! 204: error("smartcard passphrase incorrect");
! 205: return (-1);
! 206: }
! 207: } else {
! 208: /* try default AUT0 key */
! 209: if (cyberflex_verify_AUT0(sc_fd, cla, DEFAUT0, 8) < 0) {
! 210: /* default AUT0 key failed; prompt for passphrase */
! 211: if (get_AUT0(aut0) < 0 ||
! 212: cyberflex_verify_AUT0(sc_fd, cla, aut0, 8) < 0) {
! 213: error("smartcard passphrase incorrect");
! 214: return (-1);
! 215: }
! 216: }
! 217: }
! 218: return (0);
! 219: }
! 220:
1.1 markus 221: /* private key operations */
222:
223: static int
1.20 markus 224: sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
1.18 markus 225: int padding)
1.1 markus 226: {
1.4 markus 227: u_char *padded = NULL;
1.14 markus 228: int sw, len, olen, status = -1;
1.1 markus 229:
230: debug("sc_private_decrypt called");
231:
1.4 markus 232: olen = len = sw = 0;
1.8 jakob 233: if (sc_fd < 0) {
234: status = sc_init();
235: if (status < 0 )
1.5 markus 236: goto err;
1.8 jakob 237: }
1.4 markus 238: if (padding != RSA_PKCS1_PADDING)
1.1 markus 239: goto err;
240:
1.4 markus 241: len = BN_num_bytes(rsa->n);
242: padded = xmalloc(len);
1.1 markus 243:
1.22 rees 244: sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, len, padded, &sw);
245:
246: if (sw == 0x6982) {
1.23 ! markus 247: if (try_AUT0() < 0)
! 248: goto err;
1.22 rees 249: sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, len, padded, &sw);
250: }
1.4 markus 251: if (!sectok_swOK(sw)) {
252: error("sc_private_decrypt: INS_DECRYPT failed: %s",
253: sectok_get_sw(sw));
1.1 markus 254: goto err;
255: }
1.4 markus 256: olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
257: len);
1.1 markus 258: err:
259: if (padded)
260: xfree(padded);
1.14 markus 261: sc_close();
1.8 jakob 262: return (olen >= 0 ? olen : status);
1.1 markus 263: }
264:
265: static int
1.20 markus 266: sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
1.18 markus 267: int padding)
1.1 markus 268: {
1.4 markus 269: u_char *padded = NULL;
1.14 markus 270: int sw, len, status = -1;
1.1 markus 271:
1.4 markus 272: len = sw = 0;
1.8 jakob 273: if (sc_fd < 0) {
274: status = sc_init();
275: if (status < 0 )
1.5 markus 276: goto err;
1.8 jakob 277: }
1.4 markus 278: if (padding != RSA_PKCS1_PADDING)
1.1 markus 279: goto err;
280:
1.3 markus 281: debug("sc_private_encrypt called");
1.4 markus 282: len = BN_num_bytes(rsa->n);
283: padded = xmalloc(len);
284:
1.18 markus 285: if (RSA_padding_add_PKCS1_type_1(padded, len, (u_char *)from, flen) <= 0) {
1.1 markus 286: error("RSA_padding_add_PKCS1_type_1 failed");
287: goto err;
288: }
1.22 rees 289: sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, len, to, &sw);
1.23 ! markus 290: if (sw == 0x6982) {
! 291: if (try_AUT0() < 0)
! 292: goto err;
! 293: sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, len, to, &sw);
! 294: }
1.4 markus 295: if (!sectok_swOK(sw)) {
1.23 ! markus 296: error("sc_private_encrypt: INS_DECRYPT failed: %s",
1.4 markus 297: sectok_get_sw(sw));
298: goto err;
299: }
1.1 markus 300: err:
301: if (padded)
302: xfree(padded);
1.14 markus 303: sc_close();
1.8 jakob 304: return (len >= 0 ? len : status);
1.1 markus 305: }
306:
1.12 markus 307: /* called on free */
308:
309: static int (*orig_finish)(RSA *rsa) = NULL;
310:
311: static int
312: sc_finish(RSA *rsa)
313: {
314: if (orig_finish)
315: orig_finish(rsa);
316: sc_close();
317: return 1;
318: }
319:
320:
1.1 markus 321: /* engine for overloading private key operations */
322:
323: static ENGINE *smart_engine = NULL;
1.18 markus 324: static RSA_METHOD smart_rsa;
1.1 markus 325:
326: ENGINE *
1.2 itojun 327: sc_get_engine(void)
1.1 markus 328: {
1.18 markus 329: const RSA_METHOD *def;
1.1 markus 330:
331: def = RSA_get_default_openssl_method();
332:
1.18 markus 333: /* use the OpenSSL version */
334: memcpy(&smart_rsa, def, sizeof(smart_rsa));
335:
336: smart_rsa.name = "sectok";
337:
1.1 markus 338: /* overload */
339: smart_rsa.rsa_priv_enc = sc_private_encrypt;
340: smart_rsa.rsa_priv_dec = sc_private_decrypt;
341:
1.12 markus 342: /* save original */
343: orig_finish = def->finish;
344: smart_rsa.finish = sc_finish;
345:
1.17 markus 346: if ((smart_engine = ENGINE_new()) == NULL)
347: fatal("ENGINE_new failed");
1.1 markus 348:
1.4 markus 349: ENGINE_set_id(smart_engine, "sectok");
350: ENGINE_set_name(smart_engine, "libsectok");
1.18 markus 351:
1.1 markus 352: ENGINE_set_RSA(smart_engine, &smart_rsa);
353: ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
354: ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
355: ENGINE_set_RAND(smart_engine, RAND_SSLeay());
356: ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
357:
358: return smart_engine;
359: }
360:
1.5 markus 361: void
362: sc_close(void)
363: {
364: if (sc_fd >= 0) {
365: sectok_close(sc_fd);
366: sc_fd = -1;
367: }
368: }
369:
1.1 markus 370: Key *
1.22 rees 371: sc_get_key(const char *id, const char *pin)
1.1 markus 372: {
373: Key *k;
1.9 jakob 374: int status;
1.1 markus 375:
1.11 markus 376: if (sc_reader_id != NULL)
377: xfree(sc_reader_id);
378: sc_reader_id = xstrdup(id);
379:
1.22 rees 380: if (sc_pin != NULL)
381: xfree(sc_pin);
382: sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
383:
1.1 markus 384: k = key_new(KEY_RSA);
385: if (k == NULL) {
386: return NULL;
387: }
1.9 jakob 388: status = sc_read_pubkey(k);
389: if (status == SCARD_ERROR_NOCARD) {
390: key_free(k);
391: return NULL;
392: }
393: if (status < 0 ) {
1.1 markus 394: error("sc_read_pubkey failed");
395: key_free(k);
396: return NULL;
397: }
398: return k;
1.19 markus 399: }
400:
401: #define NUM_RSA_KEY_ELEMENTS 5+1
402: #define COPY_RSA_KEY(x, i) \
403: do { \
404: len = BN_num_bytes(prv->rsa->x); \
405: elements[i] = xmalloc(len); \
406: debug("#bytes %d", len); \
407: if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
408: goto done; \
409: } while (0)
410:
1.22 rees 411: static void
412: sc_mk_digest(const char *pin, u_char *digest)
1.19 markus 413: {
414: const EVP_MD *evp_md = EVP_sha1();
415: EVP_MD_CTX md;
1.22 rees 416:
417: EVP_DigestInit(&md, evp_md);
418: EVP_DigestUpdate(&md, pin, strlen(pin));
419: EVP_DigestFinal(&md, digest, NULL);
420: }
421:
422: static int
423: get_AUT0(u_char *aut0)
424: {
1.19 markus 425: char *pass;
426:
427: pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
428: if (pass == NULL)
429: return -1;
1.22 rees 430: if (!strcmp(pass, "-")) {
431: memcpy(aut0, DEFAUT0, sizeof DEFAUT0);
432: return 0;
433: }
434: sc_mk_digest(pass, aut0);
1.19 markus 435: memset(pass, 0, strlen(pass));
436: xfree(pass);
437: return 0;
438: }
439:
440: int
441: sc_put_key(Key *prv, const char *id)
442: {
443: u_char *elements[NUM_RSA_KEY_ELEMENTS];
444: u_char key_fid[2];
445: u_char AUT0[EVP_MAX_MD_SIZE];
446: int len, status = -1, i, fd = -1, ret;
447: int sw = 0, cla = 0x00;
448:
449: for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
450: elements[i] = NULL;
451:
452: COPY_RSA_KEY(q, 0);
453: COPY_RSA_KEY(p, 1);
454: COPY_RSA_KEY(iqmp, 2);
455: COPY_RSA_KEY(dmq1, 3);
456: COPY_RSA_KEY(dmp1, 4);
457: COPY_RSA_KEY(n, 5);
458: len = BN_num_bytes(prv->rsa->n);
1.21 rees 459: fd = sectok_friendly_open(id, STONOWAIT, &sw);
1.19 markus 460: if (fd < 0) {
461: error("sectok_open failed: %s", sectok_get_sw(sw));
462: goto done;
463: }
464: if (! sectok_cardpresent(fd)) {
1.21 rees 465: error("smartcard in reader %s not present", id);
1.19 markus 466: goto done;
467: }
468: ret = sectok_reset(fd, 0, NULL, &sw);
469: if (ret <= 0) {
470: error("sectok_reset failed: %s", sectok_get_sw(sw));
471: goto done;
472: }
473: if ((cla = cyberflex_inq_class(fd)) < 0) {
474: error("cyberflex_inq_class failed");
475: goto done;
476: }
477: memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
478: if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
479: if (get_AUT0(AUT0) < 0 ||
480: cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
1.22 rees 481: memset(AUT0, 0, sizeof(DEFAUT0));
482: error("smartcard passphrase incorrect");
1.19 markus 483: goto done;
484: }
485: }
1.22 rees 486: memset(AUT0, 0, sizeof(DEFAUT0));
1.19 markus 487: key_fid[0] = 0x00;
488: key_fid[1] = 0x12;
489: if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
490: &sw) < 0) {
491: error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
492: goto done;
493: }
494: if (!sectok_swOK(sw))
495: goto done;
496: log("cyberflex_load_rsa_priv done");
497: key_fid[0] = 0x73;
498: key_fid[1] = 0x68;
499: if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
500: &sw) < 0) {
501: error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
502: goto done;
503: }
504: if (!sectok_swOK(sw))
505: goto done;
506: log("cyberflex_load_rsa_pub done");
507: status = 0;
508:
509: done:
510: memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
511: memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
512: memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
513: memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
514: memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
515: memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
516:
517: for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
518: if (elements[i])
519: xfree(elements[i]);
520: if (fd != -1)
521: sectok_close(fd);
522: return (status);
1.1 markus 523: }
1.13 jakob 524: #endif /* SMARTCARD */