scoped_nss_types.h 2.2 KB

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