1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
* OCSP
* (C) 2012 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/ocsp.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/x509_ext.h>
#include <botan/base64.h>
#include <iostream>
#include <botan/hex.h>
namespace Botan {
namespace OCSP {
namespace {
void decode_optional_list(BER_Decoder& ber,
ASN1_Tag tag,
std::vector<X509_Certificate>& output)
{
BER_Object obj = ber.get_next_object();
if(obj.type_tag != tag || obj.class_tag != (CONTEXT_SPECIFIC | CONSTRUCTED))
{
ber.push_back(obj);
return;
}
BER_Decoder list(obj.value);
while(list.more_items())
{
BER_Object certbits = list.get_next_object();
X509_Certificate cert(unlock(certbits.value));
output.push_back(std::move(cert));
}
}
}
std::vector<byte> Request::BER_encode() const
{
CertID certid(m_issuer, m_subject);
return DER_Encoder().start_cons(SEQUENCE)
.start_cons(SEQUENCE)
.start_explicit(0)
.encode(static_cast<size_t>(0)) // version #
.end_explicit()
.start_cons(SEQUENCE)
.start_cons(SEQUENCE)
.encode(certid)
.end_cons()
.end_cons()
.end_cons()
.end_cons().get_contents_unlocked();
}
std::string Request::base64_encode() const
{
return Botan::base64_encode(BER_encode());
}
Response::Response(const std::vector<byte>& response_bits)
{
BER_Decoder ber(response_bits);
BER_Decoder response_outer = ber.start_cons(SEQUENCE);
size_t resp_status = 0;
response_outer.decode(resp_status, ENUMERATED, UNIVERSAL);
if(response_outer.more_items())
{
BER_Decoder response_bytes =
response_outer.start_cons(ASN1_Tag(0), CONTEXT_SPECIFIC).start_cons(SEQUENCE);
response_bytes.decode_and_check(OID("1.3.6.1.5.5.7.48.1.1"),
"Unknown response OID in OCSP response");
std::vector<byte> response_vec;
response_bytes.decode(response_vec, OCTET_STRING);
BER_Decoder basicresponse_x(response_vec);
BER_Decoder basicresponse = basicresponse_x.start_cons(SEQUENCE);
BER_Decoder tbs_response = basicresponse.start_cons(SEQUENCE);
AlgorithmIdentifier sig_algo;
std::vector<byte> signature;
basicresponse.decode(sig_algo);
basicresponse.decode(signature, BIT_STRING);
std::vector<X509_Certificate> certs;
decode_optional_list(basicresponse, ASN1_Tag(0), certs);
size_t responsedata_version = 0;
X509_DN name;
std::vector<byte> key_hash;
X509_Time produced_at;
// decode_optional_and_check(0, ASN1_Tag(0), ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC));
tbs_response.decode_optional(responsedata_version, ASN1_Tag(0),
ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC));
// Technically a choice: enforce that?
tbs_response.decode_optional(name, ASN1_Tag(1),
ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC));
tbs_response.decode_optional_string(key_hash, ASN1_Tag(2),
ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC));
tbs_response.decode(produced_at);
tbs_response.decode_list(m_responses);
Extensions extensions;
tbs_response.decode_optional(extensions, ASN1_Tag(1),
ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC));
}
response_outer.end_cons();
}
bool Response::affirmative_response_for(const Request& req)
{
for(auto response : m_responses)
if(response.affirmative_response_for(req.issuer(), req.subject()))
return true;
return false;
}
}
}
|