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
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.
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>
41 #if OPENSSL_VERSION_NUMBER < 0x00907000L && defined(CRYPTO_LOCK_ENGINE)
43 #define RSA_get_default_method RSA_get_default_openssl_method
48 #include <openssl/engine.h>
49 #define sc_get_rsa sc_get_engine
51 #define sc_get_rsa sc_get_rsa_method
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;
63 struct sc_pkcs15_id cert_id;
71 sc_pkcs15_unbind(p15card);
75 sc_disconnect_card(card, 0);
79 sc_release_context(ctx);
89 r = sc_establish_context(&ctx, "openssh");
92 r = sc_connect_card(ctx->reader[sc_reader_id], 0, &card);
95 r = sc_pkcs15_bind(card, &p15card);
104 /* private key operations */
107 sc_prkey_op_init(RSA *rsa, struct sc_pkcs15_object **key_obj_out)
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;
116 priv = (struct sc_priv_data *) RSA_get_app_data(rsa);
119 if (p15card == NULL) {
123 error("SmartCard init failed: %s", sc_strerror(r));
127 r = sc_pkcs15_find_prkey_by_id(p15card, &priv->cert_id, &key_obj);
129 error("Unable to find private key from SmartCard: %s",
134 r = sc_pkcs15_find_pin_by_auth_id(p15card, &key_obj->auth_id,
137 error("Unable to find PIN object from SmartCard: %s",
144 error("Unable to lock smartcard: %s", sc_strerror(r));
147 if (sc_pin != NULL) {
148 r = sc_pkcs15_verify_pin(p15card, pin, sc_pin,
152 error("PIN code verification failed: %s",
157 *key_obj_out = key_obj;
165 sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
168 struct sc_pkcs15_object *key_obj;
171 if (padding != RSA_PKCS1_PADDING)
173 r = sc_prkey_op_init(rsa, &key_obj);
176 r = sc_pkcs15_decipher(p15card, key_obj, 0, from, flen, to, flen);
179 error("sc_pkcs15_decipher() failed: %s", sc_strerror(r));
189 sc_sign(int type, u_char *m, unsigned int m_len,
190 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
192 struct sc_pkcs15_object *key_obj;
194 unsigned long flags = 0;
196 r = sc_prkey_op_init(rsa, &key_obj);
199 /* FIXME: length of sigret correct? */
200 /* FIXME: check 'type' and modify flags accordingly */
201 flags = SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_SHA1;
202 r = sc_pkcs15_compute_signature(p15card, key_obj, flags,
203 m, m_len, sigret, RSA_size(rsa));
206 error("sc_pkcs15_compute_signature() failed: %s",
218 sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
221 error("Private key encryption not supported");
227 static int (*orig_finish)(RSA *rsa) = NULL;
232 struct sc_priv_data *priv;
234 priv = RSA_get_app_data(rsa);
236 if (priv->ref_count == 0) {
245 /* engine for overloading private key operations */
248 sc_get_rsa_method(void)
250 static RSA_METHOD smart_rsa;
251 const RSA_METHOD *def = RSA_get_default_method();
253 /* use the OpenSSL version */
254 memcpy(&smart_rsa, def, sizeof(smart_rsa));
256 smart_rsa.name = "opensc";
259 smart_rsa.rsa_priv_enc = sc_private_encrypt;
260 smart_rsa.rsa_priv_dec = sc_private_decrypt;
261 smart_rsa.rsa_sign = sc_sign;
264 orig_finish = def->finish;
265 smart_rsa.finish = sc_finish;
274 static ENGINE *smart_engine = NULL;
276 if ((smart_engine = ENGINE_new()) == NULL)
277 fatal("ENGINE_new failed");
279 ENGINE_set_id(smart_engine, "opensc");
280 ENGINE_set_name(smart_engine, "OpenSC");
282 ENGINE_set_RSA(smart_engine, sc_get_rsa_method());
283 ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
284 ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
285 ENGINE_set_RAND(smart_engine, RAND_SSLeay());
286 ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
293 convert_rsa_to_rsa1(Key * in, Key * out)
295 struct sc_priv_data *priv;
297 out->rsa->flags = in->rsa->flags;
298 out->flags = in->flags;
299 RSA_set_method(out->rsa, RSA_get_method(in->rsa));
300 BN_copy(out->rsa->n, in->rsa->n);
301 BN_copy(out->rsa->e, in->rsa->e);
302 priv = RSA_get_app_data(in->rsa);
304 RSA_set_app_data(out->rsa, priv);
309 sc_read_pubkey(Key * k, const struct sc_pkcs15_object *cert_obj)
312 sc_pkcs15_cert_t *cert = NULL;
313 struct sc_priv_data *priv = NULL;
314 sc_pkcs15_cert_info_t *cinfo = cert_obj->data;
317 EVP_PKEY *pubkey = NULL;
321 debug("sc_read_pubkey() with cert id %02X", cinfo->id.value[0]);
322 r = sc_pkcs15_read_certificate(p15card, cinfo, &cert);
324 log("Certificate read failed: %s", sc_strerror(r));
333 if (!d2i_X509(&x509, &p, cert->data_len)) {
334 log("Unable to parse X.509 certificate");
338 sc_pkcs15_free_certificate(cert);
340 pubkey = X509_get_pubkey(x509);
343 if (pubkey->type != EVP_PKEY_RSA) {
344 log("Public key is of unknown type");
348 k->rsa = EVP_PKEY_get1_RSA(pubkey);
349 EVP_PKEY_free(pubkey);
351 k->rsa->flags |= RSA_FLAG_SIGN_VER;
352 RSA_set_method(k->rsa, sc_get_rsa_method());
353 priv = xmalloc(sizeof(struct sc_priv_data));
354 priv->cert_id = cinfo->id;
356 RSA_set_app_data(k->rsa, priv);
358 k->flags = KEY_FLAG_EXT;
359 tmp = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
360 debug("fingerprint %d %s", key_size(k), tmp);
366 sc_pkcs15_free_certificate(cert);
368 EVP_PKEY_free(pubkey);
375 sc_get_keys(const char *id, const char *pin)
378 int i, r, real_count = 0, key_count;
379 sc_pkcs15_id_t cert_id;
380 sc_pkcs15_object_t *certs[32];
381 char *buf = xstrdup(id), *p;
383 debug("sc_get_keys called: id = %s", id);
387 sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
390 if ((p = strchr(buf, ':')) != NULL) {
393 sc_pkcs15_hex_string_to_id(p, &cert_id);
395 r = sscanf(buf, "%d", &sc_reader_id);
399 if (p15card == NULL) {
403 error("Smartcard init failed: %s", sc_strerror(r));
408 r = sc_pkcs15_find_cert_by_id(p15card, &cert_id, &certs[0]);
413 r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_CERT_X509,
416 log("No certificates found on smartcard");
420 error("Certificate enumeration failed: %s",
426 /* FIXME: only keep entries with a corresponding private key */
427 keys = xmalloc(sizeof(Key *) * (key_count*2+1));
428 for (i = 0; i < key_count; i++) {
429 k = key_new(KEY_RSA);
432 r = sc_read_pubkey(k, certs[i]);
434 error("sc_read_pubkey failed: %s", sc_strerror(r));
438 keys[real_count] = k;
440 k = key_new(KEY_RSA1);
443 convert_rsa_to_rsa1(keys[real_count-1], k);
444 keys[real_count] = k;
447 keys[real_count] = NULL;
456 sc_put_key(Key *prv, const char *id)
458 error("key uploading not yet supported");
462 #endif /* SMARTCARD */