2 /* $OpenBSD: scard.c,v 1.36 2006/11/06 21:25:28 markus Exp $ */
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 __RCSID("$NetBSD: scard.c,v 1.10 2007/03/10 22:52:08 christos Exp $");
31 #include <sys/types.h>
33 #include <openssl/evp.h>
44 #if OPENSSL_VERSION_NUMBER < 0x00907000L
46 #define RSA_get_default_method RSA_get_default_openssl_method
51 #include <openssl/engine.h>
52 #define sc_get_rsa sc_get_engine
54 #define sc_get_rsa sc_get_rsa_method
58 #define INS_DECRYPT 0x10
59 #define INS_GET_KEYLENGTH 0x20
60 #define INS_GET_PUBKEY 0x30
61 #define INS_GET_RESPONSE 0xc0
63 #define MAX_BUF_SIZE 256
65 u_char DEFAUT0
[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
67 static int sc_fd
= -1;
68 static char *sc_reader_id
= NULL
;
69 static char *sc_pin
= NULL
;
70 static int cla
= 0x00; /* class */
72 static void sc_mk_digest(const char *pin
, u_char
*digest
);
73 static int get_AUT0(u_char
*aut0
);
74 static int try_AUT0(void);
76 /* interface to libsectok */
86 sc_fd
= sectok_friendly_open(sc_reader_id
, STONOWAIT
, &sw
);
88 error("sectok_open failed: %s", sectok_get_sw(sw
));
89 return SCARD_ERROR_FAIL
;
91 if (! sectok_cardpresent(sc_fd
)) {
92 debug("smartcard in reader %s not present, skipping",
95 return SCARD_ERROR_NOCARD
;
97 if (sectok_reset(sc_fd
, 0, NULL
, &sw
) <= 0) {
98 error("sectok_reset failed: %s", sectok_get_sw(sw
));
100 return SCARD_ERROR_FAIL
;
102 if ((cla
= cyberflex_inq_class(sc_fd
)) < 0)
105 debug("sc_open ok %d", sc_fd
);
110 sc_enable_applet(void)
112 static u_char aid
[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
115 /* select applet id */
116 sectok_apdu(sc_fd
, cla
, 0xa4, 0x04, 0, sizeof aid
, aid
, 0, NULL
, &sw
);
117 if (!sectok_swOK(sw
)) {
118 error("sectok_apdu failed: %s", sectok_get_sw(sw
));
131 if (status
== SCARD_ERROR_NOCARD
) {
132 return SCARD_ERROR_NOCARD
;
135 error("sc_open failed");
138 if (sc_enable_applet() < 0) {
139 error("sc_enable_applet failed");
140 return SCARD_ERROR_APPLET
;
146 sc_read_pubkey(Key
* k
)
150 int len
, sw
, status
= -1;
161 sectok_apdu(sc_fd
, CLA_SSH
, INS_GET_KEYLENGTH
, 0, 0, 0, NULL
,
162 sizeof(buf
), buf
, &sw
);
163 if (!sectok_swOK(sw
)) {
164 error("could not obtain key length: %s", sectok_get_sw(sw
));
167 len
= (buf
[0] << 8) | buf
[1];
169 debug("INS_GET_KEYLENGTH: len %d sw %s", len
, sectok_get_sw(sw
));
173 sectok_apdu(sc_fd
, CLA_SSH
, INS_GET_PUBKEY
, 0, 0, 0, NULL
, len
, n
, &sw
);
178 sectok_apdu(sc_fd
, CLA_SSH
, INS_GET_PUBKEY
, 0, 0, 0, NULL
, len
, n
, &sw
);
180 if (!sectok_swOK(sw
)) {
181 error("could not obtain public key: %s", sectok_get_sw(sw
));
185 debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw
));
187 if (BN_bin2bn(n
, len
, k
->rsa
->n
) == NULL
) {
188 error("c_read_pubkey: BN_bin2bn failed");
192 /* currently the java applet just stores 'n' */
193 if (!BN_set_word(k
->rsa
->e
, 35)) {
194 error("c_read_pubkey: BN_set_word(e, 35) failed");
199 p
= key_fingerprint(k
, SSH_FP_MD5
, SSH_FP_HEX
);
200 debug("fingerprint %u %s", key_size(k
), p
);
210 /* private key operations */
213 sc_private_decrypt(int flen
, u_char
*from
, u_char
*to
, RSA
*rsa
,
216 u_char
*padded
= NULL
;
217 int sw
, len
, olen
, status
= -1;
219 debug("sc_private_decrypt called");
227 if (padding
!= RSA_PKCS1_PADDING
)
230 len
= BN_num_bytes(rsa
->n
);
231 padded
= xmalloc(len
);
233 sectok_apdu(sc_fd
, CLA_SSH
, INS_DECRYPT
, 0, 0, len
, from
, len
, padded
, &sw
);
238 sectok_apdu(sc_fd
, CLA_SSH
, INS_DECRYPT
, 0, 0, len
, from
, len
, padded
, &sw
);
240 if (!sectok_swOK(sw
)) {
241 error("sc_private_decrypt: INS_DECRYPT failed: %s",
245 olen
= RSA_padding_check_PKCS1_type_2(to
, len
, padded
+ 1, len
- 1,
251 return (olen
>= 0 ? olen
: status
);
255 sc_private_encrypt(int flen
, u_char
*from
, u_char
*to
, RSA
*rsa
,
258 u_char
*padded
= NULL
;
259 int sw
, len
, status
= -1;
267 if (padding
!= RSA_PKCS1_PADDING
)
270 debug("sc_private_encrypt called");
271 len
= BN_num_bytes(rsa
->n
);
272 padded
= xmalloc(len
);
274 if (RSA_padding_add_PKCS1_type_1(padded
, len
, (u_char
*)from
, flen
) <= 0) {
275 error("RSA_padding_add_PKCS1_type_1 failed");
278 sectok_apdu(sc_fd
, CLA_SSH
, INS_DECRYPT
, 0, 0, len
, padded
, len
, to
, &sw
);
282 sectok_apdu(sc_fd
, CLA_SSH
, INS_DECRYPT
, 0, 0, len
, padded
, len
, to
, &sw
);
284 if (!sectok_swOK(sw
)) {
285 error("sc_private_encrypt: INS_DECRYPT failed: %s",
293 return (len
>= 0 ? len
: status
);
298 static int (*orig_finish
)(RSA
*rsa
) = NULL
;
309 /* engine for overloading private key operations */
312 sc_get_rsa_method(void)
314 static RSA_METHOD smart_rsa
;
315 const RSA_METHOD
*def
= RSA_get_default_method();
317 /* use the OpenSSL version */
318 memcpy(&smart_rsa
, def
, sizeof(smart_rsa
));
320 smart_rsa
.name
= "sectok";
323 smart_rsa
.rsa_priv_enc
= sc_private_encrypt
;
324 smart_rsa
.rsa_priv_dec
= sc_private_decrypt
;
327 orig_finish
= def
->finish
;
328 smart_rsa
.finish
= sc_finish
;
337 static ENGINE
*smart_engine
= NULL
;
339 if ((smart_engine
= ENGINE_new()) == NULL
)
340 fatal("ENGINE_new failed");
342 ENGINE_set_id(smart_engine
, "sectok");
343 ENGINE_set_name(smart_engine
, "libsectok");
345 ENGINE_set_RSA(smart_engine
, sc_get_rsa_method());
346 ENGINE_set_DSA(smart_engine
, DSA_get_default_openssl_method());
347 ENGINE_set_DH(smart_engine
, DH_get_default_openssl_method());
348 ENGINE_set_RAND(smart_engine
, RAND_SSLeay());
349 ENGINE_set_BN_mod_exp(smart_engine
, BN_mod_exp
);
365 sc_get_keys(const char *id
, const char *pin
)
368 int status
, nkeys
= 2;
370 if (sc_reader_id
!= NULL
)
372 sc_reader_id
= xstrdup(id
);
376 sc_pin
= (pin
== NULL
) ? NULL
: xstrdup(pin
);
378 k
= key_new(KEY_RSA
);
382 status
= sc_read_pubkey(k
);
383 if (status
== SCARD_ERROR_NOCARD
) {
388 error("sc_read_pubkey failed");
392 keys
= xcalloc((nkeys
+1), sizeof(Key
*));
394 n
= key_new(KEY_RSA1
);
395 if ((BN_copy(n
->rsa
->n
, k
->rsa
->n
) == NULL
) ||
396 (BN_copy(n
->rsa
->e
, k
->rsa
->e
) == NULL
))
397 fatal("sc_get_keys: BN_copy failed");
398 RSA_set_method(n
->rsa
, sc_get_rsa());
399 n
->flags
|= KEY_FLAG_EXT
;
402 n
= key_new(KEY_RSA
);
403 if ((BN_copy(n
->rsa
->n
, k
->rsa
->n
) == NULL
) ||
404 (BN_copy(n
->rsa
->e
, k
->rsa
->e
) == NULL
))
405 fatal("sc_get_keys: BN_copy failed");
406 RSA_set_method(n
->rsa
, sc_get_rsa());
407 n
->flags
|= KEY_FLAG_EXT
;
416 #define NUM_RSA_KEY_ELEMENTS 5+1
417 #define COPY_RSA_KEY(x, i) \
419 len = BN_num_bytes(prv->rsa->x); \
420 elements[i] = xmalloc(len); \
421 debug("#bytes %d", len); \
422 if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
427 sc_mk_digest(const char *pin
, u_char
*digest
)
429 const EVP_MD
*evp_md
= EVP_sha1();
432 EVP_DigestInit(&md
, evp_md
);
433 EVP_DigestUpdate(&md
, pin
, strlen(pin
));
434 EVP_DigestFinal(&md
, digest
, NULL
);
438 get_AUT0(u_char
*aut0
)
442 pass
= read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN
);
445 if (!strcmp(pass
, "-")) {
446 memcpy(aut0
, DEFAUT0
, sizeof DEFAUT0
);
449 sc_mk_digest(pass
, aut0
);
450 memset(pass
, 0, strlen(pass
));
458 u_char aut0
[EVP_MAX_MD_SIZE
];
460 /* permission denied; try PIN if provided */
461 if (sc_pin
&& strlen(sc_pin
) > 0) {
462 sc_mk_digest(sc_pin
, aut0
);
463 if (cyberflex_verify_AUT0(sc_fd
, cla
, aut0
, 8) < 0) {
464 error("smartcard passphrase incorrect");
468 /* try default AUT0 key */
469 if (cyberflex_verify_AUT0(sc_fd
, cla
, DEFAUT0
, 8) < 0) {
470 /* default AUT0 key failed; prompt for passphrase */
471 if (get_AUT0(aut0
) < 0 ||
472 cyberflex_verify_AUT0(sc_fd
, cla
, aut0
, 8) < 0) {
473 error("smartcard passphrase incorrect");
482 sc_put_key(Key
*prv
, const char *id
)
484 u_char
*elements
[NUM_RSA_KEY_ELEMENTS
];
486 u_char AUT0
[EVP_MAX_MD_SIZE
];
487 int len
, status
= -1, i
, fd
= -1, ret
;
488 int sw
= 0, cla
= 0x00;
490 for (i
= 0; i
< NUM_RSA_KEY_ELEMENTS
; i
++)
495 COPY_RSA_KEY(iqmp
, 2);
496 COPY_RSA_KEY(dmq1
, 3);
497 COPY_RSA_KEY(dmp1
, 4);
499 len
= BN_num_bytes(prv
->rsa
->n
);
500 fd
= sectok_friendly_open(id
, STONOWAIT
, &sw
);
502 error("sectok_open failed: %s", sectok_get_sw(sw
));
505 if (! sectok_cardpresent(fd
)) {
506 error("smartcard in reader %s not present", id
);
509 ret
= sectok_reset(fd
, 0, NULL
, &sw
);
511 error("sectok_reset failed: %s", sectok_get_sw(sw
));
514 if ((cla
= cyberflex_inq_class(fd
)) < 0) {
515 error("cyberflex_inq_class failed");
518 memcpy(AUT0
, DEFAUT0
, sizeof(DEFAUT0
));
519 if (cyberflex_verify_AUT0(fd
, cla
, AUT0
, sizeof(DEFAUT0
)) < 0) {
520 if (get_AUT0(AUT0
) < 0 ||
521 cyberflex_verify_AUT0(fd
, cla
, AUT0
, sizeof(DEFAUT0
)) < 0) {
522 memset(AUT0
, 0, sizeof(DEFAUT0
));
523 error("smartcard passphrase incorrect");
527 memset(AUT0
, 0, sizeof(DEFAUT0
));
530 if (cyberflex_load_rsa_priv(fd
, cla
, key_fid
, 5, 8*len
, elements
,
532 error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw
));
535 if (!sectok_swOK(sw
))
537 logit("cyberflex_load_rsa_priv done");
540 if (cyberflex_load_rsa_pub(fd
, cla
, key_fid
, len
, elements
[5],
542 error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw
));
545 if (!sectok_swOK(sw
))
547 logit("cyberflex_load_rsa_pub done");
551 memset(elements
[0], '\0', BN_num_bytes(prv
->rsa
->q
));
552 memset(elements
[1], '\0', BN_num_bytes(prv
->rsa
->p
));
553 memset(elements
[2], '\0', BN_num_bytes(prv
->rsa
->iqmp
));
554 memset(elements
[3], '\0', BN_num_bytes(prv
->rsa
->dmq1
));
555 memset(elements
[4], '\0', BN_num_bytes(prv
->rsa
->dmp1
));
556 memset(elements
[5], '\0', BN_num_bytes(prv
->rsa
->n
));
558 for (i
= 0; i
< NUM_RSA_KEY_ELEMENTS
; i
++)
567 sc_get_key_label(Key
*key
)
569 return xstrdup("smartcard key");
572 #endif /* SMARTCARD */