OpenVPN 3 Core Library
Loading...
Searching...
No Matches
verify_x509_name.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11//
12
13#pragma once
14
16
17namespace openvpn {
18
25{
26 public:
34
35 VerifyX509Name() = default;
36
37 VerifyX509Name(const OptionList &opt, const std::string &relay_prefix = "")
38 {
39 init(opt, relay_prefix);
40 }
41
42 ~VerifyX509Name() = default;
43
44 void init(const OptionList &opt, const std::string &relay_prefix)
45 {
46 const Option *o = opt.get_ptr(relay_prefix + "verify-x509-name");
47 if (o)
48 {
49 o->min_args(1);
50 verify_value = o->get(1, 256);
51 // If the mode flag is not present, we default to subject.
52 // For details, see openvpn(8) man page.
53 mode = parse_x509_verify_mode(o->get_default(2, 256, "subject"));
54 }
55 }
56
57 std::string get_mode_str() const
58 {
59 switch (mode)
60 {
62 return "VERIFY_X509_NONE";
64 return "VERIFY_X509_SUBJECT_DN";
66 return "VERIFY_X509_SUBJECT_RDN";
68 return "VERIFY_X509_SUBJECT_RDN_PREFIX";
69 default:
70 return "VERIFY_X509_NONE";
71 }
72 }
73
74 Mode get_mode() const
75 {
76 return mode;
77 }
78
79 bool verify(const std::string &value) const
80 {
81 switch (mode)
82 {
84 // If no verification is configured, it is always a pass
85 return true;
86
88 // The input value is expected to be a full subject DN
89 // where a perfect match is expected
90 return verify_value == value;
91
93 // The input value is expected to be the certificate
94 // Common Name (CN), and a perfect patch is expected
95 return verify_value == value;
96
98 // The input value contains a prefix of the certificate
99 // Common Name (CN), where we only require a perfect match
100 // only on the matching prefix
101 return value.compare(0, verify_value.length(), verify_value) == 0;
102 }
103 return false;
104 }
105
106 private:
108 std::string verify_value;
109
110 static Mode parse_x509_verify_mode(const std::string &type)
111 {
112 if (type == "subject")
113 {
115 }
116 else if (type == "name")
117 {
119 }
120 else if (type == "name-prefix")
121 {
123 }
124 throw option_error(ERR_INVALID_OPTION_CRYPTO, "Invalid verify-x509-name type: " + type);
125 }
126
127}; // class VerifyX509Name
128} // namespace openvpn
const Option * get_ptr(const std::string &name) const
Definition options.hpp:1186
std::string get_default(const size_t index, const size_t max_len, const std::string &default_value) const
Definition options.hpp:203
const std::string & get(const size_t index, const size_t max_len) const
Definition options.hpp:187
void min_args(const size_t n) const
Definition options.hpp:129
void init(const OptionList &opt, const std::string &relay_prefix)
bool verify(const std::string &value) const
VerifyX509Name(const OptionList &opt, const std::string &relay_prefix="")
static Mode parse_x509_verify_mode(const std::string &type)
std::string get_mode_str() const