]>
Commit | Line | Data |
---|---|---|
295c8801 | 1 | /* |
2 | * Copyright (c) 2002 Juha Yrjölä. All rights reserved. | |
3 | * Copyright (c) 2001 Markus Friedl. | |
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 | ||
26 | #include "includes.h" | |
27 | #if defined(SMARTCARD) && defined(USE_OPENSC) | |
28 | ||
29 | #include <openssl/evp.h> | |
30 | #include <openssl/x509.h> | |
31 | ||
46c8e0f6 | 32 | #include <opensc/opensc.h> |
33 | #include <opensc/pkcs15.h> | |
295c8801 | 34 | |
35 | #include "key.h" | |
36 | #include "log.h" | |
37 | #include "xmalloc.h" | |
38 | #include "readpass.h" | |
39 | #include "scard.h" | |
40 | ||
5866adb0 | 41 | #if OPENSSL_VERSION_NUMBER < 0x00907000L && defined(CRYPTO_LOCK_ENGINE) |
295c8801 | 42 | #define USE_ENGINE |
43 | #define RSA_get_default_method RSA_get_default_openssl_method | |
44 | #else | |
45 | #endif | |
46 | ||
47 | #ifdef USE_ENGINE | |
48 | #include <openssl/engine.h> | |
49 | #define sc_get_rsa sc_get_engine | |
50 | #else | |
51 | #define sc_get_rsa sc_get_rsa_method | |
52 | #endif | |
53 | ||
54 | static int sc_reader_id; | |
55 | static sc_context_t *ctx = NULL; | |
56 | static sc_card_t *card = NULL; | |
57 | static sc_pkcs15_card_t *p15card = NULL; | |
58 | ||
59 | static char *sc_pin = NULL; | |
60 | ||
61 | struct sc_priv_data | |
62 | { | |
63 | struct sc_pkcs15_id cert_id; | |
64 | int ref_count; | |
65 | }; | |
66 | ||
67 | void | |
68 | sc_close(void) | |
69 | { | |
70 | if (p15card) { | |
71 | sc_pkcs15_unbind(p15card); | |
72 | p15card = NULL; | |
73 | } | |
74 | if (card) { | |
75 | sc_disconnect_card(card, 0); | |
76 | card = NULL; | |
77 | } | |
78 | if (ctx) { | |
79 | sc_release_context(ctx); | |
80 | ctx = NULL; | |
81 | } | |
82 | } | |
83 | ||
84 | static int | |
85 | sc_init(void) | |
86 | { | |
87 | int r; | |
88 | ||
89 | r = sc_establish_context(&ctx, "openssh"); | |
90 | if (r) | |
91 | goto err; | |
92 | r = sc_connect_card(ctx->reader[sc_reader_id], 0, &card); | |
93 | if (r) | |
94 | goto err; | |
95 | r = sc_pkcs15_bind(card, &p15card); | |
96 | if (r) | |
97 | goto err; | |
98 | return 0; | |
99 | err: | |
100 | sc_close(); | |
101 | return r; | |
102 | } | |
103 | ||
104 | /* private key operations */ | |
105 | ||
106 | static int | |
107 | sc_prkey_op_init(RSA *rsa, struct sc_pkcs15_object **key_obj_out) | |
108 | { | |
109 | int r; | |
110 | struct sc_priv_data *priv; | |
111 | struct sc_pkcs15_object *key_obj; | |
112 | struct sc_pkcs15_prkey_info *key; | |
113 | struct sc_pkcs15_object *pin_obj; | |
114 | struct sc_pkcs15_pin_info *pin; | |
115 | ||
116 | priv = (struct sc_priv_data *) RSA_get_app_data(rsa); | |
117 | if (priv == NULL) | |
118 | return -1; | |
119 | if (p15card == NULL) { | |
120 | sc_close(); | |
121 | r = sc_init(); | |
122 | if (r) { | |
123 | error("SmartCard init failed: %s", sc_strerror(r)); | |
124 | goto err; | |
125 | } | |
126 | } | |
127 | r = sc_pkcs15_find_prkey_by_id(p15card, &priv->cert_id, &key_obj); | |
128 | if (r) { | |
129 | error("Unable to find private key from SmartCard: %s", | |
130 | sc_strerror(r)); | |
131 | goto err; | |
132 | } | |
133 | key = key_obj->data; | |
134 | r = sc_pkcs15_find_pin_by_auth_id(p15card, &key_obj->auth_id, | |
135 | &pin_obj); | |
11f1e60e | 136 | if (r == SC_ERROR_OBJECT_NOT_FOUND) { |
137 | /* no pin required */ | |
7b7f164b | 138 | r = sc_lock(card); |
139 | if (r) { | |
140 | error("Unable to lock smartcard: %s", sc_strerror(r)); | |
141 | goto err; | |
142 | } | |
11f1e60e | 143 | *key_obj_out = key_obj; |
144 | return 0; | |
145 | } else if (r) { | |
295c8801 | 146 | error("Unable to find PIN object from SmartCard: %s", |
147 | sc_strerror(r)); | |
148 | goto err; | |
149 | } | |
150 | pin = pin_obj->data; | |
151 | r = sc_lock(card); | |
152 | if (r) { | |
153 | error("Unable to lock smartcard: %s", sc_strerror(r)); | |
154 | goto err; | |
155 | } | |
156 | if (sc_pin != NULL) { | |
157 | r = sc_pkcs15_verify_pin(p15card, pin, sc_pin, | |
158 | strlen(sc_pin)); | |
159 | if (r) { | |
160 | sc_unlock(card); | |
161 | error("PIN code verification failed: %s", | |
162 | sc_strerror(r)); | |
163 | goto err; | |
164 | } | |
165 | } | |
166 | *key_obj_out = key_obj; | |
167 | return 0; | |
168 | err: | |
169 | sc_close(); | |
170 | return -1; | |
171 | } | |
172 | ||
173 | static int | |
174 | sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, | |
175 | int padding) | |
176 | { | |
177 | struct sc_pkcs15_object *key_obj; | |
178 | int r; | |
179 | ||
180 | if (padding != RSA_PKCS1_PADDING) | |
181 | return -1; | |
182 | r = sc_prkey_op_init(rsa, &key_obj); | |
183 | if (r) | |
184 | return -1; | |
8acdec60 | 185 | r = sc_pkcs15_decipher(p15card, key_obj, SC_ALGORITHM_RSA_PAD_PKCS1, |
186 | from, flen, to, flen); | |
295c8801 | 187 | sc_unlock(card); |
188 | if (r < 0) { | |
189 | error("sc_pkcs15_decipher() failed: %s", sc_strerror(r)); | |
190 | goto err; | |
191 | } | |
192 | return r; | |
193 | err: | |
194 | sc_close(); | |
195 | return -1; | |
196 | } | |
197 | ||
198 | static int | |
199 | sc_sign(int type, u_char *m, unsigned int m_len, | |
200 | unsigned char *sigret, unsigned int *siglen, RSA *rsa) | |
201 | { | |
202 | struct sc_pkcs15_object *key_obj; | |
203 | int r; | |
204 | unsigned long flags = 0; | |
205 | ||
206 | r = sc_prkey_op_init(rsa, &key_obj); | |
207 | if (r) | |
208 | return -1; | |
209 | /* FIXME: length of sigret correct? */ | |
210 | /* FIXME: check 'type' and modify flags accordingly */ | |
211 | flags = SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_SHA1; | |
212 | r = sc_pkcs15_compute_signature(p15card, key_obj, flags, | |
213 | m, m_len, sigret, RSA_size(rsa)); | |
214 | sc_unlock(card); | |
215 | if (r < 0) { | |
216 | error("sc_pkcs15_compute_signature() failed: %s", | |
217 | sc_strerror(r)); | |
218 | goto err; | |
219 | } | |
220 | *siglen = r; | |
221 | return 1; | |
222 | err: | |
223 | sc_close(); | |
224 | return 0; | |
225 | } | |
226 | ||
227 | static int | |
228 | sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, | |
229 | int padding) | |
230 | { | |
231 | error("Private key encryption not supported"); | |
232 | return -1; | |
233 | } | |
234 | ||
235 | /* called on free */ | |
236 | ||
237 | static int (*orig_finish)(RSA *rsa) = NULL; | |
238 | ||
239 | static int | |
240 | sc_finish(RSA *rsa) | |
241 | { | |
242 | struct sc_priv_data *priv; | |
243 | ||
244 | priv = RSA_get_app_data(rsa); | |
245 | priv->ref_count--; | |
246 | if (priv->ref_count == 0) { | |
247 | free(priv); | |
248 | sc_close(); | |
249 | } | |
250 | if (orig_finish) | |
251 | orig_finish(rsa); | |
252 | return 1; | |
253 | } | |
254 | ||
255 | /* engine for overloading private key operations */ | |
256 | ||
257 | static RSA_METHOD * | |
258 | sc_get_rsa_method(void) | |
259 | { | |
260 | static RSA_METHOD smart_rsa; | |
261 | const RSA_METHOD *def = RSA_get_default_method(); | |
262 | ||
263 | /* use the OpenSSL version */ | |
264 | memcpy(&smart_rsa, def, sizeof(smart_rsa)); | |
265 | ||
266 | smart_rsa.name = "opensc"; | |
267 | ||
268 | /* overload */ | |
269 | smart_rsa.rsa_priv_enc = sc_private_encrypt; | |
270 | smart_rsa.rsa_priv_dec = sc_private_decrypt; | |
271 | smart_rsa.rsa_sign = sc_sign; | |
272 | ||
273 | /* save original */ | |
274 | orig_finish = def->finish; | |
275 | smart_rsa.finish = sc_finish; | |
276 | ||
277 | return &smart_rsa; | |
278 | } | |
279 | ||
280 | #ifdef USE_ENGINE | |
281 | static ENGINE * | |
282 | sc_get_engine(void) | |
283 | { | |
284 | static ENGINE *smart_engine = NULL; | |
285 | ||
286 | if ((smart_engine = ENGINE_new()) == NULL) | |
287 | fatal("ENGINE_new failed"); | |
288 | ||
289 | ENGINE_set_id(smart_engine, "opensc"); | |
290 | ENGINE_set_name(smart_engine, "OpenSC"); | |
291 | ||
292 | ENGINE_set_RSA(smart_engine, sc_get_rsa_method()); | |
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 | #endif | |
301 | ||
302 | static void | |
303 | convert_rsa_to_rsa1(Key * in, Key * out) | |
304 | { | |
305 | struct sc_priv_data *priv; | |
306 | ||
307 | out->rsa->flags = in->rsa->flags; | |
308 | out->flags = in->flags; | |
309 | RSA_set_method(out->rsa, RSA_get_method(in->rsa)); | |
310 | BN_copy(out->rsa->n, in->rsa->n); | |
311 | BN_copy(out->rsa->e, in->rsa->e); | |
312 | priv = RSA_get_app_data(in->rsa); | |
313 | priv->ref_count++; | |
314 | RSA_set_app_data(out->rsa, priv); | |
315 | return; | |
316 | } | |
317 | ||
318 | static int | |
319 | sc_read_pubkey(Key * k, const struct sc_pkcs15_object *cert_obj) | |
320 | { | |
321 | int r; | |
322 | sc_pkcs15_cert_t *cert = NULL; | |
323 | struct sc_priv_data *priv = NULL; | |
324 | sc_pkcs15_cert_info_t *cinfo = cert_obj->data; | |
325 | ||
326 | X509 *x509 = NULL; | |
327 | EVP_PKEY *pubkey = NULL; | |
328 | u8 *p; | |
329 | char *tmp; | |
330 | ||
331 | debug("sc_read_pubkey() with cert id %02X", cinfo->id.value[0]); | |
332 | r = sc_pkcs15_read_certificate(p15card, cinfo, &cert); | |
333 | if (r) { | |
bbe88b6d | 334 | logit("Certificate read failed: %s", sc_strerror(r)); |
295c8801 | 335 | goto err; |
336 | } | |
337 | x509 = X509_new(); | |
338 | if (x509 == NULL) { | |
339 | r = -1; | |
340 | goto err; | |
341 | } | |
342 | p = cert->data; | |
343 | if (!d2i_X509(&x509, &p, cert->data_len)) { | |
bbe88b6d | 344 | logit("Unable to parse X.509 certificate"); |
295c8801 | 345 | r = -1; |
346 | goto err; | |
347 | } | |
348 | sc_pkcs15_free_certificate(cert); | |
349 | cert = NULL; | |
350 | pubkey = X509_get_pubkey(x509); | |
351 | X509_free(x509); | |
352 | x509 = NULL; | |
353 | if (pubkey->type != EVP_PKEY_RSA) { | |
bbe88b6d | 354 | logit("Public key is of unknown type"); |
295c8801 | 355 | r = -1; |
356 | goto err; | |
357 | } | |
358 | k->rsa = EVP_PKEY_get1_RSA(pubkey); | |
359 | EVP_PKEY_free(pubkey); | |
360 | ||
361 | k->rsa->flags |= RSA_FLAG_SIGN_VER; | |
362 | RSA_set_method(k->rsa, sc_get_rsa_method()); | |
363 | priv = xmalloc(sizeof(struct sc_priv_data)); | |
364 | priv->cert_id = cinfo->id; | |
365 | priv->ref_count = 1; | |
366 | RSA_set_app_data(k->rsa, priv); | |
367 | ||
368 | k->flags = KEY_FLAG_EXT; | |
369 | tmp = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX); | |
370 | debug("fingerprint %d %s", key_size(k), tmp); | |
371 | xfree(tmp); | |
372 | ||
373 | return 0; | |
374 | err: | |
375 | if (cert) | |
376 | sc_pkcs15_free_certificate(cert); | |
377 | if (pubkey) | |
378 | EVP_PKEY_free(pubkey); | |
379 | if (x509) | |
380 | X509_free(x509); | |
381 | return r; | |
382 | } | |
383 | ||
384 | Key ** | |
385 | sc_get_keys(const char *id, const char *pin) | |
386 | { | |
387 | Key *k, **keys; | |
388 | int i, r, real_count = 0, key_count; | |
389 | sc_pkcs15_id_t cert_id; | |
390 | sc_pkcs15_object_t *certs[32]; | |
391 | char *buf = xstrdup(id), *p; | |
392 | ||
393 | debug("sc_get_keys called: id = %s", id); | |
394 | ||
395 | if (sc_pin != NULL) | |
396 | xfree(sc_pin); | |
397 | sc_pin = (pin == NULL) ? NULL : xstrdup(pin); | |
398 | ||
399 | cert_id.len = 0; | |
400 | if ((p = strchr(buf, ':')) != NULL) { | |
401 | *p = 0; | |
402 | p++; | |
403 | sc_pkcs15_hex_string_to_id(p, &cert_id); | |
404 | } | |
405 | r = sscanf(buf, "%d", &sc_reader_id); | |
406 | xfree(buf); | |
407 | if (r != 1) | |
408 | goto err; | |
409 | if (p15card == NULL) { | |
410 | sc_close(); | |
411 | r = sc_init(); | |
412 | if (r) { | |
413 | error("Smartcard init failed: %s", sc_strerror(r)); | |
414 | goto err; | |
415 | } | |
416 | } | |
417 | if (cert_id.len) { | |
418 | r = sc_pkcs15_find_cert_by_id(p15card, &cert_id, &certs[0]); | |
419 | if (r < 0) | |
420 | goto err; | |
421 | key_count = 1; | |
422 | } else { | |
423 | r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_CERT_X509, | |
424 | certs, 32); | |
425 | if (r == 0) { | |
bbe88b6d | 426 | logit("No certificates found on smartcard"); |
295c8801 | 427 | r = -1; |
428 | goto err; | |
429 | } else if (r < 0) { | |
430 | error("Certificate enumeration failed: %s", | |
431 | sc_strerror(r)); | |
432 | goto err; | |
433 | } | |
434 | key_count = r; | |
435 | } | |
295c8801 | 436 | keys = xmalloc(sizeof(Key *) * (key_count*2+1)); |
437 | for (i = 0; i < key_count; i++) { | |
7b7f164b | 438 | sc_pkcs15_object_t *tmp_obj = NULL; |
439 | cert_id = ((sc_pkcs15_cert_info_t *)(certs[i]->data))->id; | |
440 | if (sc_pkcs15_find_prkey_by_id(p15card, &cert_id, &tmp_obj)) | |
441 | /* skip the public key (certificate) if no | |
442 | * corresponding private key is present */ | |
443 | continue; | |
295c8801 | 444 | k = key_new(KEY_RSA); |
445 | if (k == NULL) | |
446 | break; | |
447 | r = sc_read_pubkey(k, certs[i]); | |
448 | if (r) { | |
449 | error("sc_read_pubkey failed: %s", sc_strerror(r)); | |
450 | key_free(k); | |
451 | continue; | |
452 | } | |
453 | keys[real_count] = k; | |
454 | real_count++; | |
455 | k = key_new(KEY_RSA1); | |
456 | if (k == NULL) | |
457 | break; | |
458 | convert_rsa_to_rsa1(keys[real_count-1], k); | |
459 | keys[real_count] = k; | |
460 | real_count++; | |
461 | } | |
462 | keys[real_count] = NULL; | |
463 | ||
464 | return keys; | |
465 | err: | |
466 | sc_close(); | |
467 | return NULL; | |
468 | } | |
469 | ||
470 | int | |
471 | sc_put_key(Key *prv, const char *id) | |
472 | { | |
473 | error("key uploading not yet supported"); | |
474 | return -1; | |
475 | } | |
476 | ||
477 | #endif /* SMARTCARD */ |