blob: 4751ea295ff2fb6a00e7c08fa459bb5fd4a91817 [file] [log] [blame]
Ben Lindstroma42694f2002-04-05 16:11:45 +00001/*
2 * Copyright (c) 2002 Juha Yrjölä. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl.
Damien Millera8e06ce2003-11-21 23:48:55 +11004 *
Ben Lindstroma42694f2002-04-05 16:11:45 +00005 * 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
Damien Millerded319c2006-09-01 15:38:36 +100029#include <sys/types.h>
30
Ben Lindstroma42694f2002-04-05 16:11:45 +000031#include <openssl/evp.h>
32#include <openssl/x509.h>
33
Damien Millerded319c2006-09-01 15:38:36 +100034#include <stdarg.h>
35
Ben Lindstrom34b73202002-04-08 18:37:07 +000036#include <opensc/opensc.h>
37#include <opensc/pkcs15.h>
Ben Lindstroma42694f2002-04-05 16:11:45 +000038
39#include "key.h"
40#include "log.h"
41#include "xmalloc.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100042#include "misc.h"
Ben Lindstroma42694f2002-04-05 16:11:45 +000043#include "scard.h"
44
Ben Lindstrom8d601752002-04-06 18:19:38 +000045#if OPENSSL_VERSION_NUMBER < 0x00907000L && defined(CRYPTO_LOCK_ENGINE)
Ben Lindstroma42694f2002-04-05 16:11:45 +000046#define USE_ENGINE
47#define RSA_get_default_method RSA_get_default_openssl_method
48#else
49#endif
50
51#ifdef USE_ENGINE
52#include <openssl/engine.h>
53#define sc_get_rsa sc_get_engine
54#else
55#define sc_get_rsa sc_get_rsa_method
56#endif
57
58static int sc_reader_id;
59static sc_context_t *ctx = NULL;
60static sc_card_t *card = NULL;
61static sc_pkcs15_card_t *p15card = NULL;
62
63static char *sc_pin = NULL;
64
65struct sc_priv_data
66{
67 struct sc_pkcs15_id cert_id;
68 int ref_count;
69};
70
71void
72sc_close(void)
73{
74 if (p15card) {
75 sc_pkcs15_unbind(p15card);
76 p15card = NULL;
77 }
78 if (card) {
79 sc_disconnect_card(card, 0);
80 card = NULL;
81 }
82 if (ctx) {
83 sc_release_context(ctx);
84 ctx = NULL;
85 }
86}
87
Damien Millera8e06ce2003-11-21 23:48:55 +110088static int
Ben Lindstroma42694f2002-04-05 16:11:45 +000089sc_init(void)
90{
91 int r;
92
93 r = sc_establish_context(&ctx, "openssh");
94 if (r)
95 goto err;
Damien Miller723569b2003-06-28 18:08:16 +100096 if (sc_reader_id >= ctx->reader_count) {
97 r = SC_ERROR_NO_READERS_FOUND;
Damien Millera8e06ce2003-11-21 23:48:55 +110098 error("Illegal reader number %d (max %d)", sc_reader_id,
Damien Miller723569b2003-06-28 18:08:16 +100099 ctx->reader_count -1);
100 goto err;
101 }
Ben Lindstroma42694f2002-04-05 16:11:45 +0000102 r = sc_connect_card(ctx->reader[sc_reader_id], 0, &card);
103 if (r)
104 goto err;
105 r = sc_pkcs15_bind(card, &p15card);
106 if (r)
107 goto err;
108 return 0;
109err:
110 sc_close();
111 return r;
112}
113
114/* private key operations */
115
116static int
Damien Miller331b6af2003-08-25 10:58:26 +1000117sc_prkey_op_init(RSA *rsa, struct sc_pkcs15_object **key_obj_out,
118 unsigned int usage)
Ben Lindstroma42694f2002-04-05 16:11:45 +0000119{
120 int r;
121 struct sc_priv_data *priv;
122 struct sc_pkcs15_object *key_obj;
123 struct sc_pkcs15_prkey_info *key;
124 struct sc_pkcs15_object *pin_obj;
125 struct sc_pkcs15_pin_info *pin;
126
127 priv = (struct sc_priv_data *) RSA_get_app_data(rsa);
128 if (priv == NULL)
129 return -1;
130 if (p15card == NULL) {
131 sc_close();
132 r = sc_init();
133 if (r) {
134 error("SmartCard init failed: %s", sc_strerror(r));
135 goto err;
136 }
137 }
Damien Millera8e06ce2003-11-21 23:48:55 +1100138 r = sc_pkcs15_find_prkey_by_id_usage(p15card, &priv->cert_id,
Damien Miller331b6af2003-08-25 10:58:26 +1000139 usage, &key_obj);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000140 if (r) {
141 error("Unable to find private key from SmartCard: %s",
142 sc_strerror(r));
143 goto err;
144 }
145 key = key_obj->data;
146 r = sc_pkcs15_find_pin_by_auth_id(p15card, &key_obj->auth_id,
147 &pin_obj);
Damien Miller2527f572003-06-04 19:22:06 +1000148 if (r == SC_ERROR_OBJECT_NOT_FOUND) {
149 /* no pin required */
Damien Miller76b5c8a2003-06-05 19:19:35 +1000150 r = sc_lock(card);
151 if (r) {
152 error("Unable to lock smartcard: %s", sc_strerror(r));
153 goto err;
154 }
Damien Miller2527f572003-06-04 19:22:06 +1000155 *key_obj_out = key_obj;
156 return 0;
157 } else if (r) {
Ben Lindstroma42694f2002-04-05 16:11:45 +0000158 error("Unable to find PIN object from SmartCard: %s",
159 sc_strerror(r));
160 goto err;
161 }
162 pin = pin_obj->data;
163 r = sc_lock(card);
164 if (r) {
165 error("Unable to lock smartcard: %s", sc_strerror(r));
166 goto err;
167 }
168 if (sc_pin != NULL) {
169 r = sc_pkcs15_verify_pin(p15card, pin, sc_pin,
170 strlen(sc_pin));
171 if (r) {
172 sc_unlock(card);
173 error("PIN code verification failed: %s",
174 sc_strerror(r));
175 goto err;
176 }
177 }
178 *key_obj_out = key_obj;
179 return 0;
180err:
181 sc_close();
182 return -1;
183}
184
Damien Miller331b6af2003-08-25 10:58:26 +1000185#define SC_USAGE_DECRYPT SC_PKCS15_PRKEY_USAGE_DECRYPT | \
186 SC_PKCS15_PRKEY_USAGE_UNWRAP
187
Ben Lindstroma42694f2002-04-05 16:11:45 +0000188static int
189sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
190 int padding)
191{
192 struct sc_pkcs15_object *key_obj;
193 int r;
194
195 if (padding != RSA_PKCS1_PADDING)
Damien Miller787b2ec2003-11-21 23:56:47 +1100196 return -1;
Damien Miller331b6af2003-08-25 10:58:26 +1000197 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_DECRYPT);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000198 if (r)
199 return -1;
Damien Millera8e06ce2003-11-21 23:48:55 +1100200 r = sc_pkcs15_decipher(p15card, key_obj, SC_ALGORITHM_RSA_PAD_PKCS1,
Damien Miller485397c2003-06-04 19:15:10 +1000201 from, flen, to, flen);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000202 sc_unlock(card);
203 if (r < 0) {
204 error("sc_pkcs15_decipher() failed: %s", sc_strerror(r));
205 goto err;
206 }
207 return r;
208err:
209 sc_close();
210 return -1;
211}
212
Damien Miller331b6af2003-08-25 10:58:26 +1000213#define SC_USAGE_SIGN SC_PKCS15_PRKEY_USAGE_SIGN | \
214 SC_PKCS15_PRKEY_USAGE_SIGNRECOVER
215
Ben Lindstroma42694f2002-04-05 16:11:45 +0000216static int
217sc_sign(int type, u_char *m, unsigned int m_len,
218 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
219{
220 struct sc_pkcs15_object *key_obj;
221 int r;
222 unsigned long flags = 0;
223
Damien Miller331b6af2003-08-25 10:58:26 +1000224 /* XXX: sc_prkey_op_init will search for a pkcs15 private
225 * key object with the sign or signrecover usage flag set.
226 * If the signing key has only the non-repudiation flag set
227 * the key will be rejected as using a non-repudiation key
228 * for authentication is not recommended. Note: This does not
229 * prevent the use of a non-repudiation key for authentication
Damien Millera8e06ce2003-11-21 23:48:55 +1100230 * if the sign or signrecover flag is set as well.
Damien Miller331b6af2003-08-25 10:58:26 +1000231 */
232 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_SIGN);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000233 if (r)
234 return -1;
235 /* FIXME: length of sigret correct? */
236 /* FIXME: check 'type' and modify flags accordingly */
237 flags = SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_SHA1;
238 r = sc_pkcs15_compute_signature(p15card, key_obj, flags,
239 m, m_len, sigret, RSA_size(rsa));
240 sc_unlock(card);
241 if (r < 0) {
242 error("sc_pkcs15_compute_signature() failed: %s",
243 sc_strerror(r));
244 goto err;
245 }
246 *siglen = r;
247 return 1;
248err:
249 sc_close();
250 return 0;
251}
252
253static int
254sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
255 int padding)
256{
257 error("Private key encryption not supported");
258 return -1;
259}
260
261/* called on free */
262
263static int (*orig_finish)(RSA *rsa) = NULL;
264
265static int
266sc_finish(RSA *rsa)
267{
268 struct sc_priv_data *priv;
269
270 priv = RSA_get_app_data(rsa);
271 priv->ref_count--;
272 if (priv->ref_count == 0) {
273 free(priv);
274 sc_close();
275 }
276 if (orig_finish)
277 orig_finish(rsa);
278 return 1;
279}
280
281/* engine for overloading private key operations */
282
283static RSA_METHOD *
284sc_get_rsa_method(void)
285{
286 static RSA_METHOD smart_rsa;
287 const RSA_METHOD *def = RSA_get_default_method();
288
289 /* use the OpenSSL version */
290 memcpy(&smart_rsa, def, sizeof(smart_rsa));
291
292 smart_rsa.name = "opensc";
293
294 /* overload */
295 smart_rsa.rsa_priv_enc = sc_private_encrypt;
296 smart_rsa.rsa_priv_dec = sc_private_decrypt;
297 smart_rsa.rsa_sign = sc_sign;
298
299 /* save original */
300 orig_finish = def->finish;
301 smart_rsa.finish = sc_finish;
302
303 return &smart_rsa;
304}
305
306#ifdef USE_ENGINE
307static ENGINE *
308sc_get_engine(void)
309{
310 static ENGINE *smart_engine = NULL;
311
312 if ((smart_engine = ENGINE_new()) == NULL)
313 fatal("ENGINE_new failed");
314
315 ENGINE_set_id(smart_engine, "opensc");
316 ENGINE_set_name(smart_engine, "OpenSC");
317
318 ENGINE_set_RSA(smart_engine, sc_get_rsa_method());
319 ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
320 ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
321 ENGINE_set_RAND(smart_engine, RAND_SSLeay());
322 ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
323
324 return smart_engine;
325}
326#endif
327
328static void
329convert_rsa_to_rsa1(Key * in, Key * out)
330{
331 struct sc_priv_data *priv;
Damien Miller787b2ec2003-11-21 23:56:47 +1100332
Ben Lindstroma42694f2002-04-05 16:11:45 +0000333 out->rsa->flags = in->rsa->flags;
334 out->flags = in->flags;
335 RSA_set_method(out->rsa, RSA_get_method(in->rsa));
336 BN_copy(out->rsa->n, in->rsa->n);
337 BN_copy(out->rsa->e, in->rsa->e);
338 priv = RSA_get_app_data(in->rsa);
339 priv->ref_count++;
340 RSA_set_app_data(out->rsa, priv);
341 return;
342}
343
Damien Millera8e06ce2003-11-21 23:48:55 +1100344static int
Ben Lindstroma42694f2002-04-05 16:11:45 +0000345sc_read_pubkey(Key * k, const struct sc_pkcs15_object *cert_obj)
346{
347 int r;
348 sc_pkcs15_cert_t *cert = NULL;
349 struct sc_priv_data *priv = NULL;
350 sc_pkcs15_cert_info_t *cinfo = cert_obj->data;
351
352 X509 *x509 = NULL;
353 EVP_PKEY *pubkey = NULL;
354 u8 *p;
355 char *tmp;
Damien Miller787b2ec2003-11-21 23:56:47 +1100356
Ben Lindstroma42694f2002-04-05 16:11:45 +0000357 debug("sc_read_pubkey() with cert id %02X", cinfo->id.value[0]);
358 r = sc_pkcs15_read_certificate(p15card, cinfo, &cert);
359 if (r) {
Damien Miller996acd22003-04-09 20:59:48 +1000360 logit("Certificate read failed: %s", sc_strerror(r));
Ben Lindstroma42694f2002-04-05 16:11:45 +0000361 goto err;
362 }
363 x509 = X509_new();
364 if (x509 == NULL) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100365 r = -1;
Ben Lindstroma42694f2002-04-05 16:11:45 +0000366 goto err;
367 }
368 p = cert->data;
369 if (!d2i_X509(&x509, &p, cert->data_len)) {
Damien Miller996acd22003-04-09 20:59:48 +1000370 logit("Unable to parse X.509 certificate");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000371 r = -1;
372 goto err;
373 }
374 sc_pkcs15_free_certificate(cert);
375 cert = NULL;
376 pubkey = X509_get_pubkey(x509);
377 X509_free(x509);
378 x509 = NULL;
379 if (pubkey->type != EVP_PKEY_RSA) {
Damien Miller996acd22003-04-09 20:59:48 +1000380 logit("Public key is of unknown type");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000381 r = -1;
382 goto err;
383 }
384 k->rsa = EVP_PKEY_get1_RSA(pubkey);
385 EVP_PKEY_free(pubkey);
386
387 k->rsa->flags |= RSA_FLAG_SIGN_VER;
388 RSA_set_method(k->rsa, sc_get_rsa_method());
389 priv = xmalloc(sizeof(struct sc_priv_data));
390 priv->cert_id = cinfo->id;
391 priv->ref_count = 1;
392 RSA_set_app_data(k->rsa, priv);
393
394 k->flags = KEY_FLAG_EXT;
395 tmp = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
396 debug("fingerprint %d %s", key_size(k), tmp);
397 xfree(tmp);
Damien Miller787b2ec2003-11-21 23:56:47 +1100398
Ben Lindstroma42694f2002-04-05 16:11:45 +0000399 return 0;
400err:
401 if (cert)
402 sc_pkcs15_free_certificate(cert);
403 if (pubkey)
404 EVP_PKEY_free(pubkey);
405 if (x509)
406 X509_free(x509);
407 return r;
408}
409
410Key **
411sc_get_keys(const char *id, const char *pin)
412{
413 Key *k, **keys;
414 int i, r, real_count = 0, key_count;
415 sc_pkcs15_id_t cert_id;
416 sc_pkcs15_object_t *certs[32];
417 char *buf = xstrdup(id), *p;
418
419 debug("sc_get_keys called: id = %s", id);
420
421 if (sc_pin != NULL)
422 xfree(sc_pin);
423 sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
424
425 cert_id.len = 0;
426 if ((p = strchr(buf, ':')) != NULL) {
427 *p = 0;
428 p++;
429 sc_pkcs15_hex_string_to_id(p, &cert_id);
430 }
431 r = sscanf(buf, "%d", &sc_reader_id);
432 xfree(buf);
433 if (r != 1)
434 goto err;
435 if (p15card == NULL) {
436 sc_close();
437 r = sc_init();
438 if (r) {
439 error("Smartcard init failed: %s", sc_strerror(r));
440 goto err;
441 }
442 }
443 if (cert_id.len) {
444 r = sc_pkcs15_find_cert_by_id(p15card, &cert_id, &certs[0]);
445 if (r < 0)
446 goto err;
447 key_count = 1;
448 } else {
449 r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_CERT_X509,
450 certs, 32);
451 if (r == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000452 logit("No certificates found on smartcard");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000453 r = -1;
454 goto err;
455 } else if (r < 0) {
456 error("Certificate enumeration failed: %s",
457 sc_strerror(r));
458 goto err;
459 }
460 key_count = r;
461 }
Darren Tuckerd8093e42006-05-04 16:24:34 +1000462 if (key_count > 1024)
463 fatal("Too many keys (%u), expected <= 1024", key_count);
464 keys = xcalloc(key_count * 2 + 1, sizeof(Key *));
Ben Lindstroma42694f2002-04-05 16:11:45 +0000465 for (i = 0; i < key_count; i++) {
Damien Miller76b5c8a2003-06-05 19:19:35 +1000466 sc_pkcs15_object_t *tmp_obj = NULL;
467 cert_id = ((sc_pkcs15_cert_info_t *)(certs[i]->data))->id;
468 if (sc_pkcs15_find_prkey_by_id(p15card, &cert_id, &tmp_obj))
469 /* skip the public key (certificate) if no
470 * corresponding private key is present */
471 continue;
Ben Lindstroma42694f2002-04-05 16:11:45 +0000472 k = key_new(KEY_RSA);
473 if (k == NULL)
474 break;
475 r = sc_read_pubkey(k, certs[i]);
476 if (r) {
477 error("sc_read_pubkey failed: %s", sc_strerror(r));
478 key_free(k);
479 continue;
480 }
481 keys[real_count] = k;
482 real_count++;
483 k = key_new(KEY_RSA1);
484 if (k == NULL)
485 break;
486 convert_rsa_to_rsa1(keys[real_count-1], k);
487 keys[real_count] = k;
488 real_count++;
489 }
490 keys[real_count] = NULL;
491
492 return keys;
493err:
494 sc_close();
495 return NULL;
496}
497
498int
499sc_put_key(Key *prv, const char *id)
500{
501 error("key uploading not yet supported");
502 return -1;
503}
504
Damien Miller4a8e2842003-06-28 18:02:47 +1000505char *
506sc_get_key_label(Key *key)
507{
508 int r;
509 const struct sc_priv_data *priv;
510 struct sc_pkcs15_object *key_obj;
511
512 priv = (const struct sc_priv_data *) RSA_get_app_data(key->rsa);
513 if (priv == NULL || p15card == NULL) {
514 logit("SmartCard key not loaded");
515 /* internal error => return default label */
516 return xstrdup("smartcard key");
517 }
518 r = sc_pkcs15_find_prkey_by_id(p15card, &priv->cert_id, &key_obj);
519 if (r) {
520 logit("Unable to find private key from SmartCard: %s",
521 sc_strerror(r));
522 return xstrdup("smartcard key");
523 }
524 if (key_obj == NULL || key_obj->label == NULL)
525 /* the optional PKCS#15 label does not exists
526 * => return the default label */
527 return xstrdup("smartcard key");
528 return xstrdup(key_obj->label);
529}
530
Ben Lindstroma42694f2002-04-05 16:11:45 +0000531#endif /* SMARTCARD */