Bug 1732658 [wpt PR 30925] - [CSP] Fix WPT about worker violation event, a=testonly
[gecko.git] / security / ct / CTUtils.h
bloba6cdac25ea75ec5339db422848163d6067d141b2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef CTUtils_h
8 #define CTUtils_h
10 #include <memory>
12 #include "cryptohi.h"
13 #include "keyhi.h"
14 #include "keythi.h"
15 #include "pk11pub.h"
16 #include "mozpkix/Input.h"
17 #include "mozpkix/Result.h"
19 #define MOZILLA_CT_ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0]))
21 struct DeleteHelper {
22 void operator()(CERTSubjectPublicKeyInfo* value) {
23 SECKEY_DestroySubjectPublicKeyInfo(value);
25 void operator()(PK11Context* value) { PK11_DestroyContext(value, true); }
26 void operator()(PK11SlotInfo* value) { PK11_FreeSlot(value); }
27 void operator()(SECKEYPublicKey* value) { SECKEY_DestroyPublicKey(value); }
28 void operator()(SECItem* value) { SECITEM_FreeItem(value, true); }
31 template <class T>
32 struct MaybeDeleteHelper {
33 void operator()(T* ptr) {
34 if (ptr) {
35 DeleteHelper del;
36 del(ptr);
41 typedef std::unique_ptr<CERTSubjectPublicKeyInfo,
42 MaybeDeleteHelper<CERTSubjectPublicKeyInfo>>
43 UniqueCERTSubjectPublicKeyInfo;
44 typedef std::unique_ptr<PK11Context, MaybeDeleteHelper<PK11Context>>
45 UniquePK11Context;
46 typedef std::unique_ptr<PK11SlotInfo, MaybeDeleteHelper<PK11SlotInfo>>
47 UniquePK11SlotInfo;
48 typedef std::unique_ptr<SECKEYPublicKey, MaybeDeleteHelper<SECKEYPublicKey>>
49 UniqueSECKEYPublicKey;
50 typedef std::unique_ptr<SECItem, MaybeDeleteHelper<SECItem>> UniqueSECItem;
52 namespace mozilla {
53 namespace ct {
55 // Reads a TLS-encoded variable length unsigned integer from |in|.
56 // The integer is expected to be in big-endian order, which is used by TLS.
57 // Note: checks if the output parameter overflows while reading.
58 // |length| indicates the size (in bytes) of the serialized integer.
59 template <size_t length, typename T>
60 mozilla::pkix::Result ReadUint(mozilla::pkix::Reader& in, T& out);
62 // Reads a length-prefixed variable amount of bytes from |in|, updating |out|
63 // on success. |prefixLength| indicates the number of bytes needed to represent
64 // the length.
65 template <size_t prefixLength>
66 mozilla::pkix::Result ReadVariableBytes(mozilla::pkix::Reader& in,
67 mozilla::pkix::Input& out);
69 } // namespace ct
70 } // namespace mozilla
72 #endif // CTUtils_h