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 | |
| 25 | #ifdef SMARTCARD |
| 26 | #include "includes.h" |
Ben Lindstrom | 94baf30 | 2001-08-06 21:25:38 +0000 | [diff] [blame] | 27 | RCSID("$OpenBSD: scard.c,v 1.9 2001/07/31 08:41:10 jakob Exp $"); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 28 | |
| 29 | #include <openssl/engine.h> |
| 30 | #include <sectok.h> |
| 31 | |
| 32 | #include "key.h" |
| 33 | #include "log.h" |
| 34 | #include "xmalloc.h" |
| 35 | #include "scard.h" |
| 36 | |
| 37 | #define CLA_SSH 0x05 |
| 38 | #define INS_DECRYPT 0x10 |
| 39 | #define INS_GET_KEYLENGTH 0x20 |
| 40 | #define INS_GET_PUBKEY 0x30 |
| 41 | #define INS_GET_RESPONSE 0xc0 |
| 42 | |
| 43 | #define MAX_BUF_SIZE 256 |
| 44 | |
| 45 | static int sc_fd = -1; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 46 | static int sc_reader_num = -1; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 47 | static int cla = 0x00; /* class */ |
| 48 | |
| 49 | /* interface to libsectok */ |
| 50 | |
| 51 | static int |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 52 | sc_open(void) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 53 | { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 54 | int sw; |
| 55 | |
| 56 | if (sc_fd >= 0) |
| 57 | return sc_fd; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 58 | |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 59 | sc_fd = sectok_open(sc_reader_num, STONOWAIT, &sw); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 60 | if (sc_fd < 0) { |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 61 | error("sectok_open failed: %s", sectok_get_sw(sw)); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 62 | return SCARD_ERROR_FAIL; |
| 63 | } |
| 64 | if (! sectok_cardpresent(sc_fd)) { |
Ben Lindstrom | 94baf30 | 2001-08-06 21:25:38 +0000 | [diff] [blame] | 65 | debug("smartcard in reader %d not present, skipping", |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 66 | sc_reader_num); |
| 67 | return SCARD_ERROR_NOCARD; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 68 | } |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 69 | if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 70 | error("sectok_reset failed: %s", sectok_get_sw(sw)); |
| 71 | sc_fd = -1; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 72 | return SCARD_ERROR_FAIL; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 73 | } |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 74 | if ((cla = cyberflex_inq_class(sc_fd)) < 0) |
| 75 | cla = 0; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 76 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 77 | debug("sc_open ok %d", sc_fd); |
| 78 | return sc_fd; |
| 79 | } |
| 80 | |
| 81 | static int |
| 82 | sc_enable_applet(void) |
| 83 | { |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 84 | static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e}; |
| 85 | int sw = 0; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 86 | |
Ben Lindstrom | 60df8e4 | 2001-08-06 21:10:52 +0000 | [diff] [blame] | 87 | /* select applet id */ |
| 88 | 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] | 89 | if (!sectok_swOK(sw)) { |
| 90 | error("sectok_apdu failed: %s", sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 91 | sc_close(); |
| 92 | return -1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int |
| 98 | sc_init(void) |
| 99 | { |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 100 | int status; |
| 101 | |
| 102 | status = sc_open(); |
| 103 | if (status == SCARD_ERROR_NOCARD) { |
| 104 | return SCARD_ERROR_NOCARD; |
| 105 | } |
| 106 | if (status < 0 ) { |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 107 | error("sc_open failed"); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 108 | return status; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 109 | } |
| 110 | if (sc_enable_applet() < 0) { |
| 111 | error("sc_enable_applet failed"); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 112 | return SCARD_ERROR_APPLET; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int |
| 118 | sc_read_pubkey(Key * k) |
| 119 | { |
| 120 | u_char buf[2], *n; |
| 121 | char *p; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 122 | int len, sw, status; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 123 | |
| 124 | len = sw = 0; |
| 125 | |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 126 | if (sc_fd < 0) { |
| 127 | status = sc_init(); |
| 128 | if (status < 0 ) |
| 129 | return status; |
| 130 | } |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 131 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 132 | /* get key size */ |
| 133 | sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL, |
| 134 | sizeof(buf), buf, &sw); |
| 135 | if (!sectok_swOK(sw)) { |
| 136 | error("could not obtain key length: %s", sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 137 | sc_close(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | len = (buf[0] << 8) | buf[1]; |
| 141 | len /= 8; |
| 142 | debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw)); |
| 143 | |
| 144 | n = xmalloc(len); |
| 145 | /* get n */ |
| 146 | sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw); |
| 147 | if (!sectok_swOK(sw)) { |
| 148 | error("could not obtain public key: %s", sectok_get_sw(sw)); |
| 149 | xfree(n); |
| 150 | return -1; |
| 151 | } |
| 152 | debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw)); |
| 153 | |
| 154 | if (BN_bin2bn(n, len, k->rsa->n) == NULL) { |
| 155 | error("c_read_pubkey: BN_bin2bn failed"); |
| 156 | xfree(n); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 157 | sc_close(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 158 | return -1; |
| 159 | } |
| 160 | xfree(n); |
| 161 | |
| 162 | /* currently the java applet just stores 'n' */ |
| 163 | if (!BN_set_word(k->rsa->e, 35)) { |
| 164 | error("c_read_pubkey: BN_set_word(e, 35) failed"); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX); |
| 169 | debug("fingerprint %d %s", key_size(k), p); |
| 170 | xfree(p); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* private key operations */ |
| 176 | |
| 177 | static int |
| 178 | sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding) |
| 179 | { |
| 180 | u_char *padded = NULL; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 181 | int sw, len, olen, status; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 182 | |
| 183 | debug("sc_private_decrypt called"); |
| 184 | |
| 185 | olen = len = sw = 0; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 186 | if (sc_fd < 0) { |
| 187 | status = sc_init(); |
| 188 | if (status < 0 ) |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 189 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 190 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 191 | if (padding != RSA_PKCS1_PADDING) |
| 192 | goto err; |
| 193 | |
| 194 | len = BN_num_bytes(rsa->n); |
| 195 | padded = xmalloc(len); |
| 196 | |
| 197 | sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, 0, NULL, &sw); |
| 198 | if (!sectok_swOK(sw)) { |
| 199 | error("sc_private_decrypt: INS_DECRYPT failed: %s", |
| 200 | sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 201 | sc_close(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 202 | goto err; |
| 203 | } |
| 204 | sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL, |
| 205 | len, padded, &sw); |
| 206 | if (!sectok_swOK(sw)) { |
| 207 | error("sc_private_decrypt: INS_GET_RESPONSE failed: %s", |
| 208 | sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 209 | sc_close(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 210 | goto err; |
| 211 | } |
| 212 | olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1, |
| 213 | len); |
| 214 | err: |
| 215 | if (padded) |
| 216 | xfree(padded); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 217 | return (olen >= 0 ? olen : status); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int |
| 221 | sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding) |
| 222 | { |
| 223 | u_char *padded = NULL; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 224 | int sw, len, status; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 225 | |
| 226 | len = sw = 0; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 227 | if (sc_fd < 0) { |
| 228 | status = sc_init(); |
| 229 | if (status < 0 ) |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 230 | goto err; |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 231 | } |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 232 | if (padding != RSA_PKCS1_PADDING) |
| 233 | goto err; |
| 234 | |
| 235 | debug("sc_private_encrypt called"); |
| 236 | len = BN_num_bytes(rsa->n); |
| 237 | padded = xmalloc(len); |
| 238 | |
| 239 | if (RSA_padding_add_PKCS1_type_1(padded, len, from, flen) <= 0) { |
| 240 | error("RSA_padding_add_PKCS1_type_1 failed"); |
| 241 | goto err; |
| 242 | } |
| 243 | sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, 0, NULL, &sw); |
| 244 | if (!sectok_swOK(sw)) { |
| 245 | error("sc_private_decrypt: INS_DECRYPT failed: %s", |
| 246 | sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 247 | sc_close(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 248 | goto err; |
| 249 | } |
| 250 | sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL, |
| 251 | len, to, &sw); |
| 252 | if (!sectok_swOK(sw)) { |
| 253 | error("sc_private_decrypt: INS_GET_RESPONSE failed: %s", |
| 254 | sectok_get_sw(sw)); |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 255 | sc_close(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 256 | goto err; |
| 257 | } |
| 258 | err: |
| 259 | if (padded) |
| 260 | xfree(padded); |
Ben Lindstrom | 30b00be | 2001-08-06 21:22:10 +0000 | [diff] [blame] | 261 | return (len >= 0 ? len : status); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* engine for overloading private key operations */ |
| 265 | |
| 266 | static ENGINE *smart_engine = NULL; |
| 267 | static RSA_METHOD smart_rsa = |
| 268 | { |
| 269 | "sectok", |
| 270 | NULL, |
| 271 | NULL, |
| 272 | NULL, |
| 273 | NULL, |
| 274 | NULL, |
| 275 | NULL, |
| 276 | NULL, |
| 277 | NULL, |
| 278 | 0, |
| 279 | NULL, |
| 280 | }; |
| 281 | |
| 282 | ENGINE * |
| 283 | sc_get_engine(void) |
| 284 | { |
| 285 | RSA_METHOD *def; |
| 286 | |
| 287 | def = RSA_get_default_openssl_method(); |
| 288 | |
| 289 | /* overload */ |
| 290 | smart_rsa.rsa_priv_enc = sc_private_encrypt; |
| 291 | smart_rsa.rsa_priv_dec = sc_private_decrypt; |
| 292 | |
| 293 | /* just use the OpenSSL version */ |
| 294 | smart_rsa.rsa_pub_enc = def->rsa_pub_enc; |
| 295 | smart_rsa.rsa_pub_dec = def->rsa_pub_dec; |
| 296 | smart_rsa.rsa_mod_exp = def->rsa_mod_exp; |
| 297 | smart_rsa.bn_mod_exp = def->bn_mod_exp; |
| 298 | smart_rsa.init = def->init; |
| 299 | smart_rsa.finish = def->finish; |
| 300 | smart_rsa.flags = def->flags; |
| 301 | smart_rsa.app_data = def->app_data; |
| 302 | smart_rsa.rsa_sign = def->rsa_sign; |
| 303 | smart_rsa.rsa_verify = def->rsa_verify; |
| 304 | |
| 305 | smart_engine = ENGINE_new(); |
| 306 | |
| 307 | ENGINE_set_id(smart_engine, "sectok"); |
| 308 | ENGINE_set_name(smart_engine, "libsectok"); |
| 309 | ENGINE_set_RSA(smart_engine, &smart_rsa); |
| 310 | ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method()); |
| 311 | ENGINE_set_DH(smart_engine, DH_get_default_openssl_method()); |
| 312 | ENGINE_set_RAND(smart_engine, RAND_SSLeay()); |
| 313 | ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp); |
| 314 | |
| 315 | return smart_engine; |
| 316 | } |
| 317 | |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 318 | void |
| 319 | sc_close(void) |
| 320 | { |
| 321 | if (sc_fd >= 0) { |
| 322 | sectok_close(sc_fd); |
| 323 | sc_fd = -1; |
| 324 | } |
| 325 | } |
| 326 | |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 327 | Key * |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 328 | sc_get_key(int num) |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 329 | { |
| 330 | Key *k; |
Ben Lindstrom | 94baf30 | 2001-08-06 21:25:38 +0000 | [diff] [blame] | 331 | int status; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 332 | |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 333 | sc_reader_num = num; |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 334 | k = key_new(KEY_RSA); |
| 335 | if (k == NULL) { |
| 336 | return NULL; |
| 337 | } |
Ben Lindstrom | 94baf30 | 2001-08-06 21:25:38 +0000 | [diff] [blame] | 338 | status = sc_read_pubkey(k); |
| 339 | if (status == SCARD_ERROR_NOCARD) { |
| 340 | key_free(k); |
| 341 | return NULL; |
| 342 | } |
| 343 | if (status < 0 ) { |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 344 | error("sc_read_pubkey failed"); |
| 345 | key_free(k); |
| 346 | return NULL; |
| 347 | } |
| 348 | return k; |
Damien Miller | 694be4b | 2001-07-14 12:13:26 +1000 | [diff] [blame] | 349 | sc_close(); |
Ben Lindstrom | 3133dbb | 2001-07-04 05:35:00 +0000 | [diff] [blame] | 350 | } |
| 351 | #endif |