forked from OpenVPN/openvpn3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkuparse.hpp
142 lines (127 loc) · 4.03 KB
/
kuparse.hpp
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
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2022 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// Parse the remote-cert-tls, remote-cert-ku, and remote-cert-eku options.
#ifndef OPENVPN_SSL_KUPARSE_H
#define OPENVPN_SSL_KUPARSE_H
#include <vector>
#include <string>
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/hexstr.hpp>
#include <openvpn/common/options.hpp>
namespace openvpn {
namespace KUParse {
enum TLSWebType
{
TLS_WEB_NONE,
TLS_WEB_SERVER,
TLS_WEB_CLIENT,
};
inline void remote_cert_tls(const TLSWebType wt, std::vector<unsigned int> &ku, std::string &eku)
{
ku.clear();
eku = "";
switch (wt)
{
case TLS_WEB_NONE:
break;
case TLS_WEB_SERVER:
ku.clear();
ku.push_back(0xa0);
ku.push_back(0x88);
eku = "TLS Web Server Authentication";
break;
case TLS_WEB_CLIENT:
ku.clear();
ku.push_back(0x80);
ku.push_back(0x08);
ku.push_back(0x88);
eku = "TLS Web Client Authentication";
break;
}
}
inline TLSWebType remote_cert_type(const std::string &ct)
{
if (ct == "server")
return TLS_WEB_SERVER;
else if (ct == "client")
return TLS_WEB_CLIENT;
else
throw option_error("remote-cert-tls must be 'client' or 'server'");
}
inline void remote_cert_tls(const std::string &ct,
std::vector<unsigned int> &ku,
std::string &eku)
{
remote_cert_tls(remote_cert_type(ct), ku, eku);
}
inline void remote_cert_tls(const OptionList &opt,
const std::string &relay_prefix,
std::vector<unsigned int> &ku,
std::string &eku)
{
TLSWebType wt = TLS_WEB_NONE;
const Option *o = opt.get_ptr(relay_prefix + "remote-cert-tls");
if (o)
{
const std::string ct = o->get_optional(1, 16);
wt = remote_cert_type(ct);
}
remote_cert_tls(wt, ku, eku);
}
inline void remote_cert_ku(const OptionList &opt,
const std::string &relay_prefix,
std::vector<unsigned int> &ku)
{
ku.clear();
const Option *o = opt.get_ptr(relay_prefix + "remote-cert-ku");
if (o)
{
if (o->empty())
throw option_error("remote-cert-ku: no hex values specified");
else if (o->size() >= 64)
throw option_error("remote-cert-ku: too many parameters");
else
{
try
{
for (size_t i = 1; i < o->size(); ++i)
ku.push_back(parse_hex_number<unsigned int>(o->get(i, 16)));
}
catch (parse_hex_error &)
{
throw option_error("remote-cert-ku: error parsing hex value list");
}
}
}
}
inline void remote_cert_eku(const OptionList &opt,
const std::string &relay_prefix,
std::string &eku)
{
eku = "";
const Option *o = opt.get_ptr(relay_prefix + "remote-cert-eku");
if (o)
eku = o->get(1, 256);
}
} // namespace KUParse
} // namespace openvpn
#endif