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