]> andersk Git - openssh.git/blame - scard-opensc.c
- (dtucker) [contrib/cygwin/ssh-host-config] Add SeTcbPrivilege privilege
[openssh.git] / scard-opensc.c
CommitLineData
295c8801 1/*
2 * Copyright (c) 2002 Juha Yrjölä. All rights reserved.
3 * Copyright (c) 2001 Markus Friedl.
aff51935 4 *
295c8801 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
46c8e0f6 32#include <opensc/opensc.h>
33#include <opensc/pkcs15.h>
295c8801 34
35#include "key.h"
36#include "log.h"
37#include "xmalloc.h"
0f3ee929 38#include "misc.h"
295c8801 39#include "scard.h"
40
5866adb0 41#if OPENSSL_VERSION_NUMBER < 0x00907000L && defined(CRYPTO_LOCK_ENGINE)
295c8801 42#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
aff51935 84static int
295c8801 85sc_init(void)
86{
87 int r;
88
89 r = sc_establish_context(&ctx, "openssh");
90 if (r)
91 goto err;
d2168412 92 if (sc_reader_id >= ctx->reader_count) {
93 r = SC_ERROR_NO_READERS_FOUND;
aff51935 94 error("Illegal reader number %d (max %d)", sc_reader_id,
d2168412 95 ctx->reader_count -1);
96 goto err;
97 }
295c8801 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;
105err:
106 sc_close();
107 return r;
108}
109
110/* private key operations */
111
112static int
4e2e1af3 113sc_prkey_op_init(RSA *rsa, struct sc_pkcs15_object **key_obj_out,
114 unsigned int usage)
295c8801 115{
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 }
aff51935 134 r = sc_pkcs15_find_prkey_by_id_usage(p15card, &priv->cert_id,
4e2e1af3 135 usage, &key_obj);
295c8801 136 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);
11f1e60e 144 if (r == SC_ERROR_OBJECT_NOT_FOUND) {
145 /* no pin required */
7b7f164b 146 r = sc_lock(card);
147 if (r) {
148 error("Unable to lock smartcard: %s", sc_strerror(r));
149 goto err;
150 }
11f1e60e 151 *key_obj_out = key_obj;
152 return 0;
153 } else if (r) {
295c8801 154 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
4e2e1af3 181#define SC_USAGE_DECRYPT SC_PKCS15_PRKEY_USAGE_DECRYPT | \
182 SC_PKCS15_PRKEY_USAGE_UNWRAP
183
295c8801 184static 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)
b6453d99 192 return -1;
4e2e1af3 193 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_DECRYPT);
295c8801 194 if (r)
195 return -1;
aff51935 196 r = sc_pkcs15_decipher(p15card, key_obj, SC_ALGORITHM_RSA_PAD_PKCS1,
8acdec60 197 from, flen, to, flen);
295c8801 198 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
4e2e1af3 209#define SC_USAGE_SIGN SC_PKCS15_PRKEY_USAGE_SIGN | \
210 SC_PKCS15_PRKEY_USAGE_SIGNRECOVER
211
295c8801 212static 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
4e2e1af3 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
aff51935 226 * if the sign or signrecover flag is set as well.
4e2e1af3 227 */
228 r = sc_prkey_op_init(rsa, &key_obj, SC_USAGE_SIGN);
295c8801 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;
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;
b6453d99 328
295c8801 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;
338}
339
aff51935 340static int
295c8801 341sc_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;
b6453d99 352
295c8801 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) {
bbe88b6d 356 logit("Certificate read failed: %s", sc_strerror(r));
295c8801 357 goto err;
358 }
359 x509 = X509_new();
360 if (x509 == NULL) {
aff51935 361 r = -1;
295c8801 362 goto err;
363 }
364 p = cert->data;
365 if (!d2i_X509(&x509, &p, cert->data_len)) {
bbe88b6d 366 logit("Unable to parse X.509 certificate");
295c8801 367 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) {
bbe88b6d 376 logit("Public key is of unknown type");
295c8801 377 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);
b6453d99 394
295c8801 395 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) {
bbe88b6d 448 logit("No certificates found on smartcard");
295c8801 449 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 }
01d35895 458 if (key_count > 1024)
459 fatal("Too many keys (%u), expected <= 1024", key_count);
460 keys = xcalloc(key_count * 2 + 1, sizeof(Key *));
295c8801 461 for (i = 0; i < key_count; i++) {
7b7f164b 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;
295c8801 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;
476 }
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++;
485 }
486 keys[real_count] = NULL;
487
488 return keys;
489err:
490 sc_close();
491 return NULL;
492}
493
494int
495sc_put_key(Key *prv, const char *id)
496{
497 error("key uploading not yet supported");
498 return -1;
499}
500
9ea150a7 501char *
502sc_get_key_label(Key *key)
503{
504 int r;
505 const struct sc_priv_data *priv;
506 struct sc_pkcs15_object *key_obj;
507
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");
513 }
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");
519 }
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);
525}
526
295c8801 527#endif /* SMARTCARD */
This page took 2.989315 seconds and 5 git commands to generate.