Closed Bug 433105 Opened 18 years ago Closed 4 years ago

lg_searchKeys always must traverse all keys to find one

Categories

(NSS :: Libraries, defect)

3.12
x86
Windows XP
defect

Tracking

(Not tracked)

RESOLVED WONTFIX
3.12.5

People

(Reporter: nelson, Assigned: rrelyea)

References

Details

(Keywords: regression)

Attachments

(2 files)

I have a cert/key DB pair that has several "user certs" and their corresponding private keys. The DBs were created with certutil using NSS trunk code built earlier this week. While attempting to fill in the fields for an nssCertificate fetched from the cert DB, NSS calls NSSCertificate_IsPrivateKeyAvailable. As part of that operation, it calls lg_searchKeys. When called with a non-empty keyID, function lg_searchKeys first tries to find a private key in the database by calling nsslowkey_FindKeyByPublicKey(keyHandle, key_id, sdb); to find the key by looking up the key_id (RSA modulus) in the key DB index. If it finds the key that way, it handles it, otherwise, the function ends up traversing all the keys in the DB, looking for a match. I observe that that call to nsslowkey_FindKeyByPublicKey always fails. It NEVER finds the RSA key modulus in the key DB index. It seems to me that lg_searchKeys should be able to find the key in the DB index, but it never does. Because the DB search fails, it ends up calling PORT_SetError with (SEC_ERROR_BAD_DATABASE). The full stack is: nssutil3.dll!PORT_SetError_Util(int value=0xffffe012) Line 184 + 0xc bytes nssdbm3.dll!get_dbkey() Line 272 + 0xa bytes nssdbm3.dll!seckey_get_private_key() Line 1912 + 0xd bytes nssdbm3.dll!nsslowkey_FindKeyByPublicKey() Line 1960 + 0x13 bytes nssdbm3.dll!lg_searchKeys() Line 313 + 0x11 bytes nssdbm3.dll!lg_searchTokenList() Line 850 + 0x27 bytes nssdbm3.dll!lg_FindObjectsInit() Line 892 + 0x15 bytes softokn3.dll!sftkdb_FindObjectsInit() Line 942 + 0x18 bytes softokn3.dll!sftk_searchDatabase() Line 3937 + 0x15 bytes softokn3.dll!sftk_searchTokenList() Line 4062 + 0x15 bytes softokn3.dll!NSC_FindObjectsInit() Line 4115 + 0x1d bytes nss3.dll!pk11_FindObjectByTemplate() Line 1475 + 0x19 bytes nss3.dll!PK11_MatchItem() Line 1607 + 0x11 bytes nss3.dll!nssToken_IsPrivateKeyAvailable() Line 1625 + 0x16 bytes nss3.dll!NSSCertificate_IsPrivateKeyAvailable() Line 759 + 0x14 bytes nss3.dll!nssTrust_GetCERTCertTrustForCert() Line 623 + 0xd bytes nss3.dll!fill_CERTCertificateFields() Line 797 + 0xd bytes nss3.dll!stan_GetCERTCertificate() Line 862 + 0x11 bytes nss3.dll!STAN_GetCERTCertificate() Line 890 + 0xb bytes This unfortunate setting of the error code causes NSS to return SEC_ERROR_BAD_DATABASE from CERT_VerifyCert under some conditions. It also wastes large numbers of CPU cycles, traversing the key DB to find a key that should have been in the index, but wasn't. I will attach the cert and key DBs. The test command that reproduces this behavior is: vfychain -d DB -u 4 -v -w test myEE
Attached file the cert DB —
Attached file the key DB —
The password is "test"
Bob, I would like you to take a look at this.
Assignee: nobody → rrelyea
Keywords: regression
Version: trunk → 3.12
I think this hasn't worked for a long time, certainly wasn't working in 3.11 (probably even earlier than that).
Bob, the same test that returns SEC_ERROR_BAD_DATABASE in NSS 3.12 because of this problem does NOT return that error in NSS 3.11.
Priority: -- → P1
Target Milestone: --- → 3.12.1
Hmmm, I'm still getting NULL back for the private key in NSS 3.11. It's just that the pwitem is NULL so I never get to the db search. bob
There must be something different between our environments because 3.12 has the equivalent in it's code, so you must be logged in... bob
Bob, I have done a bunch more investigation of this problem. This problem is causing a pathological O(n^2) algorithm for simple tasks like listing certs in a cert DB. It's KILLING performance with legacy DBs, which is what all our paying customers are presently using. I recently made a cert/key DB pair that contained 12K (yes, K) self-signed certs. A simple certutil -L listing of that DB takes over *30 MINUTES* to complete and does over *144 MILLION decryptions* and decodings of private keys. The issue is that all the RSA private key records in the key3.db use the entire modulus for the Database "key" (index) value. They do NOT use the SHA1 hash of the modulus, which is the value output by softoken as the CKA_ID. The CKA_ID is generally 20 bytes, and the modulus is more commonly 128 or 256 bytes. So, when function lg_searchKeys attempts to search for a key by its CKA_ID, it never finds a match, and consequently it goes and traverses the entire key DB looking for the matching key, decoding and encrypting every key in the DB until it finds a match. Now, consider the effect of this on a simple thing like the certutil -L command. It traverses the certs, and for each one, it calls nssTrust_GetCERTCertTrustForCert which calls nssToken_IsPrivateKeyAvailable which calls lg_searchKeys to search for a key to match the cert. It ends up traversing the entire key DB, and it does that for EVERY cert in the cert DB. The one single thing we could do to best speed this up would be to make DB searches of the key DB work, so that lg_searchKeys would find the key with a fast DB search instead of with a slow traversal of the entire key DB, decrypting every record as it goes.
Bob, I'd like your guidance and advice on fixing the O(n^2) full traversal problem. I'm willing to do the coding, but I want your advice on the best way to do it. NSS seems to be of two minds about what the value of the CKA_ID is (or should be) for private keys. The PK11 wrap layer function, PK11_GenerateKeyPair, (and its helper PK11_GenerateKeyPairWithOpFlags) call C_GenerateKeyPair to generate the key pair, and the CKA_ID template value passed in is empty. Then after the key pair is generated, the PK11_GenerateKeyPair functions extract the public key value, then call pk11_MakeIDFromPublicKey to make a CKA_ID, and finally call C_SetAttributeValue to set the new value for the CKA_ID attribute on the newly generated public and private keys. But softoken basically ignores all that. When it generates the key pair, it uses the public key value as the DB "key" (index), regardless of any value passed in for CKA_ID. Function nsslowkey_StoreKeyByPublicKeyAlg, which stores the newly generated key, calls seckey_put_private_key with the public key value as the value for the new DB key (index), not the SHA1 hash of the public key value. It does not allow the CKA_ID value to be altered. When asked to change the CKA_ID, it reports success but does nothing. See http://bonsai.mozilla.org/cvsblame.cgi?file=mozilla/security/nss/lib/softoken/legacydb/lgattr.c&rev=1.7&mark=1564-1569#1554 When asked to retrieve the CKA_ID, it computes the SHA1 hash of the RSA modulus (for RSA private keys) and returns that, but that value can only be used to find a private key by exhaustive search, since it is not found in any database index. So, it seems that our Key DB is optimized for searching for private keys based on the public key value, but we rarely ever search for keys based on that value. We FAR more commonly search on CKA_ID, which requires a linear search through the DB, decrypting every entry as we go. I want to find some way to greatly improved the efficiency of key DB operations, reducing the number of linear searching with full decryption, while still preserving compatibility. Here are a couple ideas. 1. Change the legacy softoken code in nsslowkey_StoreKeyByPublicKeyAlg to call an nsslowkey_ function like pk11_MakeIDFromPublicKey to compute the CKA_ID value, and use THAT value as the DB index key value, not the raw public key value. This will not improve anything for existing DBs, but will make newly generated or updated DBs work faster, going forward. I believe it would not break anything, since the code ALWAYS falls back to doing a full key DB traversal if the fast path finds nothing. 2. Change PK11wrap code to use the public key value, rather than the CKA_ID computed as the hash of the public key, as the value for which it searches as the CKA_ID of the private keys. Note: this means that the CKA_ID of the public key will not match the CKA_ID of the corresponding private key. This change would also mean taht PK11_GenerateKeyPair would stop changing the CKA_ID value to be the hash of the public key, but rather would attempt to change it to the public key value. 3. Change PK11wrap layer code to attempt two searches for private keys, the first search assuming that the private key's CKA_ID is the public key value and the second search assuming that the private key's CKA_ID matches the public key's CKA_ID. Best case: searches for keys that are present get much faster. Worst case: searches for missing keys get twice as slow, doing two full traversals. I'm not considering a plan that would add another index to the key DB, so that it is indexed by both public value and the SHA1 hash of the public value. That's a major revision to the DBM key format, and I'm pretty sure we don't want to invest that much in that right now. On the other hand, I am certain that we cannot continue to tolerate the current very slow searches. Certutil -L needs to be zippy.
More anecdotal evidence of the impact of an inefficient key DB. I wrote a tiny test script that makes self-signed certs with unique subject names and serial numbers, placing them in the cert DB, as fast as it can. It uses certutil -S. It started out making more than one per second. After 24 hours, it had made ~12k certs, and was taking ~12 seconds for each new cert added. Then I made ONE small change to the script, and started over. After each successful self-signed cert gen, the modified script copied a virgin key DB over on top of the one in which the generated key had just been stored. This had the effect of preventing the key DB from filling up and taking a long time to search. The new script also started out making more than 1 self-signed cert per second, and after less than 24 hours, it had made over 60K certs and was still taking less than 1 second per new cert, on average. The modified script is: SERIAL=1 while (true) do SERIAL=$(expr $SERIAL + 1) date | tee datefile echo $SERIAL | tee -a datefile cp VANILLA/key3.db BG certutil -d BG -S -n "CACert $SERIAL" -s "CN=$SERIAL,O=foo" -t 'C,C,C' \ -x -m $SERIAL -f pwfile -z datefile done
one question: Are we talking about a 3.12 regression, or is this problem in NSS 3.11 as well? If the latter we can try to look for potential changes between the two which may have flipped the purpose of the index. That being said, I think the index (in the code it's called the database key, I'll call it the index throughout here to distinguish it from the cyrptographic keys) in the database was the whole 'public key' so that DH/DSA/EC style keys could have access to the public component of their respective keys*. The code was using this as a data value instead of an index value. This is the CKA_NETSCAPE_DB value that is passed down. I think the code no longer requires this, and the database key (index) can be set to CKA_ID. We still need to do the full search if we fail to locate the CKA_ID because we may be talking to an old database (in fact the traversal was added to handle the case of old databases having the wrong or different _DB attributes). Another option is to create a new record in with and index of CKA_ID and data which points to whatever index the key DB record claims (like we do with nicknames, and other cert records). In the old database, there is no place to 'store' the CKA_ID value, so it is always calculated. There are lots of values like that in the old database, leading to some of our nastier PKCS #11 problems at the higher level (failure to set the attributes correctly for certain keys, for instance). All this leads to another question: What kinds of certs are in the database. If they are predominately user certs with keys, then schemes for adjusting the index to the private key should work just fine, but if you have databases full of certs that don't have any key associated with them and a database with lots of private keys, you can run into the problem again. You will need to rev the internal DB version number and not traverse on DB's' with that new number. BTW the design point of the code was assuming small numbers of keys. One way to work around this may be to split up the databases. NSS can open more than one database at once, it might be good to see if you can specify opening more than one in the secmod.db file...
Bob, NSS does a LOT of searching for private keys to match certs and public keys. a LOT. It needs to be efficient, but it isn't. Since the key DB is indexed by the public key, we should search for keys by the public key, not by the CKA_ID. A search by CKA_ID has no choice but to walk the entire DB, decrypting every private key record as it goes. The only kind of search that has a prayer of being fast is a search by public key, but AFAICT, NSS (pk11wrap) searches by CKA_ID, not by public key. The softoken legacy code appears to me to take the value of the CKA_ID and search for that value, first in the DB index and then second by a linear walk. I think that if we pass the public key in as the CKA_ID value to be sought, it might be fast. But of course, we pass in the SHA1 hash of the public key as the sought CKA_ID, and so we get the SLOW search. I don't want to rev the old DB format and/or create a new index (if we can possibly avoid it). I just want to do the search in whatever way will be fast. I think we can make it fast by changing PK11wrap to pass in the public key value as the target CKA_ID in searches, rather than the expected 20-byte hash as the target CKA_ID. Such a hack would only be done if the search is on the internal token and the internal token is using the legacy DBs, I guess. Sigh. I have not yet determined if this is a 3.12 regression of if it's been this slow for longer than that.
> NSS does a LOT of searching for private keys to match certs and public > keys. a LOT. I said the design point is softoken is assuming a small key database. > Since the key DB > is indexed by the public key, we should search for keys by the public key, > not by the CKA_ID There is no such search. NSS isn't searching the keys by public key, it's matching the CKA_ID of the cert to the CKA_ID of the key. I think the better fix is to try to put the CKA_ID index in. Changing the upper level NSS behavior which is designed to work with multiple types of PKCS #11 modules is not really an option (particularly for a database format we eventually want to move away from). > The only kind of > search that has a prayer of being fast is a search by public key, but > AFAICT, NSS (pk11wrap) searches by CKA_ID, not by public key. That's correct, there isn't even an interface for this. > I have not yet determined if this is a 3.12 regression of if it's been > this slow for longer than that. This would be good to answer because it may affect the solutions. If it's a regression, it means that somehow the indices changed between 3.11 and 3.12. In that case we need to restore the 3.11 index (probably the actual CKA_ID value). If it is not a regression I don't see any solution to an existing database with tons of keys without doing something like reving the internal database value or adding new CKA_ID indexes.
Target Milestone: 3.12.1 → 3.12.5
Severity: normal → --
Priority: P1 → --

Since this is a performance issue that only impacts the legacy db, this is a WONTFIX.

Status: NEW → RESOLVED
Closed: 4 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: