]> andersk Git - openssh.git/blob - scard-opensc.c
- (djm) Bug #593: Sanity check OpenSC card reader number; patch from
[openssh.git] / scard-opensc.c
1 /*
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
32 #include <opensc/opensc.h>
33 #include <opensc/pkcs15.h>
34
35 #include "key.h"
36 #include "log.h"
37 #include "xmalloc.h"
38 #include "readpass.h"
39 #include "scard.h"
40
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
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
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;
58
59 static char *sc_pin = NULL;
60
61 struct sc_priv_data
62 {
63         struct sc_pkcs15_id cert_id;
64         int ref_count;
65 };
66
67 void
68 sc_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
84 static int 
85 sc_init(void)
86 {
87         int r;
88
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;
97         }
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;
108 }
109
110 /* private key operations */
111
112 static int
113 sc_prkey_op_init(RSA *rsa, struct sc_pkcs15_object **key_obj_out)
114 {
115         int r;
116         struct sc_priv_data *priv;
117         struct sc_pkcs15_object *key_obj;
118         struct sc_pkcs15_prkey_info *key;
119         struct sc_pkcs15_object *pin_obj;
120         struct sc_pkcs15_pin_info *pin;
121
122         priv = (struct sc_priv_data *) RSA_get_app_data(rsa);
123         if (priv == NULL)
124                 return -1;
125         if (p15card == NULL) {
126                 sc_close();
127                 r = sc_init();
128                 if (r) {
129                         error("SmartCard init failed: %s", sc_strerror(r));
130                         goto err;
131                 }
132         }
133         r = sc_pkcs15_find_prkey_by_id(p15card, &priv->cert_id, &key_obj);
134         if (r) {
135                 error("Unable to find private key from SmartCard: %s",
136                       sc_strerror(r));
137                 goto err;
138         }
139         key = key_obj->data;
140         r = sc_pkcs15_find_pin_by_auth_id(p15card, &key_obj->auth_id,
141                                           &pin_obj);
142         if (r == SC_ERROR_OBJECT_NOT_FOUND) {
143                 /* no pin required */
144                 r = sc_lock(card);
145                 if (r) {
146                         error("Unable to lock smartcard: %s", sc_strerror(r));
147                         goto err;
148                 }
149                 *key_obj_out = key_obj;
150                 return 0;
151         } else if (r) {
152                 error("Unable to find PIN object from SmartCard: %s",
153                       sc_strerror(r));
154                 goto err;
155         }
156         pin = pin_obj->data;
157         r = sc_lock(card);
158         if (r) {
159                 error("Unable to lock smartcard: %s", sc_strerror(r));
160                 goto err;
161         }
162         if (sc_pin != NULL) {
163                 r = sc_pkcs15_verify_pin(p15card, pin, sc_pin,
164                                          strlen(sc_pin));
165                 if (r) {
166                         sc_unlock(card);
167                         error("PIN code verification failed: %s",
168                               sc_strerror(r));
169                         goto err;
170                 }
171         }
172         *key_obj_out = key_obj;
173         return 0;
174 err:
175         sc_close();
176         return -1;
177 }
178
179 static int
180 sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
181     int padding)
182 {
183         struct sc_pkcs15_object *key_obj;
184         int r;
185
186         if (padding != RSA_PKCS1_PADDING)
187                 return -1;      
188         r = sc_prkey_op_init(rsa, &key_obj);
189         if (r)
190                 return -1;
191         r = sc_pkcs15_decipher(p15card, key_obj, SC_ALGORITHM_RSA_PAD_PKCS1, 
192             from, flen, to, flen);
193         sc_unlock(card);
194         if (r < 0) {
195                 error("sc_pkcs15_decipher() failed: %s", sc_strerror(r));
196                 goto err;
197         }
198         return r;
199 err:
200         sc_close();
201         return -1;
202 }
203
204 static int
205 sc_sign(int type, u_char *m, unsigned int m_len,
206         unsigned char *sigret, unsigned int *siglen, RSA *rsa)
207 {
208         struct sc_pkcs15_object *key_obj;
209         int r;
210         unsigned long flags = 0;
211
212         r = sc_prkey_op_init(rsa, &key_obj);
213         if (r)
214                 return -1;
215         /* FIXME: length of sigret correct? */
216         /* FIXME: check 'type' and modify flags accordingly */
217         flags = SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_SHA1;
218         r = sc_pkcs15_compute_signature(p15card, key_obj, flags,
219                                         m, m_len, sigret, RSA_size(rsa));
220         sc_unlock(card);
221         if (r < 0) {
222                 error("sc_pkcs15_compute_signature() failed: %s",
223                       sc_strerror(r));
224                 goto err;
225         }
226         *siglen = r;
227         return 1;
228 err:
229         sc_close();
230         return 0;
231 }
232
233 static int
234 sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
235     int padding)
236 {
237         error("Private key encryption not supported");
238         return -1;
239 }
240
241 /* called on free */
242
243 static int (*orig_finish)(RSA *rsa) = NULL;
244
245 static int
246 sc_finish(RSA *rsa)
247 {
248         struct sc_priv_data *priv;
249
250         priv = RSA_get_app_data(rsa);
251         priv->ref_count--;
252         if (priv->ref_count == 0) {
253                 free(priv);
254                 sc_close();
255         }
256         if (orig_finish)
257                 orig_finish(rsa);
258         return 1;
259 }
260
261 /* engine for overloading private key operations */
262
263 static RSA_METHOD *
264 sc_get_rsa_method(void)
265 {
266         static RSA_METHOD smart_rsa;
267         const RSA_METHOD *def = RSA_get_default_method();
268
269         /* use the OpenSSL version */
270         memcpy(&smart_rsa, def, sizeof(smart_rsa));
271
272         smart_rsa.name          = "opensc";
273
274         /* overload */
275         smart_rsa.rsa_priv_enc  = sc_private_encrypt;
276         smart_rsa.rsa_priv_dec  = sc_private_decrypt;
277         smart_rsa.rsa_sign      = sc_sign;
278
279         /* save original */
280         orig_finish             = def->finish;
281         smart_rsa.finish        = sc_finish;
282
283         return &smart_rsa;
284 }
285
286 #ifdef USE_ENGINE
287 static ENGINE *
288 sc_get_engine(void)
289 {
290         static ENGINE *smart_engine = NULL;
291
292         if ((smart_engine = ENGINE_new()) == NULL)
293                 fatal("ENGINE_new failed");
294
295         ENGINE_set_id(smart_engine, "opensc");
296         ENGINE_set_name(smart_engine, "OpenSC");
297
298         ENGINE_set_RSA(smart_engine, sc_get_rsa_method());
299         ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
300         ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
301         ENGINE_set_RAND(smart_engine, RAND_SSLeay());
302         ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
303
304         return smart_engine;
305 }
306 #endif
307
308 static void
309 convert_rsa_to_rsa1(Key * in, Key * out)
310 {
311         struct sc_priv_data *priv;
312         
313         out->rsa->flags = in->rsa->flags;
314         out->flags = in->flags;
315         RSA_set_method(out->rsa, RSA_get_method(in->rsa));
316         BN_copy(out->rsa->n, in->rsa->n);
317         BN_copy(out->rsa->e, in->rsa->e);
318         priv = RSA_get_app_data(in->rsa);
319         priv->ref_count++;
320         RSA_set_app_data(out->rsa, priv);
321         return;
322 }
323
324 static int 
325 sc_read_pubkey(Key * k, const struct sc_pkcs15_object *cert_obj)
326 {
327         int r;
328         sc_pkcs15_cert_t *cert = NULL;
329         struct sc_priv_data *priv = NULL;
330         sc_pkcs15_cert_info_t *cinfo = cert_obj->data;
331
332         X509 *x509 = NULL;
333         EVP_PKEY *pubkey = NULL;
334         u8 *p;
335         char *tmp;
336         
337         debug("sc_read_pubkey() with cert id %02X", cinfo->id.value[0]);
338         r = sc_pkcs15_read_certificate(p15card, cinfo, &cert);
339         if (r) {
340                 logit("Certificate read failed: %s", sc_strerror(r));
341                 goto err;
342         }
343         x509 = X509_new();
344         if (x509 == NULL) {
345                 r = -1; 
346                 goto err;
347         }
348         p = cert->data;
349         if (!d2i_X509(&x509, &p, cert->data_len)) {
350                 logit("Unable to parse X.509 certificate");
351                 r = -1;
352                 goto err;
353         }
354         sc_pkcs15_free_certificate(cert);
355         cert = NULL;
356         pubkey = X509_get_pubkey(x509);
357         X509_free(x509);
358         x509 = NULL;
359         if (pubkey->type != EVP_PKEY_RSA) {
360                 logit("Public key is of unknown type");
361                 r = -1;
362                 goto err;
363         }
364         k->rsa = EVP_PKEY_get1_RSA(pubkey);
365         EVP_PKEY_free(pubkey);
366
367         k->rsa->flags |= RSA_FLAG_SIGN_VER;
368         RSA_set_method(k->rsa, sc_get_rsa_method());
369         priv = xmalloc(sizeof(struct sc_priv_data));
370         priv->cert_id = cinfo->id;
371         priv->ref_count = 1;
372         RSA_set_app_data(k->rsa, priv);
373
374         k->flags = KEY_FLAG_EXT;
375         tmp = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
376         debug("fingerprint %d %s", key_size(k), tmp);
377         xfree(tmp);
378         
379         return 0;
380 err:
381         if (cert)
382                 sc_pkcs15_free_certificate(cert);
383         if (pubkey)
384                 EVP_PKEY_free(pubkey);
385         if (x509)
386                 X509_free(x509);
387         return r;
388 }
389
390 Key **
391 sc_get_keys(const char *id, const char *pin)
392 {
393         Key *k, **keys;
394         int i, r, real_count = 0, key_count;
395         sc_pkcs15_id_t cert_id;
396         sc_pkcs15_object_t *certs[32];
397         char *buf = xstrdup(id), *p;
398
399         debug("sc_get_keys called: id = %s", id);
400
401         if (sc_pin != NULL)
402                 xfree(sc_pin);
403         sc_pin = (pin == NULL) ? NULL : xstrdup(pin);
404
405         cert_id.len = 0;
406         if ((p = strchr(buf, ':')) != NULL) {
407                 *p = 0;
408                 p++;
409                 sc_pkcs15_hex_string_to_id(p, &cert_id);
410         }
411         r = sscanf(buf, "%d", &sc_reader_id);
412         xfree(buf);
413         if (r != 1)
414                 goto err;
415         if (p15card == NULL) {
416                 sc_close();
417                 r = sc_init();
418                 if (r) {
419                         error("Smartcard init failed: %s", sc_strerror(r));
420                         goto err;
421                 }
422         }
423         if (cert_id.len) {
424                 r = sc_pkcs15_find_cert_by_id(p15card, &cert_id, &certs[0]);
425                 if (r < 0)
426                         goto err;
427                 key_count = 1;
428         } else {
429                 r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_CERT_X509,
430                                           certs, 32);
431                 if (r == 0) {
432                         logit("No certificates found on smartcard");
433                         r = -1;
434                         goto err;
435                 } else if (r < 0) {
436                         error("Certificate enumeration failed: %s",
437                               sc_strerror(r));
438                         goto err;
439                 }
440                 key_count = r;
441         }
442         keys = xmalloc(sizeof(Key *) * (key_count*2+1));
443         for (i = 0; i < key_count; i++) {
444                 sc_pkcs15_object_t *tmp_obj = NULL;
445                 cert_id = ((sc_pkcs15_cert_info_t *)(certs[i]->data))->id;
446                 if (sc_pkcs15_find_prkey_by_id(p15card, &cert_id, &tmp_obj))
447                         /* skip the public key (certificate) if no
448                          * corresponding private key is present */
449                         continue;
450                 k = key_new(KEY_RSA);
451                 if (k == NULL)
452                         break;
453                 r = sc_read_pubkey(k, certs[i]);
454                 if (r) {
455                         error("sc_read_pubkey failed: %s", sc_strerror(r));
456                         key_free(k);
457                         continue;
458                 }
459                 keys[real_count] = k;
460                 real_count++;
461                 k = key_new(KEY_RSA1);
462                 if (k == NULL)
463                         break;
464                 convert_rsa_to_rsa1(keys[real_count-1], k);
465                 keys[real_count] = k;
466                 real_count++;
467         }
468         keys[real_count] = NULL;
469
470         return keys;
471 err:
472         sc_close();
473         return NULL;
474 }
475
476 int
477 sc_put_key(Key *prv, const char *id)
478 {
479         error("key uploading not yet supported");
480         return -1;
481 }
482
483 char *
484 sc_get_key_label(Key *key)
485 {
486         int r;
487         const struct sc_priv_data *priv;
488         struct sc_pkcs15_object *key_obj;
489
490         priv = (const struct sc_priv_data *) RSA_get_app_data(key->rsa);
491         if (priv == NULL || p15card == NULL) {
492                 logit("SmartCard key not loaded");
493                 /* internal error => return default label */
494                 return xstrdup("smartcard key");
495         }
496         r = sc_pkcs15_find_prkey_by_id(p15card, &priv->cert_id, &key_obj);
497         if (r) {
498                 logit("Unable to find private key from SmartCard: %s",
499                       sc_strerror(r));
500                 return xstrdup("smartcard key");
501         }
502         if (key_obj == NULL || key_obj->label == NULL)
503                 /* the optional PKCS#15 label does not exists
504                  * => return the default label */
505                 return xstrdup("smartcard key");
506         return xstrdup(key_obj->label);
507 }
508
509 #endif /* SMARTCARD */
This page took 0.703535 seconds and 5 git commands to generate.