]> andersk Git - openssh.git/blame - scard.c
- djm@cvs.openbsd.org 2006/03/22 21:27:15
[openssh.git] / scard.c
CommitLineData
637b033d 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
637b033d 25#include "includes.h"
295c8801 26#if defined(SMARTCARD) && defined(USE_SECTOK)
514b94dc 27
b9f62352 28#include <openssl/evp.h>
637b033d 29#include <sectok.h>
30
31#include "key.h"
32#include "log.h"
33#include "xmalloc.h"
58ae9cb8 34#include "misc.h"
86c4f63d 35#include "scard.h"
637b033d 36
0659cace 37#if OPENSSL_VERSION_NUMBER < 0x00907000L
38#define USE_ENGINE
39#define RSA_get_default_method RSA_get_default_openssl_method
40#else
7649bbfe 41#endif
0659cace 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
7649bbfe 48#endif
49
637b033d 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
86c4f63d 58u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
59
637b033d 60static int sc_fd = -1;
9ff6f66f 61static char *sc_reader_id = NULL;
86c4f63d 62static char *sc_pin = NULL;
637b033d 63static int cla = 0x00; /* class */
64
86c4f63d 65static void sc_mk_digest(const char *pin, u_char *digest);
66static int get_AUT0(u_char *aut0);
7341fad9 67static int try_AUT0(void);
86c4f63d 68
637b033d 69/* interface to libsectok */
70
184eed6a 71static int
878b5225 72sc_open(void)
637b033d 73{
637b033d 74 int sw;
75
76 if (sc_fd >= 0)
77 return sc_fd;
637b033d 78
9ff6f66f 79 sc_fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
637b033d 80 if (sc_fd < 0) {
878b5225 81 error("sectok_open failed: %s", sectok_get_sw(sw));
0b595937 82 return SCARD_ERROR_FAIL;
83 }
84 if (! sectok_cardpresent(sc_fd)) {
9ff6f66f 85 debug("smartcard in reader %s not present, skipping",
86 sc_reader_id);
2251e099 87 sc_close();
0b595937 88 return SCARD_ERROR_NOCARD;
637b033d 89 }
c42158fe 90 if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
637b033d 91 error("sectok_reset failed: %s", sectok_get_sw(sw));
92 sc_fd = -1;
0b595937 93 return SCARD_ERROR_FAIL;
637b033d 94 }
c42158fe 95 if ((cla = cyberflex_inq_class(sc_fd)) < 0)
96 cla = 0;
878b5225 97
637b033d 98 debug("sc_open ok %d", sc_fd);
99 return sc_fd;
100}
101
184eed6a 102static int
637b033d 103sc_enable_applet(void)
104{
c42158fe 105 static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
106 int sw = 0;
637b033d 107
c42158fe 108 /* select applet id */
109 sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
637b033d 110 if (!sectok_swOK(sw)) {
111 error("sectok_apdu failed: %s", sectok_get_sw(sw));
878b5225 112 sc_close();
113 return -1;
114 }
115 return 0;
116}
117
184eed6a 118static int
878b5225 119sc_init(void)
120{
0b595937 121 int status;
122
123 status = sc_open();
124 if (status == SCARD_ERROR_NOCARD) {
125 return SCARD_ERROR_NOCARD;
126 }
127 if (status < 0 ) {
878b5225 128 error("sc_open failed");
0b595937 129 return status;
878b5225 130 }
131 if (sc_enable_applet() < 0) {
132 error("sc_enable_applet failed");
0b595937 133 return SCARD_ERROR_APPLET;
637b033d 134 }
135 return 0;
136}
137
184eed6a 138static int
637b033d 139sc_read_pubkey(Key * k)
140{
141 u_char buf[2], *n;
142 char *p;
4e842b5e 143 int len, sw, status = -1;
637b033d 144
145 len = sw = 0;
fc1fc39e 146 n = NULL;
637b033d 147
0b595937 148 if (sc_fd < 0) {
0659cace 149 if (sc_init() < 0)
4e842b5e 150 goto err;
0b595937 151 }
878b5225 152
637b033d 153 /* get key size */
154 sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
184eed6a 155 sizeof(buf), buf, &sw);
637b033d 156 if (!sectok_swOK(sw)) {
157 error("could not obtain key length: %s", sectok_get_sw(sw));
4e842b5e 158 goto err;
637b033d 159 }
160 len = (buf[0] << 8) | buf[1];
161 len /= 8;
162 debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw));
163
164 n = xmalloc(len);
165 /* get n */
166 sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw);
7341fad9 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 }
637b033d 173 if (!sectok_swOK(sw)) {
174 error("could not obtain public key: %s", sectok_get_sw(sw));
4e842b5e 175 goto err;
637b033d 176 }
4e842b5e 177
637b033d 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");
4e842b5e 182 goto err;
637b033d 183 }
637b033d 184
185 /* currently the java applet just stores 'n' */
186 if (!BN_set_word(k->rsa->e, 35)) {
187 error("c_read_pubkey: BN_set_word(e, 35) failed");
4e842b5e 188 goto err;
637b033d 189 }
190
4e842b5e 191 status = 0;
637b033d 192 p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
d6133f43 193 debug("fingerprint %u %s", key_size(k), p);
637b033d 194 xfree(p);
195
4e842b5e 196err:
197 if (n != NULL)
198 xfree(n);
199 sc_close();
200 return status;
637b033d 201}
202
203/* private key operations */
204
205static int
b9f62352 206sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
7649bbfe 207 int padding)
637b033d 208{
209 u_char *padded = NULL;
4e842b5e 210 int sw, len, olen, status = -1;
637b033d 211
212 debug("sc_private_decrypt called");
213
214 olen = len = sw = 0;
0b595937 215 if (sc_fd < 0) {
216 status = sc_init();
217 if (status < 0 )
878b5225 218 goto err;
0b595937 219 }
637b033d 220 if (padding != RSA_PKCS1_PADDING)
221 goto err;
222
223 len = BN_num_bytes(rsa->n);
224 padded = xmalloc(len);
225
86c4f63d 226 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, len, padded, &sw);
227
228 if (sw == 0x6982) {
9fc0407d 229 if (try_AUT0() < 0)
230 goto err;
86c4f63d 231 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, len, padded, &sw);
637b033d 232 }
637b033d 233 if (!sectok_swOK(sw)) {
86c4f63d 234 error("sc_private_decrypt: INS_DECRYPT failed: %s",
637b033d 235 sectok_get_sw(sw));
236 goto err;
237 }
238 olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
239 len);
240err:
241 if (padded)
242 xfree(padded);
4e842b5e 243 sc_close();
0b595937 244 return (olen >= 0 ? olen : status);
637b033d 245}
246
247static int
b9f62352 248sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
7649bbfe 249 int padding)
637b033d 250{
251 u_char *padded = NULL;
4e842b5e 252 int sw, len, status = -1;
637b033d 253
254 len = sw = 0;
0b595937 255 if (sc_fd < 0) {
256 status = sc_init();
257 if (status < 0 )
878b5225 258 goto err;
0b595937 259 }
637b033d 260 if (padding != RSA_PKCS1_PADDING)
261 goto err;
262
263 debug("sc_private_encrypt called");
264 len = BN_num_bytes(rsa->n);
265 padded = xmalloc(len);
266
7649bbfe 267 if (RSA_padding_add_PKCS1_type_1(padded, len, (u_char *)from, flen) <= 0) {
637b033d 268 error("RSA_padding_add_PKCS1_type_1 failed");
269 goto err;
270 }
86c4f63d 271 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, len, to, &sw);
9fc0407d 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 }
637b033d 277 if (!sectok_swOK(sw)) {
9fc0407d 278 error("sc_private_encrypt: INS_DECRYPT failed: %s",
637b033d 279 sectok_get_sw(sw));
280 goto err;
281 }
637b033d 282err:
283 if (padded)
284 xfree(padded);
4e842b5e 285 sc_close();
0b595937 286 return (len >= 0 ? len : status);
637b033d 287}
288
05b7537a 289/* called on free */
290
291static int (*orig_finish)(RSA *rsa) = NULL;
292
293static int
294sc_finish(RSA *rsa)
295{
296 if (orig_finish)
297 orig_finish(rsa);
298 sc_close();
299 return 1;
300}
301
637b033d 302/* engine for overloading private key operations */
303
0659cace 304static RSA_METHOD *
305sc_get_rsa_method(void)
637b033d 306{
0659cace 307 static RSA_METHOD smart_rsa;
308 const RSA_METHOD *def = RSA_get_default_method();
637b033d 309
7649bbfe 310 /* use the OpenSSL version */
311 memcpy(&smart_rsa, def, sizeof(smart_rsa));
312
313 smart_rsa.name = "sectok";
314
637b033d 315 /* overload */
316 smart_rsa.rsa_priv_enc = sc_private_encrypt;
317 smart_rsa.rsa_priv_dec = sc_private_decrypt;
318
05b7537a 319 /* save original */
320 orig_finish = def->finish;
321 smart_rsa.finish = sc_finish;
322
0659cace 323 return &smart_rsa;
324}
325
326#ifdef USE_ENGINE
327static ENGINE *
328sc_get_engine(void)
329{
330 static ENGINE *smart_engine = NULL;
331
b775c6f2 332 if ((smart_engine = ENGINE_new()) == NULL)
333 fatal("ENGINE_new failed");
637b033d 334
335 ENGINE_set_id(smart_engine, "sectok");
336 ENGINE_set_name(smart_engine, "libsectok");
7649bbfe 337
0659cace 338 ENGINE_set_RSA(smart_engine, sc_get_rsa_method());
637b033d 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}
0659cace 346#endif
637b033d 347
878b5225 348void
349sc_close(void)
350{
351 if (sc_fd >= 0) {
352 sectok_close(sc_fd);
353 sc_fd = -1;
354 }
355}
356
0659cace 357Key **
358sc_get_keys(const char *id, const char *pin)
637b033d 359{
0659cace 360 Key *k, *n, **keys;
361 int status, nkeys = 2;
637b033d 362
9ff6f66f 363 if (sc_reader_id != NULL)
364 xfree(sc_reader_id);
365 sc_reader_id = xstrdup(id);
366
86c4f63d 367 if (sc_pin != NULL)
368 xfree(sc_pin);
369 sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
370
637b033d 371 k = key_new(KEY_RSA);
372 if (k == NULL) {
373 return NULL;
374 }
71b7a18e 375 status = sc_read_pubkey(k);
376 if (status == SCARD_ERROR_NOCARD) {
377 key_free(k);
378 return NULL;
379 }
380 if (status < 0 ) {
637b033d 381 error("sc_read_pubkey failed");
382 key_free(k);
383 return NULL;
384 }
0659cace 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;
637b033d 405}
b9f62352 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
86c4f63d 417static void
418sc_mk_digest(const char *pin, u_char *digest)
b9f62352 419{
420 const EVP_MD *evp_md = EVP_sha1();
421 EVP_MD_CTX md;
86c4f63d 422
423 EVP_DigestInit(&md, evp_md);
424 EVP_DigestUpdate(&md, pin, strlen(pin));
425 EVP_DigestFinal(&md, digest, NULL);
426}
427
428static int
429get_AUT0(u_char *aut0)
430{
b9f62352 431 char *pass;
432
433 pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
434 if (pass == NULL)
435 return -1;
86c4f63d 436 if (!strcmp(pass, "-")) {
437 memcpy(aut0, DEFAUT0, sizeof DEFAUT0);
438 return 0;
439 }
440 sc_mk_digest(pass, aut0);
b9f62352 441 memset(pass, 0, strlen(pass));
442 xfree(pass);
443 return 0;
444}
445
7341fad9 446static int
447try_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);
470}
471
b9f62352 472int
473sc_put_key(Key *prv, const char *id)
474{
475 u_char *elements[NUM_RSA_KEY_ELEMENTS];
476 u_char key_fid[2];
b9f62352 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);
514b94dc 491 fd = sectok_friendly_open(id, STONOWAIT, &sw);
b9f62352 492 if (fd < 0) {
493 error("sectok_open failed: %s", sectok_get_sw(sw));
494 goto done;
495 }
496 if (! sectok_cardpresent(fd)) {
514b94dc 497 error("smartcard in reader %s not present", id);
b9f62352 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) {
86c4f63d 513 memset(AUT0, 0, sizeof(DEFAUT0));
514 error("smartcard passphrase incorrect");
b9f62352 515 goto done;
516 }
517 }
86c4f63d 518 memset(AUT0, 0, sizeof(DEFAUT0));
b9f62352 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;
bbe88b6d 528 logit("cyberflex_load_rsa_priv done");
b9f62352 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;
bbe88b6d 538 logit("cyberflex_load_rsa_pub done");
b9f62352 539 status = 0;
540
541done:
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);
555}
244e796f 556
557char *
558sc_get_key_label(Key *key)
559{
560 return xstrdup("smartcard key");
561}
562
295c8801 563#endif /* SMARTCARD && USE_SECTOK */
This page took 0.273393 seconds and 5 git commands to generate.