1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CRYPTO_SCOPED_NSS_TYPES_H_ 6 #define CRYPTO_SCOPED_NSS_TYPES_H_ 7 8 #include <cert.h> 9 #include <certt.h> 10 #include <keyhi.h> 11 #include <nss.h> 12 #include <pk11pub.h> 13 #include <plarena.h> 14 15 #include <memory> 16 17 namespace crypto { 18 19 template <typename Type, void (*Destroyer)(Type*)> 20 struct NSSDestroyer { operatorNSSDestroyer21 void operator()(Type* ptr) const { Destroyer(ptr); } 22 }; 23 24 template <typename Type, void (*Destroyer)(Type*, PRBool), PRBool freeit> 25 struct NSSDestroyer1 { operatorNSSDestroyer126 void operator()(Type* ptr) const { Destroyer(ptr, freeit); } 27 }; 28 29 // Define some convenient scopers around NSS pointers. 30 typedef std::unique_ptr< 31 PK11Context, 32 NSSDestroyer1<PK11Context, PK11_DestroyContext, PR_TRUE>> 33 ScopedPK11Context; 34 typedef std::unique_ptr<PK11SlotInfo, NSSDestroyer<PK11SlotInfo, PK11_FreeSlot>> 35 ScopedPK11Slot; 36 typedef std::unique_ptr<PK11SlotList, 37 NSSDestroyer<PK11SlotList, PK11_FreeSlotList>> 38 ScopedPK11SlotList; 39 typedef std::unique_ptr< 40 SECKEYPublicKeyList, 41 NSSDestroyer<SECKEYPublicKeyList, SECKEY_DestroyPublicKeyList>> 42 ScopedSECKEYPublicKeyList; 43 typedef std::unique_ptr<PK11SymKey, NSSDestroyer<PK11SymKey, PK11_FreeSymKey>> 44 ScopedPK11SymKey; 45 typedef std::unique_ptr<SECKEYPublicKey, 46 NSSDestroyer<SECKEYPublicKey, SECKEY_DestroyPublicKey>> 47 ScopedSECKEYPublicKey; 48 typedef std::unique_ptr< 49 SECKEYPrivateKey, 50 NSSDestroyer<SECKEYPrivateKey, SECKEY_DestroyPrivateKey>> 51 ScopedSECKEYPrivateKey; 52 typedef std::unique_ptr< 53 SECAlgorithmID, 54 NSSDestroyer1<SECAlgorithmID, SECOID_DestroyAlgorithmID, PR_TRUE>> 55 ScopedSECAlgorithmID; 56 typedef std::unique_ptr<SECItem, 57 NSSDestroyer1<SECItem, SECITEM_FreeItem, PR_TRUE>> 58 ScopedSECItem; 59 typedef std::unique_ptr<PLArenaPool, 60 NSSDestroyer1<PLArenaPool, PORT_FreeArena, PR_FALSE>> 61 ScopedPLArenaPool; 62 typedef std::unique_ptr< 63 CERTSubjectPublicKeyInfo, 64 NSSDestroyer<CERTSubjectPublicKeyInfo, SECKEY_DestroySubjectPublicKeyInfo>> 65 ScopedCERTSubjectPublicKeyInfo; 66 typedef std::unique_ptr<CERTCertList, 67 NSSDestroyer<CERTCertList, CERT_DestroyCertList>> 68 ScopedCERTCertList; 69 70 } // namespace crypto 71 72 #endif // CRYPTO_SCOPED_NSS_TYPES_H_ 73