blob: 312251829f18965e9c8645408365585477c08e1c [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.
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
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"
38#include "readpass.h"
39#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
84static int
85sc_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;
99err:
100 sc_close();
101 return r;
102}
103
104/* private key operations */
105
106static int
107sc_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);
136 if (r) {
137 error("Unable to find PIN object from SmartCard: %s",
138 sc_strerror(r));
139 goto err;
140 }
141 pin = pin_obj->data;
142 r = sc_lock(card);
143 if (r) {
144 error("Unable to lock smartcard: %s", sc_strerror(r));
145 goto err;
146 }
147 if (sc_pin != NULL) {
148 r = sc_pkcs15_verify_pin(p15card, pin, sc_pin,
149 strlen(sc_pin));
150 if (r) {
151 sc_unlock(card);
152 error("PIN code verification failed: %s",
153 sc_strerror(r));
154 goto err;
155 }
156 }
157 *key_obj_out = key_obj;
158 return 0;
159err:
160 sc_close();
161 return -1;
162}
163
164static int
165sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
166 int padding)
167{
168 struct sc_pkcs15_object *key_obj;
169 int r;
170
171 if (padding != RSA_PKCS1_PADDING)
172 return -1;
173 r = sc_prkey_op_init(rsa, &key_obj);
174 if (r)
175 return -1;
Damien Miller485397c2003-06-04 19:15:10 +1000176 r = sc_pkcs15_decipher(p15card, key_obj, SC_ALGORITHM_RSA_PAD_PKCS1,
177 from, flen, to, flen);
Ben Lindstroma42694f2002-04-05 16:11:45 +0000178 sc_unlock(card);
179 if (r < 0) {
180 error("sc_pkcs15_decipher() failed: %s", sc_strerror(r));
181 goto err;
182 }
183 return r;
184err:
185 sc_close();
186 return -1;
187}
188
189static int
190sc_sign(int type, u_char *m, unsigned int m_len,
191 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
192{
193 struct sc_pkcs15_object *key_obj;
194 int r;
195 unsigned long flags = 0;
196
197 r = sc_prkey_op_init(rsa, &key_obj);
198 if (r)
199 return -1;
200 /* FIXME: length of sigret correct? */
201 /* FIXME: check 'type' and modify flags accordingly */
202 flags = SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_SHA1;
203 r = sc_pkcs15_compute_signature(p15card, key_obj, flags,
204 m, m_len, sigret, RSA_size(rsa));
205 sc_unlock(card);
206 if (r < 0) {
207 error("sc_pkcs15_compute_signature() failed: %s",
208 sc_strerror(r));
209 goto err;
210 }
211 *siglen = r;
212 return 1;
213err:
214 sc_close();
215 return 0;
216}
217
218static int
219sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
220 int padding)
221{
222 error("Private key encryption not supported");
223 return -1;
224}
225
226/* called on free */
227
228static int (*orig_finish)(RSA *rsa) = NULL;
229
230static int
231sc_finish(RSA *rsa)
232{
233 struct sc_priv_data *priv;
234
235 priv = RSA_get_app_data(rsa);
236 priv->ref_count--;
237 if (priv->ref_count == 0) {
238 free(priv);
239 sc_close();
240 }
241 if (orig_finish)
242 orig_finish(rsa);
243 return 1;
244}
245
246/* engine for overloading private key operations */
247
248static RSA_METHOD *
249sc_get_rsa_method(void)
250{
251 static RSA_METHOD smart_rsa;
252 const RSA_METHOD *def = RSA_get_default_method();
253
254 /* use the OpenSSL version */
255 memcpy(&smart_rsa, def, sizeof(smart_rsa));
256
257 smart_rsa.name = "opensc";
258
259 /* overload */
260 smart_rsa.rsa_priv_enc = sc_private_encrypt;
261 smart_rsa.rsa_priv_dec = sc_private_decrypt;
262 smart_rsa.rsa_sign = sc_sign;
263
264 /* save original */
265 orig_finish = def->finish;
266 smart_rsa.finish = sc_finish;
267
268 return &smart_rsa;
269}
270
271#ifdef USE_ENGINE
272static ENGINE *
273sc_get_engine(void)
274{
275 static ENGINE *smart_engine = NULL;
276
277 if ((smart_engine = ENGINE_new()) == NULL)
278 fatal("ENGINE_new failed");
279
280 ENGINE_set_id(smart_engine, "opensc");
281 ENGINE_set_name(smart_engine, "OpenSC");
282
283 ENGINE_set_RSA(smart_engine, sc_get_rsa_method());
284 ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
285 ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
286 ENGINE_set_RAND(smart_engine, RAND_SSLeay());
287 ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
288
289 return smart_engine;
290}
291#endif
292
293static void
294convert_rsa_to_rsa1(Key * in, Key * out)
295{
296 struct sc_priv_data *priv;
297
298 out->rsa->flags = in->rsa->flags;
299 out->flags = in->flags;
300 RSA_set_method(out->rsa, RSA_get_method(in->rsa));
301 BN_copy(out->rsa->n, in->rsa->n);
302 BN_copy(out->rsa->e, in->rsa->e);
303 priv = RSA_get_app_data(in->rsa);
304 priv->ref_count++;
305 RSA_set_app_data(out->rsa, priv);
306 return;
307}
308
309static int
310sc_read_pubkey(Key * k, const struct sc_pkcs15_object *cert_obj)
311{
312 int r;
313 sc_pkcs15_cert_t *cert = NULL;
314 struct sc_priv_data *priv = NULL;
315 sc_pkcs15_cert_info_t *cinfo = cert_obj->data;
316
317 X509 *x509 = NULL;
318 EVP_PKEY *pubkey = NULL;
319 u8 *p;
320 char *tmp;
321
322 debug("sc_read_pubkey() with cert id %02X", cinfo->id.value[0]);
323 r = sc_pkcs15_read_certificate(p15card, cinfo, &cert);
324 if (r) {
Damien Miller996acd22003-04-09 20:59:48 +1000325 logit("Certificate read failed: %s", sc_strerror(r));
Ben Lindstroma42694f2002-04-05 16:11:45 +0000326 goto err;
327 }
328 x509 = X509_new();
329 if (x509 == NULL) {
330 r = -1;
331 goto err;
332 }
333 p = cert->data;
334 if (!d2i_X509(&x509, &p, cert->data_len)) {
Damien Miller996acd22003-04-09 20:59:48 +1000335 logit("Unable to parse X.509 certificate");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000336 r = -1;
337 goto err;
338 }
339 sc_pkcs15_free_certificate(cert);
340 cert = NULL;
341 pubkey = X509_get_pubkey(x509);
342 X509_free(x509);
343 x509 = NULL;
344 if (pubkey->type != EVP_PKEY_RSA) {
Damien Miller996acd22003-04-09 20:59:48 +1000345 logit("Public key is of unknown type");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000346 r = -1;
347 goto err;
348 }
349 k->rsa = EVP_PKEY_get1_RSA(pubkey);
350 EVP_PKEY_free(pubkey);
351
352 k->rsa->flags |= RSA_FLAG_SIGN_VER;
353 RSA_set_method(k->rsa, sc_get_rsa_method());
354 priv = xmalloc(sizeof(struct sc_priv_data));
355 priv->cert_id = cinfo->id;
356 priv->ref_count = 1;
357 RSA_set_app_data(k->rsa, priv);
358
359 k->flags = KEY_FLAG_EXT;
360 tmp = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
361 debug("fingerprint %d %s", key_size(k), tmp);
362 xfree(tmp);
363
364 return 0;
365err:
366 if (cert)
367 sc_pkcs15_free_certificate(cert);
368 if (pubkey)
369 EVP_PKEY_free(pubkey);
370 if (x509)
371 X509_free(x509);
372 return r;
373}
374
375Key **
376sc_get_keys(const char *id, const char *pin)
377{
378 Key *k, **keys;
379 int i, r, real_count = 0, key_count;
380 sc_pkcs15_id_t cert_id;
381 sc_pkcs15_object_t *certs[32];
382 char *buf = xstrdup(id), *p;
383
384 debug("sc_get_keys called: id = %s", id);
385
386 if (sc_pin != NULL)
387 xfree(sc_pin);
388 sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
389
390 cert_id.len = 0;
391 if ((p = strchr(buf, ':')) != NULL) {
392 *p = 0;
393 p++;
394 sc_pkcs15_hex_string_to_id(p, &cert_id);
395 }
396 r = sscanf(buf, "%d", &sc_reader_id);
397 xfree(buf);
398 if (r != 1)
399 goto err;
400 if (p15card == NULL) {
401 sc_close();
402 r = sc_init();
403 if (r) {
404 error("Smartcard init failed: %s", sc_strerror(r));
405 goto err;
406 }
407 }
408 if (cert_id.len) {
409 r = sc_pkcs15_find_cert_by_id(p15card, &cert_id, &certs[0]);
410 if (r < 0)
411 goto err;
412 key_count = 1;
413 } else {
414 r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_CERT_X509,
415 certs, 32);
416 if (r == 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000417 logit("No certificates found on smartcard");
Ben Lindstroma42694f2002-04-05 16:11:45 +0000418 r = -1;
419 goto err;
420 } else if (r < 0) {
421 error("Certificate enumeration failed: %s",
422 sc_strerror(r));
423 goto err;
424 }
425 key_count = r;
426 }
427 /* FIXME: only keep entries with a corresponding private key */
428 keys = xmalloc(sizeof(Key *) * (key_count*2+1));
429 for (i = 0; i < key_count; i++) {
430 k = key_new(KEY_RSA);
431 if (k == NULL)
432 break;
433 r = sc_read_pubkey(k, certs[i]);
434 if (r) {
435 error("sc_read_pubkey failed: %s", sc_strerror(r));
436 key_free(k);
437 continue;
438 }
439 keys[real_count] = k;
440 real_count++;
441 k = key_new(KEY_RSA1);
442 if (k == NULL)
443 break;
444 convert_rsa_to_rsa1(keys[real_count-1], k);
445 keys[real_count] = k;
446 real_count++;
447 }
448 keys[real_count] = NULL;
449
450 return keys;
451err:
452 sc_close();
453 return NULL;
454}
455
456int
457sc_put_key(Key *prv, const char *id)
458{
459 error("key uploading not yet supported");
460 return -1;
461}
462
463#endif /* SMARTCARD */