Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 25 | #include "includes.h" |
Ben Lindstrom | a42694f | 2002-04-05 16:11:45 +0000 | [diff] [blame] | 26 | #if defined(SMARTCARD) && defined(USE_SECTOK) |
Ben Lindstrom | 818659a | 2002-03-22 03:38:35 +0000 | [diff] [blame] | 27 | |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 28 | #include <openssl/evp.h> |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 29 | #include <sectok.h> |
| 30 | |
| 31 | #include "key.h" |
| 32 | #include "log.h" |
| 33 | #include "xmalloc.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 34 | #include "misc.h" |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 35 | #include "scard.h" |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 36 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 37 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 38 | #define USE_ENGINE |
| 39 | #define RSA_get_default_method RSA_get_default_openssl_method |
| 40 | #else |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 41 | #endif |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 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 |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 58 | u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63}; |
| 59 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 60 | static int sc_fd = -1; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 61 | static char *sc_reader_id = NULL; |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 62 | static char *sc_pin = NULL; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 63 | static int cla = 0x00; /* class */ |
| 64 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 65 | static void sc_mk_digest(const char *pin, u_char *digest); |
| 66 | static int get_AUT0(u_char *aut0); |
Ben Lindstrom | 43a5e2f | 2002-03-27 17:33:17 +0000 | [diff] [blame] | 67 | static int try_AUT0(void); |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 68 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 69 | /* interface to libsectok */ |
| 70 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 71 | static int |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 72 | sc_open(void) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 73 | { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 74 | int sw; |
| 75 | |
| 76 | if (sc_fd >= 0) |
| 77 | return sc_fd; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 78 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 79 | sc_fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 80 | if (sc_fd < 0) { |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 81 | error("sectok_open failed: %s", sectok_get_sw(sw)); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 82 | return SCARD_ERROR_FAIL; |
| 83 | } |
| 84 | if (! sectok_cardpresent(sc_fd)) { |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 85 | debug("smartcard in reader %s not present, skipping", |
| 86 | sc_reader_id); |
Ben Lindstrom | 3ab1dfa | 2001-08-06 21:33:44 +0000 | [diff] [blame] | 87 | sc_close(); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 88 | return SCARD_ERROR_NOCARD; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 89 | } |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 90 | if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 91 | error("sectok_reset failed: %s", sectok_get_sw(sw)); |
| 92 | sc_fd = -1; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 93 | return SCARD_ERROR_FAIL; |
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 ((cla = cyberflex_inq_class(sc_fd)) < 0) |
| 96 | cla = 0; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 97 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 98 | debug("sc_open ok %d", sc_fd); |
| 99 | return sc_fd; |
| 100 | } |
| 101 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 102 | static int |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 103 | sc_enable_applet(void) |
| 104 | { |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 105 | static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e}; |
| 106 | int sw = 0; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 107 | |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 108 | /* select applet id */ |
| 109 | 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] | 110 | if (!sectok_swOK(sw)) { |
| 111 | error("sectok_apdu failed: %s", sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 112 | sc_close(); |
| 113 | return -1; |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 118 | static int |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 119 | sc_init(void) |
| 120 | { |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 121 | int status; |
| 122 | |
| 123 | status = sc_open(); |
| 124 | if (status == SCARD_ERROR_NOCARD) { |
| 125 | return SCARD_ERROR_NOCARD; |
| 126 | } |
| 127 | if (status < 0 ) { |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 128 | error("sc_open failed"); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 129 | return status; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 130 | } |
| 131 | if (sc_enable_applet() < 0) { |
| 132 | error("sc_enable_applet failed"); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 133 | return SCARD_ERROR_APPLET; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 138 | static int |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 139 | sc_read_pubkey(Key * k) |
| 140 | { |
| 141 | u_char buf[2], *n; |
| 142 | char *p; |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 143 | int len, sw, status = -1; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 144 | |
| 145 | len = sw = 0; |
Damien Miller | 3ff36d6 | 2001-09-28 19:51:54 +1000 | [diff] [blame] | 146 | n = NULL; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 147 | |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 148 | if (sc_fd < 0) { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 149 | if (sc_init() < 0) |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 150 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 151 | } |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 152 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 153 | /* get key size */ |
| 154 | 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] | 155 | sizeof(buf), buf, &sw); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 156 | if (!sectok_swOK(sw)) { |
| 157 | error("could not obtain key length: %s", sectok_get_sw(sw)); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 158 | goto err; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 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); |
Ben Lindstrom | 43a5e2f | 2002-03-27 17:33:17 +0000 | [diff] [blame] | 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 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 173 | if (!sectok_swOK(sw)) { |
| 174 | error("could not obtain public key: %s", sectok_get_sw(sw)); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 175 | goto err; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 176 | } |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 177 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 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"); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 182 | goto err; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 183 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 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"); |
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 | } |
| 190 | |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 191 | status = 0; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 192 | p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX); |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 193 | debug("fingerprint %u %s", key_size(k), p); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 194 | xfree(p); |
| 195 | |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 196 | err: |
| 197 | if (n != NULL) |
| 198 | xfree(n); |
| 199 | sc_close(); |
| 200 | return status; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* private key operations */ |
| 204 | |
| 205 | static int |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 206 | 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] | 207 | int padding) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 208 | { |
| 209 | u_char *padded = NULL; |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 210 | int sw, len, olen, status = -1; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 211 | |
| 212 | debug("sc_private_decrypt called"); |
| 213 | |
| 214 | olen = len = sw = 0; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 215 | if (sc_fd < 0) { |
| 216 | status = sc_init(); |
| 217 | if (status < 0 ) |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 218 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 219 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 220 | if (padding != RSA_PKCS1_PADDING) |
| 221 | goto err; |
| 222 | |
| 223 | len = BN_num_bytes(rsa->n); |
| 224 | padded = xmalloc(len); |
| 225 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 226 | sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, len, padded, &sw); |
| 227 | |
| 228 | if (sw == 0x6982) { |
Ben Lindstrom | c2c6cbc | 2002-03-26 02:44:44 +0000 | [diff] [blame] | 229 | if (try_AUT0() < 0) |
| 230 | goto err; |
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 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 233 | if (!sectok_swOK(sw)) { |
| 234 | error("sc_private_decrypt: INS_DECRYPT failed: %s", |
| 235 | sectok_get_sw(sw)); |
| 236 | goto err; |
| 237 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 238 | olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1, |
| 239 | len); |
| 240 | err: |
| 241 | if (padded) |
| 242 | xfree(padded); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 243 | sc_close(); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 244 | return (olen >= 0 ? olen : status); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 248 | 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] | 249 | int padding) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 250 | { |
| 251 | u_char *padded = NULL; |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 252 | int sw, len, status = -1; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 253 | |
| 254 | len = sw = 0; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 255 | if (sc_fd < 0) { |
| 256 | status = sc_init(); |
| 257 | if (status < 0 ) |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 258 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 259 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 267 | 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] | 268 | error("RSA_padding_add_PKCS1_type_1 failed"); |
| 269 | goto err; |
| 270 | } |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 271 | 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] | 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 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 277 | if (!sectok_swOK(sw)) { |
Ben Lindstrom | c2c6cbc | 2002-03-26 02:44:44 +0000 | [diff] [blame] | 278 | error("sc_private_encrypt: INS_DECRYPT failed: %s", |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 279 | sectok_get_sw(sw)); |
| 280 | goto err; |
| 281 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 282 | err: |
| 283 | if (padded) |
| 284 | xfree(padded); |
Ben Lindstrom | a2fec90 | 2001-09-18 05:45:44 +0000 | [diff] [blame] | 285 | sc_close(); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 286 | return (len >= 0 ? len : status); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Ben Lindstrom | a6c8a8d | 2001-08-06 21:42:00 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 302 | /* engine for overloading private key operations */ |
| 303 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 304 | static RSA_METHOD * |
| 305 | sc_get_rsa_method(void) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 306 | { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 307 | static RSA_METHOD smart_rsa; |
| 308 | const RSA_METHOD *def = RSA_get_default_method(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 309 | |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 310 | /* use the OpenSSL version */ |
| 311 | memcpy(&smart_rsa, def, sizeof(smart_rsa)); |
| 312 | |
| 313 | smart_rsa.name = "sectok"; |
| 314 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 315 | /* overload */ |
| 316 | smart_rsa.rsa_priv_enc = sc_private_encrypt; |
| 317 | smart_rsa.rsa_priv_dec = sc_private_decrypt; |
| 318 | |
Ben Lindstrom | a6c8a8d | 2001-08-06 21:42:00 +0000 | [diff] [blame] | 319 | /* save original */ |
| 320 | orig_finish = def->finish; |
| 321 | smart_rsa.finish = sc_finish; |
| 322 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 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 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 332 | if ((smart_engine = ENGINE_new()) == NULL) |
| 333 | fatal("ENGINE_new failed"); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 334 | |
| 335 | ENGINE_set_id(smart_engine, "sectok"); |
| 336 | ENGINE_set_name(smart_engine, "libsectok"); |
Ben Lindstrom | 0b675b1 | 2002-03-22 03:28:11 +0000 | [diff] [blame] | 337 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 338 | ENGINE_set_RSA(smart_engine, sc_get_rsa_method()); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 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 | } |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 346 | #endif |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 347 | |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 348 | void |
| 349 | sc_close(void) |
| 350 | { |
| 351 | if (sc_fd >= 0) { |
| 352 | sectok_close(sc_fd); |
| 353 | sc_fd = -1; |
| 354 | } |
| 355 | } |
| 356 | |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 357 | Key ** |
| 358 | sc_get_keys(const char *id, const char *pin) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 359 | { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 360 | Key *k, *n, **keys; |
| 361 | int status, nkeys = 2; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 362 | |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 363 | if (sc_reader_id != NULL) |
| 364 | xfree(sc_reader_id); |
| 365 | sc_reader_id = xstrdup(id); |
| 366 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 367 | if (sc_pin != NULL) |
| 368 | xfree(sc_pin); |
| 369 | sc_pin = (pin == NULL) ? NULL : xstrdup(pin); |
| 370 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 371 | k = key_new(KEY_RSA); |
| 372 | if (k == NULL) { |
| 373 | return NULL; |
| 374 | } |
Ben Lindstrom | 94baf30 | 2001-08-06 21:25:38 +0000 | [diff] [blame] | 375 | status = sc_read_pubkey(k); |
| 376 | if (status == SCARD_ERROR_NOCARD) { |
| 377 | key_free(k); |
| 378 | return NULL; |
| 379 | } |
| 380 | if (status < 0 ) { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 381 | error("sc_read_pubkey failed"); |
| 382 | key_free(k); |
| 383 | return NULL; |
| 384 | } |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 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; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 405 | } |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 417 | static void |
| 418 | sc_mk_digest(const char *pin, u_char *digest) |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 419 | { |
| 420 | const EVP_MD *evp_md = EVP_sha1(); |
| 421 | EVP_MD_CTX md; |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 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 | { |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 431 | char *pass; |
| 432 | |
| 433 | pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN); |
| 434 | if (pass == NULL) |
| 435 | return -1; |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 436 | if (!strcmp(pass, "-")) { |
| 437 | memcpy(aut0, DEFAUT0, sizeof DEFAUT0); |
| 438 | return 0; |
| 439 | } |
| 440 | sc_mk_digest(pass, aut0); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 441 | memset(pass, 0, strlen(pass)); |
| 442 | xfree(pass); |
| 443 | return 0; |
| 444 | } |
| 445 | |
Ben Lindstrom | 43a5e2f | 2002-03-27 17:33:17 +0000 | [diff] [blame] | 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); |
| 470 | } |
| 471 | |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 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]; |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 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); |
Ben Lindstrom | 818659a | 2002-03-22 03:38:35 +0000 | [diff] [blame] | 491 | fd = sectok_friendly_open(id, STONOWAIT, &sw); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 492 | if (fd < 0) { |
| 493 | error("sectok_open failed: %s", sectok_get_sw(sw)); |
| 494 | goto done; |
| 495 | } |
| 496 | if (! sectok_cardpresent(fd)) { |
Ben Lindstrom | 818659a | 2002-03-22 03:38:35 +0000 | [diff] [blame] | 497 | error("smartcard in reader %s not present", id); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 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) { |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 513 | memset(AUT0, 0, sizeof(DEFAUT0)); |
| 514 | error("smartcard passphrase incorrect"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 515 | goto done; |
| 516 | } |
| 517 | } |
Ben Lindstrom | 266ec63 | 2002-03-22 03:47:38 +0000 | [diff] [blame] | 518 | memset(AUT0, 0, sizeof(DEFAUT0)); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 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; |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 528 | logit("cyberflex_load_rsa_priv done"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 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; |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 538 | logit("cyberflex_load_rsa_pub done"); |
Ben Lindstrom | 70e3ad8 | 2002-03-22 03:33:43 +0000 | [diff] [blame] | 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); |
| 555 | } |
Damien Miller | 56a0bb0 | 2003-06-18 20:28:40 +1000 | [diff] [blame] | 556 | |
| 557 | char * |
| 558 | sc_get_key_label(Key *key) |
| 559 | { |
| 560 | return xstrdup("smartcard key"); |
| 561 | } |
| 562 | |
Ben Lindstrom | a42694f | 2002-04-05 16:11:45 +0000 | [diff] [blame] | 563 | #endif /* SMARTCARD && USE_SECTOK */ |