Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 1 | /* $OpenBSD: scard.c,v 1.35 2006/08/03 03:34:42 deraadt Exp $ */ |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 26 | #include "includes.h" |
Ben Lindstrom | a42694f | 2002-04-05 16:11:45 +0000 | [diff] [blame] | 27 | #if defined(SMARTCARD) && defined(USE_SECTOK) |
Ben Lindstrom | 818659a | 2002-03-22 03:38:35 +0000 | [diff] [blame] | 28 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 29 | #include <sys/types.h> |
Damien Miller | 7c6e4b0 | 2006-08-05 09:33:15 +1000 | [diff] [blame] | 30 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 31 | #include <sectok.h> |
Damien Miller | 7c6e4b0 | 2006-08-05 09:33:15 +1000 | [diff] [blame] | 32 | #include <string.h> |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 33 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 34 | #include <openssl/evp.h> |
| 35 | |
| 36 | #include "xmalloc.h" |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 37 | #include "key.h" |
| 38 | #include "log.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 39 | #include "misc.h" |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 40 | #include "scard.h" |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 41 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 42 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 43 | #define USE_ENGINE |
| 44 | #define RSA_get_default_method RSA_get_default_openssl_method |
| 45 | #else |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 46 | #endif |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 47 | |
| 48 | #ifdef USE_ENGINE |
| 49 | #include <openssl/engine.h> |
| 50 | #define sc_get_rsa sc_get_engine |
| 51 | #else |
| 52 | #define sc_get_rsa sc_get_rsa_method |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 55 | #define CLA_SSH 0x05 |
| 56 | #define INS_DECRYPT 0x10 |
| 57 | #define INS_GET_KEYLENGTH 0x20 |
| 58 | #define INS_GET_PUBKEY 0x30 |
| 59 | #define INS_GET_RESPONSE 0xc0 |
| 60 | |
| 61 | #define MAX_BUF_SIZE 256 |
| 62 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 63 | u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63}; |
| 64 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 65 | static int sc_fd = -1; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 66 | static char *sc_reader_id = NULL; |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 67 | static char *sc_pin = NULL; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 68 | static int cla = 0x00; /* class */ |
| 69 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 70 | static void sc_mk_digest(const char *pin, u_char *digest); |
| 71 | static int get_AUT0(u_char *aut0); |
Ben Lindstrom | 43a5e2f | 2002-03-27 17:33:17 +0000 | [diff] [blame] | 72 | static int try_AUT0(void); |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 73 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 74 | /* interface to libsectok */ |
| 75 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 76 | static int |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 77 | sc_open(void) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 78 | { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 79 | int sw; |
| 80 | |
| 81 | if (sc_fd >= 0) |
| 82 | return sc_fd; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 83 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 84 | sc_fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 85 | if (sc_fd < 0) { |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 86 | error("sectok_open failed: %s", sectok_get_sw(sw)); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 87 | return SCARD_ERROR_FAIL; |
| 88 | } |
| 89 | if (! sectok_cardpresent(sc_fd)) { |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 90 | debug("smartcard in reader %s not present, skipping", |
| 91 | sc_reader_id); |
Ben Lindstrom | 3ab1dfa | 2001-08-06 21:33:44 +0000 | [diff] [blame] | 92 | sc_close(); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 93 | return SCARD_ERROR_NOCARD; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 94 | } |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 95 | if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 96 | error("sectok_reset failed: %s", sectok_get_sw(sw)); |
| 97 | sc_fd = -1; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 98 | return SCARD_ERROR_FAIL; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 99 | } |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 100 | if ((cla = cyberflex_inq_class(sc_fd)) < 0) |
| 101 | cla = 0; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 102 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 103 | debug("sc_open ok %d", sc_fd); |
| 104 | return sc_fd; |
| 105 | } |
| 106 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 107 | static int |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 108 | sc_enable_applet(void) |
| 109 | { |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 110 | static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e}; |
| 111 | int sw = 0; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 112 | |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 113 | /* select applet id */ |
| 114 | sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 115 | if (!sectok_swOK(sw)) { |
| 116 | error("sectok_apdu failed: %s", sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 117 | sc_close(); |
| 118 | return -1; |
| 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 123 | static int |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 124 | sc_init(void) |
| 125 | { |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 126 | int status; |
| 127 | |
| 128 | status = sc_open(); |
| 129 | if (status == SCARD_ERROR_NOCARD) { |
| 130 | return SCARD_ERROR_NOCARD; |
| 131 | } |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 132 | if (status < 0) { |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 133 | error("sc_open failed"); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 134 | return status; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 135 | } |
| 136 | if (sc_enable_applet() < 0) { |
| 137 | error("sc_enable_applet failed"); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 138 | return SCARD_ERROR_APPLET; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 143 | static int |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 144 | sc_read_pubkey(Key * k) |
| 145 | { |
| 146 | u_char buf[2], *n; |
| 147 | char *p; |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 148 | int len, sw, status = -1; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 149 | |
| 150 | len = sw = 0; |
Damien Miller | 3ff36d6 | 2001-09-28 19:51:54 +1000 | [diff] [blame] | 151 | n = NULL; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 152 | |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 153 | if (sc_fd < 0) { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 154 | if (sc_init() < 0) |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 155 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 156 | } |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 157 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 158 | /* get key size */ |
| 159 | sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 160 | sizeof(buf), buf, &sw); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 161 | if (!sectok_swOK(sw)) { |
| 162 | error("could not obtain key length: %s", sectok_get_sw(sw)); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 163 | goto err; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 164 | } |
| 165 | len = (buf[0] << 8) | buf[1]; |
| 166 | len /= 8; |
| 167 | debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw)); |
| 168 | |
| 169 | n = xmalloc(len); |
| 170 | /* get n */ |
| 171 | sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw); |
Ben Lindstrom | 43a5e2f | 2002-03-27 17:33:17 +0000 | [diff] [blame] | 172 | |
| 173 | if (sw == 0x6982) { |
| 174 | if (try_AUT0() < 0) |
| 175 | goto err; |
| 176 | sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw); |
| 177 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 178 | if (!sectok_swOK(sw)) { |
| 179 | error("could not obtain public key: %s", sectok_get_sw(sw)); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 180 | goto err; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 181 | } |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 182 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 183 | debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw)); |
| 184 | |
| 185 | if (BN_bin2bn(n, len, k->rsa->n) == NULL) { |
| 186 | error("c_read_pubkey: BN_bin2bn failed"); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 187 | goto err; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 188 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 189 | |
| 190 | /* currently the java applet just stores 'n' */ |
| 191 | if (!BN_set_word(k->rsa->e, 35)) { |
| 192 | error("c_read_pubkey: BN_set_word(e, 35) failed"); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 193 | goto err; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 196 | status = 0; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 197 | p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX); |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 198 | debug("fingerprint %u %s", key_size(k), p); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 199 | xfree(p); |
| 200 | |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 201 | err: |
| 202 | if (n != NULL) |
| 203 | xfree(n); |
| 204 | sc_close(); |
| 205 | return status; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* private key operations */ |
| 209 | |
| 210 | static int |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 211 | sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 212 | int padding) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 213 | { |
| 214 | u_char *padded = NULL; |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 215 | int sw, len, olen, status = -1; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 216 | |
| 217 | debug("sc_private_decrypt called"); |
| 218 | |
| 219 | olen = len = sw = 0; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 220 | if (sc_fd < 0) { |
| 221 | status = sc_init(); |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 222 | if (status < 0) |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 223 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 224 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 225 | if (padding != RSA_PKCS1_PADDING) |
| 226 | goto err; |
| 227 | |
| 228 | len = BN_num_bytes(rsa->n); |
| 229 | padded = xmalloc(len); |
| 230 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 231 | sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, len, padded, &sw); |
| 232 | |
| 233 | if (sw == 0x6982) { |
Ben Lindstrom | c2c6cbc | 2002-03-26 02:44:44 +0000 | [diff] [blame] | 234 | if (try_AUT0() < 0) |
| 235 | goto err; |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 236 | sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, len, padded, &sw); |
| 237 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 238 | if (!sectok_swOK(sw)) { |
| 239 | error("sc_private_decrypt: INS_DECRYPT failed: %s", |
| 240 | sectok_get_sw(sw)); |
| 241 | goto err; |
| 242 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 243 | olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1, |
| 244 | len); |
| 245 | err: |
| 246 | if (padded) |
| 247 | xfree(padded); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 248 | sc_close(); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 249 | return (olen >= 0 ? olen : status); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 253 | sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 254 | int padding) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 255 | { |
| 256 | u_char *padded = NULL; |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 257 | int sw, len, status = -1; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 258 | |
| 259 | len = sw = 0; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 260 | if (sc_fd < 0) { |
| 261 | status = sc_init(); |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 262 | if (status < 0) |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 263 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 264 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 265 | if (padding != RSA_PKCS1_PADDING) |
| 266 | goto err; |
| 267 | |
| 268 | debug("sc_private_encrypt called"); |
| 269 | len = BN_num_bytes(rsa->n); |
| 270 | padded = xmalloc(len); |
| 271 | |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 272 | if (RSA_padding_add_PKCS1_type_1(padded, len, (u_char *)from, flen) <= 0) { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 273 | error("RSA_padding_add_PKCS1_type_1 failed"); |
| 274 | goto err; |
| 275 | } |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 276 | sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, len, to, &sw); |
Ben Lindstrom | c2c6cbc | 2002-03-26 02:44:44 +0000 | [diff] [blame] | 277 | if (sw == 0x6982) { |
| 278 | if (try_AUT0() < 0) |
| 279 | goto err; |
| 280 | sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, len, to, &sw); |
| 281 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 282 | if (!sectok_swOK(sw)) { |
Ben Lindstrom | c2c6cbc | 2002-03-26 02:44:44 +0000 | [diff] [blame] | 283 | error("sc_private_encrypt: INS_DECRYPT failed: %s", |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 284 | sectok_get_sw(sw)); |
| 285 | goto err; |
| 286 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 287 | err: |
| 288 | if (padded) |
| 289 | xfree(padded); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 290 | sc_close(); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 291 | return (len >= 0 ? len : status); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Ben Lindstrom | a6c8a8d | 2001-08-06 21:42:00 +0000 | [diff] [blame] | 294 | /* called on free */ |
| 295 | |
| 296 | static int (*orig_finish)(RSA *rsa) = NULL; |
| 297 | |
| 298 | static int |
| 299 | sc_finish(RSA *rsa) |
| 300 | { |
| 301 | if (orig_finish) |
| 302 | orig_finish(rsa); |
| 303 | sc_close(); |
| 304 | return 1; |
| 305 | } |
| 306 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 307 | /* engine for overloading private key operations */ |
| 308 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 309 | static RSA_METHOD * |
| 310 | sc_get_rsa_method(void) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 311 | { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 312 | static RSA_METHOD smart_rsa; |
| 313 | const RSA_METHOD *def = RSA_get_default_method(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 314 | |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 315 | /* use the OpenSSL version */ |
| 316 | memcpy(&smart_rsa, def, sizeof(smart_rsa)); |
| 317 | |
| 318 | smart_rsa.name = "sectok"; |
| 319 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 320 | /* overload */ |
| 321 | smart_rsa.rsa_priv_enc = sc_private_encrypt; |
| 322 | smart_rsa.rsa_priv_dec = sc_private_decrypt; |
| 323 | |
Ben Lindstrom | a6c8a8d | 2001-08-06 21:42:00 +0000 | [diff] [blame] | 324 | /* save original */ |
| 325 | orig_finish = def->finish; |
| 326 | smart_rsa.finish = sc_finish; |
| 327 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 328 | return &smart_rsa; |
| 329 | } |
| 330 | |
| 331 | #ifdef USE_ENGINE |
| 332 | static ENGINE * |
| 333 | sc_get_engine(void) |
| 334 | { |
| 335 | static ENGINE *smart_engine = NULL; |
| 336 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 337 | if ((smart_engine = ENGINE_new()) == NULL) |
| 338 | fatal("ENGINE_new failed"); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 339 | |
| 340 | ENGINE_set_id(smart_engine, "sectok"); |
| 341 | ENGINE_set_name(smart_engine, "libsectok"); |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 342 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 343 | ENGINE_set_RSA(smart_engine, sc_get_rsa_method()); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 344 | ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method()); |
| 345 | ENGINE_set_DH(smart_engine, DH_get_default_openssl_method()); |
| 346 | ENGINE_set_RAND(smart_engine, RAND_SSLeay()); |
| 347 | ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp); |
| 348 | |
| 349 | return smart_engine; |
| 350 | } |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 351 | #endif |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 352 | |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 353 | void |
| 354 | sc_close(void) |
| 355 | { |
| 356 | if (sc_fd >= 0) { |
| 357 | sectok_close(sc_fd); |
| 358 | sc_fd = -1; |
| 359 | } |
| 360 | } |
| 361 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 362 | Key ** |
| 363 | sc_get_keys(const char *id, const char *pin) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 364 | { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 365 | Key *k, *n, **keys; |
| 366 | int status, nkeys = 2; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 367 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 368 | if (sc_reader_id != NULL) |
| 369 | xfree(sc_reader_id); |
| 370 | sc_reader_id = xstrdup(id); |
| 371 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 372 | if (sc_pin != NULL) |
| 373 | xfree(sc_pin); |
| 374 | sc_pin = (pin == NULL) ? NULL : xstrdup(pin); |
| 375 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 376 | k = key_new(KEY_RSA); |
| 377 | if (k == NULL) { |
| 378 | return NULL; |
| 379 | } |
Ben Lindstrom | 94baf30 | 2001-08-06 21:25:38 +0000 | [diff] [blame] | 380 | status = sc_read_pubkey(k); |
| 381 | if (status == SCARD_ERROR_NOCARD) { |
| 382 | key_free(k); |
| 383 | return NULL; |
| 384 | } |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 385 | if (status < 0) { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 386 | error("sc_read_pubkey failed"); |
| 387 | key_free(k); |
| 388 | return NULL; |
| 389 | } |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 390 | keys = xcalloc((nkeys+1), sizeof(Key *)); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 391 | |
| 392 | n = key_new(KEY_RSA1); |
| 393 | BN_copy(n->rsa->n, k->rsa->n); |
| 394 | BN_copy(n->rsa->e, k->rsa->e); |
| 395 | RSA_set_method(n->rsa, sc_get_rsa()); |
| 396 | n->flags |= KEY_FLAG_EXT; |
| 397 | keys[0] = n; |
| 398 | |
| 399 | n = key_new(KEY_RSA); |
| 400 | BN_copy(n->rsa->n, k->rsa->n); |
| 401 | BN_copy(n->rsa->e, k->rsa->e); |
| 402 | RSA_set_method(n->rsa, sc_get_rsa()); |
| 403 | n->flags |= KEY_FLAG_EXT; |
| 404 | keys[1] = n; |
| 405 | |
| 406 | keys[2] = NULL; |
| 407 | |
| 408 | key_free(k); |
| 409 | return keys; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 410 | } |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 411 | |
| 412 | #define NUM_RSA_KEY_ELEMENTS 5+1 |
| 413 | #define COPY_RSA_KEY(x, i) \ |
| 414 | do { \ |
| 415 | len = BN_num_bytes(prv->rsa->x); \ |
| 416 | elements[i] = xmalloc(len); \ |
| 417 | debug("#bytes %d", len); \ |
| 418 | if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \ |
| 419 | goto done; \ |
| 420 | } while (0) |
| 421 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 422 | static void |
| 423 | sc_mk_digest(const char *pin, u_char *digest) |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 424 | { |
| 425 | const EVP_MD *evp_md = EVP_sha1(); |
| 426 | EVP_MD_CTX md; |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 427 | |
| 428 | EVP_DigestInit(&md, evp_md); |
| 429 | EVP_DigestUpdate(&md, pin, strlen(pin)); |
| 430 | EVP_DigestFinal(&md, digest, NULL); |
| 431 | } |
| 432 | |
| 433 | static int |
| 434 | get_AUT0(u_char *aut0) |
| 435 | { |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 436 | char *pass; |
| 437 | |
| 438 | pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN); |
| 439 | if (pass == NULL) |
| 440 | return -1; |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 441 | if (!strcmp(pass, "-")) { |
| 442 | memcpy(aut0, DEFAUT0, sizeof DEFAUT0); |
| 443 | return 0; |
| 444 | } |
| 445 | sc_mk_digest(pass, aut0); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 446 | memset(pass, 0, strlen(pass)); |
| 447 | xfree(pass); |
| 448 | return 0; |
| 449 | } |
| 450 | |
Ben Lindstrom | 43a5e2f | 2002-03-27 17:33:17 +0000 | [diff] [blame] | 451 | static int |
| 452 | try_AUT0(void) |
| 453 | { |
| 454 | u_char aut0[EVP_MAX_MD_SIZE]; |
| 455 | |
| 456 | /* permission denied; try PIN if provided */ |
| 457 | if (sc_pin && strlen(sc_pin) > 0) { |
| 458 | sc_mk_digest(sc_pin, aut0); |
| 459 | if (cyberflex_verify_AUT0(sc_fd, cla, aut0, 8) < 0) { |
| 460 | error("smartcard passphrase incorrect"); |
| 461 | return (-1); |
| 462 | } |
| 463 | } else { |
| 464 | /* try default AUT0 key */ |
| 465 | if (cyberflex_verify_AUT0(sc_fd, cla, DEFAUT0, 8) < 0) { |
| 466 | /* default AUT0 key failed; prompt for passphrase */ |
| 467 | if (get_AUT0(aut0) < 0 || |
| 468 | cyberflex_verify_AUT0(sc_fd, cla, aut0, 8) < 0) { |
| 469 | error("smartcard passphrase incorrect"); |
| 470 | return (-1); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | return (0); |
| 475 | } |
| 476 | |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 477 | int |
| 478 | sc_put_key(Key *prv, const char *id) |
| 479 | { |
| 480 | u_char *elements[NUM_RSA_KEY_ELEMENTS]; |
| 481 | u_char key_fid[2]; |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 482 | u_char AUT0[EVP_MAX_MD_SIZE]; |
| 483 | int len, status = -1, i, fd = -1, ret; |
| 484 | int sw = 0, cla = 0x00; |
| 485 | |
| 486 | for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++) |
| 487 | elements[i] = NULL; |
| 488 | |
| 489 | COPY_RSA_KEY(q, 0); |
| 490 | COPY_RSA_KEY(p, 1); |
| 491 | COPY_RSA_KEY(iqmp, 2); |
| 492 | COPY_RSA_KEY(dmq1, 3); |
| 493 | COPY_RSA_KEY(dmp1, 4); |
| 494 | COPY_RSA_KEY(n, 5); |
| 495 | len = BN_num_bytes(prv->rsa->n); |
Ben Lindstrom | 818659a | 2002-03-22 03:38:35 +0000 | [diff] [blame] | 496 | fd = sectok_friendly_open(id, STONOWAIT, &sw); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 497 | if (fd < 0) { |
| 498 | error("sectok_open failed: %s", sectok_get_sw(sw)); |
| 499 | goto done; |
| 500 | } |
| 501 | if (! sectok_cardpresent(fd)) { |
Ben Lindstrom | 818659a | 2002-03-22 03:38:35 +0000 | [diff] [blame] | 502 | error("smartcard in reader %s not present", id); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 503 | goto done; |
| 504 | } |
| 505 | ret = sectok_reset(fd, 0, NULL, &sw); |
| 506 | if (ret <= 0) { |
| 507 | error("sectok_reset failed: %s", sectok_get_sw(sw)); |
| 508 | goto done; |
| 509 | } |
| 510 | if ((cla = cyberflex_inq_class(fd)) < 0) { |
| 511 | error("cyberflex_inq_class failed"); |
| 512 | goto done; |
| 513 | } |
| 514 | memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0)); |
| 515 | if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) { |
| 516 | if (get_AUT0(AUT0) < 0 || |
| 517 | cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) { |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 518 | memset(AUT0, 0, sizeof(DEFAUT0)); |
| 519 | error("smartcard passphrase incorrect"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 520 | goto done; |
| 521 | } |
| 522 | } |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 523 | memset(AUT0, 0, sizeof(DEFAUT0)); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 524 | key_fid[0] = 0x00; |
| 525 | key_fid[1] = 0x12; |
| 526 | if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements, |
| 527 | &sw) < 0) { |
| 528 | error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw)); |
| 529 | goto done; |
| 530 | } |
| 531 | if (!sectok_swOK(sw)) |
| 532 | goto done; |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 533 | logit("cyberflex_load_rsa_priv done"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 534 | key_fid[0] = 0x73; |
| 535 | key_fid[1] = 0x68; |
| 536 | if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5], |
| 537 | &sw) < 0) { |
| 538 | error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw)); |
| 539 | goto done; |
| 540 | } |
| 541 | if (!sectok_swOK(sw)) |
| 542 | goto done; |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 543 | logit("cyberflex_load_rsa_pub done"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 544 | status = 0; |
| 545 | |
| 546 | done: |
| 547 | memset(elements[0], '\0', BN_num_bytes(prv->rsa->q)); |
| 548 | memset(elements[1], '\0', BN_num_bytes(prv->rsa->p)); |
| 549 | memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp)); |
| 550 | memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1)); |
| 551 | memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1)); |
| 552 | memset(elements[5], '\0', BN_num_bytes(prv->rsa->n)); |
| 553 | |
| 554 | for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++) |
| 555 | if (elements[i]) |
| 556 | xfree(elements[i]); |
| 557 | if (fd != -1) |
| 558 | sectok_close(fd); |
| 559 | return (status); |
| 560 | } |
Damien Miller | 56a0bb0 | 2003-06-18 20:28:40 +1000 | [diff] [blame] | 561 | |
| 562 | char * |
| 563 | sc_get_key_label(Key *key) |
| 564 | { |
| 565 | return xstrdup("smartcard key"); |
| 566 | } |
| 567 | |
Ben Lindstrom | a42694f | 2002-04-05 16:11:45 +0000 | [diff] [blame] | 568 | #endif /* SMARTCARD && USE_SECTOK */ |