Re: NSS open multiple NSS-Databses at once?

Robert Relyea <[email protected]>
Newsgroups gmane.comp.mozilla.crypto
Message-ID <[email protected]>
On 01/10/2017 02:07 PM, Opa114 wrote:
> Am Dienstag, 10. Januar 2017 22:24:10 UTC+1 schrieb Robert Relyea:
>> On 01/10/2017 10:18 AM, Opa114 wrote:
>>> thanks, but these facts i know.
>>> I don't want top let multiple applications open one Database, i want to open multiple different Mozilla databases, in the old standard format, with one (my) application.
>>>
>>> I tried to use the NSS_Init functions. These works with openening one database, but when i open a second one the whole application crashes,so that's why i asked the question and may be get some working example c++ code?
>> 1) Where are you crashing (it's not expected to work, but I don't expect
>> a crash because you called NSS_Init again).
>>
>> 2) To open additional databases you want to use SECMOD_OpenUserDB:
>>
>> https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/PKCS11_Functions#SECMOD_OpenUserDB
>>
>> You can call that multiple times.
>> Once the database is opened any of the NSS find functions will find all
>> the certs in both databases. The slot returned from SECOMD_OpenUserDB
>> can be used in functions that take a slot to narrow the operations just
>> to that particular database.
>>
>> To NSS each database will look basically like a smart card.
>>
>> When you are through with that database you can use SECMOD_CloseUserDB()
>>
>> bob
>
> thanks for reply. Here are first some little code of which did not work, that means it crashes:
>
> functionLoadFirefox() {
> SECStatus rv = NSS_InitReadWrite(PATH_TO_FF_DB);
> ... if success load Certificates with PK11_ListCerts(PK11CertListAll, NULL);
> NSS_Shutdown();
> }
>
> functionLoadThunderbird() {
> SECStatus rv = NSS_InitReadWrite(PATH_TO_TB_DB);
> ... if success load Certificates with PK11_ListCerts(PK11CertListAll, NULL);
> NSS_Shutdown();
> }
>
> So these are my two functions in which i opened and clos the databases and retrieve the certificates.
So the certs you got from the first call is likely preventing 
NSS_Shutdown from completing. The certs hold references to the 
respective slots. Those references prevent NSS_Shutdown from closing 
completely. The will prevent the second NSS_Init from succeeding, so you 
probably crash in your second shutdown. You can detect this happened by 
looking at the return value from NSS_Shutdown().
>
> --> 2) To open additional databases you want to use SECMOD_OpenUserDB
> So this means. First i have to call NSS_Init with let's say firefox database ad the i have to call SECMOD_OpenUserDB with the thudnerbirddatabse, right? Or must i load both with the SECMOD_OpenUserDB?
You can either use NSS_Init with no database and then call 
SECMOD_OpenUserDB() for both, or you can call NSS_Init with one database 
and then call SECMOD_OpenUserDB with the other.
>
> --> Once the database is opened any of the NSS find functions will find all the certs in both databases
> But i have to know from which databse the certificates are coming from. So i need to know that let's say Certificate ABC ist stored inside Firefox Databse and Certificate 123 is stored in Thunerbird Database. How can i do that? or is this not possible?
The slot the database can be found in the cert->slot entry, but this 
will only give you ONE of the slots the cert lives in. If a cert exists 
in both databases, it will have a single entry on the list and be 
"somewhat" random which slot is listed (If you open one database with 
NSS_Init and the second with SECMOD_OpenUserDB() then the one you opened 
with SECMOD_OpenUserDB() will be the slot that shows up.

To fix this issue, there's a function called PK11_GetAllSlotsForCert() 
which returns a slotList and will return all the slots that hold this 
cert. The slots map one for one to the databases you opened (or any 
smart cards you have loaded). You can control the 'tokenName' of each 
slot with the string arguments you pass to SECMOD_OpenUserDB(), and you 
can get the token name with PK11_GetTokenName() on each slot on the list..

You could also use PK11_ListCertsInSlot() which takes a slot 
(SECMOD_OpenUserDB() will return a slot for you) and lists only those 
certs in that slot.

Be sure to free all these things once you are through with them, or your 
shutdown will fail at the end again.


bob


-- 
dev-tech-crypto mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-crypto
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.