| 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 #include "cryptohi.h" | 4 #include "cryptohi.h" |
| 5 #include "keyhi.h" | 5 #include "keyhi.h" |
| 6 #include "secoid.h" | 6 #include "secoid.h" |
| 7 #include "secitem.h" | 7 #include "secitem.h" |
| 8 #include "secder.h" | 8 #include "secder.h" |
| 9 #include "base64.h" | 9 #include "base64.h" |
| 10 #include "secasn1.h" | 10 #include "secasn1.h" |
| (...skipping 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1206 return pubk; | 1206 return pubk; |
| 1207 break; | 1207 break; |
| 1208 default: | 1208 default: |
| 1209 break; | 1209 break; |
| 1210 } | 1210 } |
| 1211 | 1211 |
| 1212 PORT_FreeArena (arena, PR_FALSE); | 1212 PORT_FreeArena (arena, PR_FALSE); |
| 1213 return NULL; | 1213 return NULL; |
| 1214 } | 1214 } |
| 1215 | 1215 |
| 1216 CERTSubjectPublicKeyInfo * | 1216 static CERTSubjectPublicKeyInfo * |
| 1217 SECKEY_CreateSubjectPublicKeyInfo(SECKEYPublicKey *pubk) | 1217 seckey_CreateSubjectPublicKeyInfo_helper(SECKEYPublicKey *pubk) |
| 1218 { | 1218 { |
| 1219 CERTSubjectPublicKeyInfo *spki; | 1219 CERTSubjectPublicKeyInfo *spki; |
| 1220 PLArenaPool *arena; | 1220 PLArenaPool *arena; |
| 1221 SECItem params = { siBuffer, NULL, 0 }; | 1221 SECItem params = { siBuffer, NULL, 0 }; |
| 1222 | 1222 |
| 1223 if (!pubk) { | |
| 1224 PORT_SetError(SEC_ERROR_INVALID_ARGS); | |
| 1225 return NULL; | |
| 1226 } | |
| 1227 | |
| 1228 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1223 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
| 1229 if (arena == NULL) { | 1224 if (arena == NULL) { |
| 1230 PORT_SetError(SEC_ERROR_NO_MEMORY); | 1225 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 1231 return NULL; | 1226 return NULL; |
| 1232 } | 1227 } |
| 1233 | 1228 |
| 1234 spki = (CERTSubjectPublicKeyInfo *) PORT_ArenaZAlloc(arena, sizeof (*spki)); | 1229 spki = (CERTSubjectPublicKeyInfo *) PORT_ArenaZAlloc(arena, sizeof (*spki)); |
| 1235 if (spki != NULL) { | 1230 if (spki != NULL) { |
| 1236 SECStatus rv; | 1231 SECStatus rv; |
| 1237 SECItem *rv_item; | 1232 SECItem *rv_item; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1325 break; | 1320 break; |
| 1326 } | 1321 } |
| 1327 } else { | 1322 } else { |
| 1328 PORT_SetError(SEC_ERROR_NO_MEMORY); | 1323 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 1329 } | 1324 } |
| 1330 | 1325 |
| 1331 PORT_FreeArena(arena, PR_FALSE); | 1326 PORT_FreeArena(arena, PR_FALSE); |
| 1332 return NULL; | 1327 return NULL; |
| 1333 } | 1328 } |
| 1334 | 1329 |
| 1330 CERTSubjectPublicKeyInfo * |
| 1331 SECKEY_CreateSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) |
| 1332 { |
| 1333 CERTSubjectPublicKeyInfo *spki; |
| 1334 SECKEYPublicKey *tempKey; |
| 1335 |
| 1336 if (!pubk) { |
| 1337 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
| 1338 return NULL; |
| 1339 } |
| 1340 |
| 1341 tempKey = SECKEY_CopyPublicKey(pubk); |
| 1342 if (!tempKey) { |
| 1343 return NULL; |
| 1344 } |
| 1345 spki = seckey_CreateSubjectPublicKeyInfo_helper(tempKey); |
| 1346 SECKEY_DestroyPublicKey(tempKey); |
| 1347 return spki; |
| 1348 } |
| 1349 |
| 1335 void | 1350 void |
| 1336 SECKEY_DestroySubjectPublicKeyInfo(CERTSubjectPublicKeyInfo *spki) | 1351 SECKEY_DestroySubjectPublicKeyInfo(CERTSubjectPublicKeyInfo *spki) |
| 1337 { | 1352 { |
| 1338 if (spki && spki->arena) { | 1353 if (spki && spki->arena) { |
| 1339 PORT_FreeArena(spki->arena, PR_FALSE); | 1354 PORT_FreeArena(spki->arena, PR_FALSE); |
| 1340 } | 1355 } |
| 1341 } | 1356 } |
| 1342 | 1357 |
| 1343 /* | |
| 1344 * this only works for RSA keys... need to do something | |
| 1345 * similiar to CERT_ExtractPublicKey for other key times. | |
| 1346 */ | |
| 1347 SECKEYPublicKey * | |
| 1348 SECKEY_DecodeDERPublicKey(const SECItem *pubkder) | |
| 1349 { | |
| 1350 PLArenaPool *arena; | |
| 1351 SECKEYPublicKey *pubk; | |
| 1352 SECStatus rv; | |
| 1353 SECItem newPubkder; | |
| 1354 | |
| 1355 arena = PORT_NewArena (DER_DEFAULT_CHUNKSIZE); | |
| 1356 if (arena == NULL) { | |
| 1357 PORT_SetError (SEC_ERROR_NO_MEMORY); | |
| 1358 return NULL; | |
| 1359 } | |
| 1360 | |
| 1361 pubk = (SECKEYPublicKey *) PORT_ArenaZAlloc (arena, sizeof (SECKEYPublicKey)
); | |
| 1362 if (pubk != NULL) { | |
| 1363 pubk->arena = arena; | |
| 1364 pubk->pkcs11Slot = NULL; | |
| 1365 pubk->pkcs11ID = 0; | |
| 1366 prepare_rsa_pub_key_for_asn1(pubk); | |
| 1367 /* copy the DER into the arena, since Quick DER returns data that points | |
| 1368 into the DER input, which may get freed by the caller */ | |
| 1369 rv = SECITEM_CopyItem(arena, &newPubkder, pubkder); | |
| 1370 if ( rv == SECSuccess ) { | |
| 1371 rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_RSAPublicKeyTemplate
, | |
| 1372 &newPubkder); | |
| 1373 } | |
| 1374 if (rv == SECSuccess) | |
| 1375 return pubk; | |
| 1376 SECKEY_DestroyPublicKey (pubk); | |
| 1377 } else { | |
| 1378 PORT_SetError (SEC_ERROR_NO_MEMORY); | |
| 1379 } | |
| 1380 | |
| 1381 PORT_FreeArena (arena, PR_FALSE); | |
| 1382 return NULL; | |
| 1383 } | |
| 1384 | |
| 1385 /* | |
| 1386 * Decode a base64 ascii encoded DER encoded public key. | |
| 1387 */ | |
| 1388 SECKEYPublicKey * | |
| 1389 SECKEY_ConvertAndDecodePublicKey(const char *pubkstr) | |
| 1390 { | |
| 1391 SECKEYPublicKey *pubk; | |
| 1392 SECStatus rv; | |
| 1393 SECItem der; | |
| 1394 | |
| 1395 rv = ATOB_ConvertAsciiToItem (&der, pubkstr); | |
| 1396 if (rv != SECSuccess) | |
| 1397 return NULL; | |
| 1398 | |
| 1399 pubk = SECKEY_DecodeDERPublicKey (&der); | |
| 1400 | |
| 1401 PORT_Free (der.data); | |
| 1402 return pubk; | |
| 1403 } | |
| 1404 | |
| 1405 SECItem * | 1358 SECItem * |
| 1406 SECKEY_EncodeDERSubjectPublicKeyInfo(SECKEYPublicKey *pubk) | 1359 SECKEY_EncodeDERSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) |
| 1407 { | 1360 { |
| 1408 CERTSubjectPublicKeyInfo *spki=NULL; | 1361 CERTSubjectPublicKeyInfo *spki=NULL; |
| 1409 SECItem *spkiDER=NULL; | 1362 SECItem *spkiDER=NULL; |
| 1410 | 1363 |
| 1411 /* get the subjectpublickeyinfo */ | 1364 /* get the subjectpublickeyinfo */ |
| 1412 spki = SECKEY_CreateSubjectPublicKeyInfo(pubk); | 1365 spki = SECKEY_CreateSubjectPublicKeyInfo(pubk); |
| 1413 if( spki == NULL ) { | 1366 if( spki == NULL ) { |
| 1414 goto finish; | 1367 goto finish; |
| 1415 } | 1368 } |
| 1416 | 1369 |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1750 pubk->keyType = dhKey; | 1703 pubk->keyType = dhKey; |
| 1751 break; | 1704 break; |
| 1752 default: | 1705 default: |
| 1753 rv = SECFailure; | 1706 rv = SECFailure; |
| 1754 break; | 1707 break; |
| 1755 } | 1708 } |
| 1756 | 1709 |
| 1757 finish: | 1710 finish: |
| 1758 if (rv != SECSuccess) { | 1711 if (rv != SECSuccess) { |
| 1759 if (arena != NULL) { | 1712 if (arena != NULL) { |
| 1760 PORT_FreeArena(arena, PR_TRUE); | 1713 PORT_FreeArena(arena, PR_FALSE); |
| 1761 } | 1714 } |
| 1762 pubk = NULL; | 1715 pubk = NULL; |
| 1763 } | 1716 } |
| 1764 return pubk; | 1717 return pubk; |
| 1765 } | 1718 } |
| 1766 | 1719 |
| 1767 SECKEYPrivateKeyList* | 1720 SECKEYPrivateKeyList* |
| 1768 SECKEY_NewPrivateKeyList(void) | 1721 SECKEY_NewPrivateKeyList(void) |
| 1769 { | 1722 { |
| 1770 PLArenaPool *arena = NULL; | 1723 PLArenaPool *arena = NULL; |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1929 { | 1882 { |
| 1930 SECStatus rv = SECFailure; | 1883 SECStatus rv = SECFailure; |
| 1931 if (key && key->pkcs11Slot && key->pkcs11ID) { | 1884 if (key && key->pkcs11Slot && key->pkcs11ID) { |
| 1932 key->staticflags |= SECKEY_Attributes_Cached; | 1885 key->staticflags |= SECKEY_Attributes_Cached; |
| 1933 SECKEY_CacheAttribute(key, CKA_PRIVATE); | 1886 SECKEY_CacheAttribute(key, CKA_PRIVATE); |
| 1934 SECKEY_CacheAttribute(key, CKA_ALWAYS_AUTHENTICATE); | 1887 SECKEY_CacheAttribute(key, CKA_ALWAYS_AUTHENTICATE); |
| 1935 rv = SECSuccess; | 1888 rv = SECSuccess; |
| 1936 } | 1889 } |
| 1937 return rv; | 1890 return rv; |
| 1938 } | 1891 } |
| OLD | NEW |