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