blob: dd2c28df250c13cd4b5f9a8c145341d010ef9f2e [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
29#include <openssl/evp.h>
30#include <openssl/x509.h>
31
Ben Lindstrom34b73202002-04-08 18:37:07 +000032#include <opensc/opensc.h>
33#include <opensc/pkcs15.h>
Ben Lindstroma42694f2002-04-05 16:11:45 +000034
35#include "key.h"
36#include "log.h"
37#include "xmalloc.h"
Darren Tuckerb6db1722004-05-13 17:29:35 +100038#include "misc.h"
Ben Lindstroma42694f2002-04-05 16:11:45 +000039#include "scard.h"
40
Ben Lindstrom8d601752002-04-06 18:19:38 +000041#if OPENSSL_VERSION_NUMBER < 0x00907000L && defined(CRYPTO_LOCK_ENGINE)
Ben Lindstroma42694f2002-04-05 16:11:45 +000042#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
54static int sc_reader_id;
55static sc_context_t *ctx = NULL;
56static sc_card_t *card = NULL;
57static sc_pkcs15_card_t *p15card = NULL;
58
59static char *sc_pin = NULL;
60
61struct sc_priv_data
62{
63 struct sc_pkcs15_id cert_id;
64 int ref_count;
65};
66
67void
68sc_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
Damien Millera8e06ce2003-11-21 23:48:55 +110084static int
Ben Lindstroma42694f2002-04-05 16:11:45 +000085sc_init(void)
86{
87 int r;
88
89 r = sc_establish_context(&ctx, "openssh");
90 if (r)
91 goto err;
Damien Miller723569b2003-06-28 18:08:16 +100092 if (sc_reader_id >= ctx->reader_count) {
93 r = SC_ERROR_NO_READERS_FOUND;
Damien Millera8e06ce2003-11-21 23:48:55 +110094 error("Illegal reader number %d (max %d)", sc_reader_id,
Damien Miller723569b2003-06-28 18:08:16 +100095 ctx->reader_count -1);
96 goto err;
97 }
Ben Lindstroma42694f2002-04-05 16:11:45 +000098 r = sc_connect_card(ctx->reader[sc_reader_id], 0, &card);
99 if (r)
100 goto err;
101 r = sc_pkcs15_bind(card, &p15card);
102 if (r)
103 goto err;
104 return 0;
105err:
106 sc_close();
107 return r;
108}
109
110/* private key operations */
111
112static int
Damien Miller331b6af2003-08-25 10:58:26 +1000113sc_prkey_op_init(RSA *rsa, struct sc_pkcs15_object **key_obj_out,
114 unsigned int usage)
Ben Lindstroma42694f2002-04-05 16:11:45 +0000115{
116 int r;
117 struct sc_priv_data *priv;
118 struct sc_pkcs15_object *key_obj;
119 struct sc_pkcs15_prkey_info *key;
120 struct sc_pkcs15_object *pin_obj;
121 struct sc_pkcs15_pin_info *pin;
122
123 priv = (struct sc_priv_data *) RSA_get_app_data(rsa);
124 if (priv == NULL)
125 return -1;
126 if (p15card == NULL) {
127 sc_close();
128 r = sc_init();
129 if (r) {
130 error("SmartCard init failed: %s", sc_strerror(r));
131 goto err;
132 }
133 }
Damien Millera8e06ce2003-11-21 23:48:55 +1100134 r = sc_pkcs15_find_prkey_by_id_usage(p15card, &priv->cert_id,
Damien Miller331b6af2003-08-25 10:58:26 +1000135 usage, &key_obj);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000136 if (r) {
137 error("Unable to find private key from SmartCard: %s",
138 sc_strerror(r));
139 goto err;
140 }
141 key = key_obj->data;
142 r = sc_pkcs15_find_pin_by_auth_id(p15card, &key_obj->auth_id,
143 &pin_obj);
Damien Miller2527f572003-06-04 19:22:06 +1000144 if (r == SC_ERROR_OBJECT_NOT_FOUND) {
145 /* no pin required */
Damien Miller76b5c8a2003-06-05 19:19:35 +1000146 r = sc_lock(card);
147 if (r) {
148 error("Unable to lock smartcard: %s", sc_strerror(r));
149 goto err;
150 }
Damien Miller2527f572003-06-04 19:22:06 +1000151 *key_obj_out = key_obj;
152 return 0;
153 } else if (r) {
Ben Lindstroma42694f2002-04-05 16:11:45 +0000154 error("Unable to find PIN object from SmartCard: %s",
155 sc_strerror(r));
156 goto err;
157 }
158 pin = pin_obj->data;
159 r = sc_lock(card);
160 if (r) {
161 error("Unable to lock smartcard: %s", sc_strerror(r));
162 goto err;
163 }
164 if (sc_pin != NULL) {
165 r = sc_pkcs15_verify_pin(p15card, pin, sc_pin,
166 strlen(sc_pin));
167 if (r) {
168 sc_unlock(card);
169 error("PIN code verification failed: %s",
170 sc_strerror(r));
171 goto err;
172 }
173 }
174 *key_obj_out = key_obj;
175 return 0;
176err:
177 sc_close();
178 return -1;
179}
180
Damien Miller331b6af2003-08-25 10:58:26 +1000181#define SC_USAGE_DECRYPT SC_PKCS15_PRKEY_USAGE_DECRYPT | \
182 SC_PKCS15_PRKEY_USAGE_UNWRAP
183
Ben Lindstroma42694f2002-04-05 16:11:45 +0000184static int
185sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
186 int padding)
187{
188 struct sc_pkcs15_object *key_obj;
189 int r;
190
191 if (padding != RSA_PKCS1_PADDING)
Damien Miller787b2ec2003-11-21 23:56:47 +1100192 return -1;
Damien Miller331b6af2003-08-25 10:58:26 +1000193 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_DECRYPT);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000194 if (r)
195 return -1;
Damien Millera8e06ce2003-11-21 23:48:55 +1100196 r = sc_pkcs15_decipher(p15card, key_obj, SC_ALGORITHM_RSA_PAD_PKCS1,
Damien Miller485397c2003-06-04 19:15:10 +1000197 from, flen, to, flen);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000198 sc_unlock(card);
199 if (r < 0) {
200 error("sc_pkcs15_decipher() failed: %s", sc_strerror(r));
201 goto err;
202 }
203 return r;
204err:
205 sc_close();
206 return -1;
207}
208
Damien Miller331b6af2003-08-25 10:58:26 +1000209#define SC_USAGE_SIGN SC_PKCS15_PRKEY_USAGE_SIGN | \
210 SC_PKCS15_PRKEY_USAGE_SIGNRECOVER
211
Ben Lindstroma42694f2002-04-05 16:11:45 +0000212static int
213sc_sign(int type, u_char *m, unsigned int m_len,
214 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
215{
216 struct sc_pkcs15_object *key_obj;
217 int r;
218 unsigned long flags = 0;
219
Damien Miller331b6af2003-08-25 10:58:26 +1000220 /* XXX: sc_prkey_op_init will search for a pkcs15 private
221 * key object with the sign or signrecover usage flag set.
222 * If the signing key has only the non-repudiation flag set
223 * the key will be rejected as using a non-repudiation key
224 * for authentication is not recommended. Note: This does not
225 * prevent the use of a non-repudiation key for authentication
Damien Millera8e06ce2003-11-21 23:48:55 +1100226 * if the sign or signrecover flag is set as well.
Damien Miller331b6af2003-08-25 10:58:26 +1000227 */
228 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_SIGN);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000229 if (r)
230 return -1;
231 /* FIXME: length of sigret correct? */
232 /* FIXME: check 'type' and modify flags accordingly */
233 flags = SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_SHA1;
234 r = sc_pkcs15_compute_signature(p15card, key_obj, flags,
235 m, m_len, sigret, RSA_size(rsa));
236 sc_unlock(card);
237 if (r < 0) {
238 error("sc_pkcs15_compute_signature() failed: %s",
239 sc_strerror(r));
240 goto err;
241 }
242 *siglen = r;
243 return 1;
244err:
245 sc_close();
246 return 0;
247}
248
249static int
250sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
251 int padding)
252{
253 error("Private key encryption not supported");
254 return -1;
255}
256
257/* called on free */
258
259static int (*orig_finish)(RSA *rsa) = NULL;
260
261static int
262sc_finish(RSA *rsa)
263{
264 struct sc_priv_data *priv;
265
266 priv = RSA_get_app_data(rsa);
267 priv->ref_count--;
268 if (priv->ref_count == 0) {
269 free(priv);
270 sc_close();
271 }
272 if (orig_finish)
273 orig_finish(rsa);
274 return 1;
275}
276
277/* engine for overloading private key operations */
278
279static RSA_METHOD *
280sc_get_rsa_method(void)
281{
282 static RSA_METHOD smart_rsa;
283 const RSA_METHOD *def = RSA_get_default_method();
284
285 /* use the OpenSSL version */
286 memcpy(&smart_rsa, def, sizeof(smart_rsa));
287
288 smart_rsa.name = "opensc";
289
290 /* overload */
291 smart_rsa.rsa_priv_enc = sc_private_encrypt;
292 smart_rsa.rsa_priv_dec = sc_private_decrypt;
293 smart_rsa.rsa_sign = sc_sign;
294
295 /* save original */
296 orig_finish = def->finish;
297 smart_rsa.finish = sc_finish;
298
299 return &smart_rsa;
300}
301
302#ifdef USE_ENGINE
303static ENGINE *
304sc_get_engine(void)
305{
306 static ENGINE *smart_engine = NULL;
307
308 if ((smart_engine = ENGINE_new()) == NULL)
309 fatal("ENGINE_new failed");
310
311 ENGINE_set_id(smart_engine, "opensc");
312 ENGINE_set_name(smart_engine, "OpenSC");
313
314 ENGINE_set_RSA(smart_engine, sc_get_rsa_method());
315 ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
316 ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
317 ENGINE_set_RAND(smart_engine, RAND_SSLeay());
318 ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
319
320 return smart_engine;
321}
322#endif
323
324static void
325convert_rsa_to_rsa1(Key * in, Key * out)
326{
327 struct sc_priv_data *priv;
Damien Miller787b2ec2003-11-21 23:56:47 +1100328
Ben Lindstroma42694f2002-04-05 16:11:45 +0000329 out->rsa->flags = in->rsa->flags;
330 out->flags = in->flags;
331 RSA_set_method(out->rsa, RSA_get_method(in->rsa));
332 BN_copy(out->rsa->n, in->rsa->n);
333 BN_copy(out->rsa->e, in->rsa->e);
334 priv = RSA_get_app_data(in->rsa);
335 priv->ref_count++;
336 RSA_set_app_data(out->rsa, priv);
337 return;
338}
339
Damien Millera8e06ce2003-11-21 23:48:55 +1100340static int
Ben Lindstroma42694f2002-04-05 16:11:45 +0000341sc_read_pubkey(Key * k, const struct sc_pkcs15_object *cert_obj)
342{
343 int r;
344 sc_pkcs15_cert_t *cert = NULL;
345 struct sc_priv_data *priv = NULL;
346 sc_pkcs15_cert_info_t *cinfo = cert_obj->data;
347
348 X509 *x509 = NULL;
349 EVP_PKEY *pubkey = NULL;
350 u8 *p;
351 char *tmp;
Damien Miller787b2ec2003-11-21 23:56:47 +1100352
Ben Lindstroma42694f2002-04-05 16:11:45 +0000353 debug("sc_read_pubkey() with cert id %02X", cinfo->id.value[0]);
354 r = sc_pkcs15_read_certificate(p15card, cinfo, &cert);
355 if (r) {
Damien Miller996acd22003-04-09 20:59:48 +1000356 logit("Certificate read failed: %s", sc_strerror(r));
Ben Lindstroma42694f2002-04-05 16:11:45 +0000357 goto err;
358 }
359 x509 = X509_new();
360 if (x509 == NULL) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100361 r = -1;
Ben Lindstroma42694f2002-04-05 16:11:45 +0000362 goto err;
363 }
364 p = cert->data;
365 if (!d2i_X509(&x509, &p, cert->data_len)) {
Damien Miller996acd22003-04-09 20:59:48 +1000366 logit("Unable to parse X.509 certificate");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000367 r = -1;
368 goto err;
369 }
370 sc_pkcs15_free_certificate(cert);
371 cert = NULL;
372 pubkey = X509_get_pubkey(x509);
373 X509_free(x509);
374 x509 = NULL;
375 if (pubkey->type != EVP_PKEY_RSA) {
Damien Miller996acd22003-04-09 20:59:48 +1000376 logit("Public key is of unknown type");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000377 r = -1;
378 goto err;
379 }
380 k->rsa = EVP_PKEY_get1_RSA(pubkey);
381 EVP_PKEY_free(pubkey);
382
383 k->rsa->flags |= RSA_FLAG_SIGN_VER;
384 RSA_set_method(k->rsa, sc_get_rsa_method());
385 priv = xmalloc(sizeof(struct sc_priv_data));
386 priv->cert_id = cinfo->id;
387 priv->ref_count = 1;
388 RSA_set_app_data(k->rsa, priv);
389
390 k->flags = KEY_FLAG_EXT;
391 tmp = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
392 debug("fingerprint %d %s", key_size(k), tmp);
393 xfree(tmp);
Damien Miller787b2ec2003-11-21 23:56:47 +1100394
Ben Lindstroma42694f2002-04-05 16:11:45 +0000395 return 0;
396err:
397 if (cert)
398 sc_pkcs15_free_certificate(cert);
399 if (pubkey)
400 EVP_PKEY_free(pubkey);
401 if (x509)
402 X509_free(x509);
403 return r;
404}
405
406Key **
407sc_get_keys(const char *id, const char *pin)
408{
409 Key *k, **keys;
410 int i, r, real_count = 0, key_count;
411 sc_pkcs15_id_t cert_id;
412 sc_pkcs15_object_t *certs[32];
413 char *buf = xstrdup(id), *p;
414
415 debug("sc_get_keys called: id = %s", id);
416
417 if (sc_pin != NULL)
418 xfree(sc_pin);
419 sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
420
421 cert_id.len = 0;
422 if ((p = strchr(buf, ':')) != NULL) {
423 *p = 0;
424 p++;
425 sc_pkcs15_hex_string_to_id(p, &cert_id);
426 }
427 r = sscanf(buf, "%d", &sc_reader_id);
428 xfree(buf);
429 if (r != 1)
430 goto err;
431 if (p15card == NULL) {
432 sc_close();
433 r = sc_init();
434 if (r) {
435 error("Smartcard init failed: %s", sc_strerror(r));
436 goto err;
437 }
438 }
439 if (cert_id.len) {
440 r = sc_pkcs15_find_cert_by_id(p15card, &cert_id, &certs[0]);
441 if (r < 0)
442 goto err;
443 key_count = 1;
444 } else {
445 r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_CERT_X509,
446 certs, 32);
447 if (r == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000448 logit("No certificates found on smartcard");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000449 r = -1;
450 goto err;
451 } else if (r < 0) {
452 error("Certificate enumeration failed: %s",
453 sc_strerror(r));
454 goto err;
455 }
456 key_count = r;
457 }
Ben Lindstroma42694f2002-04-05 16:11:45 +0000458 keys = xmalloc(sizeof(Key *) * (key_count*2+1));
459 for (i = 0; i < key_count; i++) {
Damien Miller76b5c8a2003-06-05 19:19:35 +1000460 sc_pkcs15_object_t *tmp_obj = NULL;
461 cert_id = ((sc_pkcs15_cert_info_t *)(certs[i]->data))->id;
462 if (sc_pkcs15_find_prkey_by_id(p15card, &cert_id, &tmp_obj))
463 /* skip the public key (certificate) if no
464 * corresponding private key is present */
465 continue;
Ben Lindstroma42694f2002-04-05 16:11:45 +0000466 k = key_new(KEY_RSA);
467 if (k == NULL)
468 break;
469 r = sc_read_pubkey(k, certs[i]);
470 if (r) {
471 error("sc_read_pubkey failed: %s", sc_strerror(r));
472 key_free(k);
473 continue;
474 }
475 keys[real_count] = k;
476 real_count++;
477 k = key_new(KEY_RSA1);
478 if (k == NULL)
479 break;
480 convert_rsa_to_rsa1(keys[real_count-1], k);
481 keys[real_count] = k;
482 real_count++;
483 }
484 keys[real_count] = NULL;
485
486 return keys;
487err:
488 sc_close();
489 return NULL;
490}
491
492int
493sc_put_key(Key *prv, const char *id)
494{
495 error("key uploading not yet supported");
496 return -1;
497}
498
Damien Miller4a8e2842003-06-28 18:02:47 +1000499char *
500sc_get_key_label(Key *key)
501{
502 int r;
503 const struct sc_priv_data *priv;
504 struct sc_pkcs15_object *key_obj;
505
506 priv = (const struct sc_priv_data *) RSA_get_app_data(key->rsa);
507 if (priv == NULL || p15card == NULL) {
508 logit("SmartCard key not loaded");
509 /* internal error => return default label */
510 return xstrdup("smartcard key");
511 }
512 r = sc_pkcs15_find_prkey_by_id(p15card, &priv->cert_id, &key_obj);
513 if (r) {
514 logit("Unable to find private key from SmartCard: %s",
515 sc_strerror(r));
516 return xstrdup("smartcard key");
517 }
518 if (key_obj == NULL || key_obj->label == NULL)
519 /* the optional PKCS#15 label does not exists
520 * => return the default label */
521 return xstrdup("smartcard key");
522 return xstrdup(key_obj->label);
523}
524
Ben Lindstroma42694f2002-04-05 16:11:45 +0000525#endif /* SMARTCARD */