nss_scoped_ptrs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef nss_scoped_ptrs_h__
  7. #define nss_scoped_ptrs_h__
  8. #include <memory>
  9. #include "cert.h"
  10. #include "keyhi.h"
  11. #include "p12.h"
  12. #include "pk11pqg.h"
  13. #include "pk11pub.h"
  14. #include "pkcs11uri.h"
  15. #include "secmod.h"
  16. struct ScopedDelete {
  17. void operator()(CERTCertificate* cert) { CERT_DestroyCertificate(cert); }
  18. void operator()(CERTCertificateList* list) {
  19. CERT_DestroyCertificateList(list);
  20. }
  21. void operator()(CERTDistNames* names) { CERT_FreeDistNames(names); }
  22. void operator()(CERTName* name) { CERT_DestroyName(name); }
  23. void operator()(CERTCertList* list) { CERT_DestroyCertList(list); }
  24. void operator()(CERTSubjectPublicKeyInfo* spki) {
  25. SECKEY_DestroySubjectPublicKeyInfo(spki);
  26. }
  27. void operator()(PK11Context* context) { PK11_DestroyContext(context, true); }
  28. void operator()(PK11GenericObject* obj) { PK11_DestroyGenericObject(obj); }
  29. void operator()(PK11SlotInfo* slot) { PK11_FreeSlot(slot); }
  30. void operator()(PK11SlotList* slots) { PK11_FreeSlotList(slots); }
  31. void operator()(PK11SymKey* key) { PK11_FreeSymKey(key); }
  32. void operator()(PK11URI* uri) { PK11URI_DestroyURI(uri); }
  33. void operator()(PLArenaPool* arena) { PORT_FreeArena(arena, PR_FALSE); }
  34. void operator()(PQGParams* pqg) { PK11_PQG_DestroyParams(pqg); }
  35. void operator()(PRFileDesc* fd) { PR_Close(fd); }
  36. void operator()(SECAlgorithmID* id) { SECOID_DestroyAlgorithmID(id, true); }
  37. void operator()(SECKEYEncryptedPrivateKeyInfo* e) {
  38. SECKEY_DestroyEncryptedPrivateKeyInfo(e, true);
  39. }
  40. void operator()(SECItem* item) { SECITEM_FreeItem(item, true); }
  41. void operator()(SECKEYPublicKey* key) { SECKEY_DestroyPublicKey(key); }
  42. void operator()(SECKEYPrivateKey* key) { SECKEY_DestroyPrivateKey(key); }
  43. void operator()(SECKEYPrivateKeyList* list) {
  44. SECKEY_DestroyPrivateKeyList(list);
  45. }
  46. void operator()(SECMODModule* module) { SECMOD_DestroyModule(module); }
  47. void operator()(SEC_PKCS12DecoderContext* dcx) {
  48. SEC_PKCS12DecoderFinish(dcx);
  49. }
  50. };
  51. template <class T>
  52. struct ScopedMaybeDelete {
  53. void operator()(T* ptr) {
  54. if (ptr) {
  55. ScopedDelete del;
  56. del(ptr);
  57. }
  58. }
  59. };
  60. #define SCOPED(x) typedef std::unique_ptr<x, ScopedMaybeDelete<x> > Scoped##x
  61. SCOPED(CERTCertList);
  62. SCOPED(CERTCertificate);
  63. SCOPED(CERTCertificateList);
  64. SCOPED(CERTDistNames);
  65. SCOPED(CERTName);
  66. SCOPED(CERTSubjectPublicKeyInfo);
  67. SCOPED(PK11Context);
  68. SCOPED(PK11GenericObject);
  69. SCOPED(PK11SlotInfo);
  70. SCOPED(PK11SlotList);
  71. SCOPED(PK11SymKey);
  72. SCOPED(PK11URI);
  73. SCOPED(PLArenaPool);
  74. SCOPED(PQGParams);
  75. SCOPED(PRFileDesc);
  76. SCOPED(SECAlgorithmID);
  77. SCOPED(SECItem);
  78. SCOPED(SECKEYEncryptedPrivateKeyInfo);
  79. SCOPED(SECKEYPrivateKey);
  80. SCOPED(SECKEYPrivateKeyList);
  81. SCOPED(SECKEYPublicKey);
  82. SCOPED(SECMODModule);
  83. SCOPED(SEC_PKCS12DecoderContext);
  84. #undef SCOPED
  85. struct StackSECItem : public SECItem {
  86. StackSECItem() : SECItem({siBuffer, nullptr, 0}) {}
  87. ~StackSECItem() { Reset(); }
  88. void Reset() { SECITEM_FreeItem(this, PR_FALSE); }
  89. };
  90. #endif // nss_scoped_ptrs_h__