tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nss_scoped_ptrs.h (3892B)


      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 
      7 #ifndef nss_scoped_ptrs_h__
      8 #define nss_scoped_ptrs_h__
      9 
     10 #include <memory>
     11 
     12 #include "cert.h"
     13 #include "keyhi.h"
     14 #include "nss.h"
     15 #include "p12.h"
     16 #include "pk11hpke.h"
     17 #include "pk11pqg.h"
     18 #include "pk11pub.h"
     19 #include "pkcs11uri.h"
     20 #include "secmod.h"
     21 
     22 struct ScopedDelete {
     23  void operator()(CERTCertificate* cert) { CERT_DestroyCertificate(cert); }
     24  void operator()(CERTCertificateList* list) {
     25    CERT_DestroyCertificateList(list);
     26  }
     27  void operator()(CERTDistNames* names) { CERT_FreeDistNames(names); }
     28  void operator()(CERTName* name) { CERT_DestroyName(name); }
     29  void operator()(CERTCertList* list) { CERT_DestroyCertList(list); }
     30  void operator()(CERTSubjectPublicKeyInfo* spki) {
     31    SECKEY_DestroySubjectPublicKeyInfo(spki);
     32  }
     33  void operator()(HpkeContext* context) {
     34    PK11_HPKE_DestroyContext(context, true);
     35  }
     36  void operator()(PK11Context* context) { PK11_DestroyContext(context, true); }
     37  void operator()(PK11GenericObject* obj) { PK11_DestroyGenericObject(obj); }
     38  void operator()(PK11SlotInfo* slot) { PK11_FreeSlot(slot); }
     39  void operator()(PK11SlotList* slots) { PK11_FreeSlotList(slots); }
     40  void operator()(PK11SymKey* key) { PK11_FreeSymKey(key); }
     41  void operator()(PK11URI* uri) { PK11URI_DestroyURI(uri); }
     42  void operator()(PLArenaPool* arena) { PORT_FreeArena(arena, PR_FALSE); }
     43  void operator()(PQGParams* pqg) { PK11_PQG_DestroyParams(pqg); }
     44  void operator()(PRFileDesc* fd) { PR_Close(fd); }
     45  void operator()(SECAlgorithmID* id) { SECOID_DestroyAlgorithmID(id, true); }
     46  void operator()(SECKEYEncryptedPrivateKeyInfo* e) {
     47    SECKEY_DestroyEncryptedPrivateKeyInfo(e, true);
     48  }
     49  void operator()(SECItem* item) { SECITEM_FreeItem(item, true); }
     50  void operator()(SECKEYPublicKey* key) { SECKEY_DestroyPublicKey(key); }
     51  void operator()(SECKEYPrivateKey* key) { SECKEY_DestroyPrivateKey(key); }
     52  void operator()(SECKEYPrivateKeyList* list) {
     53    SECKEY_DestroyPrivateKeyList(list);
     54  }
     55  void operator()(SECMODModule* module) { SECMOD_DestroyModule(module); }
     56  void operator()(SEC_PKCS12DecoderContext* dcx) {
     57    SEC_PKCS12DecoderFinish(dcx);
     58  }
     59  void operator()(SEC_PKCS7DecoderContext* dcx) {
     60    SEC_PKCS7ContentInfo* cinfo = SEC_PKCS7DecoderFinish(dcx);
     61    if (cinfo) {
     62      SEC_PKCS7DestroyContentInfo(cinfo);
     63    }
     64  }
     65  void operator()(SEC_PKCS7ContentInfo* cinfo) {
     66    SEC_PKCS7DestroyContentInfo(cinfo);
     67  }
     68  void operator()(NSSInitContext* init) { NSS_ShutdownContext(init); }
     69 };
     70 
     71 template <class T>
     72 struct ScopedMaybeDelete {
     73  void operator()(T* ptr) {
     74    if (ptr) {
     75      ScopedDelete del;
     76      del(ptr);
     77    }
     78  }
     79 };
     80 
     81 #define SCOPED(x) typedef std::unique_ptr<x, ScopedMaybeDelete<x> > Scoped##x
     82 
     83 SCOPED(CERTCertList);
     84 SCOPED(CERTCertificate);
     85 SCOPED(CERTCertificateList);
     86 SCOPED(CERTDistNames);
     87 SCOPED(CERTName);
     88 SCOPED(CERTSubjectPublicKeyInfo);
     89 SCOPED(HpkeContext);
     90 SCOPED(NSSInitContext);
     91 SCOPED(PK11Context);
     92 SCOPED(PK11GenericObject);
     93 SCOPED(PK11SlotInfo);
     94 SCOPED(PK11SlotList);
     95 SCOPED(PK11SymKey);
     96 SCOPED(PK11URI);
     97 SCOPED(PLArenaPool);
     98 SCOPED(PQGParams);
     99 SCOPED(PRFileDesc);
    100 SCOPED(SECAlgorithmID);
    101 SCOPED(SECItem);
    102 SCOPED(SECKEYEncryptedPrivateKeyInfo);
    103 SCOPED(SECKEYPrivateKey);
    104 SCOPED(SECKEYPrivateKeyList);
    105 SCOPED(SECKEYPublicKey);
    106 SCOPED(SECMODModule);
    107 SCOPED(SEC_PKCS12DecoderContext);
    108 SCOPED(SEC_PKCS7DecoderContext);
    109 SCOPED(SEC_PKCS7ContentInfo);
    110 
    111 #undef SCOPED
    112 
    113 struct StackSECItem : public SECItem {
    114  StackSECItem() : SECItem({siBuffer, nullptr, 0}) {}
    115  ~StackSECItem() { Reset(); }
    116  void Reset() { SECITEM_FreeItem(this, PR_FALSE); }
    117 };
    118 
    119 #endif  // nss_scoped_ptrs_h__