1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/* $Id: cyberflex.c,v 1.2 2001/06/25 23:14:29 markus Exp $ */
/*
copyright 2000
the regents of the university of michigan
all rights reserved
permission is granted to use, copy, create derivative works
and redistribute this software and such derivative works
for any purpose, so long as the name of the university of
michigan is not used in any advertising or publicity
pertaining to the use or distribution of this software
without specific, written prior authorization. if the
above copyright notice or any other identification of the
university of michigan is included in any copy of any
portion of this software, then the disclaimer below must
also be included.
this software is provided as is, without representation
from the university of michigan as to its fitness for any
purpose, and without warranty by the university of
michigan of any kind, either express or implied, including
without limitation the implied warranties of
merchantability and fitness for a particular purpose. the
regents of the university of michigan shall not be liable
for any damages, including special, indirect, incidental, or
consequential damages, with respect to any claim arising
out of or in connection with the use of the software, even
if it has been or is hereafter advised of the possibility of
such damages.
*/
/*
* Cyberflex routines
*
* University of Michigan CITI, July 2001
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#ifdef __linux
#include <openssl/des.h>
#else /* __linux */
#include <des.h>
#endif
#include "sectok.h"
#define MAX_APDU_SIZE 0xfa
#define MAX_KEY_FILE_SIZE 1024
#define PRV_KEY_SIZE 64*6
#define key_number 0x10
#define key_type 0xc8 /* key type 0xc8 (1024 bit RSA private) */
#define KEY_FILE_HEADER_SIZE 8
static unsigned char root_fid[] = {0x3f, 0x00};
int
cyberflex_load_rsa_pub(int fd, int cla, unsigned char *key_fid,
int key_len, unsigned char *key_data)
{
int rv, i, r1, r2;
unsigned char data[16];
char kfname[5];
rv = sectok_selectfile(fd, cla, root_fid, 0);
if (rv < 0)
return rv;
sectok_fmt_fid(kfname, key_fid[0], key_fid[1]);
rv = sectok_selectfile(fd, cla, key_fid, 0);
if (rv < 0) {
printf ("public key file does not exist. create it.\n");
data[0] = 0x01;
data[1] = 0x00;
data[2] = key_fid[0];
data[3] = key_fid[1];
data[4] = 0x02; /* file type = 2 (binary file) */
data[5] = 0x01; /* status = 1 */
data[6] = data[7] = 0x00; /* record related */
data[8] = 0xff; /* ACL can do everything with AUT0 */
for (i = 9; i < 16; i++ ) {
data[i] = 0x00; /* ACL : cannot do anything without AUT0 */
}
rv = scwrite(fd, cla, 0xe0, 0, 0, 0x10, data, &r1, &r2);
if (r1 != 0x90 && r1 != 0x61) {
/* error */
printf("creating file %s: %s\n", kfname, get_r1r2s(r1, r2));
return -1;
}
rv = sectok_selectfile(fd, cla, key_fid, 0);
}
/* Write the key data */
rv = scwrite(fd, cla, 0xd6, 0, 0, key_len, key_data, &r1, &r2);
if (r1 != 0x90 && r1 != 0x61) {
/* error */
printf("creating file %s: %s\n", kfname, get_r1r2s(r1, r2));
return -1;
}
return rv;
}
/* download RSA private key into 3f.00/00.12 */
int
cyberflex_load_rsa_priv(int fd, int cla, unsigned char *key_fid,
int nkey_elems, int key_len, unsigned char *key_elems[])
{
int i, j, rv, r1, r2, offset=0, size;
unsigned char data[MAX_KEY_FILE_SIZE];
static unsigned char key_file_header[KEY_FILE_HEADER_SIZE] =
{0xC2, 0x06, 0xC1, 0x08, 0x13, 0x00, 0x00, 0x05};
static unsigned char key_header[3] = {0xC2, 0x41, 0x00};
char kfname[5];
/* select 3f.00 */
rv = sectok_selectfile(fd, cla, root_fid, 0);
if (rv < 0) return rv;
/* select 00.12 */
sectok_fmt_fid(kfname, key_fid[0], key_fid[1]);
rv = sectok_selectfile(fd, cla, key_fid, 0);
if (rv < 0) {
/* rv != 0, 00.12 does not exist. create it. */
printf ("private key file does not exist. create it.\n");
/* create private key file */
data[0] = (PRV_KEY_SIZE + 16) / 256; /* size, upper byte */
data[1] = (PRV_KEY_SIZE + 16) % 256; /* size, lower byte */
data[2] = key_fid[0];
data[3] = key_fid[1];
data[4] = 0x03; /* file type */
data[5] = 0x01; /* status = 1 */
data[6] = data[7] = 0x00; /* record related */
data[8] = 0xff; /* ACL can do everything with AUT0 */
for (i = 9; i < 16; i++ ) {
data[i] = 0x00; /* ACL : cannot do anything without AUT0 */
}
rv = scwrite(fd, cla, 0xe0, 0, 0, 0x10, data, &r1, &r2);
if (r1 != 0x90 && r1 != 0x61) {
/* error */
printf("creating file %s: %s\n", kfname, get_r1r2s(r1, r2));
return -1;
}
}
/* burn the key */
data[0] = 0x01; /* key size, I guess */
data[1] = 0x5b; /* key size, I guess */
data[2] = key_number; /* key number */
data[3] = key_type;
offset = 4;
for (j = 0 ; j < KEY_FILE_HEADER_SIZE ; j ++)
data[offset++] = key_file_header[j];
for (i = 0 ; i < nkey_elems; i ++) {
/* put the key header */
for (j = 0 ; j < 3 ; j ++) {
data[offset++] = key_header[j];
}
for (j = 0 ; j < key_len/2/8 ; j ++) {
data[offset++] = key_elems [i][j];
}
}
for (j = 0 ; j < 2 ; j ++) data[offset++] = 0;
#ifdef DEBUG
printf ("data:\n");
for (i = 0 ; i < 0x015d; i ++) {
printf ("%02x ", data[i]);
}
printf ("\n");
#endif
/* now send this to the card */
/* select private key file */
rv = sectok_selectfile(fd, cla, key_fid, 0);
if (rv < 0) return rv;
/* update binary */
size = offset;
for (i = 0; i < size; i += MAX_APDU_SIZE) {
int send_size;
/* compute the size to be sent */
if (size - i > MAX_APDU_SIZE) send_size = MAX_APDU_SIZE;
else send_size = size - i;
rv = scwrite(fd, cla, 0xd6,
i / 256, /* offset, upper byte */
i % 256, /* offset, lower byte */
send_size,
data + i, /* key file */
&r1, &r2);
if (r1 != 0x90 && r1 != 0x61) {
/* error */
printf("updating binary %s: %s\n", kfname, get_r1r2s(r1, r2));
return -1;
}
}
printf ("rsa key loading done! :)\n");
return 0;
}
int
cyberflex_verify_AUT0(int fd, int cla, unsigned char *aut0, int aut0len)
{
int n, r1, r2;
n = scwrite(fd, cla, 0x2a, 0, 0, aut0len, aut0, &r1, &r2);
if (n >= 0 && cla == 0 && r1 == 0x6d) {
/* F0 card? */
n = scwrite(fd, 0xf0, 0x2a, 0, 0, aut0len, aut0, &r1, &r2);
if (r1 == 0x90) {
printf("class F0\n");
cla = 0xf0;
}
}
printf("Verify key: ");
if (n < 0) {
printf("scwrite failed\n");
return -1;
}
dump_reply(NULL, 0, r1, r2);
return 0;
}
|