blob: b8640b890d88720d6e27ec7e2496bf2acde0c14d [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 Lindstrom60df8e42001-08-06 21:10:52 +000027RCSID("$OpenBSD: scard.c,v 1.7 2001/07/26 20:04:27 rees 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
Damien Miller694be4b2001-07-14 12:13:26 +100059 sc_fd = sectok_open(sc_reader_num, 0, &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));
62 return -1;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000063 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +000064 if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000065 error("sectok_reset failed: %s", sectok_get_sw(sw));
66 sc_fd = -1;
67 return sc_fd;
68 }
Ben Lindstrom60df8e42001-08-06 21:10:52 +000069 if ((cla = cyberflex_inq_class(sc_fd)) < 0)
70 cla = 0;
Damien Miller694be4b2001-07-14 12:13:26 +100071
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000072 debug("sc_open ok %d", sc_fd);
73 return sc_fd;
74}
75
76static int
77sc_enable_applet(void)
78{
Ben Lindstrom60df8e42001-08-06 21:10:52 +000079 static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
80 int sw = 0;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000081
Ben Lindstrom60df8e42001-08-06 21:10:52 +000082 /* select applet id */
83 sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
Ben Lindstrom3133dbb2001-07-04 05:35:00 +000084 if (!sectok_swOK(sw)) {
85 error("sectok_apdu failed: %s", sectok_get_sw(sw));
Damien Miller694be4b2001-07-14 12:13:26 +100086 sc_close();
87 return -1;
88 }
89 return 0;
90}
91
92static int
93sc_init(void)
94{
95 if (sc_open() < 0) {
96 error("sc_open failed");
97 return -1;
98 }
99 if (sc_enable_applet() < 0) {
100 error("sc_enable_applet failed");
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000101 return -1;
102 }
103 return 0;
104}
105
106static int
107sc_read_pubkey(Key * k)
108{
109 u_char buf[2], *n;
110 char *p;
111 int len, sw;
112
113 len = sw = 0;
114
Damien Miller694be4b2001-07-14 12:13:26 +1000115 if (sc_fd < 0)
116 if (sc_init() < 0)
117 return -1;
118
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000119 /* get key size */
120 sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
121 sizeof(buf), buf, &sw);
122 if (!sectok_swOK(sw)) {
123 error("could not obtain key length: %s", sectok_get_sw(sw));
Damien Miller694be4b2001-07-14 12:13:26 +1000124 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000125 return -1;
126 }
127 len = (buf[0] << 8) | buf[1];
128 len /= 8;
129 debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw));
130
131 n = xmalloc(len);
132 /* get n */
133 sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw);
134 if (!sectok_swOK(sw)) {
135 error("could not obtain public key: %s", sectok_get_sw(sw));
136 xfree(n);
137 return -1;
138 }
139 debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw));
140
141 if (BN_bin2bn(n, len, k->rsa->n) == NULL) {
142 error("c_read_pubkey: BN_bin2bn failed");
143 xfree(n);
Damien Miller694be4b2001-07-14 12:13:26 +1000144 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000145 return -1;
146 }
147 xfree(n);
148
149 /* currently the java applet just stores 'n' */
150 if (!BN_set_word(k->rsa->e, 35)) {
151 error("c_read_pubkey: BN_set_word(e, 35) failed");
152 return -1;
153 }
154
155 p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
156 debug("fingerprint %d %s", key_size(k), p);
157 xfree(p);
158
159 return 0;
160}
161
162/* private key operations */
163
164static int
165sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
166{
167 u_char *padded = NULL;
168 int sw, len, olen;
169
170 debug("sc_private_decrypt called");
171
172 olen = len = sw = 0;
Damien Miller694be4b2001-07-14 12:13:26 +1000173 if (sc_fd < 0)
174 if (sc_init() < 0)
175 goto err;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000176 if (padding != RSA_PKCS1_PADDING)
177 goto err;
178
179 len = BN_num_bytes(rsa->n);
180 padded = xmalloc(len);
181
182 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, from, 0, NULL, &sw);
183 if (!sectok_swOK(sw)) {
184 error("sc_private_decrypt: INS_DECRYPT failed: %s",
185 sectok_get_sw(sw));
Damien Miller694be4b2001-07-14 12:13:26 +1000186 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000187 goto err;
188 }
189 sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
190 len, padded, &sw);
191 if (!sectok_swOK(sw)) {
192 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
193 sectok_get_sw(sw));
Damien Miller694be4b2001-07-14 12:13:26 +1000194 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000195 goto err;
196 }
197 olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
198 len);
199err:
200 if (padded)
201 xfree(padded);
202 return olen;
203}
204
205static int
206sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
207{
208 u_char *padded = NULL;
209 int sw, len;
210
211 len = sw = 0;
Damien Miller694be4b2001-07-14 12:13:26 +1000212 if (sc_fd < 0)
213 if (sc_init() < 0)
214 goto err;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000215 if (padding != RSA_PKCS1_PADDING)
216 goto err;
217
218 debug("sc_private_encrypt called");
219 len = BN_num_bytes(rsa->n);
220 padded = xmalloc(len);
221
222 if (RSA_padding_add_PKCS1_type_1(padded, len, from, flen) <= 0) {
223 error("RSA_padding_add_PKCS1_type_1 failed");
224 goto err;
225 }
226 sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, 0, NULL, &sw);
227 if (!sectok_swOK(sw)) {
228 error("sc_private_decrypt: INS_DECRYPT failed: %s",
229 sectok_get_sw(sw));
Damien Miller694be4b2001-07-14 12:13:26 +1000230 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000231 goto err;
232 }
233 sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
234 len, to, &sw);
235 if (!sectok_swOK(sw)) {
236 error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
237 sectok_get_sw(sw));
Damien Miller694be4b2001-07-14 12:13:26 +1000238 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000239 goto err;
240 }
241err:
242 if (padded)
243 xfree(padded);
244 return len;
245}
246
247/* engine for overloading private key operations */
248
249static ENGINE *smart_engine = NULL;
250static RSA_METHOD smart_rsa =
251{
252 "sectok",
253 NULL,
254 NULL,
255 NULL,
256 NULL,
257 NULL,
258 NULL,
259 NULL,
260 NULL,
261 0,
262 NULL,
263};
264
265ENGINE *
266sc_get_engine(void)
267{
268 RSA_METHOD *def;
269
270 def = RSA_get_default_openssl_method();
271
272 /* overload */
273 smart_rsa.rsa_priv_enc = sc_private_encrypt;
274 smart_rsa.rsa_priv_dec = sc_private_decrypt;
275
276 /* just use the OpenSSL version */
277 smart_rsa.rsa_pub_enc = def->rsa_pub_enc;
278 smart_rsa.rsa_pub_dec = def->rsa_pub_dec;
279 smart_rsa.rsa_mod_exp = def->rsa_mod_exp;
280 smart_rsa.bn_mod_exp = def->bn_mod_exp;
281 smart_rsa.init = def->init;
282 smart_rsa.finish = def->finish;
283 smart_rsa.flags = def->flags;
284 smart_rsa.app_data = def->app_data;
285 smart_rsa.rsa_sign = def->rsa_sign;
286 smart_rsa.rsa_verify = def->rsa_verify;
287
288 smart_engine = ENGINE_new();
289
290 ENGINE_set_id(smart_engine, "sectok");
291 ENGINE_set_name(smart_engine, "libsectok");
292 ENGINE_set_RSA(smart_engine, &smart_rsa);
293 ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
294 ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
295 ENGINE_set_RAND(smart_engine, RAND_SSLeay());
296 ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
297
298 return smart_engine;
299}
300
Damien Miller694be4b2001-07-14 12:13:26 +1000301void
302sc_close(void)
303{
304 if (sc_fd >= 0) {
305 sectok_close(sc_fd);
306 sc_fd = -1;
307 }
308}
309
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000310Key *
Damien Miller694be4b2001-07-14 12:13:26 +1000311sc_get_key(int num)
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000312{
313 Key *k;
314
Damien Miller694be4b2001-07-14 12:13:26 +1000315 sc_reader_num = num;
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000316 k = key_new(KEY_RSA);
317 if (k == NULL) {
318 return NULL;
319 }
320 if (sc_read_pubkey(k) < 0) {
321 error("sc_read_pubkey failed");
322 key_free(k);
323 return NULL;
324 }
325 return k;
Damien Miller694be4b2001-07-14 12:13:26 +1000326 sc_close();
Ben Lindstrom3133dbb2001-07-04 05:35:00 +0000327}
328#endif