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