2 * This is the actual card emulator.
4 * These functions can be implemented in different ways on different platforms
5 * using the underlying system primitives. For Linux it uses NSS, though direct
6 * to PKCS #11, openssl+pkcs11, or even gnu crypto libraries+pkcs #11 could be
7 * used. On Windows CAPI could be used.
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
17 /* avoid including prototypes.h that redefines uint32 */
18 #define NO_NSPR_10_SUPPORT
28 #include "qemu-common.h"
31 #include "card_7816t.h"
32 #include "vcard_emul.h"
37 VCardEmulUnknown
= -1,
42 struct VCardKeyStruct
{
43 CERTCertificate
*cert
;
45 SECKEYPrivateKey
*key
;
46 VCardEmulTriState failedX509
;
50 typedef struct VirtualReaderOptionsStruct VirtualReaderOptions
;
52 struct VReaderEmulStruct
{
54 VCardEmulType default_type
;
62 * NSS Specific options
64 struct VirtualReaderOptionsStruct
{
67 VCardEmulType card_type
;
73 struct VCardEmulOptionsStruct
{
75 VirtualReaderOptions
*vreader
;
77 VCardEmulType hw_card_type
;
78 const char *hw_type_params
;
82 static int nss_emul_init
;
84 /* if we have more that just the slot, define
85 * VCardEmulStruct here */
88 * allocate the set of arrays for certs, cert_len, key
91 vcard_emul_alloc_arrays(unsigned char ***certsp
, int **cert_lenp
,
92 VCardKey
***keysp
, int cert_count
)
97 *certsp
= (unsigned char **)g_malloc(sizeof(unsigned char *)*cert_count
);
98 *cert_lenp
= (int *)g_malloc(sizeof(int)*cert_count
);
99 *keysp
= (VCardKey
**)g_malloc(sizeof(VCardKey
*)*cert_count
);
104 * Emulator specific card information
106 typedef struct CardEmulCardStruct CardEmulPrivate
;
109 vcard_emul_new_card(PK11SlotInfo
*slot
)
111 PK11_ReferenceSlot(slot
);
112 /* currently we don't need anything other than the slot */
113 return (VCardEmul
*)slot
;
117 vcard_emul_delete_card(VCardEmul
*vcard_emul
)
119 PK11SlotInfo
*slot
= (PK11SlotInfo
*)vcard_emul
;
126 static PK11SlotInfo
*
127 vcard_emul_card_get_slot(VCard
*card
)
129 /* note, the card is holding the reference, no need to get another one */
130 return (PK11SlotInfo
*)vcard_get_private(card
);
137 /* private constructure */
139 vcard_emul_make_key(PK11SlotInfo
*slot
, CERTCertificate
*cert
)
143 key
= (VCardKey
*)g_malloc(sizeof(VCardKey
));
144 key
->slot
= PK11_ReferenceSlot(slot
);
145 key
->cert
= CERT_DupCertificate(cert
);
146 /* NOTE: if we aren't logged into the token, this could return NULL */
147 /* NOTE: the cert is a temp cert, not necessarily the cert in the token,
148 * use the DER version of this function */
149 key
->key
= PK11_FindKeyByDERCert(slot
, cert
, NULL
);
150 key
->failedX509
= VCardEmulUnknown
;
156 vcard_emul_delete_key(VCardKey
*key
)
158 if (!nss_emul_init
|| (key
== NULL
)) {
162 SECKEY_DestroyPrivateKey(key
->key
);
166 CERT_DestroyCertificate(key
->cert
);
169 PK11_FreeSlot(key
->slot
);
175 * grab the nss key from a VCardKey. If it doesn't exist, try to look it up
177 static SECKEYPrivateKey
*
178 vcard_emul_get_nss_key(VCardKey
*key
)
183 /* NOTE: if we aren't logged into the token, this could return NULL */
184 key
->key
= PK11_FindPrivateKeyFromCert(key
->slot
, key
->cert
, NULL
);
189 * Map NSS errors to 7816 errors
191 static vcard_7816_status_t
192 vcard_emul_map_error(int error
)
195 case SEC_ERROR_TOKEN_NOT_LOGGED_IN
:
196 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
197 case SEC_ERROR_BAD_DATA
:
198 case SEC_ERROR_OUTPUT_LEN
:
199 case SEC_ERROR_INPUT_LEN
:
200 case SEC_ERROR_INVALID_ARGS
:
201 case SEC_ERROR_INVALID_ALGORITHM
:
202 case SEC_ERROR_NO_KEY
:
203 case SEC_ERROR_INVALID_KEY
:
204 case SEC_ERROR_DECRYPTION_DISALLOWED
:
205 return VCARD7816_STATUS_ERROR_DATA_INVALID
;
206 case SEC_ERROR_NO_MEMORY
:
207 return VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE
;
209 return VCARD7816_STATUS_EXC_ERROR_CHANGE
;
212 /* RSA sign/decrypt with the key, signature happens 'in place' */
214 vcard_emul_rsa_op(VCard
*card
, VCardKey
*key
,
215 unsigned char *buffer
, int buffer_size
)
217 SECKEYPrivateKey
*priv_key
;
218 unsigned signature_len
;
221 unsigned char buf
[2048];
222 unsigned char *bp
= NULL
;
224 vcard_7816_status_t ret
= VCARD7816_STATUS_SUCCESS
;
226 if ((!nss_emul_init
) || (key
== NULL
)) {
227 /* couldn't get the key, indicate that we aren't logged in */
228 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
230 priv_key
= vcard_emul_get_nss_key(key
);
231 if (priv_key
== NULL
) {
232 /* couldn't get the key, indicate that we aren't logged in */
233 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
235 slot
= vcard_emul_card_get_slot(card
);
238 * this is only true of the rsa signature
240 signature_len
= PK11_SignatureLen(priv_key
);
241 if (buffer_size
!= signature_len
) {
242 return VCARD7816_STATUS_ERROR_DATA_INVALID
;
244 /* be able to handle larger keys if necessariy */
246 if (sizeof(buf
) < signature_len
) {
247 bp
= g_malloc(signature_len
);
251 * do the raw operations. Some tokens claim to do CKM_RSA_X_509, but then
252 * choke when they try to do the actual operations. Try to detect
253 * those cases and treat them as if the token didn't claim support for
256 if (key
->failedX509
!= VCardEmulTrue
257 && PK11_DoesMechanism(slot
, CKM_RSA_X_509
)) {
258 rv
= PK11_PrivDecryptRaw(priv_key
, bp
, &signature_len
, signature_len
,
259 buffer
, buffer_size
);
260 if (rv
== SECSuccess
) {
261 assert(buffer_size
== signature_len
);
262 memcpy(buffer
, bp
, signature_len
);
263 key
->failedX509
= VCardEmulFalse
;
267 * we've had a successful X509 operation, this failure must be
270 if (key
->failedX509
== VCardEmulFalse
) {
271 ret
= vcard_emul_map_error(PORT_GetError());
275 * key->failedX509 must be Unknown at this point, try the
279 /* token does not support CKM_RSA_X509, emulate that with CKM_RSA_PKCS */
280 /* is this a PKCS #1 formatted signature? */
281 if ((buffer
[0] == 0) && (buffer
[1] == 1)) {
284 for (i
= 2; i
< buffer_size
; i
++) {
285 /* rsa signature pad */
286 if (buffer
[i
] != 0xff) {
290 if ((i
< buffer_size
) && (buffer
[i
] == 0)) {
291 /* yes, we have a properly formated PKCS #1 signature */
293 * NOTE: even if we accidentally got an encrypt buffer, which
294 * through shear luck started with 00, 01, ff, 00, it won't matter
295 * because the resulting Sign operation will effectively decrypt
302 hash
.data
= &buffer
[i
];
303 hash
.len
= buffer_size
- i
;
305 signature
.len
= signature_len
;
306 rv
= PK11_Sign(priv_key
, &signature
, &hash
);
307 if (rv
!= SECSuccess
) {
308 ret
= vcard_emul_map_error(PORT_GetError());
311 assert(buffer_size
== signature
.len
);
312 memcpy(buffer
, bp
, signature
.len
);
314 * we got here because either the X509 attempt failed, or the
315 * token couldn't do the X509 operation, in either case stay
316 * with the PKCS version for future operations on this key
318 key
->failedX509
= VCardEmulTrue
;
322 pad_len
= buffer_size
- signature_len
;
325 * OK now we've decrypted the payload, package it up in PKCS #1 for the
329 buffer
[1] = 2; /* RSA_encrypt */
330 pad_len
-= 3; /* format is 0 || 2 || pad || 0 || data */
332 * padding for PKCS #1 encrypted data is a string of random bytes. The
333 * random butes protect against potential decryption attacks against RSA.
334 * Since PrivDecrypt has already stripped those bytes, we can't reconstruct
335 * them. This shouldn't matter to the upper level code which should just
336 * strip this code out anyway, so We'll pad with a constant 3.
338 memset(&buffer
[2], 0x03, pad_len
);
339 pad_len
+= 2; /* index to the end of the pad */
341 pad_len
++; /* index to the start of the data */
342 memcpy(&buffer
[pad_len
], bp
, signature_len
);
344 * we got here because either the X509 attempt failed, or the
345 * token couldn't do the X509 operation, in either case stay
346 * with the PKCS version for future operations on this key
348 key
->failedX509
= VCardEmulTrue
;
359 /* return the number of login attempts still possible on the card. if unknown,
362 vcard_emul_get_login_count(VCard
*card
)
367 /* login into the card, return the 7816 status word (sw2 || sw1) */
369 vcard_emul_login(VCard
*card
, unsigned char *pin
, int pin_len
)
372 unsigned char *pin_string
= NULL
;
376 if (!nss_emul_init
) {
377 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
379 slot
= vcard_emul_card_get_slot(card
);
380 /* We depend on the PKCS #11 module internal login state here because we
381 * create a separate process to handle each guest instance. If we needed
382 * to handle multiple guests from one process, then we would need to keep
383 * a lot of extra state in our card structure
385 pin_string
= g_malloc(pin_len
+1);
386 memcpy(pin_string
, pin
, pin_len
);
387 pin_string
[pin_len
] = 0;
389 /* handle CAC expanded pins correctly */
390 for (i
= pin_len
-1; i
>= 0 && (pin_string
[i
] == 0xff); i
--) {
394 rv
= PK11_Authenticate(slot
, PR_FALSE
, pin_string
);
395 memset(pin_string
, 0, pin_len
); /* don't let the pin hang around in memory
398 if (rv
== SECSuccess
) {
399 return VCARD7816_STATUS_SUCCESS
;
401 /* map the error from port get error */
402 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
406 vcard_emul_reset(VCard
*card
, VCardPower power
)
410 if (!nss_emul_init
) {
415 * if we reset the card (either power on or power off), we lose our login
418 /* TODO: we may also need to send insertion/removal events? */
419 slot
= vcard_emul_card_get_slot(card
);
420 PK11_Logout(slot
); /* NOTE: ignoring SECStatus return value */
426 vcard_emul_find_vreader_from_slot(PK11SlotInfo
*slot
)
428 VReaderList
*reader_list
= vreader_get_reader_list();
429 VReaderListEntry
*current_entry
= NULL
;
431 if (reader_list
== NULL
) {
434 for (current_entry
= vreader_list_get_first(reader_list
); current_entry
;
435 current_entry
= vreader_list_get_next(current_entry
)) {
436 VReader
*reader
= vreader_list_get_reader(current_entry
);
437 VReaderEmul
*reader_emul
= vreader_get_private(reader
);
438 if (reader_emul
->slot
== slot
) {
441 vreader_free(reader
);
448 * create a new reader emul
451 vreader_emul_new(PK11SlotInfo
*slot
, VCardEmulType type
, const char *params
)
453 VReaderEmul
*new_reader_emul
;
455 new_reader_emul
= (VReaderEmul
*)g_malloc(sizeof(VReaderEmul
));
457 new_reader_emul
->slot
= PK11_ReferenceSlot(slot
);
458 new_reader_emul
->default_type
= type
;
459 new_reader_emul
->type_params
= strdup(params
);
460 new_reader_emul
->present
= PR_FALSE
;
461 new_reader_emul
->series
= 0;
462 new_reader_emul
->saved_vcard
= NULL
;
463 return new_reader_emul
;
467 vreader_emul_delete(VReaderEmul
*vreader_emul
)
469 if (vreader_emul
== NULL
) {
472 if (vreader_emul
->slot
) {
473 PK11_FreeSlot(vreader_emul
->slot
);
475 if (vreader_emul
->type_params
) {
476 g_free(vreader_emul
->type_params
);
478 g_free(vreader_emul
);
482 * TODO: move this to emulater non-specific file
485 vcard_emul_get_type(VReader
*vreader
)
487 VReaderEmul
*vreader_emul
;
489 vreader_emul
= vreader_get_private(vreader
);
490 if (vreader_emul
&& vreader_emul
->default_type
!= VCARD_EMUL_NONE
) {
491 return vreader_emul
->default_type
;
494 return vcard_emul_type_select(vreader
);
497 * TODO: move this to emulater non-specific file
500 vcard_emul_get_type_params(VReader
*vreader
)
502 VReaderEmul
*vreader_emul
;
504 vreader_emul
= vreader_get_private(vreader
);
505 if (vreader_emul
&& vreader_emul
->type_params
) {
506 return vreader_emul
->type_params
;
512 /* pull the slot out of the reader private data */
513 static PK11SlotInfo
*
514 vcard_emul_reader_get_slot(VReader
*vreader
)
516 VReaderEmul
*vreader_emul
= vreader_get_private(vreader
);
517 if (vreader_emul
== NULL
) {
520 return vreader_emul
->slot
;
524 * Card ATR's map to physical cards. VCARD_ATR_PREFIX will set appropriate
525 * historical bytes for any software emulated card. The remaining bytes can be
526 * used to indicate the actual emulator
528 static const unsigned char nss_atr
[] = { VCARD_ATR_PREFIX(3), 'N', 'S', 'S' };
531 vcard_emul_get_atr(VCard
*card
, unsigned char *atr
, int *atr_len
)
533 int len
= MIN(sizeof(nss_atr
), *atr_len
);
536 memcpy(atr
, nss_atr
, len
);
542 * create a new card from certs and keys
545 vcard_emul_make_card(VReader
*reader
,
546 unsigned char * const *certs
, int *cert_len
,
547 VCardKey
*keys
[], int cert_count
)
549 VCardEmul
*vcard_emul
;
555 type
= vcard_emul_get_type(reader
);
557 /* ignore the inserted card */
558 if (type
== VCARD_EMUL_NONE
) {
561 slot
= vcard_emul_reader_get_slot(reader
);
566 params
= vcard_emul_get_type_params(reader
);
567 /* params these can be NULL */
569 vcard_emul
= vcard_emul_new_card(slot
);
570 if (vcard_emul
== NULL
) {
573 vcard
= vcard_new(vcard_emul
, vcard_emul_delete_card
);
575 vcard_emul_delete_card(vcard_emul
);
578 vcard_init(reader
, vcard
, type
, params
, certs
, cert_len
, keys
, cert_count
);
584 * 'clone' a physical card as a virtual card
587 vcard_emul_mirror_card(VReader
*vreader
)
590 * lookup certs using the C_FindObjects. The Stan Cert handle won't give
591 * us the real certs until we log in.
593 PK11GenericObject
*firstObj
, *thisObj
;
595 unsigned char **certs
;
602 slot
= vcard_emul_reader_get_slot(vreader
);
607 firstObj
= PK11_FindGenericObjects(slot
, CKO_CERTIFICATE
);
608 if (firstObj
== NULL
) {
612 /* count the certs */
614 for (thisObj
= firstObj
; thisObj
;
615 thisObj
= PK11_GetNextGenericObject(thisObj
)) {
619 if (cert_count
== 0) {
620 PK11_DestroyGenericObjects(firstObj
);
624 /* allocate the arrays */
625 ret
= vcard_emul_alloc_arrays(&certs
, &cert_len
, &keys
, cert_count
);
626 if (ret
== PR_FALSE
) {
630 /* fill in the arrays */
632 for (thisObj
= firstObj
; thisObj
;
633 thisObj
= PK11_GetNextGenericObject(thisObj
)) {
635 CERTCertificate
*cert
;
638 rv
= PK11_ReadRawAttribute(PK11_TypeGeneric
, thisObj
,
639 CKA_VALUE
, &derCert
);
640 if (rv
!= SECSuccess
) {
643 /* create floating temp cert. This gives us a cert structure even if
644 * the token isn't logged in */
645 cert
= CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &derCert
,
646 NULL
, PR_FALSE
, PR_TRUE
);
647 SECITEM_FreeItem(&derCert
, PR_FALSE
);
652 certs
[cert_count
] = cert
->derCert
.data
;
653 cert_len
[cert_count
] = cert
->derCert
.len
;
654 keys
[cert_count
] = vcard_emul_make_key(slot
, cert
);
656 CERT_DestroyCertificate(cert
); /* key obj still has a reference */
659 /* now create the card */
660 card
= vcard_emul_make_card(vreader
, certs
, cert_len
, keys
, cert_count
);
668 static VCardEmulType default_card_type
= VCARD_EMUL_NONE
;
669 static const char *default_type_params
= "";
672 * This thread looks for card and reader insertions and puts events on the
676 vcard_emul_event_thread(void *arg
)
680 VReaderEmul
*vreader_emul
;
682 SECMODModule
*module
= (SECMODModule
*)arg
;
686 * XXX - the latency value doesn't matter one bit. you only get no
687 * blocking (flags |= CKF_DONT_BLOCK) or PKCS11_WAIT_LATENCY (==500),
688 * hard coded in coolkey. And it isn't coolkey's fault - the timeout
689 * value we pass get's dropped on the floor before C_WaitForSlotEvent
692 slot
= SECMOD_WaitForAnyTokenEvent(module
, 0, 500);
694 /* this could be just a no event indication */
695 if (PORT_GetError() == SEC_ERROR_NO_EVENT
) {
700 vreader
= vcard_emul_find_vreader_from_slot(slot
);
701 if (vreader
== NULL
) {
703 vreader_emul
= vreader_emul_new(slot
, default_card_type
,
704 default_type_params
);
705 vreader
= vreader_new(PK11_GetSlotName(slot
), vreader_emul
,
706 vreader_emul_delete
);
709 vreader_add_reader(vreader
);
710 vreader_free(vreader
);
713 /* card remove/insert */
714 vreader_emul
= vreader_get_private(vreader
);
715 if (PK11_IsPresent(slot
)) {
716 int series
= PK11_GetSlotSeries(slot
);
717 if (series
!= vreader_emul
->series
) {
718 if (vreader_emul
->present
) {
719 vreader_insert_card(vreader
, NULL
);
721 vcard
= vcard_emul_mirror_card(vreader
);
722 vreader_insert_card(vreader
, vcard
);
725 vreader_emul
->series
= series
;
726 vreader_emul
->present
= 1;
727 vreader_free(vreader
);
731 if (vreader_emul
->present
) {
732 vreader_insert_card(vreader
, NULL
);
734 vreader_emul
->series
= 0;
735 vreader_emul
->present
= 0;
737 vreader_free(vreader
);
741 /* if the card is inserted when we start up, make sure our state is correct */
743 vcard_emul_init_series(VReader
*vreader
, VCard
*vcard
)
745 VReaderEmul
*vreader_emul
= vreader_get_private(vreader
);
746 PK11SlotInfo
*slot
= vreader_emul
->slot
;
748 vreader_emul
->present
= PK11_IsPresent(slot
);
749 vreader_emul
->series
= PK11_GetSlotSeries(slot
);
750 if (vreader_emul
->present
== 0) {
751 vreader_insert_card(vreader
, NULL
);
756 * each module has a separate wait call, create a thread for each module that
760 vcard_emul_new_event_thread(SECMODModule
*module
)
762 PR_CreateThread(PR_SYSTEM_THREAD
, vcard_emul_event_thread
,
763 module
, PR_PRIORITY_HIGH
, PR_GLOBAL_THREAD
,
764 PR_UNJOINABLE_THREAD
, 0);
767 static const VCardEmulOptions default_options
= {
771 .hw_card_type
= VCARD_EMUL_CAC
,
772 .hw_type_params
= "",
778 * NSS needs the app to supply a password prompt. In our case the only time
779 * the password is supplied is as part of the Login APDU. The actual password
780 * is passed in the pw_arg in that case. In all other cases pw_arg should be
784 vcard_emul_get_password(PK11SlotInfo
*slot
, PRBool retries
, void *pw_arg
)
786 /* if it didn't work the first time, don't keep trying */
790 /* we are looking up a password when we don't have one in hand */
791 if (pw_arg
== NULL
) {
794 /* TODO: we really should verify that were are using the right slot */
795 return PORT_Strdup(pw_arg
);
798 /* Force a card removal even if the card is not physically removed */
800 vcard_emul_force_card_remove(VReader
*vreader
)
802 if (!nss_emul_init
|| (vreader_card_is_present(vreader
) != VREADER_OK
)) {
803 return VCARD_EMUL_FAIL
; /* card is already removed */
807 vreader_insert_card(vreader
, NULL
);
808 return VCARD_EMUL_OK
;
811 /* Re-insert of a card that has been removed by force removal */
813 vcard_emul_force_card_insert(VReader
*vreader
)
815 VReaderEmul
*vreader_emul
;
818 if (!nss_emul_init
|| (vreader_card_is_present(vreader
) == VREADER_OK
)) {
819 return VCARD_EMUL_FAIL
; /* card is already removed */
821 vreader_emul
= vreader_get_private(vreader
);
823 /* if it's a softcard, get the saved vcard from the reader emul structure */
824 if (vreader_emul
->saved_vcard
) {
825 vcard
= vcard_reference(vreader_emul
->saved_vcard
);
827 /* it must be a physical card, rebuild it */
828 if (!PK11_IsPresent(vreader_emul
->slot
)) {
829 /* physical card has been removed, not way to reinsert it */
830 return VCARD_EMUL_FAIL
;
832 vcard
= vcard_emul_mirror_card(vreader
);
834 vreader_insert_card(vreader
, vcard
);
837 return VCARD_EMUL_OK
;
842 module_has_removable_hw_slots(SECMODModule
*mod
)
845 PRBool ret
= PR_FALSE
;
846 SECMODListLock
*moduleLock
= SECMOD_GetDefaultModuleListLock();
849 PORT_SetError(SEC_ERROR_NOT_INITIALIZED
);
852 SECMOD_GetReadLock(moduleLock
);
853 for (i
= 0; i
< mod
->slotCount
; i
++) {
854 PK11SlotInfo
*slot
= mod
->slots
[i
];
855 if (PK11_IsRemovable(slot
) && PK11_IsHW(slot
)) {
860 SECMOD_ReleaseReadLock(moduleLock
);
864 /* Previously we returned FAIL if no readers found. This makes
865 * no sense when using hardware, since there may be no readers connected
866 * at the time vcard_emul_init is called, but they will be properly
867 * recognized later. So Instead return FAIL only if no_hw==1 and no
868 * vcards can be created (indicates error with certificates provided
869 * or db), or if any other higher level error (NSS error, missing coolkey). */
870 static int vcard_emul_init_called
;
873 vcard_emul_init(const VCardEmulOptions
*options
)
876 PRBool ret
, has_readers
= PR_FALSE
, need_coolkey_module
;
878 VReaderEmul
*vreader_emul
;
879 SECMODListLock
*module_lock
;
880 SECMODModuleList
*module_list
;
881 SECMODModuleList
*mlp
;
884 if (vcard_emul_init_called
) {
885 return VCARD_EMUL_INIT_ALREADY_INITED
;
887 vcard_emul_init_called
= 1;
891 if (options
== NULL
) {
892 options
= &default_options
;
895 /* first initialize NSS */
896 if (options
->nss_db
) {
897 rv
= NSS_Init(options
->nss_db
);
899 rv
= NSS_Init("sql:/etc/pki/nssdb");
901 if (rv
!= SECSuccess
) {
902 return VCARD_EMUL_FAIL
;
904 /* Set password callback function */
905 PK11_SetPasswordFunc(vcard_emul_get_password
);
907 /* set up soft cards emulated by software certs rather than physical cards
909 for (i
= 0; i
< options
->vreader_count
; i
++) {
912 unsigned char **certs
;
917 slot
= PK11_FindSlotByName(options
->vreader
[i
].name
);
921 vreader_emul
= vreader_emul_new(slot
, options
->vreader
[i
].card_type
,
922 options
->vreader
[i
].type_params
);
923 vreader
= vreader_new(options
->vreader
[i
].vname
, vreader_emul
,
924 vreader_emul_delete
);
925 vreader_add_reader(vreader
);
926 cert_count
= options
->vreader
[i
].cert_count
;
928 ret
= vcard_emul_alloc_arrays(&certs
, &cert_len
, &keys
,
929 options
->vreader
[i
].cert_count
);
930 if (ret
== PR_FALSE
) {
934 for (j
= 0; j
< options
->vreader
[i
].cert_count
; j
++) {
935 /* we should have a better way of identifying certs than by
937 CERTCertificate
*cert
= PK11_FindCertFromNickname(
938 options
->vreader
[i
].cert_name
[j
],
943 certs
[cert_count
] = cert
->derCert
.data
;
944 cert_len
[cert_count
] = cert
->derCert
.len
;
945 keys
[cert_count
] = vcard_emul_make_key(slot
, cert
);
946 /* this is safe because the key is still holding a cert reference */
947 CERT_DestroyCertificate(cert
);
951 VCard
*vcard
= vcard_emul_make_card(vreader
, certs
, cert_len
,
953 vreader_insert_card(vreader
, vcard
);
954 vcard_emul_init_series(vreader
, vcard
);
955 /* allow insertion and removal of soft cards */
956 vreader_emul
->saved_vcard
= vcard_reference(vcard
);
958 vreader_free(vreader
);
959 has_readers
= PR_TRUE
;
966 /* if we aren't suppose to use hw, skip looking up hardware tokens */
967 if (!options
->use_hw
) {
968 nss_emul_init
= has_readers
;
969 return has_readers
? VCARD_EMUL_OK
: VCARD_EMUL_FAIL
;
972 /* make sure we have some PKCS #11 module loaded */
973 module_lock
= SECMOD_GetDefaultModuleListLock();
974 module_list
= SECMOD_GetDefaultModuleList();
975 need_coolkey_module
= !has_readers
;
976 SECMOD_GetReadLock(module_lock
);
977 for (mlp
= module_list
; mlp
; mlp
= mlp
->next
) {
978 SECMODModule
*module
= mlp
->module
;
979 if (module_has_removable_hw_slots(module
)) {
980 need_coolkey_module
= PR_FALSE
;
984 SECMOD_ReleaseReadLock(module_lock
);
986 if (need_coolkey_module
) {
987 SECMODModule
*module
;
988 module
= SECMOD_LoadUserModule(
989 (char *)"library=libcoolkeypk11.so name=Coolkey",
991 if (module
== NULL
) {
992 return VCARD_EMUL_FAIL
;
994 SECMOD_DestroyModule(module
); /* free our reference, Module will still
996 * until we destroy it */
999 /* now examine all the slots, finding which should be readers */
1000 /* We should control this with options. For now we mirror out any
1001 * removable hardware slot */
1002 default_card_type
= options
->hw_card_type
;
1003 default_type_params
= strdup(options
->hw_type_params
);
1005 SECMOD_GetReadLock(module_lock
);
1006 for (mlp
= module_list
; mlp
; mlp
= mlp
->next
) {
1007 SECMODModule
*module
= mlp
->module
;
1009 /* Ignore the internal module */
1010 if (module
== NULL
|| module
== SECMOD_GetInternalModule()) {
1014 for (i
= 0; i
< module
->slotCount
; i
++) {
1015 PK11SlotInfo
*slot
= module
->slots
[i
];
1017 /* only map removable HW slots */
1018 if (slot
== NULL
|| !PK11_IsRemovable(slot
) || !PK11_IsHW(slot
)) {
1021 if (strcmp("E-Gate 0 0", PK11_GetSlotName(slot
)) == 0) {
1023 * coolkey <= 1.1.0-20 emulates this reader if it can't find
1024 * any hardware readers. This causes problems, warn user of
1027 fprintf(stderr
, "known bad coolkey version - see "
1028 "https://bugzilla.redhat.com/show_bug.cgi?id=802435\n");
1031 vreader_emul
= vreader_emul_new(slot
, options
->hw_card_type
,
1032 options
->hw_type_params
);
1033 vreader
= vreader_new(PK11_GetSlotName(slot
), vreader_emul
,
1034 vreader_emul_delete
);
1035 vreader_add_reader(vreader
);
1037 if (PK11_IsPresent(slot
)) {
1039 vcard
= vcard_emul_mirror_card(vreader
);
1040 vreader_insert_card(vreader
, vcard
);
1041 vcard_emul_init_series(vreader
, vcard
);
1045 vcard_emul_new_event_thread(module
);
1047 SECMOD_ReleaseReadLock(module_lock
);
1048 nss_emul_init
= PR_TRUE
;
1050 return VCARD_EMUL_OK
;
1053 /* Recreate card insert events for all readers (user should
1054 * deduce implied reader insert. perhaps do a reader insert as well?)
1057 vcard_emul_replay_insertion_events(void)
1059 VReaderListEntry
*current_entry
;
1060 VReaderListEntry
*next_entry
= NULL
;
1061 VReaderList
*list
= vreader_get_reader_list();
1063 for (current_entry
= vreader_list_get_first(list
); current_entry
;
1064 current_entry
= next_entry
) {
1065 VReader
*vreader
= vreader_list_get_reader(current_entry
);
1066 next_entry
= vreader_list_get_next(current_entry
);
1067 vreader_queue_card_event(vreader
);
1072 * Silly little functions to help parsing our argument string
1075 count_tokens(const char *str
, char token
, char token_end
)
1079 for (; *str
; str
++) {
1080 if (*str
== token
) {
1083 if (*str
== token_end
) {
1091 strip(const char *str
)
1093 for (; *str
&& isspace(*str
); str
++) {
1099 find_blank(const char *str
)
1101 for (; *str
&& !isspace(*str
); str
++) {
1108 * We really want to use some existing argument parsing library here. That
1109 * would give us a consistent look */
1110 static VCardEmulOptions options
;
1111 #define READER_STEP 4
1113 /* Expects "args" to be at the beginning of a token (ie right after the ','
1114 * ending the previous token), and puts the next token start in "token",
1115 * and its length in "token_length". "token" will not be nul-terminated.
1116 * After calling the macro, "args" will be advanced to the beginning of
1118 * This macro may call continue or break.
1120 #define NEXT_TOKEN(token) \
1122 args = strpbrk(args, ",)"); \
1126 if (*args == ')') { \
1130 (token##_length) = args - (token); \
1131 args = strip(args+1);
1134 vcard_emul_options(const char *args
)
1136 int reader_count
= 0;
1137 VCardEmulOptions
*opts
;
1139 /* Allow the future use of allocating the options structure on the fly */
1140 memcpy(&options
, &default_options
, sizeof(options
));
1144 args
= strip(args
); /* strip off the leading spaces */
1148 /* soft=(slot_name,virt_name,emul_type,emul_flags,cert_1, (no eol)
1149 * cert_2,cert_3...) */
1150 if (strncmp(args
, "soft=", 5) == 0) {
1154 size_t vname_length
;
1155 const char *type_params
;
1156 size_t type_params_length
;
1160 VirtualReaderOptions
*vreaderOpt
= NULL
;
1162 args
= strip(args
+ 5);
1166 args
= strip(args
+1);
1170 NEXT_TOKEN(type_params
)
1171 type_params_length
= MIN(type_params_length
, sizeof(type_str
)-1);
1172 strncpy(type_str
, type_params
, type_params_length
);
1173 type_str
[type_params_length
] = 0;
1174 type
= vcard_emul_type_from_string(type_str
);
1176 NEXT_TOKEN(type_params
)
1182 if (opts
->vreader_count
>= reader_count
) {
1183 reader_count
+= READER_STEP
;
1184 vreaderOpt
= realloc(opts
->vreader
,
1185 reader_count
* sizeof(*vreaderOpt
));
1186 if (vreaderOpt
== NULL
) {
1187 return opts
; /* we're done */
1190 opts
->vreader
= vreaderOpt
;
1191 vreaderOpt
= &vreaderOpt
[opts
->vreader_count
];
1192 vreaderOpt
->name
= g_strndup(name
, name_length
);
1193 vreaderOpt
->vname
= g_strndup(vname
, vname_length
);
1194 vreaderOpt
->card_type
= type
;
1195 vreaderOpt
->type_params
=
1196 g_strndup(type_params
, type_params_length
);
1197 count
= count_tokens(args
, ',', ')') + 1;
1198 vreaderOpt
->cert_count
= count
;
1199 vreaderOpt
->cert_name
= (char **)g_malloc(count
*sizeof(char *));
1200 for (i
= 0; i
< count
; i
++) {
1201 const char *cert
= args
;
1202 args
= strpbrk(args
, ",)");
1203 vreaderOpt
->cert_name
[i
] = g_strndup(cert
, args
- cert
);
1204 args
= strip(args
+1);
1209 opts
->vreader_count
++;
1211 } else if (strncmp(args
, "use_hw=", 7) == 0) {
1212 args
= strip(args
+7);
1213 if (*args
== '0' || *args
== 'N' || *args
== 'n' || *args
== 'F') {
1214 opts
->use_hw
= PR_FALSE
;
1216 opts
->use_hw
= PR_TRUE
;
1218 args
= find_blank(args
);
1220 } else if (strncmp(args
, "hw_type=", 8) == 0) {
1221 args
= strip(args
+8);
1222 opts
->hw_card_type
= vcard_emul_type_from_string(args
);
1223 args
= find_blank(args
);
1225 } else if (strncmp(args
, "hw_params=", 10) == 0) {
1227 args
= strip(args
+10);
1229 args
= find_blank(args
);
1230 opts
->hw_type_params
= g_strndup(params
, args
-params
);
1231 /* db="/data/base/path" */
1232 } else if (strncmp(args
, "db=", 3) == 0) {
1234 args
= strip(args
+3);
1240 args
= strpbrk(args
, "\"\n");
1241 opts
->nss_db
= g_strndup(db
, args
-db
);
1246 args
= find_blank(args
);
1248 } while (*args
!= 0);
1254 vcard_emul_usage(void)
1257 "emul args: comma separated list of the following arguments\n"
1258 " db={nss_database} (default sql:/etc/pki/nssdb)\n"
1259 " use_hw=[yes|no] (default yes)\n"
1260 " hw_type={card_type_to_emulate} (default CAC)\n"
1261 " hw_param={param_for_card} (default \"\")\n"
1262 " soft=({slot_name},{vreader_name},{card_type_to_emulate},{params_for_card},\n"
1263 " {cert1},{cert2},{cert3} (default none)\n"
1265 " {nss_database} The location of the NSS cert & key database\n"
1266 " {card_type_to_emulate} What card interface to present to the guest\n"
1267 " {param_for_card} Card interface specific parameters\n"
1268 " {slot_name} NSS slot that contains the certs\n"
1269 " {vreader_name} Virtual reader name to present to the guest\n"
1270 " {certN} Nickname of the certificate n on the virtual card\n"
1272 "These parameters come as a single string separated by blanks or newlines."
1274 "Unless use_hw is set to no, all tokens that look like removable hardware\n"
1275 "tokens will be presented to the guest using the emulator specified by\n"
1276 "hw_type, and parameters of hw_param.\n"
1278 "If more one or more soft= parameters are specified, these readers will be\n"
1279 "presented to the guest\n");