blob: 9f22fdf39fef7be590ae5f9275937c140117e352 [file] [log] [blame]
Ben Lindstrom3133dbb2001-07-04 05:35:00 +00001/*
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"
Ben Lindstrom94baf302001-08-06 21:25:38 +000027RCSID("$OpenBSD: scard.c,v 1.9 2001/07/31 08:41:10 jakob Exp $");
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000028
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
45static int sc_fd = -1;
Damien Miller694be4b2001-07-14 12:13:26 +100046static int sc_reader_num = -1;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000047static int cla = 0x00; /* class */
48
49/* interface to libsectok */
50
51static int
Damien Miller694be4b2001-07-14 12:13:26 +100052sc_open(void)
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000053{
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000054 int sw;
55
56 if (sc_fd >= 0)
57 return sc_fd;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000058
Ben Lindstrom30b00be2001-08-06 21:22:10 +000059 sc_fd = sectok_open(sc_reader_num, STONOWAIT, &sw);
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000060 if (sc_fd < 0) {
Damien Miller694be4b2001-07-14 12:13:26 +100061 error("sectok_open failed: %s", sectok_get_sw(sw));
Ben Lindstrom30b00be2001-08-06 21:22:10 +000062 return SCARD_ERROR_FAIL;
63 }
64 if (! sectok_cardpresent(sc_fd)) {
Ben Lindstrom94baf302001-08-06 21:25:38 +000065 debug("smartcard in reader %d not present, skipping",
Ben Lindstrom30b00be2001-08-06 21:22:10 +000066 sc_reader_num);
67 return SCARD_ERROR_NOCARD;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000068 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +000069 if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000070 error("sectok_reset failed: %s", sectok_get_sw(sw));
71 sc_fd = -1;
Ben Lindstrom30b00be2001-08-06 21:22:10 +000072 return SCARD_ERROR_FAIL;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000073 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +000074 if ((cla = cyberflex_inq_class(sc_fd)) < 0)
75 cla = 0;
Damien Miller694be4b2001-07-14 12:13:26 +100076
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000077 debug("sc_open ok %d", sc_fd);
78 return sc_fd;
79}
80
81static int
82sc_enable_applet(void)
83{
Ben Lindstrom60df8e42001-08-06 21:10:52 +000084 static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
85 int sw = 0;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000086
Ben Lindstrom60df8e42001-08-06 21:10:52 +000087 /* select applet id */
88 sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000089 if (!sectok_swOK(sw)) {
90 error("sectok_apdu failed: %s", sectok_get_sw(sw));
Damien Miller694be4b2001-07-14 12:13:26 +100091 sc_close();
92 return -1;
93 }
94 return 0;
95}
96
97static int
98sc_init(void)
99{
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000100 int status;
101
102 status = sc_open();
103 if (status == SCARD_ERROR_NOCARD) {
104 return SCARD_ERROR_NOCARD;
105 }
106 if (status < 0 ) {
Damien Miller694be4b2001-07-14 12:13:26 +1000107 error("sc_open failed");
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000108 return status;
Damien Miller694be4b2001-07-14 12:13:26 +1000109 }
110 if (sc_enable_applet() < 0) {
111 error("sc_enable_applet failed");
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000112 return SCARD_ERROR_APPLET;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000113 }
114 return 0;
115}
116
117static int
118sc_read_pubkey(Key * k)
119{
120 u_char buf[2], *n;
121 char *p;
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000122 int len, sw, status;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000123
124 len = sw = 0;
125
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000126 if (sc_fd < 0) {
127 status = sc_init();
128 if (status < 0 )
129 return status;
130 }
Damien Miller694be4b2001-07-14 12:13:26 +1000131
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000132 /* 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));
Damien Miller694be4b2001-07-14 12:13:26 +1000137 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000138 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);
Damien Miller694be4b2001-07-14 12:13:26 +1000157 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000158 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
177static int
178sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
179{
180 u_char *padded = NULL;
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000181 int sw, len, olen, status;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000182
183 debug("sc_private_decrypt called");
184
185 olen = len = sw = 0;
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000186 if (sc_fd < 0) {
187 status = sc_init();
188 if (status < 0 )
Damien Miller694be4b2001-07-14 12:13:26 +1000189 goto err;
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000190 }
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000191 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));
Damien Miller694be4b2001-07-14 12:13:26 +1000201 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000202 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));
Damien Miller694be4b2001-07-14 12:13:26 +1000209 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000210 goto err;
211 }
212 olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
213 len);
214err:
215 if (padded)
216 xfree(padded);
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000217 return (olen >= 0 ? olen : status);
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000218}
219
220static int
221sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
222{
223 u_char *padded = NULL;
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000224 int sw, len, status;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000225
226 len = sw = 0;
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000227 if (sc_fd < 0) {
228 status = sc_init();
229 if (status < 0 )
Damien Miller694be4b2001-07-14 12:13:26 +1000230 goto err;
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000231 }
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000232 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));
Damien Miller694be4b2001-07-14 12:13:26 +1000247 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000248 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));
Damien Miller694be4b2001-07-14 12:13:26 +1000255 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000256 goto err;
257 }
258err:
259 if (padded)
260 xfree(padded);
Ben Lindstrom30b00be2001-08-06 21:22:10 +0000261 return (len >= 0 ? len : status);
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000262}
263
264/* engine for overloading private key operations */
265
266static ENGINE *smart_engine = NULL;
267static 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
282ENGINE *
283sc_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
Damien Miller694be4b2001-07-14 12:13:26 +1000318void
319sc_close(void)
320{
321 if (sc_fd >= 0) {
322 sectok_close(sc_fd);
323 sc_fd = -1;
324 }
325}
326
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000327Key *
Damien Miller694be4b2001-07-14 12:13:26 +1000328sc_get_key(int num)
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000329{
330 Key *k;
Ben Lindstrom94baf302001-08-06 21:25:38 +0000331 int status;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000332
Damien Miller694be4b2001-07-14 12:13:26 +1000333 sc_reader_num = num;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000334 k = key_new(KEY_RSA);
335 if (k == NULL) {
336 return NULL;
337 }
Ben Lindstrom94baf302001-08-06 21:25:38 +0000338 status = sc_read_pubkey(k);
339 if (status == SCARD_ERROR_NOCARD) {
340 key_free(k);
341 return NULL;
342 }
343 if (status < 0 ) {
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000344 error("sc_read_pubkey failed");
345 key_free(k);
346 return NULL;
347 }
348 return k;
Damien Miller694be4b2001-07-14 12:13:26 +1000349 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000350}
351#endif