OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 /* | 4 /* |
5 * This file contains functions to manage asymetric keys, (public and | 5 * This file contains functions to manage asymetric keys, (public and |
6 * private keys). | 6 * private keys). |
7 */ | 7 */ |
8 #include "seccomon.h" | 8 #include "seccomon.h" |
9 #include "secmod.h" | 9 #include "secmod.h" |
10 #include "secmodi.h" | 10 #include "secmodi.h" |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 pubKey->pkcs11ID = objectID; | 207 pubKey->pkcs11ID = objectID; |
208 pubKey->pkcs11Slot = PK11_ReferenceSlot(slot); | 208 pubKey->pkcs11Slot = PK11_ReferenceSlot(slot); |
209 | 209 |
210 return objectID; | 210 return objectID; |
211 } | 211 } |
212 | 212 |
213 /* | 213 /* |
214 * take an attribute and copy it into a secitem | 214 * take an attribute and copy it into a secitem |
215 */ | 215 */ |
216 static CK_RV | 216 static CK_RV |
217 pk11_Attr2SecItem(PRArenaPool *arena, const CK_ATTRIBUTE *attr, SECItem *item) | 217 pk11_Attr2SecItem(PLArenaPool *arena, const CK_ATTRIBUTE *attr, SECItem *item) |
218 { | 218 { |
219 item->data = NULL; | 219 item->data = NULL; |
220 | 220 |
221 (void)SECITEM_AllocItem(arena, item, attr->ulValueLen); | 221 (void)SECITEM_AllocItem(arena, item, attr->ulValueLen); |
222 if (item->data == NULL) { | 222 if (item->data == NULL) { |
223 return CKR_HOST_MEMORY; | 223 return CKR_HOST_MEMORY; |
224 } | 224 } |
225 PORT_Memcpy(item->data, attr->pValue, item->len); | 225 PORT_Memcpy(item->data, attr->pValue, item->len); |
226 return CKR_OK; | 226 return CKR_OK; |
227 } | 227 } |
228 | 228 |
229 | 229 |
230 /* | 230 /* |
231 * get a curve length from a set of ecParams. | 231 * get a curve length from a set of ecParams. |
232 * | 232 * |
233 * We need this so we can reliably determine if the ecPoint passed to us | 233 * We need this so we can reliably determine if the ecPoint passed to us |
234 * was encoded or not. With out this, for many curves, we would incorrectly | 234 * was encoded or not. With out this, for many curves, we would incorrectly |
235 * identify an unencoded curve as an encoded curve 1 in 65536 times, and for | 235 * identify an unencoded curve as an encoded curve 1 in 65536 times, and for |
236 * a few we would make that same mistake 1 in 32768 times. These are bad | 236 * a few we would make that same mistake 1 in 32768 times. These are bad |
237 * numbers since they are rare enough to pass tests, but common enough to | 237 * numbers since they are rare enough to pass tests, but common enough to |
238 * be tripped over in the field. | 238 * be tripped over in the field. |
239 * | 239 * |
240 * This function will only work for curves we recognized as of March 2009. | 240 * This function will only work for curves we recognized as of March 2009. |
241 * The assumption is curves in use after March of 2009 would be supplied by | 241 * The assumption is curves in use after March of 2009 would be supplied by |
242 * PKCS #11 modules that already pass the correct encoding to us. | 242 * PKCS #11 modules that already pass the correct encoding to us. |
243 * | 243 * |
244 * Point length = (Roundup(curveLenInBits/8)*2+1) | 244 * Point length = (Roundup(curveLenInBits/8)*2+1) |
245 */ | 245 */ |
246 static int | 246 static int |
247 pk11_get_EC_PointLenInBytes(PRArenaPool *arena, const SECItem *ecParams) | 247 pk11_get_EC_PointLenInBytes(PLArenaPool *arena, const SECItem *ecParams) |
248 { | 248 { |
249 SECItem oid; | 249 SECItem oid; |
250 SECOidTag tag; | 250 SECOidTag tag; |
251 SECStatus rv; | 251 SECStatus rv; |
252 | 252 |
253 /* decode the OID tag */ | 253 /* decode the OID tag */ |
254 rv = SEC_QuickDERDecodeItem(arena, &oid, | 254 rv = SEC_QuickDERDecodeItem(arena, &oid, |
255 SEC_ASN1_GET(SEC_ObjectIDTemplate), ecParams); | 255 SEC_ASN1_GET(SEC_ObjectIDTemplate), ecParams); |
256 if (rv != SECSuccess) { | 256 if (rv != SECSuccess) { |
257 /* could be explict curves, allow them to work if the | 257 /* could be explict curves, allow them to work if the |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 } | 349 } |
350 | 350 |
351 /* | 351 /* |
352 * returns the decoded point. In some cases the point may already be decoded. | 352 * returns the decoded point. In some cases the point may already be decoded. |
353 * this function tries to detect those cases and return the point in | 353 * this function tries to detect those cases and return the point in |
354 * publicKeyValue. In other cases it's DER encoded. In those cases the point | 354 * publicKeyValue. In other cases it's DER encoded. In those cases the point |
355 * is first decoded and returned. Space for the point is allocated out of | 355 * is first decoded and returned. Space for the point is allocated out of |
356 * the passed in arena. | 356 * the passed in arena. |
357 */ | 357 */ |
358 static CK_RV | 358 static CK_RV |
359 pk11_get_Decoded_ECPoint(PRArenaPool *arena, const SECItem *ecParams, | 359 pk11_get_Decoded_ECPoint(PLArenaPool *arena, const SECItem *ecParams, |
360 const CK_ATTRIBUTE *ecPoint, SECItem *publicKeyValue) | 360 const CK_ATTRIBUTE *ecPoint, SECItem *publicKeyValue) |
361 { | 361 { |
362 SECItem encodedPublicValue; | 362 SECItem encodedPublicValue; |
363 SECStatus rv; | 363 SECStatus rv; |
364 int keyLen; | 364 int keyLen; |
365 | 365 |
366 if (ecPoint->ulValueLen == 0) { | 366 if (ecPoint->ulValueLen == 0) { |
367 return CKR_ATTRIBUTE_VALUE_INVALID; | 367 return CKR_ATTRIBUTE_VALUE_INVALID; |
368 } | 368 } |
369 | 369 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 return CKR_ATTRIBUTE_VALUE_INVALID; | 529 return CKR_ATTRIBUTE_VALUE_INVALID; |
530 } | 530 } |
531 | 531 |
532 /* | 532 /* |
533 * extract a public key from a slot and id | 533 * extract a public key from a slot and id |
534 */ | 534 */ |
535 SECKEYPublicKey * | 535 SECKEYPublicKey * |
536 PK11_ExtractPublicKey(PK11SlotInfo *slot,KeyType keyType,CK_OBJECT_HANDLE id) | 536 PK11_ExtractPublicKey(PK11SlotInfo *slot,KeyType keyType,CK_OBJECT_HANDLE id) |
537 { | 537 { |
538 CK_OBJECT_CLASS keyClass = CKO_PUBLIC_KEY; | 538 CK_OBJECT_CLASS keyClass = CKO_PUBLIC_KEY; |
539 PRArenaPool *arena; | 539 PLArenaPool *arena; |
540 PRArenaPool *tmp_arena; | 540 PLArenaPool *tmp_arena; |
541 SECKEYPublicKey *pubKey; | 541 SECKEYPublicKey *pubKey; |
542 int templateCount = 0; | 542 int templateCount = 0; |
543 CK_KEY_TYPE pk11KeyType; | 543 CK_KEY_TYPE pk11KeyType; |
544 CK_RV crv; | 544 CK_RV crv; |
545 CK_ATTRIBUTE template[8]; | 545 CK_ATTRIBUTE template[8]; |
546 CK_ATTRIBUTE *attrs= template; | 546 CK_ATTRIBUTE *attrs= template; |
547 CK_ATTRIBUTE *modulus,*exponent,*base,*prime,*subprime,*value; | 547 CK_ATTRIBUTE *modulus,*exponent,*base,*prime,*subprime,*value; |
548 CK_ATTRIBUTE *ecparams; | 548 CK_ATTRIBUTE *ecparams; |
549 | 549 |
550 /* if we didn't know the key type, get it */ | 550 /* if we didn't know the key type, get it */ |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 return pubKey; | 713 return pubKey; |
714 } | 714 } |
715 | 715 |
716 /* | 716 /* |
717 * Build a Private Key structure from raw PKCS #11 information. | 717 * Build a Private Key structure from raw PKCS #11 information. |
718 */ | 718 */ |
719 SECKEYPrivateKey * | 719 SECKEYPrivateKey * |
720 PK11_MakePrivKey(PK11SlotInfo *slot, KeyType keyType, | 720 PK11_MakePrivKey(PK11SlotInfo *slot, KeyType keyType, |
721 PRBool isTemp, CK_OBJECT_HANDLE privID, void *wincx) | 721 PRBool isTemp, CK_OBJECT_HANDLE privID, void *wincx) |
722 { | 722 { |
723 PRArenaPool *arena; | 723 PLArenaPool *arena; |
724 SECKEYPrivateKey *privKey; | 724 SECKEYPrivateKey *privKey; |
725 PRBool isPrivate; | 725 PRBool isPrivate; |
726 SECStatus rv; | 726 SECStatus rv; |
727 | 727 |
728 /* don't know? look it up */ | 728 /* don't know? look it up */ |
729 if (keyType == nullKey) { | 729 if (keyType == nullKey) { |
730 CK_KEY_TYPE pk11Type = CKK_RSA; | 730 CK_KEY_TYPE pk11Type = CKK_RSA; |
731 | 731 |
732 pk11Type = PK11_ReadULongAttribute(slot,privID,CKA_KEY_TYPE); | 732 pk11Type = PK11_ReadULongAttribute(slot,privID,CKA_KEY_TYPE); |
733 isTemp = (PRBool)!PK11_HasAttributeSet(slot,privID,CKA_TOKEN,PR_FALSE); | 733 isTemp = (PRBool)!PK11_HasAttributeSet(slot,privID,CKA_TOKEN,PR_FALSE); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 { CKA_PRIVATE, NULL, 0 }, | 859 { CKA_PRIVATE, NULL, 0 }, |
860 { CKA_MODIFIABLE, NULL, 0 }, | 860 { CKA_MODIFIABLE, NULL, 0 }, |
861 { CKA_SENSITIVE, NULL, 0 }, | 861 { CKA_SENSITIVE, NULL, 0 }, |
862 { CKA_EXTRACTABLE, NULL, 0 }, | 862 { CKA_EXTRACTABLE, NULL, 0 }, |
863 #define NUM_RESERVED_ATTRS 5 /* number of reserved attributes above */ | 863 #define NUM_RESERVED_ATTRS 5 /* number of reserved attributes above */ |
864 }; | 864 }; |
865 CK_BBOOL cktrue = CK_TRUE; | 865 CK_BBOOL cktrue = CK_TRUE; |
866 CK_BBOOL ckfalse = CK_FALSE; | 866 CK_BBOOL ckfalse = CK_FALSE; |
867 CK_ATTRIBUTE *attrs = NULL, *ap; | 867 CK_ATTRIBUTE *attrs = NULL, *ap; |
868 const int templateSize = sizeof(privTemplate)/sizeof(privTemplate[0]); | 868 const int templateSize = sizeof(privTemplate)/sizeof(privTemplate[0]); |
869 PRArenaPool *arena; | 869 PLArenaPool *arena; |
870 CK_OBJECT_HANDLE objectID; | 870 CK_OBJECT_HANDLE objectID; |
871 int i, count = 0; | 871 int i, count = 0; |
872 int extra_count = 0; | 872 int extra_count = 0; |
873 CK_RV crv; | 873 CK_RV crv; |
874 SECStatus rv; | 874 SECStatus rv; |
875 PRBool token = ((attrFlags & PK11_ATTR_TOKEN) != 0); | 875 PRBool token = ((attrFlags & PK11_ATTR_TOKEN) != 0); |
876 | 876 |
877 if (pk11_BadAttrFlags(attrFlags)) { | 877 if (pk11_BadAttrFlags(attrFlags)) { |
878 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 878 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
879 return NULL; | 879 return NULL; |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1495 attrFlags, wincx); | 1495 attrFlags, wincx); |
1496 } | 1496 } |
1497 | 1497 |
1498 /* build a public KEA key from the public value */ | 1498 /* build a public KEA key from the public value */ |
1499 SECKEYPublicKey * | 1499 SECKEYPublicKey * |
1500 PK11_MakeKEAPubKey(unsigned char *keyData,int length) | 1500 PK11_MakeKEAPubKey(unsigned char *keyData,int length) |
1501 { | 1501 { |
1502 SECKEYPublicKey *pubk; | 1502 SECKEYPublicKey *pubk; |
1503 SECItem pkData; | 1503 SECItem pkData; |
1504 SECStatus rv; | 1504 SECStatus rv; |
1505 PRArenaPool *arena; | 1505 PLArenaPool *arena; |
1506 | 1506 |
1507 pkData.data = keyData; | 1507 pkData.data = keyData; |
1508 pkData.len = length; | 1508 pkData.len = length; |
1509 | 1509 |
1510 arena = PORT_NewArena (DER_DEFAULT_CHUNKSIZE); | 1510 arena = PORT_NewArena (DER_DEFAULT_CHUNKSIZE); |
1511 if (arena == NULL) | 1511 if (arena == NULL) |
1512 return NULL; | 1512 return NULL; |
1513 | 1513 |
1514 pubk = (SECKEYPublicKey *) PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); | 1514 pubk = (SECKEYPublicKey *) PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); |
1515 if (pubk == NULL) { | 1515 if (pubk == NULL) { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1709 SECKEYEncryptedPrivateKeyInfo * | 1709 SECKEYEncryptedPrivateKeyInfo * |
1710 PK11_ExportEncryptedPrivKeyInfo( | 1710 PK11_ExportEncryptedPrivKeyInfo( |
1711 PK11SlotInfo *slot, /* optional, encrypt key in this slot */ | 1711 PK11SlotInfo *slot, /* optional, encrypt key in this slot */ |
1712 SECOidTag algTag, /* encrypt key with this algorithm */ | 1712 SECOidTag algTag, /* encrypt key with this algorithm */ |
1713 SECItem *pwitem, /* password for PBE encryption */ | 1713 SECItem *pwitem, /* password for PBE encryption */ |
1714 SECKEYPrivateKey *pk, /* encrypt this private key */ | 1714 SECKEYPrivateKey *pk, /* encrypt this private key */ |
1715 int iteration, /* interations for PBE alg */ | 1715 int iteration, /* interations for PBE alg */ |
1716 void *wincx) /* context for password callback ? */ | 1716 void *wincx) /* context for password callback ? */ |
1717 { | 1717 { |
1718 SECKEYEncryptedPrivateKeyInfo *epki = NULL; | 1718 SECKEYEncryptedPrivateKeyInfo *epki = NULL; |
1719 PRArenaPool *arena = NULL; | 1719 PLArenaPool *arena = NULL; |
1720 SECAlgorithmID *algid; | 1720 SECAlgorithmID *algid; |
1721 SECOidTag pbeAlgTag = SEC_OID_UNKNOWN; | 1721 SECOidTag pbeAlgTag = SEC_OID_UNKNOWN; |
1722 SECItem *crypto_param = NULL; | 1722 SECItem *crypto_param = NULL; |
1723 PK11SymKey *key = NULL; | 1723 PK11SymKey *key = NULL; |
1724 SECKEYPrivateKey *tmpPK = NULL; | 1724 SECKEYPrivateKey *tmpPK = NULL; |
1725 SECStatus rv = SECSuccess; | 1725 SECStatus rv = SECSuccess; |
1726 CK_RV crv; | 1726 CK_RV crv; |
1727 CK_ULONG encBufLen; | 1727 CK_ULONG encBufLen; |
1728 CK_MECHANISM_TYPE pbeMechType; | 1728 CK_MECHANISM_TYPE pbeMechType; |
1729 CK_MECHANISM_TYPE cryptoMechType; | 1729 CK_MECHANISM_TYPE cryptoMechType; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1915 | 1915 |
1916 SECKEYPQGParams * | 1916 SECKEYPQGParams * |
1917 PK11_GetPQGParamsFromPrivateKey(SECKEYPrivateKey *privKey) | 1917 PK11_GetPQGParamsFromPrivateKey(SECKEYPrivateKey *privKey) |
1918 { | 1918 { |
1919 CK_ATTRIBUTE pTemplate[] = { | 1919 CK_ATTRIBUTE pTemplate[] = { |
1920 { CKA_PRIME, NULL, 0 }, | 1920 { CKA_PRIME, NULL, 0 }, |
1921 { CKA_SUBPRIME, NULL, 0 }, | 1921 { CKA_SUBPRIME, NULL, 0 }, |
1922 { CKA_BASE, NULL, 0 }, | 1922 { CKA_BASE, NULL, 0 }, |
1923 }; | 1923 }; |
1924 int pTemplateLen = sizeof(pTemplate)/sizeof(pTemplate[0]); | 1924 int pTemplateLen = sizeof(pTemplate)/sizeof(pTemplate[0]); |
1925 PRArenaPool *arena = NULL; | 1925 PLArenaPool *arena = NULL; |
1926 SECKEYPQGParams *params; | 1926 SECKEYPQGParams *params; |
1927 CK_RV crv; | 1927 CK_RV crv; |
1928 | 1928 |
1929 | 1929 |
1930 arena = PORT_NewArena(2048); | 1930 arena = PORT_NewArena(2048); |
1931 if (arena == NULL) { | 1931 if (arena == NULL) { |
1932 goto loser; | 1932 goto loser; |
1933 } | 1933 } |
1934 params=(SECKEYPQGParams *)PORT_ArenaZAlloc(arena,sizeof(SECKEYPQGParams)); | 1934 params=(SECKEYPQGParams *)PORT_ArenaZAlloc(arena,sizeof(SECKEYPQGParams)); |
1935 if (params == NULL) { | 1935 if (params == NULL) { |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2368 for (i=0; i < objCount ; i++) { | 2368 for (i=0; i < objCount ; i++) { |
2369 SECKEYPrivateKey *privKey = | 2369 SECKEYPrivateKey *privKey = |
2370 PK11_MakePrivKey(slot,nullKey,PR_TRUE,key_ids[i],wincx); | 2370 PK11_MakePrivKey(slot,nullKey,PR_TRUE,key_ids[i],wincx); |
2371 SECKEY_AddPrivateKeyToListTail(keys, privKey); | 2371 SECKEY_AddPrivateKeyToListTail(keys, privKey); |
2372 } | 2372 } |
2373 | 2373 |
2374 PORT_Free(key_ids); | 2374 PORT_Free(key_ids); |
2375 return keys; | 2375 return keys; |
2376 } | 2376 } |
2377 | 2377 |
OLD | NEW |