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"
36 #include "libcacard/vcardt_internal.h"
40 VCardEmulUnknown
= -1,
45 struct VCardKeyStruct
{
46 CERTCertificate
*cert
;
48 SECKEYPrivateKey
*key
;
49 VCardEmulTriState failedX509
;
53 typedef struct VirtualReaderOptionsStruct VirtualReaderOptions
;
55 struct VReaderEmulStruct
{
57 VCardEmulType default_type
;
65 * NSS Specific options
67 struct VirtualReaderOptionsStruct
{
70 VCardEmulType card_type
;
76 struct VCardEmulOptionsStruct
{
78 VirtualReaderOptions
*vreader
;
80 VCardEmulType hw_card_type
;
81 const char *hw_type_params
;
85 static int nss_emul_init
;
87 /* if we have more that just the slot, define
88 * VCardEmulStruct here */
91 * allocate the set of arrays for certs, cert_len, key
94 vcard_emul_alloc_arrays(unsigned char ***certsp
, int **cert_lenp
,
95 VCardKey
***keysp
, int cert_count
)
100 *certsp
= (unsigned char **)g_malloc(sizeof(unsigned char *)*cert_count
);
101 *cert_lenp
= (int *)g_malloc(sizeof(int)*cert_count
);
102 *keysp
= (VCardKey
**)g_malloc(sizeof(VCardKey
*)*cert_count
);
107 * Emulator specific card information
109 typedef struct CardEmulCardStruct CardEmulPrivate
;
112 vcard_emul_new_card(PK11SlotInfo
*slot
)
114 PK11_ReferenceSlot(slot
);
115 /* currently we don't need anything other than the slot */
116 return (VCardEmul
*)slot
;
120 vcard_emul_delete_card(VCardEmul
*vcard_emul
)
122 PK11SlotInfo
*slot
= (PK11SlotInfo
*)vcard_emul
;
129 static PK11SlotInfo
*
130 vcard_emul_card_get_slot(VCard
*card
)
132 /* note, the card is holding the reference, no need to get another one */
133 return (PK11SlotInfo
*)vcard_get_private(card
);
140 /* private constructure */
142 vcard_emul_make_key(PK11SlotInfo
*slot
, CERTCertificate
*cert
)
146 key
= (VCardKey
*)g_malloc(sizeof(VCardKey
));
147 key
->slot
= PK11_ReferenceSlot(slot
);
148 key
->cert
= CERT_DupCertificate(cert
);
149 /* NOTE: if we aren't logged into the token, this could return NULL */
150 /* NOTE: the cert is a temp cert, not necessarily the cert in the token,
151 * use the DER version of this function */
152 key
->key
= PK11_FindKeyByDERCert(slot
, cert
, NULL
);
153 key
->failedX509
= VCardEmulUnknown
;
159 vcard_emul_delete_key(VCardKey
*key
)
161 if (!nss_emul_init
|| (key
== NULL
)) {
165 SECKEY_DestroyPrivateKey(key
->key
);
169 CERT_DestroyCertificate(key
->cert
);
172 PK11_FreeSlot(key
->slot
);
177 * grab the nss key from a VCardKey. If it doesn't exist, try to look it up
179 static SECKEYPrivateKey
*
180 vcard_emul_get_nss_key(VCardKey
*key
)
185 /* NOTE: if we aren't logged into the token, this could return NULL */
186 key
->key
= PK11_FindPrivateKeyFromCert(key
->slot
, key
->cert
, NULL
);
191 * Map NSS errors to 7816 errors
193 static vcard_7816_status_t
194 vcard_emul_map_error(int error
)
197 case SEC_ERROR_TOKEN_NOT_LOGGED_IN
:
198 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
199 case SEC_ERROR_BAD_DATA
:
200 case SEC_ERROR_OUTPUT_LEN
:
201 case SEC_ERROR_INPUT_LEN
:
202 case SEC_ERROR_INVALID_ARGS
:
203 case SEC_ERROR_INVALID_ALGORITHM
:
204 case SEC_ERROR_NO_KEY
:
205 case SEC_ERROR_INVALID_KEY
:
206 case SEC_ERROR_DECRYPTION_DISALLOWED
:
207 return VCARD7816_STATUS_ERROR_DATA_INVALID
;
208 case SEC_ERROR_NO_MEMORY
:
209 return VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE
;
211 return VCARD7816_STATUS_EXC_ERROR_CHANGE
;
214 /* RSA sign/decrypt with the key, signature happens 'in place' */
216 vcard_emul_rsa_op(VCard
*card
, VCardKey
*key
,
217 unsigned char *buffer
, int buffer_size
)
219 SECKEYPrivateKey
*priv_key
;
220 unsigned signature_len
;
223 unsigned char buf
[2048];
224 unsigned char *bp
= NULL
;
226 vcard_7816_status_t ret
= VCARD7816_STATUS_SUCCESS
;
228 if ((!nss_emul_init
) || (key
== NULL
)) {
229 /* couldn't get the key, indicate that we aren't logged in */
230 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
232 priv_key
= vcard_emul_get_nss_key(key
);
233 if (priv_key
== NULL
) {
234 /* couldn't get the key, indicate that we aren't logged in */
235 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
237 slot
= vcard_emul_card_get_slot(card
);
240 * this is only true of the rsa signature
242 signature_len
= PK11_SignatureLen(priv_key
);
243 if (buffer_size
!= signature_len
) {
244 return VCARD7816_STATUS_ERROR_DATA_INVALID
;
246 /* be able to handle larger keys if necessariy */
248 if (sizeof(buf
) < signature_len
) {
249 bp
= g_malloc(signature_len
);
253 * do the raw operations. Some tokens claim to do CKM_RSA_X_509, but then
254 * choke when they try to do the actual operations. Try to detect
255 * those cases and treat them as if the token didn't claim support for
258 if (key
->failedX509
!= VCardEmulTrue
259 && PK11_DoesMechanism(slot
, CKM_RSA_X_509
)) {
260 rv
= PK11_PrivDecryptRaw(priv_key
, bp
, &signature_len
, signature_len
,
261 buffer
, buffer_size
);
262 if (rv
== SECSuccess
) {
263 assert(buffer_size
== signature_len
);
264 memcpy(buffer
, bp
, signature_len
);
265 key
->failedX509
= VCardEmulFalse
;
269 * we've had a successful X509 operation, this failure must be
272 if (key
->failedX509
== VCardEmulFalse
) {
273 ret
= vcard_emul_map_error(PORT_GetError());
277 * key->failedX509 must be Unknown at this point, try the
281 /* token does not support CKM_RSA_X509, emulate that with CKM_RSA_PKCS */
282 /* is this a PKCS #1 formatted signature? */
283 if ((buffer
[0] == 0) && (buffer
[1] == 1)) {
286 for (i
= 2; i
< buffer_size
; i
++) {
287 /* rsa signature pad */
288 if (buffer
[i
] != 0xff) {
292 if ((i
< buffer_size
) && (buffer
[i
] == 0)) {
293 /* yes, we have a properly formated PKCS #1 signature */
295 * NOTE: even if we accidentally got an encrypt buffer, which
296 * through shear luck started with 00, 01, ff, 00, it won't matter
297 * because the resulting Sign operation will effectively decrypt
304 hash
.data
= &buffer
[i
];
305 hash
.len
= buffer_size
- i
;
307 signature
.len
= signature_len
;
308 rv
= PK11_Sign(priv_key
, &signature
, &hash
);
309 if (rv
!= SECSuccess
) {
310 ret
= vcard_emul_map_error(PORT_GetError());
313 assert(buffer_size
== signature
.len
);
314 memcpy(buffer
, bp
, signature
.len
);
316 * we got here because either the X509 attempt failed, or the
317 * token couldn't do the X509 operation, in either case stay
318 * with the PKCS version for future operations on this key
320 key
->failedX509
= VCardEmulTrue
;
324 pad_len
= buffer_size
- signature_len
;
327 * OK now we've decrypted the payload, package it up in PKCS #1 for the
331 buffer
[1] = 2; /* RSA_encrypt */
332 pad_len
-= 3; /* format is 0 || 2 || pad || 0 || data */
334 * padding for PKCS #1 encrypted data is a string of random bytes. The
335 * random butes protect against potential decryption attacks against RSA.
336 * Since PrivDecrypt has already stripped those bytes, we can't reconstruct
337 * them. This shouldn't matter to the upper level code which should just
338 * strip this code out anyway, so We'll pad with a constant 3.
340 memset(&buffer
[2], 0x03, pad_len
);
341 pad_len
+= 2; /* index to the end of the pad */
343 pad_len
++; /* index to the start of the data */
344 memcpy(&buffer
[pad_len
], bp
, signature_len
);
346 * we got here because either the X509 attempt failed, or the
347 * token couldn't do the X509 operation, in either case stay
348 * with the PKCS version for future operations on this key
350 key
->failedX509
= VCardEmulTrue
;
361 /* return the number of login attempts still possible on the card. if unknown,
364 vcard_emul_get_login_count(VCard
*card
)
369 /* login into the card, return the 7816 status word (sw2 || sw1) */
371 vcard_emul_login(VCard
*card
, unsigned char *pin
, int pin_len
)
374 unsigned char *pin_string
= NULL
;
378 if (!nss_emul_init
) {
379 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
381 slot
= vcard_emul_card_get_slot(card
);
382 /* We depend on the PKCS #11 module internal login state here because we
383 * create a separate process to handle each guest instance. If we needed
384 * to handle multiple guests from one process, then we would need to keep
385 * a lot of extra state in our card structure
387 pin_string
= g_malloc(pin_len
+1);
388 memcpy(pin_string
, pin
, pin_len
);
389 pin_string
[pin_len
] = 0;
391 /* handle CAC expanded pins correctly */
392 for (i
= pin_len
-1; i
>= 0 && (pin_string
[i
] == 0xff); i
--) {
396 rv
= PK11_Authenticate(slot
, PR_FALSE
, pin_string
);
397 memset(pin_string
, 0, pin_len
); /* don't let the pin hang around in memory
400 if (rv
== SECSuccess
) {
401 return VCARD7816_STATUS_SUCCESS
;
403 /* map the error from port get error */
404 return VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED
;
408 vcard_emul_reset(VCard
*card
, VCardPower power
)
412 if (!nss_emul_init
) {
417 * if we reset the card (either power on or power off), we lose our login
420 /* TODO: we may also need to send insertion/removal events? */
421 slot
= vcard_emul_card_get_slot(card
);
422 PK11_Logout(slot
); /* NOTE: ignoring SECStatus return value */
427 vcard_emul_find_vreader_from_slot(PK11SlotInfo
*slot
)
429 VReaderList
*reader_list
= vreader_get_reader_list();
430 VReaderListEntry
*current_entry
= NULL
;
432 if (reader_list
== NULL
) {
435 for (current_entry
= vreader_list_get_first(reader_list
); current_entry
;
436 current_entry
= vreader_list_get_next(current_entry
)) {
437 VReader
*reader
= vreader_list_get_reader(current_entry
);
438 VReaderEmul
*reader_emul
= vreader_get_private(reader
);
439 if (reader_emul
->slot
== slot
) {
442 vreader_free(reader
);
449 * create a new reader emul
452 vreader_emul_new(PK11SlotInfo
*slot
, VCardEmulType type
, const char *params
)
454 VReaderEmul
*new_reader_emul
;
456 new_reader_emul
= (VReaderEmul
*)g_malloc(sizeof(VReaderEmul
));
458 new_reader_emul
->slot
= PK11_ReferenceSlot(slot
);
459 new_reader_emul
->default_type
= type
;
460 new_reader_emul
->type_params
= g_strdup(params
);
461 new_reader_emul
->present
= PR_FALSE
;
462 new_reader_emul
->series
= 0;
463 new_reader_emul
->saved_vcard
= NULL
;
464 return new_reader_emul
;
468 vreader_emul_delete(VReaderEmul
*vreader_emul
)
470 if (vreader_emul
== NULL
) {
473 if (vreader_emul
->slot
) {
474 PK11_FreeSlot(vreader_emul
->slot
);
476 if (vreader_emul
->type_params
) {
477 g_free(vreader_emul
->type_params
);
479 g_free(vreader_emul
);
483 * TODO: move this to emulater non-specific file
486 vcard_emul_get_type(VReader
*vreader
)
488 VReaderEmul
*vreader_emul
;
490 vreader_emul
= vreader_get_private(vreader
);
491 if (vreader_emul
&& vreader_emul
->default_type
!= VCARD_EMUL_NONE
) {
492 return vreader_emul
->default_type
;
495 return vcard_emul_type_select(vreader
);
498 * TODO: move this to emulater non-specific file
501 vcard_emul_get_type_params(VReader
*vreader
)
503 VReaderEmul
*vreader_emul
;
505 vreader_emul
= vreader_get_private(vreader
);
506 if (vreader_emul
&& vreader_emul
->type_params
) {
507 return vreader_emul
->type_params
;
513 /* pull the slot out of the reader private data */
514 static PK11SlotInfo
*
515 vcard_emul_reader_get_slot(VReader
*vreader
)
517 VReaderEmul
*vreader_emul
= vreader_get_private(vreader
);
518 if (vreader_emul
== NULL
) {
521 return vreader_emul
->slot
;
525 * Card ATR's map to physical cards. vcard_alloc_atr will set appropriate
526 * historical bytes for any software emulated card. The remaining bytes can be
527 * used to indicate the actual emulator
529 static unsigned char *nss_atr
;
530 static int nss_atr_len
;
533 vcard_emul_get_atr(VCard
*card
, unsigned char *atr
, int *atr_len
)
538 if (nss_atr
== NULL
) {
539 nss_atr
= vcard_alloc_atr("NSS", &nss_atr_len
);
541 len
= MIN(nss_atr_len
, *atr_len
);
542 memcpy(atr
, nss_atr
, len
);
547 * create a new card from certs and keys
550 vcard_emul_make_card(VReader
*reader
,
551 unsigned char * const *certs
, int *cert_len
,
552 VCardKey
*keys
[], int cert_count
)
554 VCardEmul
*vcard_emul
;
560 type
= vcard_emul_get_type(reader
);
562 /* ignore the inserted card */
563 if (type
== VCARD_EMUL_NONE
) {
566 slot
= vcard_emul_reader_get_slot(reader
);
571 params
= vcard_emul_get_type_params(reader
);
572 /* params these can be NULL */
574 vcard_emul
= vcard_emul_new_card(slot
);
575 if (vcard_emul
== NULL
) {
578 vcard
= vcard_new(vcard_emul
, vcard_emul_delete_card
);
580 vcard_emul_delete_card(vcard_emul
);
583 vcard_init(reader
, vcard
, type
, params
, certs
, cert_len
, keys
, cert_count
);
589 * 'clone' a physical card as a virtual card
592 vcard_emul_mirror_card(VReader
*vreader
)
595 * lookup certs using the C_FindObjects. The Stan Cert handle won't give
596 * us the real certs until we log in.
598 PK11GenericObject
*firstObj
, *thisObj
;
600 unsigned char **certs
;
607 slot
= vcard_emul_reader_get_slot(vreader
);
612 firstObj
= PK11_FindGenericObjects(slot
, CKO_CERTIFICATE
);
613 if (firstObj
== NULL
) {
617 /* count the certs */
619 for (thisObj
= firstObj
; thisObj
;
620 thisObj
= PK11_GetNextGenericObject(thisObj
)) {
624 if (cert_count
== 0) {
625 PK11_DestroyGenericObjects(firstObj
);
629 /* allocate the arrays */
630 ret
= vcard_emul_alloc_arrays(&certs
, &cert_len
, &keys
, cert_count
);
631 if (ret
== PR_FALSE
) {
635 /* fill in the arrays */
637 for (thisObj
= firstObj
; thisObj
;
638 thisObj
= PK11_GetNextGenericObject(thisObj
)) {
640 CERTCertificate
*cert
;
643 rv
= PK11_ReadRawAttribute(PK11_TypeGeneric
, thisObj
,
644 CKA_VALUE
, &derCert
);
645 if (rv
!= SECSuccess
) {
648 /* create floating temp cert. This gives us a cert structure even if
649 * the token isn't logged in */
650 cert
= CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &derCert
,
651 NULL
, PR_FALSE
, PR_TRUE
);
652 SECITEM_FreeItem(&derCert
, PR_FALSE
);
657 certs
[cert_count
] = cert
->derCert
.data
;
658 cert_len
[cert_count
] = cert
->derCert
.len
;
659 keys
[cert_count
] = vcard_emul_make_key(slot
, cert
);
661 CERT_DestroyCertificate(cert
); /* key obj still has a reference */
664 /* now create the card */
665 card
= vcard_emul_make_card(vreader
, certs
, cert_len
, keys
, cert_count
);
673 static VCardEmulType default_card_type
= VCARD_EMUL_NONE
;
674 static const char *default_type_params
= "";
677 * This thread looks for card and reader insertions and puts events on the
681 vcard_emul_event_thread(void *arg
)
685 VReaderEmul
*vreader_emul
;
687 SECMODModule
*module
= (SECMODModule
*)arg
;
691 * XXX - the latency value doesn't matter one bit. you only get no
692 * blocking (flags |= CKF_DONT_BLOCK) or PKCS11_WAIT_LATENCY (==500),
693 * hard coded in coolkey. And it isn't coolkey's fault - the timeout
694 * value we pass get's dropped on the floor before C_WaitForSlotEvent
697 slot
= SECMOD_WaitForAnyTokenEvent(module
, 0, 500);
699 /* this could be just a no event indication */
700 if (PORT_GetError() == SEC_ERROR_NO_EVENT
) {
705 vreader
= vcard_emul_find_vreader_from_slot(slot
);
706 if (vreader
== NULL
) {
708 vreader_emul
= vreader_emul_new(slot
, default_card_type
,
709 default_type_params
);
710 vreader
= vreader_new(PK11_GetSlotName(slot
), vreader_emul
,
711 vreader_emul_delete
);
714 vreader_add_reader(vreader
);
715 vreader_free(vreader
);
718 /* card remove/insert */
719 vreader_emul
= vreader_get_private(vreader
);
720 if (PK11_IsPresent(slot
)) {
721 int series
= PK11_GetSlotSeries(slot
);
722 if (series
!= vreader_emul
->series
) {
723 if (vreader_emul
->present
) {
724 vreader_insert_card(vreader
, NULL
);
726 vcard
= vcard_emul_mirror_card(vreader
);
727 vreader_insert_card(vreader
, vcard
);
730 vreader_emul
->series
= series
;
731 vreader_emul
->present
= 1;
732 vreader_free(vreader
);
736 if (vreader_emul
->present
) {
737 vreader_insert_card(vreader
, NULL
);
739 vreader_emul
->series
= 0;
740 vreader_emul
->present
= 0;
742 vreader_free(vreader
);
746 /* if the card is inserted when we start up, make sure our state is correct */
748 vcard_emul_init_series(VReader
*vreader
, VCard
*vcard
)
750 VReaderEmul
*vreader_emul
= vreader_get_private(vreader
);
751 PK11SlotInfo
*slot
= vreader_emul
->slot
;
753 vreader_emul
->present
= PK11_IsPresent(slot
);
754 vreader_emul
->series
= PK11_GetSlotSeries(slot
);
755 if (vreader_emul
->present
== 0) {
756 vreader_insert_card(vreader
, NULL
);
761 * each module has a separate wait call, create a thread for each module that
765 vcard_emul_new_event_thread(SECMODModule
*module
)
767 PR_CreateThread(PR_SYSTEM_THREAD
, vcard_emul_event_thread
,
768 module
, PR_PRIORITY_HIGH
, PR_GLOBAL_THREAD
,
769 PR_UNJOINABLE_THREAD
, 0);
772 static const VCardEmulOptions default_options
= {
776 .hw_card_type
= VCARD_EMUL_CAC
,
777 .hw_type_params
= "",
783 * NSS needs the app to supply a password prompt. In our case the only time
784 * the password is supplied is as part of the Login APDU. The actual password
785 * is passed in the pw_arg in that case. In all other cases pw_arg should be
789 vcard_emul_get_password(PK11SlotInfo
*slot
, PRBool retries
, void *pw_arg
)
791 /* if it didn't work the first time, don't keep trying */
795 /* we are looking up a password when we don't have one in hand */
796 if (pw_arg
== NULL
) {
799 /* TODO: we really should verify that were are using the right slot */
800 return PORT_Strdup(pw_arg
);
803 /* Force a card removal even if the card is not physically removed */
805 vcard_emul_force_card_remove(VReader
*vreader
)
807 if (!nss_emul_init
|| (vreader_card_is_present(vreader
) != VREADER_OK
)) {
808 return VCARD_EMUL_FAIL
; /* card is already removed */
812 vreader_insert_card(vreader
, NULL
);
813 return VCARD_EMUL_OK
;
816 /* Re-insert of a card that has been removed by force removal */
818 vcard_emul_force_card_insert(VReader
*vreader
)
820 VReaderEmul
*vreader_emul
;
823 if (!nss_emul_init
|| (vreader_card_is_present(vreader
) == VREADER_OK
)) {
824 return VCARD_EMUL_FAIL
; /* card is already removed */
826 vreader_emul
= vreader_get_private(vreader
);
828 /* if it's a softcard, get the saved vcard from the reader emul structure */
829 if (vreader_emul
->saved_vcard
) {
830 vcard
= vcard_reference(vreader_emul
->saved_vcard
);
832 /* it must be a physical card, rebuild it */
833 if (!PK11_IsPresent(vreader_emul
->slot
)) {
834 /* physical card has been removed, not way to reinsert it */
835 return VCARD_EMUL_FAIL
;
837 vcard
= vcard_emul_mirror_card(vreader
);
839 vreader_insert_card(vreader
, vcard
);
842 return VCARD_EMUL_OK
;
847 module_has_removable_hw_slots(SECMODModule
*mod
)
850 PRBool ret
= PR_FALSE
;
851 SECMODListLock
*moduleLock
= SECMOD_GetDefaultModuleListLock();
854 PORT_SetError(SEC_ERROR_NOT_INITIALIZED
);
857 SECMOD_GetReadLock(moduleLock
);
858 for (i
= 0; i
< mod
->slotCount
; i
++) {
859 PK11SlotInfo
*slot
= mod
->slots
[i
];
860 if (PK11_IsRemovable(slot
) && PK11_IsHW(slot
)) {
865 SECMOD_ReleaseReadLock(moduleLock
);
869 /* Previously we returned FAIL if no readers found. This makes
870 * no sense when using hardware, since there may be no readers connected
871 * at the time vcard_emul_init is called, but they will be properly
872 * recognized later. So Instead return FAIL only if no_hw==1 and no
873 * vcards can be created (indicates error with certificates provided
874 * or db), or if any other higher level error (NSS error, missing coolkey). */
875 static int vcard_emul_init_called
;
878 vcard_emul_init(const VCardEmulOptions
*options
)
881 PRBool ret
, has_readers
= PR_FALSE
;
883 VReaderEmul
*vreader_emul
;
884 SECMODListLock
*module_lock
;
885 SECMODModuleList
*module_list
;
886 SECMODModuleList
*mlp
;
889 if (vcard_emul_init_called
) {
890 return VCARD_EMUL_INIT_ALREADY_INITED
;
892 vcard_emul_init_called
= 1;
896 if (options
== NULL
) {
897 options
= &default_options
;
900 /* first initialize NSS */
901 if (options
->nss_db
) {
902 rv
= NSS_Init(options
->nss_db
);
906 path
= g_strdup("/etc/pki/nssdb");
908 if (g_get_system_config_dirs() == NULL
||
909 g_get_system_config_dirs()[0] == NULL
) {
910 return VCARD_EMUL_FAIL
;
913 path
= g_build_filename(
914 g_get_system_config_dirs()[0], "pki", "nssdb", NULL
);
920 if (rv
!= SECSuccess
) {
921 return VCARD_EMUL_FAIL
;
923 /* Set password callback function */
924 PK11_SetPasswordFunc(vcard_emul_get_password
);
926 /* set up soft cards emulated by software certs rather than physical cards
928 for (i
= 0; i
< options
->vreader_count
; i
++) {
931 unsigned char **certs
;
936 slot
= PK11_FindSlotByName(options
->vreader
[i
].name
);
940 vreader_emul
= vreader_emul_new(slot
, options
->vreader
[i
].card_type
,
941 options
->vreader
[i
].type_params
);
942 vreader
= vreader_new(options
->vreader
[i
].vname
, vreader_emul
,
943 vreader_emul_delete
);
944 vreader_add_reader(vreader
);
945 cert_count
= options
->vreader
[i
].cert_count
;
947 ret
= vcard_emul_alloc_arrays(&certs
, &cert_len
, &keys
,
948 options
->vreader
[i
].cert_count
);
949 if (ret
== PR_FALSE
) {
953 for (j
= 0; j
< options
->vreader
[i
].cert_count
; j
++) {
954 /* we should have a better way of identifying certs than by
956 CERTCertificate
*cert
= PK11_FindCertFromNickname(
957 options
->vreader
[i
].cert_name
[j
],
962 certs
[cert_count
] = cert
->derCert
.data
;
963 cert_len
[cert_count
] = cert
->derCert
.len
;
964 keys
[cert_count
] = vcard_emul_make_key(slot
, cert
);
965 /* this is safe because the key is still holding a cert reference */
966 CERT_DestroyCertificate(cert
);
970 VCard
*vcard
= vcard_emul_make_card(vreader
, certs
, cert_len
,
972 vreader_insert_card(vreader
, vcard
);
973 vcard_emul_init_series(vreader
, vcard
);
974 /* allow insertion and removal of soft cards */
975 vreader_emul
->saved_vcard
= vcard_reference(vcard
);
977 vreader_free(vreader
);
978 has_readers
= PR_TRUE
;
985 /* if we aren't suppose to use hw, skip looking up hardware tokens */
986 if (!options
->use_hw
) {
987 nss_emul_init
= has_readers
;
988 return has_readers
? VCARD_EMUL_OK
: VCARD_EMUL_FAIL
;
991 /* make sure we have some PKCS #11 module loaded */
992 module_lock
= SECMOD_GetDefaultModuleListLock();
993 module_list
= SECMOD_GetDefaultModuleList();
994 SECMOD_GetReadLock(module_lock
);
995 for (mlp
= module_list
; mlp
; mlp
= mlp
->next
) {
996 SECMODModule
*module
= mlp
->module
;
997 if (module_has_removable_hw_slots(module
)) {
1001 SECMOD_ReleaseReadLock(module_lock
);
1003 /* now examine all the slots, finding which should be readers */
1004 /* We should control this with options. For now we mirror out any
1005 * removable hardware slot */
1006 default_card_type
= options
->hw_card_type
;
1007 default_type_params
= g_strdup(options
->hw_type_params
);
1009 SECMOD_GetReadLock(module_lock
);
1010 for (mlp
= module_list
; mlp
; mlp
= mlp
->next
) {
1011 SECMODModule
*module
= mlp
->module
;
1013 /* Ignore the internal module */
1014 if (module
== NULL
|| module
== SECMOD_GetInternalModule()) {
1018 for (i
= 0; i
< module
->slotCount
; i
++) {
1019 PK11SlotInfo
*slot
= module
->slots
[i
];
1021 /* only map removable HW slots */
1022 if (slot
== NULL
|| !PK11_IsRemovable(slot
) || !PK11_IsHW(slot
)) {
1025 if (strcmp("E-Gate 0 0", PK11_GetSlotName(slot
)) == 0) {
1027 * coolkey <= 1.1.0-20 emulates this reader if it can't find
1028 * any hardware readers. This causes problems, warn user of
1031 fprintf(stderr
, "known bad coolkey version - see "
1032 "https://bugzilla.redhat.com/show_bug.cgi?id=802435\n");
1035 vreader_emul
= vreader_emul_new(slot
, options
->hw_card_type
,
1036 options
->hw_type_params
);
1037 vreader
= vreader_new(PK11_GetSlotName(slot
), vreader_emul
,
1038 vreader_emul_delete
);
1039 vreader_add_reader(vreader
);
1041 if (PK11_IsPresent(slot
)) {
1043 vcard
= vcard_emul_mirror_card(vreader
);
1044 vreader_insert_card(vreader
, vcard
);
1045 vcard_emul_init_series(vreader
, vcard
);
1049 vcard_emul_new_event_thread(module
);
1051 SECMOD_ReleaseReadLock(module_lock
);
1052 nss_emul_init
= PR_TRUE
;
1054 return VCARD_EMUL_OK
;
1057 /* Recreate card insert events for all readers (user should
1058 * deduce implied reader insert. perhaps do a reader insert as well?)
1061 vcard_emul_replay_insertion_events(void)
1063 VReaderListEntry
*current_entry
;
1064 VReaderListEntry
*next_entry
= NULL
;
1065 VReaderList
*list
= vreader_get_reader_list();
1067 for (current_entry
= vreader_list_get_first(list
); current_entry
;
1068 current_entry
= next_entry
) {
1069 VReader
*vreader
= vreader_list_get_reader(current_entry
);
1070 next_entry
= vreader_list_get_next(current_entry
);
1071 vreader_queue_card_event(vreader
);
1076 * Silly little functions to help parsing our argument string
1079 count_tokens(const char *str
, char token
, char token_end
)
1083 for (; *str
; str
++) {
1084 if (*str
== token
) {
1087 if (*str
== token_end
) {
1095 strip(const char *str
)
1097 for (; *str
&& isspace(*str
); str
++) {
1103 find_blank(const char *str
)
1105 for (; *str
&& !isspace(*str
); str
++) {
1112 * We really want to use some existing argument parsing library here. That
1113 * would give us a consistent look */
1114 static VCardEmulOptions options
;
1115 #define READER_STEP 4
1117 /* Expects "args" to be at the beginning of a token (ie right after the ','
1118 * ending the previous token), and puts the next token start in "token",
1119 * and its length in "token_length". "token" will not be nul-terminated.
1120 * After calling the macro, "args" will be advanced to the beginning of
1122 * This macro may call continue or break.
1124 #define NEXT_TOKEN(token) \
1126 args = strpbrk(args, ",)"); \
1130 if (*args == ')') { \
1134 (token##_length) = args - (token); \
1135 args = strip(args+1);
1138 vcard_emul_options(const char *args
)
1140 int reader_count
= 0;
1141 VCardEmulOptions
*opts
;
1143 /* Allow the future use of allocating the options structure on the fly */
1144 memcpy(&options
, &default_options
, sizeof(options
));
1148 args
= strip(args
); /* strip off the leading spaces */
1152 /* soft=(slot_name,virt_name,emul_type,emul_flags,cert_1, (no eol)
1153 * cert_2,cert_3...) */
1154 if (strncmp(args
, "soft=", 5) == 0) {
1158 size_t vname_length
;
1159 const char *type_params
;
1160 size_t type_params_length
;
1164 VirtualReaderOptions
*vreaderOpt
= NULL
;
1166 args
= strip(args
+ 5);
1170 args
= strip(args
+1);
1174 NEXT_TOKEN(type_params
)
1175 type_params_length
= MIN(type_params_length
, sizeof(type_str
)-1);
1176 pstrcpy(type_str
, type_params_length
, type_params
);
1177 type
= vcard_emul_type_from_string(type_str
);
1179 NEXT_TOKEN(type_params
)
1185 if (opts
->vreader_count
>= reader_count
) {
1186 reader_count
+= READER_STEP
;
1187 vreaderOpt
= realloc(opts
->vreader
,
1188 reader_count
* sizeof(*vreaderOpt
));
1189 if (vreaderOpt
== NULL
) {
1190 return opts
; /* we're done */
1193 opts
->vreader
= vreaderOpt
;
1194 vreaderOpt
= &vreaderOpt
[opts
->vreader_count
];
1195 vreaderOpt
->name
= g_strndup(name
, name_length
);
1196 vreaderOpt
->vname
= g_strndup(vname
, vname_length
);
1197 vreaderOpt
->card_type
= type
;
1198 vreaderOpt
->type_params
=
1199 g_strndup(type_params
, type_params_length
);
1200 count
= count_tokens(args
, ',', ')') + 1;
1201 vreaderOpt
->cert_count
= count
;
1202 vreaderOpt
->cert_name
= (char **)g_malloc(count
*sizeof(char *));
1203 for (i
= 0; i
< count
; i
++) {
1204 const char *cert
= args
;
1205 args
= strpbrk(args
, ",)");
1206 vreaderOpt
->cert_name
[i
] = g_strndup(cert
, args
- cert
);
1207 args
= strip(args
+1);
1212 opts
->vreader_count
++;
1214 } else if (strncmp(args
, "use_hw=", 7) == 0) {
1215 args
= strip(args
+7);
1216 if (*args
== '0' || *args
== 'N' || *args
== 'n' || *args
== 'F') {
1217 opts
->use_hw
= PR_FALSE
;
1219 opts
->use_hw
= PR_TRUE
;
1221 args
= find_blank(args
);
1223 } else if (strncmp(args
, "hw_type=", 8) == 0) {
1224 args
= strip(args
+8);
1225 opts
->hw_card_type
= vcard_emul_type_from_string(args
);
1226 args
= find_blank(args
);
1228 } else if (strncmp(args
, "hw_params=", 10) == 0) {
1230 args
= strip(args
+10);
1232 args
= find_blank(args
);
1233 opts
->hw_type_params
= g_strndup(params
, args
-params
);
1234 /* db="/data/base/path" */
1235 } else if (strncmp(args
, "db=", 3) == 0) {
1237 args
= strip(args
+3);
1243 args
= strpbrk(args
, "\"\n");
1244 opts
->nss_db
= g_strndup(db
, args
-db
);
1249 args
= find_blank(args
);
1251 } while (*args
!= 0);
1257 vcard_emul_usage(void)
1260 "emul args: comma separated list of the following arguments\n"
1261 " db={nss_database} (default sql:/etc/pki/nssdb)\n"
1262 " use_hw=[yes|no] (default yes)\n"
1263 " hw_type={card_type_to_emulate} (default CAC)\n"
1264 " hw_param={param_for_card} (default \"\")\n"
1265 " soft=({slot_name},{vreader_name},{card_type_to_emulate},{params_for_card},\n"
1266 " {cert1},{cert2},{cert3} (default none)\n"
1268 " {nss_database} The location of the NSS cert & key database\n"
1269 " {card_type_to_emulate} What card interface to present to the guest\n"
1270 " {param_for_card} Card interface specific parameters\n"
1271 " {slot_name} NSS slot that contains the certs\n"
1272 " {vreader_name} Virtual reader name to present to the guest\n"
1273 " {certN} Nickname of the certificate n on the virtual card\n"
1275 "These parameters come as a single string separated by blanks or newlines."
1277 "Unless use_hw is set to no, all tokens that look like removable hardware\n"
1278 "tokens will be presented to the guest using the emulator specified by\n"
1279 "hw_type, and parameters of hw_param.\n"
1281 "If more one or more soft= parameters are specified, these readers will be\n"
1282 "presented to the guest\n");