- (djm) [session.c]
[openssh-git.git] / scard-opensc.c
blob7a496dff8c6883ef80bb999236fb499ed692b770
1 /*
2 * Copyright (c) 2002 Juha Yrjölä. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl.
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.
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.
26 #include "includes.h"
27 #if defined(SMARTCARD) && defined(USE_OPENSC)
29 #include <openssl/evp.h>
30 #include <openssl/x509.h>
32 #include <opensc/opensc.h>
33 #include <opensc/pkcs15.h>
35 #include "key.h"
36 #include "log.h"
37 #include "xmalloc.h"
38 #include "misc.h"
39 #include "scard.h"
41 #if OPENSSL_VERSION_NUMBER < 0x00907000L && defined(CRYPTO_LOCK_ENGINE)
42 #define USE_ENGINE
43 #define RSA_get_default_method RSA_get_default_openssl_method
44 #else
45 #endif
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
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;
59 static char *sc_pin = NULL;
61 struct sc_priv_data
63 struct sc_pkcs15_id cert_id;
64 int ref_count;
67 void
68 sc_close(void)
70 if (p15card) {
71 sc_pkcs15_unbind(p15card);
72 p15card = NULL;
74 if (card) {
75 sc_disconnect_card(card, 0);
76 card = NULL;
78 if (ctx) {
79 sc_release_context(ctx);
80 ctx = NULL;
84 static int
85 sc_init(void)
87 int r;
89 r = sc_establish_context(&ctx, "openssh");
90 if (r)
91 goto err;
92 if (sc_reader_id >= ctx->reader_count) {
93 r = SC_ERROR_NO_READERS_FOUND;
94 error("Illegal reader number %d (max %d)", sc_reader_id,
95 ctx->reader_count -1);
96 goto err;
98 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;
105 err:
106 sc_close();
107 return r;
110 /* private key operations */
112 static int
113 sc_prkey_op_init(RSA *rsa, struct sc_pkcs15_object **key_obj_out,
114 unsigned int usage)
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;
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;
134 r = sc_pkcs15_find_prkey_by_id_usage(p15card, &priv->cert_id,
135 usage, &key_obj);
136 if (r) {
137 error("Unable to find private key from SmartCard: %s",
138 sc_strerror(r));
139 goto err;
141 key = key_obj->data;
142 r = sc_pkcs15_find_pin_by_auth_id(p15card, &key_obj->auth_id,
143 &pin_obj);
144 if (r == SC_ERROR_OBJECT_NOT_FOUND) {
145 /* no pin required */
146 r = sc_lock(card);
147 if (r) {
148 error("Unable to lock smartcard: %s", sc_strerror(r));
149 goto err;
151 *key_obj_out = key_obj;
152 return 0;
153 } else if (r) {
154 error("Unable to find PIN object from SmartCard: %s",
155 sc_strerror(r));
156 goto err;
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;
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;
174 *key_obj_out = key_obj;
175 return 0;
176 err:
177 sc_close();
178 return -1;
181 #define SC_USAGE_DECRYPT SC_PKCS15_PRKEY_USAGE_DECRYPT | \
182 SC_PKCS15_PRKEY_USAGE_UNWRAP
184 static int
185 sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
186 int padding)
188 struct sc_pkcs15_object *key_obj;
189 int r;
191 if (padding != RSA_PKCS1_PADDING)
192 return -1;
193 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_DECRYPT);
194 if (r)
195 return -1;
196 r = sc_pkcs15_decipher(p15card, key_obj, SC_ALGORITHM_RSA_PAD_PKCS1,
197 from, flen, to, flen);
198 sc_unlock(card);
199 if (r < 0) {
200 error("sc_pkcs15_decipher() failed: %s", sc_strerror(r));
201 goto err;
203 return r;
204 err:
205 sc_close();
206 return -1;
209 #define SC_USAGE_SIGN SC_PKCS15_PRKEY_USAGE_SIGN | \
210 SC_PKCS15_PRKEY_USAGE_SIGNRECOVER
212 static int
213 sc_sign(int type, u_char *m, unsigned int m_len,
214 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
216 struct sc_pkcs15_object *key_obj;
217 int r;
218 unsigned long flags = 0;
220 /* 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
226 * if the sign or signrecover flag is set as well.
228 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_SIGN);
229 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;
242 *siglen = r;
243 return 1;
244 err:
245 sc_close();
246 return 0;
249 static int
250 sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
251 int padding)
253 error("Private key encryption not supported");
254 return -1;
257 /* called on free */
259 static int (*orig_finish)(RSA *rsa) = NULL;
261 static int
262 sc_finish(RSA *rsa)
264 struct sc_priv_data *priv;
266 priv = RSA_get_app_data(rsa);
267 priv->ref_count--;
268 if (priv->ref_count == 0) {
269 free(priv);
270 sc_close();
272 if (orig_finish)
273 orig_finish(rsa);
274 return 1;
277 /* engine for overloading private key operations */
279 static RSA_METHOD *
280 sc_get_rsa_method(void)
282 static RSA_METHOD smart_rsa;
283 const RSA_METHOD *def = RSA_get_default_method();
285 /* use the OpenSSL version */
286 memcpy(&smart_rsa, def, sizeof(smart_rsa));
288 smart_rsa.name = "opensc";
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;
295 /* save original */
296 orig_finish = def->finish;
297 smart_rsa.finish = sc_finish;
299 return &smart_rsa;
302 #ifdef USE_ENGINE
303 static ENGINE *
304 sc_get_engine(void)
306 static ENGINE *smart_engine = NULL;
308 if ((smart_engine = ENGINE_new()) == NULL)
309 fatal("ENGINE_new failed");
311 ENGINE_set_id(smart_engine, "opensc");
312 ENGINE_set_name(smart_engine, "OpenSC");
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);
320 return smart_engine;
322 #endif
324 static void
325 convert_rsa_to_rsa1(Key * in, Key * out)
327 struct sc_priv_data *priv;
329 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;
340 static int
341 sc_read_pubkey(Key * k, const struct sc_pkcs15_object *cert_obj)
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;
348 X509 *x509 = NULL;
349 EVP_PKEY *pubkey = NULL;
350 u8 *p;
351 char *tmp;
353 debug("sc_read_pubkey() with cert id %02X", cinfo->id.value[0]);
354 r = sc_pkcs15_read_certificate(p15card, cinfo, &cert);
355 if (r) {
356 logit("Certificate read failed: %s", sc_strerror(r));
357 goto err;
359 x509 = X509_new();
360 if (x509 == NULL) {
361 r = -1;
362 goto err;
364 p = cert->data;
365 if (!d2i_X509(&x509, &p, cert->data_len)) {
366 logit("Unable to parse X.509 certificate");
367 r = -1;
368 goto err;
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) {
376 logit("Public key is of unknown type");
377 r = -1;
378 goto err;
380 k->rsa = EVP_PKEY_get1_RSA(pubkey);
381 EVP_PKEY_free(pubkey);
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);
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);
395 return 0;
396 err:
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;
406 Key **
407 sc_get_keys(const char *id, const char *pin)
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;
415 debug("sc_get_keys called: id = %s", id);
417 if (sc_pin != NULL)
418 xfree(sc_pin);
419 sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
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);
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;
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) {
448 logit("No certificates found on smartcard");
449 r = -1;
450 goto err;
451 } else if (r < 0) {
452 error("Certificate enumeration failed: %s",
453 sc_strerror(r));
454 goto err;
456 key_count = r;
458 if (key_count > 1024)
459 fatal("Too many keys (%u), expected <= 1024", key_count);
460 keys = xcalloc(key_count * 2 + 1, sizeof(Key *));
461 for (i = 0; i < key_count; i++) {
462 sc_pkcs15_object_t *tmp_obj = NULL;
463 cert_id = ((sc_pkcs15_cert_info_t *)(certs[i]->data))->id;
464 if (sc_pkcs15_find_prkey_by_id(p15card, &cert_id, &tmp_obj))
465 /* skip the public key (certificate) if no
466 * corresponding private key is present */
467 continue;
468 k = key_new(KEY_RSA);
469 if (k == NULL)
470 break;
471 r = sc_read_pubkey(k, certs[i]);
472 if (r) {
473 error("sc_read_pubkey failed: %s", sc_strerror(r));
474 key_free(k);
475 continue;
477 keys[real_count] = k;
478 real_count++;
479 k = key_new(KEY_RSA1);
480 if (k == NULL)
481 break;
482 convert_rsa_to_rsa1(keys[real_count-1], k);
483 keys[real_count] = k;
484 real_count++;
486 keys[real_count] = NULL;
488 return keys;
489 err:
490 sc_close();
491 return NULL;
495 sc_put_key(Key *prv, const char *id)
497 error("key uploading not yet supported");
498 return -1;
501 char *
502 sc_get_key_label(Key *key)
504 int r;
505 const struct sc_priv_data *priv;
506 struct sc_pkcs15_object *key_obj;
508 priv = (const struct sc_priv_data *) RSA_get_app_data(key->rsa);
509 if (priv == NULL || p15card == NULL) {
510 logit("SmartCard key not loaded");
511 /* internal error => return default label */
512 return xstrdup("smartcard key");
514 r = sc_pkcs15_find_prkey_by_id(p15card, &priv->cert_id, &key_obj);
515 if (r) {
516 logit("Unable to find private key from SmartCard: %s",
517 sc_strerror(r));
518 return xstrdup("smartcard key");
520 if (key_obj == NULL || key_obj->label == NULL)
521 /* the optional PKCS#15 label does not exists
522 * => return the default label */
523 return xstrdup("smartcard key");
524 return xstrdup(key_obj->label);
527 #endif /* SMARTCARD */