OpenVPN 3 Core Library
Loading...
Searching...
No Matches
x509certinfo.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
15#include <cstring>
16#include <string>
17#include <vector>
18
19#include <mbedtls/x509.h>
20#include <mbedtls/x509_crt.h>
21#include <mbedtls/oid.h>
22
24
25#define MBEDTLS_MAX_SUBJECT_LENGTH 256
26
27namespace openvpn::MbedTLSPKI {
28
56static std::string x509_get_subject(const mbedtls_x509_crt *cert,
57 bool new_format = false)
58{
59 if (!new_format)
60 {
61 // Try to return the x509 subject formatted like the OpenSSL
62 // X509_NAME_oneline method. Only attributes matched in the switch
63 // statements below will be rendered. All other attributes will be
64 // ignored.
65
66 std::string ret;
67 for (const mbedtls_x509_name *name = &cert->subject;
68 name != nullptr;
69 name = name->next)
70 {
71 const char *key = nullptr;
72 if (!MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid))
73 key = "CN";
74 else if (!MBEDTLS_OID_CMP(MBEDTLS_OID_AT_COUNTRY, &name->oid))
75 key = "C";
76 else if (!MBEDTLS_OID_CMP(MBEDTLS_OID_AT_LOCALITY, &name->oid))
77 key = "L";
78 else if (!MBEDTLS_OID_CMP(MBEDTLS_OID_AT_STATE, &name->oid))
79 key = "ST";
80 else if (!MBEDTLS_OID_CMP(MBEDTLS_OID_AT_ORGANIZATION, &name->oid))
81 key = "O";
82 else if (!MBEDTLS_OID_CMP(MBEDTLS_OID_AT_ORG_UNIT, &name->oid))
83 key = "OU";
84 else if (!MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS9_EMAIL, &name->oid))
85 key = "emailAddress";
86
87 // make sure that key is defined and value has no embedded nulls
88 if (key && !string::embedded_null((const char *)name->val.p, name->val.len))
89 ret += "/" + std::string(key)
90 + "=" + std::string((const char *)name->val.p, name->val.len);
91 }
92 return ret;
93 }
94
95 char tmp_subj[MBEDTLS_MAX_SUBJECT_LENGTH] = {0};
96 int ret = mbedtls_x509_dn_gets(tmp_subj,
98 &cert->subject);
99 return (ret > 0 ? std::string(tmp_subj) : std::string(""));
100}
101
112static std::string x509_get_common_name(const mbedtls_x509_crt *cert)
113{
114 const mbedtls_x509_name *name = &cert->subject;
115
116 // find common name
117 while (name != nullptr)
118 {
119 if (!MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid))
120 {
121 break;
122 }
123 name = name->next;
124 }
125
126 return (name ? std::string((const char *)name->val.p, name->val.len)
127 : std::string(""));
128}
129
130} // namespace openvpn::MbedTLSPKI
#define MBEDTLS_MAX_SUBJECT_LENGTH
static std::string x509_get_common_name(const mbedtls_x509_crt *cert)
static std::string x509_get_subject(const mbedtls_x509_crt *cert, bool new_format=false)
bool embedded_null(const char *str, size_t len)
Definition string.hpp:238
std::string ret