OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tls_cert_profile.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// Parse the tls-cert-profile option.
13
14#ifndef OPENVPN_SSL_TLS_CERT_PROFILE_H
15#define OPENVPN_SSL_TLS_CERT_PROFILE_H
16
17#include <string>
18
22
24enum Type
25{
26 UNDEF = 0,
27#ifdef OPENVPN_ALLOW_INSECURE_CERTPROFILE
28 INSECURE,
29#endif
33};
34
35inline Type default_if_undef(const Type type)
36{
37 if (type == UNDEF)
38 return LEGACY; // this is the default if unspecified
39 else
40 return type;
41}
42
43inline const std::string to_string(const Type type)
44{
45 switch (type)
46 {
47 case UNDEF:
48 return "UNDEF";
49#ifdef OPENVPN_ALLOW_INSECURE_CERTPROFILE
50 case INSECURE:
51 return "INSECURE";
52#endif
53 case LEGACY:
54 return "LEGACY";
55 case PREFERRED:
56 return "PREFERRED";
57 case SUITEB:
58 return "SUITEB";
59 default:
60 return "???";
61 }
62}
63
64inline Type parse_tls_cert_profile(const std::string &profile_name)
65{
66#ifdef OPENVPN_ALLOW_INSECURE_CERTPROFILE
67 if (profile_name == "insecure")
68 return INSECURE;
69 else
70#endif
71 if (profile_name == "legacy")
72 return LEGACY;
73 else if (profile_name == "preferred")
74 return PREFERRED;
75 else if (profile_name == "suiteb")
76 return SUITEB;
77 else
78 throw option_error(ERR_INVALID_OPTION_CRYPTO, "tls-cert-profile: unrecognized profile name");
79}
80
82 const std::string &relay_prefix)
83{
84 const Option *o = opt.get_ptr(relay_prefix + "tls-cert-profile");
85 if (o)
86 {
87 const std::string profile_name = o->get_optional(1, 16);
88 return parse_tls_cert_profile(profile_name);
89 }
90 return UNDEF;
91}
92
93// If the override ends with "default", it is only applied
94// if the config doesn't specify tls-cert-profile.
95// Otherwise, the override has priority over the config.
96inline void apply_override(Type &type, const std::string &override)
97{
98 const Type orig = type;
99 if (override.empty() || override == "default")
100 ;
101#ifdef OPENVPN_ALLOW_INSECURE_CERTPROFILE
102 else if (override == "insecure-default")
103 {
104 if (orig == UNDEF)
105 type = INSECURE;
106 }
107#endif
108 else if (override == "legacy-default")
109 {
110 if (orig == UNDEF)
111 type = LEGACY;
112 }
113 else if (override == "preferred-default")
114 {
115 if (orig == UNDEF)
116 type = PREFERRED;
117 }
118#ifdef OPENVPN_ALLOW_INSECURE_CERTPROFILE
119 else if (override == "insecure")
120 type = INSECURE;
121#endif
122 else if (override == "legacy")
123 type = LEGACY;
124 else if (override == "preferred")
125 type = PREFERRED;
126 else if (override == "suiteb")
127 type = SUITEB;
128 else
129 throw option_error(ERR_INVALID_OPTION_CRYPTO, "tls-cert-profile: unrecognized override string");
130 // OPENVPN_LOG("*** tls-cert-profile before=" << to_string(orig) << " override=" << override << " after=" << to_string(type));
131}
132} // namespace openvpn::TLSCertProfile
133
134#endif
const Option * get_ptr(const std::string &name) const
Definition options.hpp:1186
std::string get_optional(const size_t index, const size_t max_len) const
Definition options.hpp:194
void apply_override(Type &type, const std::string &override)
Type default_if_undef(const Type type)
Type parse_tls_cert_profile(const std::string &profile_name)