OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tls_remote.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// test certificate subject and common name against tls_remote parameter
13
14#ifndef OPENVPN_SSL_TLS_REMOTE_H
15#define OPENVPN_SSL_TLS_REMOTE_H
16
17#include <cstring>
18#include <string>
19
21
23inline bool test(const std::string &tls_remote, const std::string &subject, const std::string &common_name)
24{
25 return tls_remote == subject || string::starts_with(common_name, tls_remote);
26}
27
28inline void log(const std::string &tls_remote, const std::string &subject, const std::string &common_name)
29{
30 OPENVPN_LOG("tls-remote validation"
31 << std::endl
32 << " tls-remote: '" << tls_remote << '\'' << std::endl
33 << " Subj: '" << subject << '\'' << std::endl
34 << " CN: '" << common_name << '\'');
35}
36
37// modifies x509 name in a way that is compatible with
38// name remapping behavior on OpenVPN 2.x
39inline std::string sanitize_x509_name(const std::string &str)
40{
41 std::string ret;
42 bool leading_dash = true;
43 ret.reserve(str.length());
44 for (size_t i = 0; i < str.length(); ++i)
45 {
46 const char c = str[i];
47 if (c == '-' && leading_dash)
48 {
49 ret += '_';
50 continue;
51 }
52 leading_dash = false;
53 if ((c >= 'a' && c <= 'z')
54 || (c >= 'A' && c <= 'Z')
55 || (c >= '0' && c <= '9')
56 || c == '_' || c == '-' || c == '.'
57 || c == '@' || c == ':' || c == '/'
58 || c == '=')
59 ret += c;
60 else
61 ret += '_';
62 }
63 return ret;
64}
65
66// modifies common name in a way that is compatible with
67// name remapping behavior on OpenVPN 2.x
68inline std::string sanitize_common_name(const std::string &str)
69{
70 std::string ret;
71 ret.reserve(str.length());
72 for (size_t i = 0; i < str.length(); ++i)
73 {
74 const char c = str[i];
75 if ((c >= 'a' && c <= 'z')
76 || (c >= 'A' && c <= 'Z')
77 || (c >= '0' && c <= '9')
78 || c == '_' || c == '-' || c == '.'
79 || c == '@' || c == '/')
80 ret += c;
81 else
82 ret += '_';
83 }
84 return ret;
85}
86} // namespace openvpn::TLSRemote
87
88#endif
#define OPENVPN_LOG(args)
std::string sanitize_x509_name(const std::string &str)
std::string sanitize_common_name(const std::string &str)
void log(const std::string &tls_remote, const std::string &subject, const std::string &common_name)
bool starts_with(const STRING &str, const std::string &prefix)
Definition string.hpp:79
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret
void test()
Definition test_rc.cpp:80