Skip to content

Commit 7e1cfb9

Browse files
authored
Merge pull request #5504 from randombit/jack/pimpl-x509-object
Use a shared_ptr pimpl for X509_Object
2 parents 3275f63 + b121293 commit 7e1cfb9

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

src/lib/x509/crl_ent.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,30 @@
1717

1818
namespace Botan {
1919

20-
struct CRL_Entry_Data {
20+
class CRL_Entry_Data final {
21+
public:
22+
CRL_Entry_Data(const X509_Certificate& cert, CRL_Code why) :
23+
m_serial(cert.serial_number()), m_time(X509_Time(std::chrono::system_clock::now())), m_reason(why) {
24+
if(why != CRL_Code::Unspecified) {
25+
m_extensions.add(std::make_unique<Cert_Extension::CRL_ReasonCode>(why));
26+
}
27+
}
28+
29+
CRL_Entry_Data() = default;
30+
31+
// NOLINTBEGIN(*non-private-member-variables-in-classes)
2132
std::vector<uint8_t> m_serial;
2233
X509_Time m_time;
2334
CRL_Code m_reason = CRL_Code::Unspecified;
2435
Extensions m_extensions;
36+
// NOLINTEND(*non-private-member-variables-in-classes)
2537
};
2638

2739
/*
2840
* Create a CRL_Entry
2941
*/
3042
CRL_Entry::CRL_Entry(const X509_Certificate& cert, CRL_Code why) {
31-
m_data = std::make_shared<CRL_Entry_Data>();
32-
m_data->m_serial = cert.serial_number();
33-
m_data->m_time = X509_Time(std::chrono::system_clock::now());
34-
m_data->m_reason = why;
35-
36-
if(why != CRL_Code::Unspecified) {
37-
m_data->m_extensions.add(std::make_unique<Cert_Extension::CRL_ReasonCode>(why));
38-
}
43+
m_data = std::make_shared<CRL_Entry_Data>(cert, why);
3944
}
4045

4146
/*

src/lib/x509/x509_crl.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@
1616

1717
namespace Botan {
1818

19-
struct CRL_Data {
19+
class CRL_Data final {
20+
public:
21+
CRL_Data(const X509_DN& issuer,
22+
const X509_Time& this_update,
23+
const X509_Time& next_update,
24+
const std::vector<CRL_Entry>& revoked) :
25+
m_issuer(issuer), m_this_update(this_update), m_next_update(next_update), m_entries(revoked) {}
26+
27+
CRL_Data() = default;
28+
29+
// NOLINTBEGIN(*non-private-member-variables-in-classes)
2030
X509_DN m_issuer;
2131
size_t m_version{};
2232
X509_Time m_this_update;
@@ -28,6 +38,7 @@ struct CRL_Data {
2838
size_t m_crl_number = 0;
2939
std::vector<uint8_t> m_auth_key_id;
3040
std::vector<std::string> m_idp_urls;
41+
// NOLINTEND(*non-private-member-variables-in-classes)
3142
};
3243

3344
std::string X509_CRL::PEM_label() const {
@@ -58,11 +69,7 @@ X509_CRL::X509_CRL(const X509_DN& issuer,
5869
const X509_Time& this_update,
5970
const X509_Time& next_update,
6071
const std::vector<CRL_Entry>& revoked) {
61-
m_data = std::make_shared<CRL_Data>();
62-
m_data->m_issuer = issuer;
63-
m_data->m_this_update = this_update;
64-
m_data->m_next_update = next_update;
65-
m_data->m_entries = revoked;
72+
m_data = std::make_shared<CRL_Data>(issuer, this_update, next_update, revoked);
6673
}
6774

6875
/**

src/lib/x509/x509_crl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class Extensions;
2020
class X509_Certificate;
2121
class X509_DN;
2222

23-
struct CRL_Entry_Data;
24-
struct CRL_Data;
23+
class CRL_Entry_Data;
24+
class CRL_Data;
2525

2626
/**
2727
* This class represents CRL entries
@@ -71,7 +71,7 @@ class BOTAN_PUBLIC_API(2, 0) CRL_Entry final : public ASN1_Object {
7171

7272
const CRL_Entry_Data& data() const;
7373

74-
std::shared_ptr<CRL_Entry_Data> m_data;
74+
std::shared_ptr<const CRL_Entry_Data> m_data;
7575
};
7676

7777
/**
@@ -207,7 +207,7 @@ class BOTAN_PUBLIC_API(2, 0) X509_CRL final : public X509_Object {
207207

208208
const CRL_Data& data() const;
209209

210-
std::shared_ptr<CRL_Data> m_data;
210+
std::shared_ptr<const CRL_Data> m_data;
211211
};
212212

213213
} // namespace Botan

src/lib/x509/x509_obj.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ void X509_Object::encode_into(DER_Encoder& to) const {
6666
* Read a BER encoded X.509 object
6767
*/
6868
void X509_Object::decode_from(BER_Decoder& from) {
69+
auto data = std::make_shared<Signed_Data>();
70+
6971
from.start_sequence()
7072
.start_sequence()
71-
.raw_bytes(m_tbs_bits)
73+
.raw_bytes(data->m_tbs_bits)
7274
.end_cons()
73-
.decode(m_sig_algo)
74-
.decode(m_sig, ASN1_Type::BitString)
75+
.decode(data->m_sig_algo)
76+
.decode(data->m_sig, ASN1_Type::BitString)
7577
.end_cons();
7678

79+
m_signed_data = std::move(data);
7780
force_decode();
7881
}
7982

@@ -88,7 +91,7 @@ std::string X509_Object::PEM_encode() const {
8891
* Return the TBS data
8992
*/
9093
std::vector<uint8_t> X509_Object::tbs_data() const {
91-
return ASN1::put_in_sequence(m_tbs_bits);
94+
return ASN1::put_in_sequence(signed_body());
9295
}
9396

9497
/*

src/lib/x509/x509_obj.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ class BOTAN_PUBLIC_API(2, 0) X509_Object : public ASN1_Object {
3838
/**
3939
* @return signature on tbs_data()
4040
*/
41-
const std::vector<uint8_t>& signature() const { return m_sig; }
41+
const std::vector<uint8_t>& signature() const { return m_signed_data->m_sig; }
4242

4343
/**
4444
* @return signed body
4545
*/
46-
const std::vector<uint8_t>& signed_body() const { return m_tbs_bits; }
46+
const std::vector<uint8_t>& signed_body() const { return m_signed_data->m_tbs_bits; }
4747

4848
/**
4949
* @return signature algorithm that was used to generate signature
5050
*/
51-
const AlgorithmIdentifier& signature_algorithm() const { return m_sig_algo; }
51+
const AlgorithmIdentifier& signature_algorithm() const { return m_signed_data->m_sig_algo; }
5252

5353
/**
5454
* Create a signed X509 object.
@@ -127,9 +127,13 @@ class BOTAN_PUBLIC_API(2, 0) X509_Object : public ASN1_Object {
127127
private:
128128
virtual void force_decode() = 0;
129129

130-
AlgorithmIdentifier m_sig_algo;
131-
std::vector<uint8_t> m_tbs_bits;
132-
std::vector<uint8_t> m_sig;
130+
struct Signed_Data {
131+
AlgorithmIdentifier m_sig_algo;
132+
std::vector<uint8_t> m_tbs_bits;
133+
std::vector<uint8_t> m_sig;
134+
};
135+
136+
std::shared_ptr<const Signed_Data> m_signed_data;
133137
};
134138

135139
} // namespace Botan

src/lib/x509/x509cert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class BOTAN_PUBLIC_API(2, 0) X509_Certificate : public X509_Object {
436436

437437
const X509_Certificate_Data& data() const;
438438

439-
std::shared_ptr<X509_Certificate_Data> m_data;
439+
std::shared_ptr<const X509_Certificate_Data> m_data;
440440
};
441441

442442
/**

0 commit comments

Comments
 (0)