Home | History | Annotate | Download | only in crypto
      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 
      5 #ifndef CRYPTO_SCOPED_NSS_TYPES_H_
      6 #define CRYPTO_SCOPED_NSS_TYPES_H_
      7 
      8 #include <keyhi.h>
      9 #include <nss.h>
     10 #include <pk11pub.h>
     11 #include <plarena.h>
     12 
     13 #include <memory>
     14 
     15 namespace crypto {
     16 
     17 template <typename Type, void (*Destroyer)(Type*)>
     18 struct NSSDestroyer {
     19   void operator()(Type* ptr) const {
     20     Destroyer(ptr);
     21   }
     22 };
     23 
     24 template <typename Type, void (*Destroyer)(Type*, PRBool), PRBool freeit>
     25 struct NSSDestroyer1 {
     26   void operator()(Type* ptr) const {
     27     Destroyer(ptr, freeit);
     28   }
     29 };
     30 
     31 // Define some convenient scopers around NSS pointers.
     32 typedef std::unique_ptr<
     33     PK11Context,
     34     NSSDestroyer1<PK11Context, PK11_DestroyContext, PR_TRUE>>
     35     ScopedPK11Context;
     36 typedef std::unique_ptr<PK11SlotInfo, NSSDestroyer<PK11SlotInfo, PK11_FreeSlot>>
     37     ScopedPK11Slot;
     38 typedef std::unique_ptr<PK11SlotList,
     39                         NSSDestroyer<PK11SlotList, PK11_FreeSlotList>>
     40     ScopedPK11SlotList;
     41 typedef std::unique_ptr<PK11SymKey, NSSDestroyer<PK11SymKey, PK11_FreeSymKey>>
     42     ScopedPK11SymKey;
     43 typedef std::unique_ptr<SECKEYPublicKey,
     44                         NSSDestroyer<SECKEYPublicKey, SECKEY_DestroyPublicKey>>
     45     ScopedSECKEYPublicKey;
     46 typedef std::unique_ptr<
     47     SECKEYPrivateKey,
     48     NSSDestroyer<SECKEYPrivateKey, SECKEY_DestroyPrivateKey>>
     49     ScopedSECKEYPrivateKey;
     50 typedef std::unique_ptr<
     51     SECAlgorithmID,
     52     NSSDestroyer1<SECAlgorithmID, SECOID_DestroyAlgorithmID, PR_TRUE>>
     53     ScopedSECAlgorithmID;
     54 typedef std::unique_ptr<SECItem,
     55                         NSSDestroyer1<SECItem, SECITEM_FreeItem, PR_TRUE>>
     56     ScopedSECItem;
     57 typedef std::unique_ptr<PLArenaPool,
     58                         NSSDestroyer1<PLArenaPool, PORT_FreeArena, PR_FALSE>>
     59     ScopedPLArenaPool;
     60 
     61 }  // namespace crypto
     62 
     63 #endif  // CRYPTO_SCOPED_NSS_TYPES_H_
     64