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