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