]> andersk Git - openssh.git/blob - scard.c
- jakob@cvs.openbsd.org 2001/07/31 08:41:10
[openssh.git] / scard.c
1 /*
2  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #ifdef SMARTCARD
26 #include "includes.h"
27 RCSID("$OpenBSD: scard.c,v 1.9 2001/07/31 08:41:10 jakob Exp $");
28
29 #include <openssl/engine.h>
30 #include <sectok.h>
31
32 #include "key.h"
33 #include "log.h"
34 #include "xmalloc.h"
35 #include "scard.h"
36
37 #define CLA_SSH 0x05
38 #define INS_DECRYPT 0x10
39 #define INS_GET_KEYLENGTH 0x20
40 #define INS_GET_PUBKEY 0x30
41 #define INS_GET_RESPONSE 0xc0
42
43 #define MAX_BUF_SIZE 256
44
45 static int sc_fd = -1;
46 static int sc_reader_num = -1;
47 static int cla = 0x00;  /* class */
48
49 /* interface to libsectok */
50
51 static int 
52 sc_open(void)
53 {
54         int sw;
55
56         if (sc_fd >= 0)
57                 return sc_fd;
58
59         sc_fd = sectok_open(sc_reader_num, STONOWAIT, &sw);
60         if (sc_fd < 0) {
61                 error("sectok_open failed: %s", sectok_get_sw(sw));
62                 return SCARD_ERROR_FAIL;
63         }
64         if (! sectok_cardpresent(sc_fd)) {
65                 debug("smartcard in reader %d not present, skipping",
66                     sc_reader_num);
67                 return SCARD_ERROR_NOCARD;
68         }
69         if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
70                 error("sectok_reset failed: %s", sectok_get_sw(sw));
71                 sc_fd = -1;
72                 return SCARD_ERROR_FAIL;
73         }
74         if ((cla = cyberflex_inq_class(sc_fd)) < 0)
75                 cla = 0;
76
77         debug("sc_open ok %d", sc_fd);
78         return sc_fd;
79 }
80
81 static int 
82 sc_enable_applet(void)
83 {
84         static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
85         int sw = 0;
86
87         /* select applet id */
88         sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
89         if (!sectok_swOK(sw)) {
90                 error("sectok_apdu failed: %s", sectok_get_sw(sw));
91                 sc_close();
92                 return -1;
93         }
94         return 0;
95 }
96
97 static int 
98 sc_init(void)
99 {
100         int status;
101
102         status = sc_open();
103         if (status == SCARD_ERROR_NOCARD) {
104                 return SCARD_ERROR_NOCARD;
105         }
106         if (status < 0 ) {
107                 error("sc_open failed");
108                 return status;
109         }
110         if (sc_enable_applet() < 0) {
111                 error("sc_enable_applet failed");
112                 return SCARD_ERROR_APPLET;
113         }
114         return 0;
115 }
116
117 static int 
118 sc_read_pubkey(Key * k)
119 {
120         u_char buf[2], *n;
121         char *p;
122         int len, sw, status;
123
124         len = sw = 0;
125
126         if (sc_fd < 0) {
127                 status = sc_init();
128                 if (status < 0 )
129                         return status;
130         }
131
132         /* get key size */
133         sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
134              sizeof(buf), buf, &sw);
135         if (!sectok_swOK(sw)) {
136                 error("could not obtain key length: %s", sectok_get_sw(sw));
137                 sc_close();
138                 return -1;
139         }
140         len = (buf[0] << 8) | buf[1];
141         len /= 8;
142         debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw));
143
144         n = xmalloc(len);
145         /* get n */
146         sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw);
147         if (!sectok_swOK(sw)) {
148                 error("could not obtain public key: %s", sectok_get_sw(sw));
149                 xfree(n);
150                 return -1;
151         }
152         debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw));
153
154         if (BN_bin2bn(n, len, k->rsa->n) == NULL) {
155                 error("c_read_pubkey: BN_bin2bn failed");
156                 xfree(n);
157                 sc_close();
158                 return -1;
159         }
160         xfree(n);
161
162         /* currently the java applet just stores 'n' */
163         if (!BN_set_word(k->rsa->e, 35)) {
164                 error("c_read_pubkey: BN_set_word(e, 35) failed");
165                 return -1;
166         }
167
168         p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
169         debug("fingerprint %d %s", key_size(k), p);
170         xfree(p);
171
172         return 0;
173 }
174
175 /* private key operations */
176
177 static int
178 sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
179 {
180         u_char *padded = NULL;
181         int sw, len, olen, status;
182
183         debug("sc_private_decrypt called");
184
185         olen = len = sw = 0;
186         if (sc_fd < 0) {
187                 status = sc_init();
188                 if (status < 0 )
189                         goto err;
190         }
191         if (padding != RSA_PKCS1_PADDING)
192                 goto err;
193
194         len = BN_num_bytes(rsa->n);
195         padded = xmalloc(len);
196
197         sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, 0, NULL, &sw);
198         if (!sectok_swOK(sw)) {
199                 error("sc_private_decrypt: INS_DECRYPT failed: %s",
200                     sectok_get_sw(sw));
201                 sc_close();
202                 goto err;
203         }
204         sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
205              len, padded, &sw);
206         if (!sectok_swOK(sw)) {
207                 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
208                     sectok_get_sw(sw));
209                 sc_close();
210                 goto err;
211         }
212         olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
213             len);
214 err:
215         if (padded)
216                 xfree(padded);
217         return (olen >= 0 ? olen : status);
218 }
219
220 static int
221 sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
222 {
223         u_char *padded = NULL;
224         int sw, len, status;
225
226         len = sw = 0;
227         if (sc_fd < 0) {
228                 status = sc_init();
229                 if (status < 0 )
230                         goto err;
231         }
232         if (padding != RSA_PKCS1_PADDING)
233                 goto err;
234
235         debug("sc_private_encrypt called");
236         len = BN_num_bytes(rsa->n);
237         padded = xmalloc(len);
238
239         if (RSA_padding_add_PKCS1_type_1(padded, len, from, flen) <= 0) {
240                 error("RSA_padding_add_PKCS1_type_1 failed");
241                 goto err;
242         }
243         sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, 0, NULL, &sw);
244         if (!sectok_swOK(sw)) {
245                 error("sc_private_decrypt: INS_DECRYPT failed: %s",
246                     sectok_get_sw(sw));
247                 sc_close();
248                 goto err;
249         }
250         sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
251              len, to, &sw);
252         if (!sectok_swOK(sw)) {
253                 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
254                     sectok_get_sw(sw));
255                 sc_close();
256                 goto err;
257         }
258 err:
259         if (padded)
260                 xfree(padded);
261         return (len >= 0 ? len : status);
262 }
263
264 /* engine for overloading private key operations */
265
266 static ENGINE *smart_engine = NULL;
267 static RSA_METHOD smart_rsa =
268 {
269         "sectok",
270         NULL,
271         NULL,
272         NULL,
273         NULL,
274         NULL,
275         NULL,
276         NULL,
277         NULL,
278         0,
279         NULL,
280 };
281
282 ENGINE *
283 sc_get_engine(void)
284 {
285         RSA_METHOD *def;
286
287         def = RSA_get_default_openssl_method();
288
289         /* overload */
290         smart_rsa.rsa_priv_enc  = sc_private_encrypt;
291         smart_rsa.rsa_priv_dec  = sc_private_decrypt;
292
293         /* just use the OpenSSL version */
294         smart_rsa.rsa_pub_enc   = def->rsa_pub_enc;
295         smart_rsa.rsa_pub_dec   = def->rsa_pub_dec;
296         smart_rsa.rsa_mod_exp   = def->rsa_mod_exp;
297         smart_rsa.bn_mod_exp    = def->bn_mod_exp;
298         smart_rsa.init          = def->init;
299         smart_rsa.finish        = def->finish;
300         smart_rsa.flags         = def->flags;
301         smart_rsa.app_data      = def->app_data;
302         smart_rsa.rsa_sign      = def->rsa_sign;
303         smart_rsa.rsa_verify    = def->rsa_verify;
304
305         smart_engine = ENGINE_new();
306
307         ENGINE_set_id(smart_engine, "sectok");
308         ENGINE_set_name(smart_engine, "libsectok");
309         ENGINE_set_RSA(smart_engine, &smart_rsa);
310         ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
311         ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
312         ENGINE_set_RAND(smart_engine, RAND_SSLeay());
313         ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
314
315         return smart_engine;
316 }
317
318 void
319 sc_close(void)
320 {
321         if (sc_fd >= 0) {
322                 sectok_close(sc_fd);
323                 sc_fd = -1;
324         }
325 }
326
327 Key *
328 sc_get_key(int num)
329 {
330         Key *k;
331         int status;
332
333         sc_reader_num = num;
334         k = key_new(KEY_RSA);
335         if (k == NULL) {
336                 return NULL;
337         }
338         status = sc_read_pubkey(k);
339         if (status == SCARD_ERROR_NOCARD) {
340                 key_free(k);
341                 return NULL;
342         }
343         if (status < 0 ) {
344                 error("sc_read_pubkey failed");
345                 key_free(k);
346                 return NULL;
347         }
348         return k;
349         sc_close();
350 }
351 #endif
This page took 0.11078 seconds and 5 git commands to generate.