net/cadence_gem: Don't rx packets when no rx buffer available
[qemu.git] / libcacard / cac.c
blob74ef3e3cec12a71a537d90ccd407fffadf76cf6f
1 /*
2 * implement the applets for the CAC card.
4 * This code is licensed under the GNU LGPL, version 2.1 or later.
5 * See the COPYING.LIB file in the top-level directory.
6 */
8 #include "qemu-common.h"
10 #include "cac.h"
11 #include "vcard.h"
12 #include "vcard_emul.h"
13 #include "card_7816.h"
15 /* private data for PKI applets */
16 typedef struct CACPKIAppletDataStruct {
17 unsigned char *cert;
18 int cert_len;
19 unsigned char *cert_buffer;
20 int cert_buffer_len;
21 unsigned char *sign_buffer;
22 int sign_buffer_len;
23 VCardKey *key;
24 } CACPKIAppletData;
27 * CAC applet private data
29 struct VCardAppletPrivateStruct {
30 union {
31 CACPKIAppletData pki_data;
32 void *reserved;
33 } u;
37 * handle all the APDU's that are common to all CAC applets
39 static VCardStatus
40 cac_common_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response)
42 int ef;
43 VCardStatus ret = VCARD_FAIL;
45 switch (apdu->a_ins) {
46 case VCARD7816_INS_SELECT_FILE:
47 if (apdu->a_p1 != 0x02) {
48 /* let the 7816 code handle applet switches */
49 ret = VCARD_NEXT;
50 break;
52 /* handle file id setting */
53 if (apdu->a_Lc != 2) {
54 *response = vcard_make_response(
55 VCARD7816_STATUS_ERROR_DATA_INVALID);
56 ret = VCARD_DONE;
57 break;
59 /* CAC 1.0 only supports ef = 0 */
60 ef = apdu->a_body[0] | (apdu->a_body[1] << 8);
61 if (ef != 0) {
62 *response = vcard_make_response(
63 VCARD7816_STATUS_ERROR_FILE_NOT_FOUND);
64 ret = VCARD_DONE;
65 break;
67 *response = vcard_make_response(VCARD7816_STATUS_SUCCESS);
68 ret = VCARD_DONE;
69 break;
70 case VCARD7816_INS_GET_RESPONSE:
71 case VCARD7816_INS_VERIFY:
72 /* let the 7816 code handle these */
73 ret = VCARD_NEXT;
74 break;
75 case CAC_GET_PROPERTIES:
76 case CAC_GET_ACR:
77 /* skip these for now, this will probably be needed */
78 *response = vcard_make_response(VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
79 ret = VCARD_DONE;
80 break;
81 default:
82 *response = vcard_make_response(
83 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
84 ret = VCARD_DONE;
85 break;
87 return ret;
91 * reset the inter call state between applet selects
93 static VCardStatus
94 cac_applet_pki_reset(VCard *card, int channel)
96 VCardAppletPrivate *applet_private = NULL;
97 CACPKIAppletData *pki_applet = NULL;
98 applet_private = vcard_get_current_applet_private(card, channel);
99 assert(applet_private);
100 pki_applet = &(applet_private->u.pki_data);
102 pki_applet->cert_buffer = NULL;
103 if (pki_applet->sign_buffer) {
104 g_free(pki_applet->sign_buffer);
105 pki_applet->sign_buffer = NULL;
107 pki_applet->cert_buffer_len = 0;
108 pki_applet->sign_buffer_len = 0;
109 return VCARD_DONE;
112 static VCardStatus
113 cac_applet_pki_process_apdu(VCard *card, VCardAPDU *apdu,
114 VCardResponse **response)
116 CACPKIAppletData *pki_applet = NULL;
117 VCardAppletPrivate *applet_private = NULL;
118 int size, next;
119 unsigned char *sign_buffer;
120 vcard_7816_status_t status;
121 VCardStatus ret = VCARD_FAIL;
123 applet_private = vcard_get_current_applet_private(card, apdu->a_channel);
124 assert(applet_private);
125 pki_applet = &(applet_private->u.pki_data);
127 switch (apdu->a_ins) {
128 case CAC_UPDATE_BUFFER:
129 *response = vcard_make_response(
130 VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED);
131 ret = VCARD_DONE;
132 break;
133 case CAC_GET_CERTIFICATE:
134 if ((apdu->a_p2 != 0) || (apdu->a_p1 != 0)) {
135 *response = vcard_make_response(
136 VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
137 break;
139 assert(pki_applet->cert != NULL);
140 size = apdu->a_Le;
141 if (pki_applet->cert_buffer == NULL) {
142 pki_applet->cert_buffer = pki_applet->cert;
143 pki_applet->cert_buffer_len = pki_applet->cert_len;
145 size = MIN(size, pki_applet->cert_buffer_len);
146 next = MIN(255, pki_applet->cert_buffer_len - size);
147 *response = vcard_response_new_bytes(
148 card, pki_applet->cert_buffer, size,
149 apdu->a_Le, next ?
150 VCARD7816_SW1_WARNING_CHANGE :
151 VCARD7816_SW1_SUCCESS,
152 next);
153 pki_applet->cert_buffer += size;
154 pki_applet->cert_buffer_len -= size;
155 if ((*response == NULL) || (next == 0)) {
156 pki_applet->cert_buffer = NULL;
158 if (*response == NULL) {
159 *response = vcard_make_response(
160 VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
162 ret = VCARD_DONE;
163 break;
164 case CAC_SIGN_DECRYPT:
165 if (apdu->a_p2 != 0) {
166 *response = vcard_make_response(
167 VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
168 break;
170 size = apdu->a_Lc;
172 sign_buffer = realloc(pki_applet->sign_buffer,
173 pki_applet->sign_buffer_len+size);
174 if (sign_buffer == NULL) {
175 g_free(pki_applet->sign_buffer);
176 pki_applet->sign_buffer = NULL;
177 pki_applet->sign_buffer_len = 0;
178 *response = vcard_make_response(
179 VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
180 ret = VCARD_DONE;
181 break;
183 memcpy(sign_buffer+pki_applet->sign_buffer_len, apdu->a_body, size);
184 size += pki_applet->sign_buffer_len;
185 switch (apdu->a_p1) {
186 case 0x80:
187 /* p1 == 0x80 means we haven't yet sent the whole buffer, wait for
188 * the rest */
189 pki_applet->sign_buffer = sign_buffer;
190 pki_applet->sign_buffer_len = size;
191 *response = vcard_make_response(VCARD7816_STATUS_SUCCESS);
192 break;
193 case 0x00:
194 /* we now have the whole buffer, do the operation, result will be
195 * in the sign_buffer */
196 status = vcard_emul_rsa_op(card, pki_applet->key,
197 sign_buffer, size);
198 if (status != VCARD7816_STATUS_SUCCESS) {
199 *response = vcard_make_response(status);
200 break;
202 *response = vcard_response_new(card, sign_buffer, size, apdu->a_Le,
203 VCARD7816_STATUS_SUCCESS);
204 if (*response == NULL) {
205 *response = vcard_make_response(
206 VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
208 break;
209 default:
210 *response = vcard_make_response(
211 VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
212 break;
214 g_free(sign_buffer);
215 pki_applet->sign_buffer = NULL;
216 pki_applet->sign_buffer_len = 0;
217 ret = VCARD_DONE;
218 break;
219 case CAC_READ_BUFFER:
220 /* new CAC call, go ahead and use the old version for now */
221 /* TODO: implement */
222 *response = vcard_make_response(
223 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
224 ret = VCARD_DONE;
225 break;
226 default:
227 ret = cac_common_process_apdu(card, apdu, response);
228 break;
230 return ret;
234 static VCardStatus
235 cac_applet_id_process_apdu(VCard *card, VCardAPDU *apdu,
236 VCardResponse **response)
238 VCardStatus ret = VCARD_FAIL;
240 switch (apdu->a_ins) {
241 case CAC_UPDATE_BUFFER:
242 *response = vcard_make_response(
243 VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED);
244 ret = VCARD_DONE;
245 break;
246 case CAC_READ_BUFFER:
247 /* new CAC call, go ahead and use the old version for now */
248 /* TODO: implement */
249 *response = vcard_make_response(
250 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
251 ret = VCARD_DONE;
252 break;
253 default:
254 ret = cac_common_process_apdu(card, apdu, response);
255 break;
257 return ret;
262 * TODO: if we ever want to support general CAC middleware, we will need to
263 * implement the various containers.
265 static VCardStatus
266 cac_applet_container_process_apdu(VCard *card, VCardAPDU *apdu,
267 VCardResponse **response)
269 VCardStatus ret = VCARD_FAIL;
271 switch (apdu->a_ins) {
272 case CAC_READ_BUFFER:
273 case CAC_UPDATE_BUFFER:
274 *response = vcard_make_response(
275 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
276 ret = VCARD_DONE;
277 break;
278 default:
279 ret = cac_common_process_apdu(card, apdu, response);
280 break;
282 return ret;
286 * utilities for creating and destroying the private applet data
288 static void
289 cac_delete_pki_applet_private(VCardAppletPrivate *applet_private)
291 CACPKIAppletData *pki_applet_data = NULL;
293 if (applet_private == NULL) {
294 return;
296 pki_applet_data = &(applet_private->u.pki_data);
297 if (pki_applet_data->cert != NULL) {
298 g_free(pki_applet_data->cert);
300 if (pki_applet_data->sign_buffer != NULL) {
301 g_free(pki_applet_data->sign_buffer);
303 if (pki_applet_data->key != NULL) {
304 vcard_emul_delete_key(pki_applet_data->key);
306 g_free(applet_private);
309 static VCardAppletPrivate *
310 cac_new_pki_applet_private(const unsigned char *cert,
311 int cert_len, VCardKey *key)
313 CACPKIAppletData *pki_applet_data = NULL;
314 VCardAppletPrivate *applet_private = NULL;
315 applet_private = (VCardAppletPrivate *)g_malloc(sizeof(VCardAppletPrivate));
317 pki_applet_data = &(applet_private->u.pki_data);
318 pki_applet_data->cert_buffer = NULL;
319 pki_applet_data->cert_buffer_len = 0;
320 pki_applet_data->sign_buffer = NULL;
321 pki_applet_data->sign_buffer_len = 0;
322 pki_applet_data->key = NULL;
323 pki_applet_data->cert = (unsigned char *)g_malloc(cert_len+1);
325 * if we want to support compression, then we simply change the 0 to a 1
326 * and compress the cert data with libz
328 pki_applet_data->cert[0] = 0; /* not compressed */
329 memcpy(&pki_applet_data->cert[1], cert, cert_len);
330 pki_applet_data->cert_len = cert_len+1;
332 pki_applet_data->key = key;
333 return applet_private;
338 * create a new cac applet which links to a given cert
340 static VCardApplet *
341 cac_new_pki_applet(int i, const unsigned char *cert,
342 int cert_len, VCardKey *key)
344 VCardAppletPrivate *applet_private = NULL;
345 VCardApplet *applet = NULL;
346 unsigned char pki_aid[] = { 0xa0, 0x00, 0x00, 0x00, 0x79, 0x01, 0x00 };
347 int pki_aid_len = sizeof(pki_aid);
349 pki_aid[pki_aid_len-1] = i;
351 applet_private = cac_new_pki_applet_private(cert, cert_len, key);
352 if (applet_private == NULL) {
353 goto failure;
355 applet = vcard_new_applet(cac_applet_pki_process_apdu, cac_applet_pki_reset,
356 pki_aid, pki_aid_len);
357 if (applet == NULL) {
358 goto failure;
360 vcard_set_applet_private(applet, applet_private,
361 cac_delete_pki_applet_private);
362 applet_private = NULL;
364 return applet;
366 failure:
367 if (applet_private != NULL) {
368 cac_delete_pki_applet_private(applet_private);
370 return NULL;
374 static unsigned char cac_default_container_aid[] = {
375 0xa0, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00 };
376 static unsigned char cac_id_aid[] = {
377 0xa0, 0x00, 0x00, 0x00, 0x79, 0x03, 0x00 };
379 * Initialize the cac card. This is the only public function in this file. All
380 * the rest are connected through function pointers.
382 VCardStatus
383 cac_card_init(VReader *reader, VCard *card,
384 const char *params,
385 unsigned char * const *cert,
386 int cert_len[],
387 VCardKey *key[] /* adopt the keys*/,
388 int cert_count)
390 int i;
391 VCardApplet *applet;
393 /* CAC Cards are VM Cards */
394 vcard_set_type(card, VCARD_VM);
396 /* create one PKI applet for each cert */
397 for (i = 0; i < cert_count; i++) {
398 applet = cac_new_pki_applet(i, cert[i], cert_len[i], key[i]);
399 if (applet == NULL) {
400 goto failure;
402 vcard_add_applet(card, applet);
405 /* create a default blank container applet */
406 applet = vcard_new_applet(cac_applet_container_process_apdu,
407 NULL, cac_default_container_aid,
408 sizeof(cac_default_container_aid));
409 if (applet == NULL) {
410 goto failure;
412 vcard_add_applet(card, applet);
414 /* create a default blank container applet */
415 applet = vcard_new_applet(cac_applet_id_process_apdu,
416 NULL, cac_id_aid,
417 sizeof(cac_id_aid));
418 if (applet == NULL) {
419 goto failure;
421 vcard_add_applet(card, applet);
422 return VCARD_DONE;
424 failure:
425 return VCARD_FAIL;