Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 302769 | Differences between
and this patch

Collapse All | Expand All

(-)src/coolkey/slot.h (+9 lines)
Lines 294-299 Link Here
294
				 const CKYBuffer *paddedOutput) const = 0;
294
				 const CKYBuffer *paddedOutput) const = 0;
295
};
295
};
296
296
297
#define MAX_CERT_SLOTS 3
297
class Slot {
298
class Slot {
298
299
299
  public:
300
  public:
Lines 328-333 Link Here
328
    CKYBuffer nonce;
329
    CKYBuffer nonce;
329
    CKYBuffer cardATR;
330
    CKYBuffer cardATR;
330
    CKYBuffer mCUID;
331
    CKYBuffer mCUID;
332
    CKYBuffer cardAID[MAX_CERT_SLOTS];
333
    unsigned short cardEF[MAX_CERT_SLOTS];
331
    bool isVersion1Key;
334
    bool isVersion1Key;
332
    bool needLogin;
335
    bool needLogin;
333
    long publicFree;
336
    long publicFree;
Lines 335-340 Link Here
335
    long privateFree;
338
    long privateFree;
336
    bool fullTokenName;
339
    bool fullTokenName;
337
    bool mCoolkey;
340
    bool mCoolkey;
341
    bool mOldCAC;
338
342
339
    //enum { RW_SESSION_HANDLE = 1, RO_SESSION_HANDLE = 2 };
343
    //enum { RW_SESSION_HANDLE = 1, RO_SESSION_HANDLE = 2 };
340
344
Lines 398-403 Link Here
398
    list<ListObjectInfo> fetchCombinedObjects(const CKYBuffer *header);
402
    list<ListObjectInfo> fetchCombinedObjects(const CKYBuffer *header);
399
    list<ListObjectInfo> fetchSeparateObjects();
403
    list<ListObjectInfo> fetchSeparateObjects();
400
404
405
    CKYStatus getCACAid();
406
    CKYStatus readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize,
407
                              bool throwException);
408
    CKYStatus readCACCertificateAppend(CKYBuffer *cert, CKYSize nextSize);
409
401
    void selectApplet();
410
    void selectApplet();
402
    void selectCACApplet(CKYByte instance);
411
    void selectCACApplet(CKYByte instance);
403
    void unloadObjects();
412
    void unloadObjects();
(-)src/coolkey/object.cpp (+4 lines)
Lines 505-510 Link Here
505
    unsigned char tag;
505
    unsigned char tag;
506
    unsigned int used_length= 0;
506
    unsigned int used_length= 0;
507
507
508
    if(!buf) {
509
        return NULL;
510
    }
511
508
    tag = buf[used_length++];
512
    tag = buf[used_length++];
509
513
510
    /* blow out when we come to the end */
514
    /* blow out when we come to the end */
(-)src/libckyapplet/cky_base.c (+94 lines)
Lines 220-225 Link Here
220
    return CKYSUCCESS;
220
    return CKYSUCCESS;
221
}
221
}
222
222
223
/* append a short in network order */
224
CKYStatus
225
CKYBuffer_AppendShortLE(CKYBuffer *buf, unsigned short val)
226
{
227
    CKYStatus ret;
228
229
    ret = CKYBuffer_Reserve(buf, buf->len + 2);
230
    if (ret != CKYSUCCESS) {
231
	return ret;
232
    }
233
    buf->data[buf->len+1] = (CKYByte) ((val >> 8) & 0xff);
234
    buf->data[buf->len+0] = (CKYByte) ((val >> 0) & 0xff);
235
    buf->len += 2;
236
    return CKYSUCCESS;
237
}
238
223
/* append a long in applet order */
239
/* append a long in applet order */
224
CKYStatus
240
CKYStatus
225
CKYBuffer_AppendLong(CKYBuffer *buf, unsigned long val)
241
CKYBuffer_AppendLong(CKYBuffer *buf, unsigned long val)
Lines 238-243 Link Here
238
    return CKYSUCCESS;
254
    return CKYSUCCESS;
239
}
255
}
240
256
257
/* append a long in applet order */
258
CKYStatus
259
CKYBuffer_AppendLongLE(CKYBuffer *buf, unsigned long val)
260
{
261
    CKYStatus ret;
262
263
    ret = CKYBuffer_Reserve(buf, buf->len + 4);
264
    if (ret != CKYSUCCESS) {
265
	return ret;
266
    }
267
    buf->data[buf->len+3] = (CKYByte) ((val >> 24) & 0xff);
268
    buf->data[buf->len+2] = (CKYByte) ((val >> 16) & 0xff);
269
    buf->data[buf->len+1] = (CKYByte) ((val >>  8) & 0xff);
270
    buf->data[buf->len+0] = (CKYByte) ((val >>  0) & 0xff);
271
    buf->len += 4;
272
    return CKYSUCCESS;
273
}
274
241
CKYStatus
275
CKYStatus
242
CKYBuffer_Replace(CKYBuffer *buf, CKYOffset offset, const CKYByte *data, CKYSize len)
276
CKYBuffer_Replace(CKYBuffer *buf, CKYOffset offset, const CKYByte *data, CKYSize len)
243
{
277
{
Lines 351-356 Link Here
351
}
385
}
352
386
353
CKYStatus
387
CKYStatus
388
CKYBuffer_SetShortLE(CKYBuffer *buf, CKYOffset offset, unsigned short val)
389
{
390
    CKYStatus ret;
391
392
    if (buf->len < offset+2) {
393
	ret = CKYBuffer_Resize(buf,offset+2);
394
	if (ret != CKYSUCCESS) {
395
	    return ret;
396
	}
397
    }
398
    buf->data[offset+1] = (CKYByte) ((val >> 8) & 0xff);
399
    buf->data[offset+0] = (CKYByte) ((val >> 0) & 0xff);
400
    return CKYSUCCESS;
401
}
402
403
CKYStatus
354
CKYBuffer_SetLong(CKYBuffer *buf, CKYOffset offset, unsigned long val)
404
CKYBuffer_SetLong(CKYBuffer *buf, CKYOffset offset, unsigned long val)
355
{
405
{
356
    CKYStatus ret;
406
    CKYStatus ret;
Lines 368-373 Link Here
368
    return CKYSUCCESS;
418
    return CKYSUCCESS;
369
}
419
}
370
420
421
CKYStatus
422
CKYBuffer_SetLongLE(CKYBuffer *buf, CKYOffset offset, unsigned long val)
423
{
424
    CKYStatus ret;
425
426
    if (buf->len < offset+4) {
427
	ret = CKYBuffer_Resize(buf,offset+4);
428
	if (ret != CKYSUCCESS) {
429
	    return ret;
430
	}
431
    }
432
    buf->data[offset+3] = (CKYByte) ((val >> 24) & 0xff);
433
    buf->data[offset+2] = (CKYByte) ((val >> 16) & 0xff);
434
    buf->data[offset+1] = (CKYByte) ((val >>  8) & 0xff);
435
    buf->data[offset+0] = (CKYByte) ((val >>  0) & 0xff);
436
    return CKYSUCCESS;
437
}
438
371
CKYByte
439
CKYByte
372
CKYBuffer_GetChar(const CKYBuffer *buf, CKYOffset offset)
440
CKYBuffer_GetChar(const CKYBuffer *buf, CKYOffset offset)
373
{
441
{
Lines 389-394 Link Here
389
    return val;
457
    return val;
390
}
458
}
391
	
459
	
460
unsigned short
461
CKYBuffer_GetShortLE(const CKYBuffer *buf, CKYOffset offset)
462
{
463
    unsigned short val;
464
    if (buf->len < offset+2) {
465
	return 0;
466
    }
467
    val  = ((unsigned short)buf->data[offset+1]) << 8;
468
    val |= ((unsigned short)buf->data[offset+0]) << 0;
469
    return val;
470
}
471
	
392
unsigned long
472
unsigned long
393
CKYBuffer_GetLong(const CKYBuffer *buf, CKYOffset offset)
473
CKYBuffer_GetLong(const CKYBuffer *buf, CKYOffset offset)
394
{
474
{
Lines 403-408 Link Here
403
    return val;
483
    return val;
404
}
484
}
405
	
485
	
486
unsigned long
487
CKYBuffer_GetLongLE(const CKYBuffer *buf, CKYOffset offset)
488
{
489
    unsigned long val;
490
    if (buf->len < offset+4) {
491
	return 0;
492
    }
493
    val  = ((unsigned long)buf->data[offset+3]) << 24;
494
    val |= ((unsigned long)buf->data[offset+2]) << 16;
495
    val |= ((unsigned long)buf->data[offset+1]) << 8;
496
    val |= ((unsigned long)buf->data[offset+0]) << 0;
497
    return val;
498
}
499
	
406
CKYStatus
500
CKYStatus
407
CKYBuffer_Resize(CKYBuffer *buf, CKYSize newLen)
501
CKYBuffer_Resize(CKYBuffer *buf, CKYSize newLen)
408
{
502
{
(-)src/libckyapplet/cky_base.h (+12 lines)
Lines 170-178 Link Here
170
/* append a short in applet order */
170
/* append a short in applet order */
171
CKYStatus CKYBuffer_AppendShort(CKYBuffer *buf, unsigned short val);
171
CKYStatus CKYBuffer_AppendShort(CKYBuffer *buf, unsigned short val);
172
172
173
/* append a short in little endian order */
174
CKYStatus CKYBuffer_AppendShortLE(CKYBuffer *buf, unsigned short val);
175
173
/* append a long in applet order */
176
/* append a long in applet order */
174
CKYStatus CKYBuffer_AppendLong(CKYBuffer *buf, unsigned long val);
177
CKYStatus CKYBuffer_AppendLong(CKYBuffer *buf, unsigned long val);
175
178
179
/* append a long in little endian order */
180
CKYStatus CKYBuffer_AppendLongLE(CKYBuffer *buf, unsigned long val);
181
176
/* append data. the data starts at data and extends len bytes */
182
/* append data. the data starts at data and extends len bytes */
177
CKYStatus CKYBuffer_AppendData(CKYBuffer *buf, const CKYByte *data, CKYSize len);
183
CKYStatus CKYBuffer_AppendData(CKYBuffer *buf, const CKYByte *data, CKYSize len);
178
184
Lines 210-221 Link Here
210
CKYStatus CKYBuffer_SetShort(CKYBuffer *buf, CKYOffset offset, unsigned short val);
216
CKYStatus CKYBuffer_SetShort(CKYBuffer *buf, CKYOffset offset, unsigned short val);
211
CKYStatus CKYBuffer_SetLong(CKYBuffer *buf, CKYOffset offset, unsigned long val);
217
CKYStatus CKYBuffer_SetLong(CKYBuffer *buf, CKYOffset offset, unsigned long val);
212
218
219
/* These functions work in little endian order */
220
CKYStatus CKYBuffer_SetShortLE(CKYBuffer *buf, CKYOffset offset, unsigned short val);
221
CKYStatus CKYBuffer_SetLongLE(CKYBuffer *buf, CKYOffset offset, unsigned long val);
213
/* read a character from offset. If offset is beyond the end of the buffer,
222
/* read a character from offset. If offset is beyond the end of the buffer,
214
 * then the function returns '0' */
223
 * then the function returns '0' */
215
CKYByte CKYBuffer_GetChar(const CKYBuffer *buf, CKYOffset offset);
224
CKYByte CKYBuffer_GetChar(const CKYBuffer *buf, CKYOffset offset);
216
/* These functions work in applet order */
225
/* These functions work in applet order */
217
unsigned short CKYBuffer_GetShort(const CKYBuffer *buf, CKYOffset offset);
226
unsigned short CKYBuffer_GetShort(const CKYBuffer *buf, CKYOffset offset);
218
unsigned long CKYBuffer_GetLong(const CKYBuffer *buf, CKYOffset offset);
227
unsigned long CKYBuffer_GetLong(const CKYBuffer *buf, CKYOffset offset);
228
/* These functions work in little endian order */
229
unsigned short CKYBuffer_GetShortLE(const CKYBuffer *buf, CKYOffset offset);
230
unsigned long CKYBuffer_GetLongLE(const CKYBuffer *buf, CKYOffset offset);
219
231
220
/* clear out all the data in a buffer */
232
/* clear out all the data in a buffer */
221
void CKYBuffer_Zero(CKYBuffer *buf);
233
void CKYBuffer_Zero(CKYBuffer *buf);
(-)src/libckyapplet/cky_card.h (-12 / +12 lines)
Lines 41-63 Link Here
41
CKYLIST_DECLARE(CKYCardConnection, CKYCardConnection *)
41
CKYLIST_DECLARE(CKYCardConnection, CKYCardConnection *)
42
42
43
CKY_BEGIN_PROTOS
43
CKY_BEGIN_PROTOS
44
void CKYReader_Init(SCARD_READERSTATE_A *reader);
44
void CKYReader_Init(SCARD_READERSTATE *reader);
45
void CKYReader_FreeData(SCARD_READERSTATE_A *reader);
45
void CKYReader_FreeData(SCARD_READERSTATE *reader);
46
46
47
/*
47
/*
48
 * "Accessors": for SCARD_READERSTATE_A structure as a class.
48
 * "Accessors": for SCARD_READERSTATE structure as a class.
49
 * These functions take an SCARD_READERSTATE_A which can also be referenced
49
 * These functions take an SCARD_READERSTATE which can also be referenced
50
 * directly.
50
 * directly.
51
 */
51
 */
52
CKYStatus CKYReader_SetReaderName(SCARD_READERSTATE_A *reader, const char *name);
52
CKYStatus CKYReader_SetReaderName(SCARD_READERSTATE *reader, const char *name);
53
const char *CKYReader_GetReaderName(const SCARD_READERSTATE_A *reader);
53
const char *CKYReader_GetReaderName(const SCARD_READERSTATE *reader);
54
CKYStatus CKYReader_SetKnownState(SCARD_READERSTATE_A *reader, 
54
CKYStatus CKYReader_SetKnownState(SCARD_READERSTATE *reader, 
55
						unsigned long state);
55
						unsigned long state);
56
unsigned long CKYReader_GetKnownState(const SCARD_READERSTATE_A *reader);
56
unsigned long CKYReader_GetKnownState(const SCARD_READERSTATE *reader);
57
unsigned long CKYReader_GetEventState(const SCARD_READERSTATE_A *reader);
57
unsigned long CKYReader_GetEventState(const SCARD_READERSTATE *reader);
58
CKYStatus CKYReader_GetATR(const SCARD_READERSTATE_A *reader, CKYBuffer *buf);
58
CKYStatus CKYReader_GetATR(const SCARD_READERSTATE *reader, CKYBuffer *buf);
59
/* create an array of READERSTATEs from a LIST of Readers */
59
/* create an array of READERSTATEs from a LIST of Readers */
60
SCARD_READERSTATE_A *CKYReader_CreateArray(const CKYReaderNameList readerNames, 
60
SCARD_READERSTATE *CKYReader_CreateArray(const CKYReaderNameList readerNames, 
61
					  unsigned long *readerCount);
61
					  unsigned long *readerCount);
62
/* frees the reader, then the full array */
62
/* frees the reader, then the full array */
63
void CKYReader_DestroyArray(SCARD_READERSTATE *reader, unsigned long count);
63
void CKYReader_DestroyArray(SCARD_READERSTATE *reader, unsigned long count);
Lines 88-94 Link Here
88
				const CKYBuffer *targetATR);
88
				const CKYBuffer *targetATR);
89
/* return if any of the readers in our array has changed in status */
89
/* return if any of the readers in our array has changed in status */
90
CKYStatus CKYCardContext_WaitForStatusChange(CKYCardContext *context,
90
CKYStatus CKYCardContext_WaitForStatusChange(CKYCardContext *context,
91
				SCARD_READERSTATE_A *readers,
91
				SCARD_READERSTATE *readers,
92
				unsigned long readerCount,
92
				unsigned long readerCount,
93
				unsigned long timeout);
93
				unsigned long timeout);
94
/* cancel any current operation (such as wait for status change) on this
94
/* cancel any current operation (such as wait for status change) on this
(-)src/libckyapplet/cky_applet.c (-19 / +152 lines)
Lines 41-47 Link Here
41
CKYStatus
41
CKYStatus
42
CKYAppletFactory_SelectFile(CKYAPDU *apdu, const void *param)
42
CKYAppletFactory_SelectFile(CKYAPDU *apdu, const void *param)
43
{
43
{
44
    return CKYAPDUFactory_SelectFile(apdu,(const CKYBuffer *)param);
44
    return CKYAPDUFactory_SelectFile(apdu, 4, 0, (const CKYBuffer *)param);
45
}
46
47
CKYStatus
48
CACAppletFactory_SelectFile(CKYAPDU *apdu, const void *param)
49
{
50
    return CKYAPDUFactory_SelectFile(apdu, 2, 12, (const CKYBuffer *)param);
45
}
51
}
46
52
47
CKYStatus
53
CKYStatus
Lines 225-234 Link Here
225
}
231
}
226
232
227
CKYStatus
233
CKYStatus
228
CACAppletFactory_SignDecrypt(CKYAPDU *apdu, const void *param)
234
CACAppletFactory_SignDecryptStep(CKYAPDU *apdu, const void *param)
235
{
236
    const CKYBuffer *buf=(CKYBuffer *)param;
237
    return CACAPDUFactory_SignDecrypt(apdu, CAC_P1_STEP, buf);
238
}
239
240
CKYStatus
241
CACAppletFactory_SignDecryptFinal(CKYAPDU *apdu, const void *param)
229
{
242
{
230
    const CKYBuffer *buf=(CKYBuffer *)param;
243
    const CKYBuffer *buf=(CKYBuffer *)param;
231
    return CACAPDUFactory_SignDecrypt(apdu, buf);
244
    return CACAPDUFactory_SignDecrypt(apdu, CAC_P1_FINAL, buf);
232
}
245
}
233
246
234
CKYStatus
247
CKYStatus
Lines 246-251 Link Here
246
}
259
}
247
260
248
CKYStatus
261
CKYStatus
262
CACAppletFactory_ReadFile(CKYAPDU *apdu, const void *param)
263
{
264
    const CACAppletArgReadFile *rfs = (const CACAppletArgReadFile *)param;
265
    return CACAPDUFactory_ReadFile(apdu, rfs->offset, rfs->type, rfs->count);
266
}
267
268
CKYStatus
249
CACAppletFactory_GetProperties(CKYAPDU *apdu, const void *param)
269
CACAppletFactory_GetProperties(CKYAPDU *apdu, const void *param)
250
{
270
{
251
    return CACAPDUFactory_GetProperties(apdu);
271
    return CACAPDUFactory_GetProperties(apdu);
Lines 457-463 Link Here
457
							 CKYISOStatus *apduRC)
477
							 CKYISOStatus *apduRC)
458
{
478
{
459
    return CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, AID, NULL,
479
    return CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, AID, NULL,
460
		0, CKYAppletFill_Null, NULL, apduRC);
480
		CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC);
461
}
481
}
462
482
463
static CKYByte coolkeyid[] = {0x62, 0x76, 0x01, 0xff, 0x00, 0x00, 0x00 };
483
static CKYByte coolkeyid[] = {0x62, 0x76, 0x01, 0xff, 0x00, 0x00, 0x00 };
Lines 477-498 Link Here
477
    return ret;
497
    return ret;
478
}
498
}
479
499
480
static CKYByte CACPKIid[] = {0xa0, 0x00, 0x00, 0x00, 0x79, 0x01, 0x00 };
500
static CKYByte CACPKIid[] = { 0xa0, 0x00, 0x00, 0x00, 0x79, 0x01 };
481
/*
501
/*
482
 * Select the CoolKey applet. Must happen after we start a transaction and 
502
 * Select the CoolKey applet. Must happen after we start a transaction and 
483
 * before we issue any applet specific command.
503
 * before we issue any applet specific command.
484
 */
504
 */
485
CKYStatus
505
CKYStatus
486
CACApplet_SelectPKI(CKYCardConnection *conn, CKYByte instance, 
506
CACApplet_SelectPKI(CKYCardConnection *conn, CKYBuffer *cacAID, 
487
			       CKYISOStatus *apduRC)
507
				CKYByte instance, CKYISOStatus *apduRC)
488
{
508
{
489
    CKYStatus ret;
509
    CKYStatus ret;
490
    CKYBuffer CACPKIAID;
510
    CKYBuffer_AppendData(cacAID, CACPKIid, sizeof(CACPKIid));
491
    CKYBuffer_InitFromData(&CACPKIAID, CACPKIid, sizeof(CACPKIid));
511
    CKYBuffer_AppendChar(cacAID, instance);
492
    CKYBuffer_SetChar(&CACPKIAID, 6, instance);
512
    ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, cacAID,
493
    ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CACPKIAID,
494
		 NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC);
513
		 NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC);
495
    CKYBuffer_FreeData(&CACPKIAID);
514
    if (ret != CKYSUCCESS) {
515
	CKYBuffer_Resize(cacAID, 0);
516
    }
496
    return ret;
517
    return ret;
497
}
518
}
498
519
Lines 515-525 Link Here
515
    CKYBuffer CAC_CM_AID;
536
    CKYBuffer CAC_CM_AID;
516
    CKYBuffer_InitFromData(&CAC_CM_AID, cacmgrid, sizeof(cacmgrid));
537
    CKYBuffer_InitFromData(&CAC_CM_AID, cacmgrid, sizeof(cacmgrid));
517
    ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CAC_CM_AID,
538
    ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CAC_CM_AID,
518
		 NULL, 0, CKYAppletFill_Null, NULL, apduRC);
539
		 NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC);
519
    CKYBuffer_FreeData(&CAC_CM_AID);
540
    CKYBuffer_FreeData(&CAC_CM_AID);
520
    return ret;
541
    return ret;
521
}
542
}
522
543
544
static CKYByte cacCCCid[] = {0xa0, 0x00, 0x00, 0x01, 0x16, 0xdb, 0x00 };
545
CKYStatus
546
CACApplet_SelectCCC(CKYCardConnection *conn, CKYISOStatus *apduRC)
547
{
548
    CKYStatus ret;
549
    CKYBuffer CAC_CM_AID;
550
    CKYBuffer_InitFromData(&CAC_CM_AID, cacCCCid, sizeof(cacCCCid));
551
    ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CAC_CM_AID,
552
		 NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC);
553
    CKYBuffer_FreeData(&CAC_CM_AID);
554
    return ret;
555
}
556
557
CKYStatus
558
CACApplet_SelectFile(CKYCardConnection *conn, unsigned short ef,
559
						 CKYISOStatus *apduRC)
560
{
561
    CKYStatus ret;
562
    CKYBuffer efBuf;
563
    CKYBuffer_InitEmpty(&efBuf);
564
    CKYBuffer_AppendShortLE(&efBuf, ef);
565
    ret = CKYApplet_HandleAPDU(conn, CACAppletFactory_SelectFile, &efBuf,
566
		 NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC);
567
    CKYBuffer_FreeData(&efBuf);
568
    return ret;
569
}
570
523
/*
571
/*
524
 * GetCPLC cluster -- must be called with CM selected
572
 * GetCPLC cluster -- must be called with CM selected
525
 */
573
 */
Lines 673-680 Link Here
673
    ccd.keyNumber = keyNumber;
721
    ccd.keyNumber = keyNumber;
674
    ccd.location = location;
722
    ccd.location = location;
675
    ccd.data = data;
723
    ccd.data = data;
676
    return CKYApplet_HandleAPDU(conn, CKYAppletFactory_ComputeCryptProcess, &ccd,
724
    return CKYApplet_HandleAPDU(conn, CKYAppletFactory_ComputeCryptProcess, 
677
	nonce, 0, CKYAppletFill_Null, NULL, apduRC);
725
	&ccd, nonce, 0, CKYAppletFill_Null, NULL, apduRC);
678
}
726
}
679
727
680
/* computeCrypt returns data in the form :
728
/* computeCrypt returns data in the form :
Lines 832-842 Link Here
832
	 	CKYBuffer *result, CKYISOStatus *apduRC)
880
	 	CKYBuffer *result, CKYISOStatus *apduRC)
833
{
881
{
834
    CKYStatus ret;
882
    CKYStatus ret;
835
883
    CKYSize dataSize = CKYBuffer_Size(data);
836
    ret = CKYApplet_HandleAPDU(conn, 
884
    CKYOffset offset = 0;
837
			    CACAppletFactory_SignDecrypt, data, NULL, 
885
    CKYBuffer tmp;
838
			    CKYBuffer_Size(data), CKYAppletFill_ReplaceBuffer, 
886
887
    CKYBuffer_InitEmpty(&tmp);
888
889
    CKYBuffer_Resize(result, 0);
890
    for(offset = 0; (dataSize-offset) > CKY_MAX_WRITE_CHUNK_SIZE; 
891
				offset += CKY_MAX_WRITE_CHUNK_SIZE) {
892
	CKYBuffer_Resize(&tmp,0);
893
	CKYBuffer_AppendBuffer(&tmp, data, offset, CKY_MAX_WRITE_CHUNK_SIZE);
894
        ret = CKYApplet_HandleAPDU(conn, CACAppletFactory_SignDecryptStep, 
895
			    &tmp, NULL, CKY_SIZE_UNKNOWN, 
896
			    CKYAppletFill_AppendBuffer, 
897
			    result, apduRC);
898
	if (ret != CKYSUCCESS) {
899
	    goto done;
900
	}
901
    }
902
    CKYBuffer_Resize(&tmp,0);
903
    CKYBuffer_AppendBuffer(&tmp, data, offset, dataSize - offset);
904
    ret = CKYApplet_HandleAPDU(conn, CACAppletFactory_SignDecryptFinal, 
905
			    &tmp, NULL, CKY_SIZE_UNKNOWN, 
906
			    CKYAppletFill_AppendBuffer, 
839
			    result, apduRC);
907
			    result, apduRC);
908
909
    if ((ret == CKYSUCCESS) && (CKYBuffer_Size(result) != dataSize)) {
910
	/* RSA returns the same data size as input, didn't happen, so
911
	 * something is wrong. */
912
    }
913
914
done:
915
    CKYBuffer_FreeData(&tmp);
840
    return ret;
916
    return ret;
841
}
917
}
842
918
Lines 895-900 Link Here
895
    }
971
    }
896
    return ret;
972
    return ret;
897
}
973
}
974
975
/*
976
 * Read a CAC Tag/Value file 
977
 */
978
CKYStatus
979
CACApplet_ReadFile(CKYCardConnection *conn, CKYByte type, CKYBuffer *buffer, 
980
		    CKYISOStatus *apduRC)
981
{
982
    CKYStatus ret;
983
    CKYISOStatus status;
984
    CKYByte maxtransfer;
985
    unsigned short offset = 0;
986
    unsigned short size;
987
    CACAppletArgReadFile rfs;
988
989
    CKYBuffer_Resize(buffer,0);
990
    if (apduRC == NULL) {
991
	apduRC = &status;
992
    }
993
    rfs.offset = 0;
994
    rfs.count = 2;
995
    rfs.type = type;
996
997
    /* APDU's are expensive, Grab a big chunk of the file first if possible */
998
    ret = CKYApplet_HandleAPDU(conn, 
999
			    CACAppletFactory_ReadFile, &rfs, NULL, 
1000
			    rfs.count, CKYAppletFill_AppendBuffer,
1001
			    buffer, apduRC);
1002
    /* file is probably smaller than 100 bytes, get the actual size first */
1003
    if (ret != CKYSUCCESS) {
1004
	return ret;
1005
    }
1006
    size = CKYBuffer_GetShortLE(buffer, 0) + 2 /* include the length itself */;
1007
    maxtransfer = CKY_MAX_READ_CHUNK_SIZE;
1008
    /* get the rest of the buffer if necessary */
1009
    for (offset = CKYBuffer_Size(buffer); size > offset; 
1010
				offset = CKYBuffer_Size(buffer)) {
1011
	rfs.offset = offset;
1012
	rfs.count = MIN(size - offset, maxtransfer);
1013
	ret = CKYApplet_HandleAPDU(conn, 
1014
			    CACAppletFactory_ReadFile, &rfs, NULL, 
1015
			    rfs.count, CKYAppletFill_AppendBuffer,
1016
			    buffer, apduRC);
1017
	if (ret != CKYSUCCESS) {
1018
	    if (*apduRC == CAC_INVALID_PARAMS) {
1019
		maxtransfer = maxtransfer/2;
1020
		if (maxtransfer == 0) {
1021
		    return ret;
1022
		}
1023
	    } else {
1024
		return ret;
1025
	    }
1026
 	}
1027
    }
1028
    return ret;
1029
}
1030
898
CKYStatus 
1031
CKYStatus 
899
CACApplet_GetCertificateFirst(CKYCardConnection *conn, CKYBuffer *cert, 
1032
CACApplet_GetCertificateFirst(CKYCardConnection *conn, CKYBuffer *cert, 
900
			CKYSize *nextSize, CKYISOStatus *apduRC)
1033
			CKYSize *nextSize, CKYISOStatus *apduRC)
(-)src/libckyapplet/cky_applet.h (-2 / +26 lines)
Lines 43-48 Link Here
43
#define CKYISO_MORE_MASK	    0xff00  /* More data mask */
43
#define CKYISO_MORE_MASK	    0xff00  /* More data mask */
44
#define CKYISO_MORE		    0x6300  /* More data available */
44
#define CKYISO_MORE		    0x6300  /* More data available */
45
#define CKYISO_DATA_INVALID	    0x6984
45
#define CKYISO_DATA_INVALID	    0x6984
46
#define CKYISO_CONDITION_NOT_SATISFIED 0x6985  /* AKA not logged in */
46
/* Applet Defined Return codes */
47
/* Applet Defined Return codes */
47
#define CKYISO_NO_MEMORY_LEFT        0x9c01  /* There have been memory 
48
#define CKYISO_NO_MEMORY_LEFT        0x9c01  /* There have been memory 
48
                                             * problems on the card */
49
                                             * problems on the card */
Lines 71-76 Link Here
71
#define CKYISO_INTERNAL_ERROR        0x9cff  /* Reserved for debugging, 
72
#define CKYISO_INTERNAL_ERROR        0x9cff  /* Reserved for debugging, 
72
					     * shouldn't happen */
73
					     * shouldn't happen */
73
74
75
#define CAC_INVALID_PARAMS	    0x6a83
76
#define CAC_TAG_FILE			1
77
#define CAC_VALUE_FILE			2
78
79
80
#define CAC_TAG_CARDURL			0xf3
81
#define CAC_TAG_CERTIFICATE		0x70
82
#define CAC_TLV_APP_PKI			0x04
83
74
/*
84
/*
75
 * Pin Constants as used by our applet
85
 * Pin Constants as used by our applet
76
 */
86
 */
Lines 209-214 Link Here
209
    const CKYBuffer *sig;
219
    const CKYBuffer *sig;
210
} CKYAppletArgComputeCrypt;
220
} CKYAppletArgComputeCrypt;
211
221
222
typedef struct _CACAppletArgReadFile {
223
    CKYByte   type;
224
    CKYByte   count;
225
    unsigned short offset;
226
} CACAppletArgReadFile;
227
212
/* fills in an APDU from a structure -- form of all the generic factories*/
228
/* fills in an APDU from a structure -- form of all the generic factories*/
213
typedef CKYStatus (*CKYAppletFactory)(CKYAPDU *apdu, const void *param);
229
typedef CKYStatus (*CKYAppletFactory)(CKYAPDU *apdu, const void *param);
214
/* fills in an a structure from a response -- form of all the fill structures*/
230
/* fills in an a structure from a response -- form of all the fill structures*/
Lines 451-459 Link Here
451
/* Select the CAC card manager.  Can happen with either applet selected */
467
/* Select the CAC card manager.  Can happen with either applet selected */
452
CKYStatus CACApplet_SelectCardManager(CKYCardConnection *conn, 
468
CKYStatus CACApplet_SelectCardManager(CKYCardConnection *conn, 
453
							CKYISOStatus *apduRC);
469
							CKYISOStatus *apduRC);
454
/* Can happen with either applet selected */
470
/* Select the CAC CC container. Can happen with either applet selected */
455
CKYStatus CACApplet_SelectPKI(CKYCardConnection *conn, CKYByte instance,
471
CKYStatus CACApplet_SelectCCC(CKYCardConnection *conn, CKYISOStatus *apduRC);
472
/* Select an old CAC applet and fill in the cardAID */
473
CKYStatus CACApplet_SelectPKI(CKYCardConnection *conn, CKYBuffer *cardAid,
474
			      CKYByte instance, CKYISOStatus *apduRC);
475
/* read a TLV file */
476
CKYStatus CACApplet_ReadFile(CKYCardConnection *conn, CKYByte type, 
477
			     CKYBuffer *buffer, CKYISOStatus *apduRC);
478
CKYStatus CACApplet_SelectFile(CKYCardConnection *conn, unsigned short ef,
456
			      CKYISOStatus *apduRC);
479
			      CKYISOStatus *apduRC);
480
457
/* must happen with PKI applet selected */
481
/* must happen with PKI applet selected */
458
CKYStatus CACApplet_SignDecrypt(CKYCardConnection *conn, const CKYBuffer *data,
482
CKYStatus CACApplet_SignDecrypt(CKYCardConnection *conn, const CKYBuffer *data,
459
		CKYBuffer *result, CKYISOStatus *apduRC);
483
		CKYBuffer *result, CKYISOStatus *apduRC);
(-)src/libckyapplet/cky_factory.h (-2 / +10 lines)
Lines 86-92 Link Here
86
#define CAC_INS_SIGN_DECRYPT	0x42
86
#define CAC_INS_SIGN_DECRYPT	0x42
87
#define CAC_INS_VERIFY_PIN	0x20
87
#define CAC_INS_VERIFY_PIN	0x20
88
#define CAC_INS_GET_PROPERTIES	0x56
88
#define CAC_INS_GET_PROPERTIES	0x56
89
#define CAC_INS_READ_FILE	0x52
90
89
#define CAC_SIZE_GET_PROPERTIES	48
91
#define CAC_SIZE_GET_PROPERTIES	48
92
#define CAC_P1_STEP		0x80
93
#define CAC_P1_FINAL		0x00
90
94
91
/*
95
/*
92
 * Fixed return sized from various commands
96
 * Fixed return sized from various commands
Lines 169-175 Link Here
169
CKY_BEGIN_PROTOS
173
CKY_BEGIN_PROTOS
170
174
171
/* function based factorys */
175
/* function based factorys */
172
CKYStatus CKYAPDUFactory_SelectFile(CKYAPDU *apdu, const CKYBuffer *AID);
176
CKYStatus CKYAPDUFactory_SelectFile(CKYAPDU *apdu, CKYByte p1, CKYByte p2,
177
				    const CKYBuffer *AID);
173
CKYStatus CKYAPDUFactory_SelectCardManager(CKYAPDU *apdu);
178
CKYStatus CKYAPDUFactory_SelectCardManager(CKYAPDU *apdu);
174
CKYStatus CKYAPDUFactory_GetCPLCData(CKYAPDU *apdu);
179
CKYStatus CKYAPDUFactory_GetCPLCData(CKYAPDU *apdu);
175
CKYStatus CKYAPDUFactory_ListKeys(CKYAPDU *apdu, CKYByte sequence);
180
CKYStatus CKYAPDUFactory_ListKeys(CKYAPDU *apdu, CKYByte sequence);
Lines 211-219 Link Here
211
CKYStatus CKYAPDUFactory_GetIssuerInfo(CKYAPDU *apdu);
216
CKYStatus CKYAPDUFactory_GetIssuerInfo(CKYAPDU *apdu);
212
CKYStatus CKYAPDUFactory_GetBuiltinACL(CKYAPDU *apdu);
217
CKYStatus CKYAPDUFactory_GetBuiltinACL(CKYAPDU *apdu);
213
218
214
CKYStatus CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, const CKYBuffer *data);
219
CKYStatus CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, CKYByte type, 
220
				     const CKYBuffer *data);
215
CKYStatus CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, const char *pin);
221
CKYStatus CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, const char *pin);
216
CKYStatus CACAPDUFactory_GetCertificate(CKYAPDU *apdu, CKYSize size);
222
CKYStatus CACAPDUFactory_GetCertificate(CKYAPDU *apdu, CKYSize size);
223
CKYStatus CACAPDUFactory_ReadFile(CKYAPDU *apdu, unsigned short offset, 
224
				  CKYByte type, CKYByte count);
217
CKYStatus CACAPDUFactory_GetProperties(CKYAPDU *apdu);
225
CKYStatus CACAPDUFactory_GetProperties(CKYAPDU *apdu);
218
226
219
CKY_END_PROTOS
227
CKY_END_PROTOS
(-)src/libckyapplet/cky_factory.c (-5 / +36 lines)
Lines 25-36 Link Here
25
 * special commands can be issued at any time 
25
 * special commands can be issued at any time 
26
 */
26
 */
27
CKYStatus
27
CKYStatus
28
CKYAPDUFactory_SelectFile(CKYAPDU *apdu, const CKYBuffer *AID)
28
CKYAPDUFactory_SelectFile(CKYAPDU *apdu, CKYByte p1, CKYByte p2,
29
			  const CKYBuffer *AID)
29
{
30
{
30
    CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816);
31
    CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816);
31
    CKYAPDU_SetINS(apdu, CKY_INS_SELECT_FILE);
32
    CKYAPDU_SetINS(apdu, CKY_INS_SELECT_FILE);
32
    CKYAPDU_SetP1(apdu, 0x04);
33
    CKYAPDU_SetP1(apdu, p1);
33
    CKYAPDU_SetP2(apdu, 0x00);
34
    CKYAPDU_SetP2(apdu, p2);
34
    return CKYAPDU_SetSendDataBuffer(apdu, AID);
35
    return CKYAPDU_SetSendDataBuffer(apdu, AID);
35
}
36
}
36
37
Lines 572-582 Link Here
572
}
574
}
573
575
574
CKYStatus
576
CKYStatus
575
CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, const CKYBuffer *data)
577
CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, CKYByte type, const CKYBuffer *data)
576
{
578
{
577
    CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816);
579
    CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816);
578
    CKYAPDU_SetINS(apdu, CAC_INS_SIGN_DECRYPT);
580
    CKYAPDU_SetINS(apdu, CAC_INS_SIGN_DECRYPT);
579
    CKYAPDU_SetP1(apdu, 0x00);
581
    CKYAPDU_SetP1(apdu, type);
580
    CKYAPDU_SetP2(apdu, 0x00);
582
    CKYAPDU_SetP2(apdu, 0x00);
581
    return CKYAPDU_SetSendDataBuffer(apdu, data);
583
    return CKYAPDU_SetSendDataBuffer(apdu, data);
582
}
584
}
Lines 592-597 Link Here
592
}
594
}
593
595
594
CKYStatus
596
CKYStatus
597
CACAPDUFactory_ReadFile(CKYAPDU *apdu, unsigned short offset, 	
598
					CKYByte type, CKYByte count)
599
{
600
    CKYStatus ret;
601
    CKYBuffer buf;
602
603
    CKYBuffer_InitEmpty(&buf);
604
    CKYAPDU_SetCLA(apdu, CKY_CLASS_GLOBAL_PLATFORM);
605
    CKYAPDU_SetINS(apdu, CAC_INS_READ_FILE);
606
    CKYAPDU_SetP1(apdu, (offset >> 8) & 0xff);
607
    CKYAPDU_SetP2(apdu, offset & 0xff);
608
    ret = CKYBuffer_Reserve(&buf, 2);
609
    if (ret != CKYSUCCESS) {
610
	    goto fail;
611
    }
612
    ret = CKYBuffer_AppendChar(&buf, type);
613
    if (ret != CKYSUCCESS) {
614
	    goto fail;
615
    }
616
    ret = CKYBuffer_AppendChar(&buf, count);
617
    if (ret != CKYSUCCESS) {
618
	    goto fail;
619
    } 
620
    ret = CKYAPDU_SetSendDataBuffer(apdu, &buf);
621
fail:
622
    CKYBuffer_FreeData(&buf);
623
    return ret;
624
}
625
626
CKYStatus
595
CACAPDUFactory_GetProperties(CKYAPDU *apdu)
627
CACAPDUFactory_GetProperties(CKYAPDU *apdu)
596
{
628
{
597
    CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816);
629
    CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816);
(-)src/libckyapplet/cky_card.c (-10 / +10 lines)
Lines 50-59 Link Here
50
    SCARDCONTEXT hContext);
50
    SCARDCONTEXT hContext);
51
51
52
typedef long (WINAPI * SCardBeginTransactionFn) (
52
typedef long (WINAPI * SCardBeginTransactionFn) (
53
    long hCard);
53
    SCARDHANDLE hCard);
54
54
55
typedef long (WINAPI * SCardEndTransactionFn) (
55
typedef long (WINAPI * SCardEndTransactionFn) (
56
    long hCard,
56
    SCARDHANDLE hCard,
57
    unsigned long dwDisposition);
57
    unsigned long dwDisposition);
58
58
59
typedef long (WINAPI * SCardConnectFn) (
59
typedef long (WINAPI * SCardConnectFn) (
Lines 61-75 Link Here
61
    const char *szReader,
61
    const char *szReader,
62
    unsigned long dwShareMode,
62
    unsigned long dwShareMode,
63
    unsigned long dwPreferredProtocols,
63
    unsigned long dwPreferredProtocols,
64
    long *phCard,
64
    SCARDHANDLE *phCard,
65
    unsigned long *pdwActiveProtocol);
65
    unsigned long *pdwActiveProtocol);
66
66
67
typedef long (WINAPI * SCardDisconnectFn) (
67
typedef long (WINAPI * SCardDisconnectFn) (
68
    long hCard,
68
    SCARDHANDLE hCard,
69
    unsigned long dwDisposition);
69
    unsigned long dwDisposition);
70
70
71
typedef long (WINAPI * SCardTransmitFn) (
71
typedef long (WINAPI * SCardTransmitFn) (
72
    long hCard,
72
    SCARDHANDLE hCard,
73
    LPCSCARD_IO_REQUEST pioSendPci,
73
    LPCSCARD_IO_REQUEST pioSendPci,
74
    const unsigned char *pbSendBuffer,
74
    const unsigned char *pbSendBuffer,
75
    unsigned long cbSendLength,
75
    unsigned long cbSendLength,
Lines 78-84 Link Here
78
    unsigned long *pcbRecvLength);
78
    unsigned long *pcbRecvLength);
79
79
80
typedef long (WINAPI * SCardReconnectFn) (
80
typedef long (WINAPI * SCardReconnectFn) (
81
    long hCard,
81
    SCARDHANDLE hCard,
82
    unsigned long dwShareMode,
82
    unsigned long dwShareMode,
83
    unsigned long dwPreferredProtocols,
83
    unsigned long dwPreferredProtocols,
84
    unsigned long dwInitialization,
84
    unsigned long dwInitialization,
Lines 91-97 Link Here
91
    unsigned long *pcchReaders);
91
    unsigned long *pcchReaders);
92
92
93
typedef long (WINAPI * SCardStatusFn) (
93
typedef long (WINAPI * SCardStatusFn) (
94
    long hCard,
94
    SCARDHANDLE hCard,
95
    char *mszReaderNames,
95
    char *mszReaderNames,
96
    unsigned long *pcchReaderLen,
96
    unsigned long *pcchReaderLen,
97
    unsigned long *pdwState,
97
    unsigned long *pdwState,
Lines 100-106 Link Here
100
    unsigned long *pcbAtrLen);
100
    unsigned long *pcbAtrLen);
101
101
102
typedef long (WINAPI * SCardGetAttribFn) (
102
typedef long (WINAPI * SCardGetAttribFn) (
103
    long hCard,
103
    SCARDHANDLE hCard,
104
    unsigned long dwAttId,
104
    unsigned long dwAttId,
105
    char *pbAttr,
105
    char *pbAttr,
106
    unsigned long *pchAttrLen);
106
    unsigned long *pchAttrLen);
Lines 493-499 Link Here
493
 * condition. Detect this case and continue. We'll establish the connection
493
 * condition. Detect this case and continue. We'll establish the connection
494
 * later..
494
 * later..
495
 */
495
 */
496
    if (ctx->lastError == SCARD_F_INTERNAL_ERROR) {
496
    if (ctx->lastError == SCARD_F_INTERNAL_ERROR || ctx->lastError == SCARD_E_NO_SERVICE) {
497
	ctx->context = 0; /* make sure it's not established */
497
	ctx->context = 0; /* make sure it's not established */
498
	return ctx;
498
	return ctx;
499
    }
499
    }
Lines 545-551 Link Here
545
 	if (ret != CKYSUCCESS) {
545
 	if (ret != CKYSUCCESS) {
546
546
547
#ifdef MAC
547
#ifdef MAC
548
	    if (ctx->lastError == SCARD_F_INTERNAL_ERROR) {
548
	    if (ctx->lastError == SCARD_F_INTERNAL_ERROR || ctx->lastError == SCARD_E_NO_SERVICE) {
549
		/* Still can't establish, just treat it as 'zero' readers */
549
		/* Still can't establish, just treat it as 'zero' readers */
550
		return CKYSUCCESS;
550
		return CKYSUCCESS;
551
	    }
551
	    }

Return to bug 302769