31#ifndef ASYNC_SSL_X509_EXT_SUBJECT_ALT_NAME_INCLUDED
32#define ASYNC_SSL_X509_EXT_SUBJECT_ALT_NAME_INCLUDED
134 m_ext = X509V3_EXT_conf_nid(NULL, NULL, NID_subject_alt_name,
144#if OPENSSL_VERSION_MAJOR >= 3
145 m_ext = X509_EXTENSION_dup(ext);
147 m_ext = X509_EXTENSION_dup(
const_cast<X509_EXTENSION*
>(ext));
158 m_ext = X509V3_EXT_i2d(NID_subject_alt_name, crit, names);
168 other.m_ext =
nullptr;
187 if (m_ext !=
nullptr)
189 X509_EXTENSION_free(m_ext);
198 bool isNull(
void)
const {
return m_ext ==
nullptr; }
201 bool add(
const std::string& name)
203 auto names =
reinterpret_cast<GENERAL_NAMES*
>(X509V3_EXT_d2i(m_ext));
204 if (names ==
nullptr)
206 names = GENERAL_NAME_new();
208 auto asn1_str = ASN1_STRING_new();
209 ASN1_STRING_set(asn1_str, name.c_str(), name.size());
212 ASN1_STRING_free(asn1_str);
213 sk_GENERAL_NAME_push(names, general_name);
221 operator const X509_EXTENSION*()
const {
return m_ext; }
232 if (m_ext ==
nullptr)
237 const auto names =
reinterpret_cast<GENERAL_NAMES*
>(X509V3_EXT_d2i(m_ext));
238 const int count = sk_GENERAL_NAME_num(names);
239 for (
int i = 0; i < count; ++i)
241 const GENERAL_NAME* entry = sk_GENERAL_NAME_value(names, i);
242 if ((entry ==
nullptr) || ((type >= 0) && (entry->type != type)))
254 name = asn1StringToUtf8(entry->d.rfc822Name);
258 name = asn1StringToUtf8(entry->d.dNSName);
269 int len = ASN1_STRING_length(entry->d.iPAddress);
272 const unsigned char* data =
273 ASN1_STRING_get0_data(entry->d.iPAddress);
274 struct in_addr in_addr = {0};
275 in_addr.s_addr = *
reinterpret_cast<const unsigned long*
>(data);
295 f(entry->type, name);
298 GENERAL_NAMES_free(names);
313 [&](
int type, std::string name)
321 str += sep +
"email:" + name;
325 str += sep +
"DNS:" + name;
335 str += sep +
"IP Address:" + name;
352 X509_EXTENSION* m_ext =
nullptr;
354 std::string asn1StringToUtf8(ASN1_IA5STRING* asn1str)
const
357 if (asn1str ==
nullptr)
361 unsigned char* utf8 =
nullptr;
362 const int len = ASN1_STRING_to_UTF8(&utf8, asn1str);
363 if ((utf8 !=
nullptr) && (len > 0))
365 str.assign(utf8, utf8+len);
A class for representing an IP address in an OS independent way.
std::string toString(void) const
Return the string representation of the IP address.
A class representing the X.509 Subject Alternative Name extension.
void forEach(ForeachFunction f, int type=-1) const
Loop through all names calling the given function for each one.
bool isNull(void) const
Check if the object is initialized.
~SslX509ExtSubjectAltName(void)
Destructor.
SslX509ExtSubjectAltName(const std::string &names)
Default constructor.
SslX509ExtSubjectAltName & operator=(const SslX509ExtSubjectAltName &)=delete
Disallow copy assignment.
std::string toString(int type=-1) const
Convert all SANs to a string.
SslX509ExtSubjectAltName(const SslX509ExtSubjectAltName &)=delete
Disallow copy construction.
SslX509ExtSubjectAltName(SslX509ExtSubjectAltName &&other)
Move Constructor.
SslX509ExtSubjectAltName(const X509_EXTENSION *ext)
Constructor.
SslX509ExtSubjectAltName(GENERAL_NAMES *names)
Constructor.
std::function< void(int, std::string)> ForeachFunction
Namespace for the asynchronous programming classes.