{"id":390325,"date":"2024-06-29T09:10:37","date_gmt":"2024-06-29T09:10:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=390325"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T09:10:37","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=390325","title":{"rendered":"<span>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c X509 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u0441 OpenSSL C++<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/612\/884\/6c4\/6128846c40172146473de0ad918e85fe.png\" alt=\"\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\" title=\"\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\" width=\"1200\" height=\"675\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/612\/884\/6c4\/6128846c40172146473de0ad918e85fe.png\"\/><\/p>\n<div><figcaption>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u044f \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 X509 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f OpenSSL 3.0.0 \u0432 \u0421++, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0432\u043e\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430 \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044f \u0435\u0433\u043e \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435 \u043f\u043e \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435 \u043f\u043e\u0447\u0442\u0438 \u043d\u0435\u0442, \u0442\u043e \u0432\u0441\u0435, \u0447\u0442\u043e \u044f \u0432\u0430\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u044f \u0443\u0437\u043d\u0430\u043b \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0435\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043f\u044b\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439. \u042f \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432\u0430\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0438 \u0441\u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u0430\u0448\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0430\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 X509 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u0432\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u0442\u0435, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u044e \u0432\u043e\u0442 <a href=\"https:\/\/habr.com\/ru\/articles\/194664\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0444\u0430\u0439\u043b &lt;openssl\/x509v3.h>. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f &#8212; \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c X509_new(), \u0430 \u0434\u043b\u044f \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 &#8212; X509_free().<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/x509v3.h> #include &lt;iostream>  int main() {     std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> certificate(X509_new(), ::X509_free);     if (certificate == nullptr) {         std::cerr &lt;&lt; \"Failed to create certificate\" &lt;&lt; std::endl;         return -1;     } }<\/code><\/pre>\n<h2>\u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 X509 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0435\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0448\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 X509_dup(), \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/x509v3.h> #include &lt;iostream>  int main() {     std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> certificate(X509_new(), ::X509_free);     if (certificate == nullptr) {         std::cerr &lt;&lt; \"Failed to create certificate\" &lt;&lt; std::endl;         return -1;     }        std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> duplicate(X509_dup(certificate.get()), ::X509_free);     if (duplicate == nullptr) {         std::cerr &lt;&lt; \"Failed to duplicate certificate\" &lt;&lt; std::endl;         return -1;     } }<\/code><\/pre>\n<h2>\u0414\u043e\u0431\u0430\u0432\u0438\u043c Serial Number<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0443 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0435\u0439, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043d\u043e\u043c\u0435\u0440\u0430, \u043e\u043d \u0436\u0435 serial number. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e X509_get_serialNumber \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u044c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0430 \u0434\u0430\u043b\u0435\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e RAND_bytes \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0435\u0440\u0438\u0439\u043d\u044b\u0439 \u043d\u043e\u043c\u0435\u0440 \u0438 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f ASN1_STRING_set(). ASN1_STRING_set() \u0432\u0435\u0440\u043d\u0435\u0442 1 \u043d\u0430 \u0443\u0434\u0430\u0447\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432, 0 \u043d\u0430 \u0437\u0430\u0432\u0430\u043b\u0438\u0432\u0448\u0438\u0439\u0441\u044f. \u042d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u043c.<\/p>\n<pre><code class=\"cpp\">bool setSerialNumber(X509* cert, uint32_t bytes) {     bool result = false;     ASN1_STRING* serialNumber = X509_get_serialNumber(cert);     if (serialNumber != nullptr &amp;&amp; bytes != 0) {        std::vector&lt;unsigned char> serial(bytes);         RAND_bytes(serial.data(), static_cast&lt;int>(serial.size()));         if (ASN1_STRING_set(serialNumber, serial.data(), static_cast&lt;int>(serial.size())) == 1) {             result = true;         }     }     return result; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0435\u0440\u0441\u0438\u044e<\/h2>\n<p>\u0412\u044b\u0441\u0442\u0430\u0432\u0438\u043c \u0432\u0435\u0440\u0441\u0438\u044e \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e X509_set_version(). \u041d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>0x0<\/p>\n<\/li>\n<li>\n<p>0x1<\/p>\n<\/li>\n<li>\n<p>0x2<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"cpp\">bool setVersion(X509* cert, long version) {     return X509_set_version(cert, version) == 1; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c Subject Name<\/h2>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e X509_get_subject_name \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 subject name \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0430 \u0432\u044b\u0437\u0432\u0430\u0432 X509_NAME_add_entry_by_txt(), \u043c\u043e\u0436\u0435\u043c \u0435\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">bool updateSubjectName(X509* cert, const char* key, const char* value) {     bool result = false;     X509_NAME* subjectName = X509_get_subject_name(cert);     if (subjectName != nullptr) {         const int res = X509_NAME_add_entry_by_txt(subjectName, key, MBSTRING_ASC, (unsigned char*)value, -1, -1, 0);         result = res == 1;     }     return result; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0440\u043e\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u0440\u043e\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0441 \u043a\u0430\u043a\u043e\u0439 \u043f\u043e \u043a\u0430\u043a\u0443\u044e \u0434\u0430\u0442\u0443 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 X509_getm_notAfter() \u0438 X509_getm_notBefore(). \u0414\u0430\u043b\u044c\u0448\u0435 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u044f \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043e\u043a, \u043d\u043e \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0435\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435.<\/p>\n<pre><code class=\"cpp\">bool setNotAfter(X509* cert, uint32_t y, uint32_t m, uint32_t d, int32_t offset_days) {     struct tm base;     memset(&amp;base, 0, sizeof(base));     base.tm_year = y - 1900;     base.tm_mon = m - 1;     base.tm_mday = d;     time_t tm = mktime(&amp;base);      bool result = false;     ASN1_STRING* notAfter = X509_getm_notAfter(cert);     if (notAfter != nullptr) {         X509_time_adj(notAfter, 86400L * offset_days, &amp;tm);         result = true;     }     return result; }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0443\u044e \u0442\u043e\u0447\u043a\u0443. \u041a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u0436\u0435, \u043a\u0430\u043a \u0438 \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0438\u043c\u0435\u043d\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0438 &#8212; X509_getm_notBefore()<\/p>\n<pre><code class=\"cpp\">bool setNotBefore(X509* cert, uint32_t y, uint32_t m, uint32_t d, int32_t offset_days) {     struct tm base;     memset(&amp;base, 0, sizeof(base));     base.tm_year = y - 1900;     base.tm_mon = m - 1;     base.tm_mday = d;     time_t tm = mktime(&amp;base);      bool result = false;     ASN1_STRING* notBefore = X509_getm_notBefore(cert);     if (notBefore != nullptr) {         X509_time_adj(notBefore, 86400L * offset_days, &amp;tm);         result = true;     }     return result; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c Issuer<\/h2>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0432\u044b\u0441\u0442\u0430\u0432\u0438\u0442\u044c Issuer \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0442\u043e\u0442 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d \u043d\u0430\u0448. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c X509_set_issuer_name().<\/p>\n<pre><code class=\"cpp\">bool setIssuer(X509* cert, X509* issuer) {     bool result = false;     X509_NAME* subjectName = X509_get_subject_name(issuer);     if (subjectName != nullptr) {         result = X509_set_issuer_name(cert, subjectName) == 1;     }     return result; }<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432 \u043f\u043e\u043b\u0435 Issuer \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0443 \u0442\u043e\u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e, \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u0443\u044e \u043d\u0430\u043c X509_NAME_add_entry_by_txt():<\/p>\n<pre><code class=\"cpp\">bool addIssuerInfo(X509* cert, const char* key, const char* value) {     bool result = false;     X509_NAME* issuerName = X509_get_issuer_name(cert);     if (issuerName != nullptr) {         result = X509_NAME_add_entry_by_txt(issuerName, key, MBSTRING_ASC, (unsigned char*)value, -1, -1, 0) == 1;     }     return result; }<\/code><\/pre>\n<h2>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u0440\u0443 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 (standart extensions) \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430. \u0414\u043b\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u0432 OpenSSL \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 ID (nid). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f Basic Constraints  &#8212; \u044d\u0442\u043e NID_basic_constraints, \u0430 \u0434\u043b\u044f Key Usage &#8212; NID_key_usage. \u042d\u0442\u0438 \u0430\u0439\u0434\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b, \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0437\u0430\u0434\u0430\u0442\u044c \u0442\u0435 \u0438\u043b\u0438 \u0438\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430.<\/p>\n<pre><code class=\"cpp\">bool addStandardExtension(X509* cert, X509* issuer, int nid, const char* value) {     X509V3_CTX ctx; \/\/ create context     X509V3_set_ctx_nodb(&amp;ctx); \/\/ init context     X509V3_set_ctx(&amp;ctx, issuer, cert, nullptr, nullptr, 0); \/\/ set context      std::unique_ptr&lt;X509_EXTENSION, decltype(&amp;::X509_EXTENSION_free)> ex(X509V3_EXT_conf_nid(nullptr, &amp;ctx, nid, value), ::X509_EXTENSION_free);     if (ex != nullptr) {         return X509_add_ext(cert, ex.get(), -1) == 1;     }     return false; }<\/code><\/pre>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f<\/h2>\n<p>\u042d\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043d\u043e \u0442\u043e\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c\u043e. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 OpenSSL, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0434\u043d\u0435\u0435 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u043d\u0430\u0448\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043b\u044e\u0447 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442. <\/p>\n<pre><code class=\"cpp\">bool addCustomExtension(X509* cert, const char* key, const char* value, bool critical) {     const int nid = OBJ_create(key, value, nullptr);      std::unique_ptr&lt;ASN1_OCTET_STRING, decltype(&amp;::ASN1_OCTET_STRING_free)> data(ASN1_OCTET_STRING_new(), ::ASN1_OCTET_STRING_free);     int ret = ASN1_OCTET_STRING_set(data.get(), reinterpret_cast&lt;unsigned const char*>(value), strlen(value));     if (ret != 1) {         return false;     }      std::unique_ptr&lt;X509_EXTENSION, decltype(&amp;::X509_EXTENSION_free)> ex(X509_EXTENSION_create_by_NID(nullptr, nid, critical, data.get()), ::X509_EXTENSION_free);     return X509_add_ext(cert, ex.get(), -1) == 1; }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0438 \u0434\u043e\u043b\u0433\u0438\u0439 \u043f\u0443\u0442\u044c, \u043d\u043e, \u0440\u0430\u0431\u043e\u0447\u0438\u0439, \u043a\u0430\u043a \u044f \u0432\u044b\u044f\u0441\u043d\u0438\u043b \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u041d\u043e \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0438\u043b\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 &#8212; \u043f\u0440\u043e\u0448\u0443 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438.<\/p>\n<p>\u041d\u0443 \u0438 \u043a\u0430\u043a\u043e\u0439 \u0436\u0435 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u0431\u0435\u0437 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c X509_set_pubkey() \u0438 \u0433\u043e\u0442\u043e\u0432\u043e. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0438 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u043c \u043a\u043b\u044e\u0447\u0430\u043c\u0438 \u0432 OpenSSL, \u0442\u043e \u043f\u0438\u0448\u0438\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438.<\/p>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447<\/h2>\n<p>\u0422\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447\u0435\u0439 &#8212; \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u0438 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f EVP_RSA_gen(). \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0439 \u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043d\u0438\u043c\u0438 \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043b\u044e\u0447 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e X509_set_pubkey()<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/evp.h> \/\/\/ std::unique_ptr&lt;EVP_PKEY, decltype(&amp;::EVP_PKEY_free)> generateKeyPair(int32_t bits) {     std::unique_ptr&lt;EVP_PKEY, decltype(&amp;::EVP_PKEY_free)> key(EVP_RSA_gen(bits), ::EVP_PKEY_free);     return std::move(key); } \/\/\/ bool setPublicKey(X509* cert, EVP_PKEY* key) {     return X509_set_pubkey(cert, key) == 1; }<\/code><\/pre>\n<h2>\u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/h2>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0448 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442. \u0415\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u0438, \u043d\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 &#8212; \u044d\u0442\u043e SHA256, \u043e\u043d \u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435. \u041f\u043e \u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0430 \u043f\u043e \u0435\u0433\u043e \u0438\u043c\u0435\u043d\u0438 \u043d\u0430\u0439\u0442\u0438 \u043d\u0443\u0436\u043d\u044b\u0439 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435 OpenSSL.<\/p>\n<pre><code class=\"cpp\">signCert(certificate.get(), keyPair.get(), EVP_sha256()); \/\/\/ bool signCert(X509* cert, EVP_PKEY* key, const EVP_MD* algo) {     return X509_sign(cert, key, algo) != 0; }<\/code><\/pre>\n<h2>\u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u0432 \u0444\u0430\u0439\u043b<\/h2>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 BIO \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0438\u043e BIO_new(BIO_s_file(), \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 BIO_write_filename \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f PEM_write_bio_X509() \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u0432 \u0444\u0430\u0439\u043b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 PEM.<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/pem.h> \/\/\/ bool saveCertToPemFile(X509* cert, const std::string&amp; file) {     bool result = false;     std::unique_ptr&lt;BIO, decltype(&amp;::BIO_free)> bio(BIO_new(BIO_s_file()), ::BIO_free);     if (bio != nullptr) {         if (BIO_write_filename(bio.get(), const_cast&lt;char*>(file.c_str())) > 0) {             result = PEM_write_bio_X509(bio.get(), cert) == 1;         }     }     return result; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"cpp\">#include &lt;openssl\/x509v3.h> #include &lt;openssl\/evp.h> #include &lt;openssl\/pem.h>  #include &lt;memory> #include &lt;iostream>  bool setSerialNumber(X509* cert, uint32_t bytes) {     bool result = false;     ASN1_STRING* serialNumber = X509_get_serialNumber(cert);     if (serialNumber != nullptr &amp;&amp; bytes != 0) {        std::vector&lt;unsigned char> serial(bytes);         RAND_bytes(serial.data(), static_cast&lt;int>(serial.size()));         if (ASN1_STRING_set(serialNumber, serial.data(), static_cast&lt;int>(serial.size())) == 1) {             result = true;         }     }     return result; }  bool setVersion(X509* cert, long version) {     return X509_set_version(cert, version) == 1; }  bool updateSubjectName(X509* cert, const char* key, const char* value) {     bool result = false;     X509_NAME* subjectName = X509_get_subject_name(cert);     if (subjectName != nullptr) {         const int res = X509_NAME_add_entry_by_txt(subjectName, key, MBSTRING_ASC, (unsigned char*)value, -1, -1, 0);         result = res == 1;     }     return result; }  bool setNotAfter(X509* cert, uint32_t y, uint32_t m, uint32_t d, int32_t offset_days) {     struct tm base;     memset(&amp;base, 0, sizeof(base));     base.tm_year = y - 1900;     base.tm_mon = m - 1;     base.tm_mday = d;     time_t tm = mktime(&amp;base);      bool result = false;     ASN1_STRING* notAfter = X509_getm_notAfter(cert);     if (notAfter != nullptr) {         X509_time_adj(notAfter, 86400L * offset_days, &amp;tm);         result = true;     }     return result; }  bool setNotBefore(X509* cert, uint32_t y, uint32_t m, uint32_t d, int32_t offset_days) {     struct tm base;     memset(&amp;base, 0, sizeof(base));     base.tm_year = y - 1900;     base.tm_mon = m - 1;     base.tm_mday = d;     time_t tm = mktime(&amp;base);      bool result = false;     ASN1_STRING* notBefore = X509_getm_notBefore(cert);     if (notBefore != nullptr) {         X509_time_adj(notBefore, 86400L * offset_days, &amp;tm);         result = true;     }     return result; }  bool setPublicKey(X509* cert, EVP_PKEY* key) {     return X509_set_pubkey(cert, key) == 1; }  bool signCert(X509* cert, EVP_PKEY* key, const EVP_MD* algo) {     return X509_sign(cert, key, algo) != 0; }  bool saveCertToPemFile(X509* cert, const std::string&amp; file) {     bool result = false;     std::unique_ptr&lt;BIO, decltype(&amp;::BIO_free)> bio(BIO_new(BIO_s_file()), ::BIO_free);     if (bio != nullptr) {         if (BIO_write_filename(bio.get(), const_cast&lt;char*>(file.c_str())) > 0) {             result = PEM_write_bio_X509(bio.get(), cert) == 1;         }     }     return result; }  std::unique_ptr&lt;EVP_PKEY, decltype(&amp;::EVP_PKEY_free)> generateKeyPair(int32_t bits) {     std::unique_ptr&lt;EVP_PKEY, decltype(&amp;::EVP_PKEY_free)> key(EVP_RSA_gen(bits), ::EVP_PKEY_free);     return std::move(key); }  bool addCustomExtension(X509* cert, const char* key, const char* value, bool critical) {     const int nid = OBJ_create(key, value, nullptr);      std::unique_ptr&lt;ASN1_OCTET_STRING, decltype(&amp;::ASN1_OCTET_STRING_free)> data(ASN1_OCTET_STRING_new(), ::ASN1_OCTET_STRING_free);     int ret = ASN1_OCTET_STRING_set(data.get(), reinterpret_cast&lt;unsigned const char*>(value), strlen(value));     if (ret != 1) {         return false;     }      std::unique_ptr&lt;X509_EXTENSION, decltype(&amp;::X509_EXTENSION_free)> ex(X509_EXTENSION_create_by_NID(nullptr, nid, critical, data.get()), ::X509_EXTENSION_free);     return X509_add_ext(cert, ex.get(), -1) == 1; }  bool addStandardExtension(X509* cert, X509* issuer, int nid, const char* value) {     X509V3_CTX ctx; \/\/ create context     X509V3_set_ctx_nodb(&amp;ctx); \/\/ init context     X509V3_set_ctx(&amp;ctx, issuer, cert, nullptr, nullptr, 0); \/\/ set context      std::unique_ptr&lt;X509_EXTENSION, decltype(&amp;::X509_EXTENSION_free)> ex(X509V3_EXT_conf_nid(nullptr, &amp;ctx, nid, value), ::X509_EXTENSION_free);     if (ex != nullptr) {         return X509_add_ext(cert, ex.get(), -1) == 1;     }     return false; }  bool setIssuer(X509* cert, X509* issuer) {     bool result = false;     X509_NAME* subjectName = X509_get_subject_name(issuer);     if (subjectName != nullptr) {         result = X509_set_issuer_name(cert, subjectName) == 1;     }     return result; }  bool addIssuerInfo(X509* cert, const char* key, const char* value) {     bool result = false;     X509_NAME* issuerName = X509_get_issuer_name(cert);     if (issuerName != nullptr) {         result = X509_NAME_add_entry_by_txt(issuerName, key, MBSTRING_ASC, (unsigned char*)value, -1, -1, 0) == 1;     }     return result; }  int main() {     std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> certificate(X509_new(), ::X509_free);     if (certificate == nullptr) {         std::cerr &lt;&lt; \"Failed to create certificate\" &lt;&lt; std::endl;         return -1;     }      const unt32_t serialNum = 20;     bool res = setSerialNumber(certificate.get(), serialNum);     if (!res) {         std::cerr &lt;&lt; \"Failed to setSerialNumber\" &lt;&lt; std::endl;         return -1;     }      const long ver = 0x0; \/\/ version 1     res = setVersion(certificate.get(), ver);     if (!res) {         std::cerr &lt;&lt; \"Failed to setVersion\" &lt;&lt; std::endl;         return -1;     }      static constexpr const char* key = \"CN\";     static constexpr const char* value = \"Common Name\";     res = updateSubjectName(certificate.get(), key, value);     if (!res) {         std::cerr &lt;&lt; \"Failed to updateSubjectName\" &lt;&lt; std::endl;         return -1;     }      const uint32_t y = 2022;     const uint32_t m = 12;     const uint32_t d = 25;     const int32_t offset_days = 0;     res = setNotAfter(certificate.get(), y, m, d, offset_days);     if (!res) {         std::cerr &lt;&lt; \"Failed to setNotAfter\" &lt;&lt; std::endl;         return -1;     }      res = setNotBefore(certificate.get(), y, m, d, offset_days);     if (!res) {         std::cerr &lt;&lt; \"Failed to setNotBefore\" &lt;&lt; std::endl;         return -1;     }      const int32_t bits = 2048;     std::unique_ptr&lt;EVP_PKEY, decltype(&amp;::EVP_PKEY_free)> keyPair = generateKeyPair(bits);     res = setPublicKey(certificate.get(), keyPair.get());     if (!res) {         std::cerr &lt;&lt; \"Failed to setPublicKey\" &lt;&lt; std::endl;         return -1;     }      const int nid = NID_basic_constraints;     static const char* extensionValue = \"critical,CA:TRUE\";      res = addStandardExtension(certificate.get(), nullptr, nid, extensionValue);     if (!res) {         std::cerr &lt;&lt; \"Failed to addStandardExtension\" &lt;&lt; std::endl;         return -1;     }      res = addCustomExtension(certificate.get(), \"1.2.3\", \"myvalue\", false);     if (!res) {         std::cerr &lt;&lt; \"Failed to addCustomExtension\" &lt;&lt; std::endl;         return -1;     }      res = signCert(certificate.get(), keyPair.get(), EVP_sha256());     if (!res) {         std::cerr &lt;&lt; \"Failed to signCert\" &lt;&lt; std::endl;         return -1;     }      std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> duplicate(X509_dup(certificate.get()), ::X509_free);     if (duplicate == nullptr) {         std::cerr &lt;&lt; \"Failed to duplicate certificate\" &lt;&lt; std::endl;         return -1;     }      res = setIssuer(certificate.get(), duplicate.get());     if (!res) {         std::cerr &lt;&lt; \"Failed to setIssuer\" &lt;&lt; std::endl;         return -1;     }      res = addIssuerInfo(certificate.get(), key, value);     if (!res) {         std::cerr &lt;&lt; \"Failed to addIssuerInfo\" &lt;&lt; std::endl;         return -1;     }      res = signCert(certificate.get(), keyPair.get(), EVP_sha256());     if (!res) {         std::cerr &lt;&lt; \"Failed to signCert\" &lt;&lt; std::endl;         return -1;     }      static const std::string filename = \"certificate.pem\";     res = saveCertToPemFile(certificate.get(), filename);     if (!res) {         std::cerr &lt;&lt; \"Failed to saveCertToPemFile\" &lt;&lt; std::endl;         return -1;     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<h2>\u041f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0430\u0441\u044c \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435, \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043a\u043b\u044e\u0447\u0430\u043c\u0438 \u0438 \u0441\u0442\u043e\u0440\u0430\u0434\u0436\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0438\u0445 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c &#8212; \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u043b\u0430\u0439\u043a\u0438 \u0438 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438. <\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u0430\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0430\u043c \u043a\u0443\u0447\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0438\u0437\u0431\u0430\u0432\u0438\u0432 \u043e\u0442 \u0447\u0430\u0441\u043e\u0432 \u0447\u0442\u0435\u043d\u0438\u044f \u0441\u043a\u0443\u0434\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0438 \u0440\u0435\u0434\u043a\u0438\u0445 \u043c\u0430\u043d\u0443\u0430\u043b\u043e\u0432.<\/p>\n<p>UPD: \u0414\u043e\u0431\u0430\u0432\u0438\u043b \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u043d\u0430 <a href=\"https:\/\/github.com\/KovalevVasiliy\/opensslexamples\" rel=\"noopener noreferrer nofollow\">\u0433\u0438\u0442\u0445\u0430\u0431<\/a>, \u0431\u0443\u0434\u0443 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043f\u043e \u043c\u0435\u0440\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0435\u0439<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><\/p>\n<div class=\"tm-article-poll-container\"><!--[--><\/p>\n<div class=\"tm-article-poll tm-article-poll_variant-bordered\">\n<div class=\"tm-notice tm-notice_positive tm-article-poll__notice\"><!----><\/p>\n<div class=\"tm-notice__inner\"><!----><\/p>\n<div class=\"tm-notice__content\" data-test-id=\"notice-content\"><!--[--><span>\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a rel=\"nofollow\" href=\"\/kek\/v1\/auth\/habrahabr\/?back=\/ru\/articles\/730852\/&#038;hl=ru\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/span><!--]--><\/div>\n<\/div>\n<\/div>\n<p><!--[--><\/p>\n<div class=\"tm-article-poll__header\">\u0425\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u043e \u043a\u043b\u044e\u0447\u0438, \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0432?<\/div>\n<div class=\"tm-article-poll__answers\"><!--[--><\/p>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent tm-article-poll__answer-percent_winning\">78.05% <\/span><span class=\"tm-article-poll__answer-label\">\u0414\u0430<\/span><span class=\"tm-article-poll__answer-votes\">32<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress tm-article-poll__answer-progress_winning\" style=\"width: 78.05%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">12.2% <\/span><span class=\"tm-article-poll__answer-label\">\u041d\u0435\u0442<\/span><span class=\"tm-article-poll__answer-votes\">5<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 12.2%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">9.76% <\/span><span class=\"tm-article-poll__answer-label\">\u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435<\/span><span class=\"tm-article-poll__answer-votes\">4<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 9.76%\"><\/div>\n<\/div>\n<\/div>\n<p><!--]--><\/div>\n<div class=\"tm-article-poll__stats\"> \u041f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b 41 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c.   \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u043b\u0438\u0441\u044c 4 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <\/div>\n<p><!--]--><\/div>\n<p><!--]--><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/730852\/\"> https:\/\/habr.com\/ru\/articles\/730852\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\">\n<div><figcaption>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u044f \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 X509 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f OpenSSL 3.0.0 \u0432 \u0421++, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0432\u043e\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430 \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044f \u0435\u0433\u043e \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435 \u043f\u043e \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435 \u043f\u043e\u0447\u0442\u0438 \u043d\u0435\u0442, \u0442\u043e \u0432\u0441\u0435, \u0447\u0442\u043e \u044f \u0432\u0430\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u044f \u0443\u0437\u043d\u0430\u043b \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0435\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043f\u044b\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439. \u042f \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432\u0430\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0438 \u0441\u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u0430\u0448\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0430\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 X509 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u0432\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u0442\u0435, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u044e \u0432\u043e\u0442 <a href=\"https:\/\/habr.com\/ru\/articles\/194664\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0444\u0430\u0439\u043b &lt;openssl\/x509v3.h>. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f &#8212; \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c X509_new(), \u0430 \u0434\u043b\u044f \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 &#8212; X509_free().<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/x509v3.h> #include &lt;iostream>  int main() {     std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> certificate(X509_new(), ::X509_free);     if (certificate == nullptr) {         std::cerr &lt;&lt; \"Failed to create certificate\" &lt;&lt; std::endl;         return -1;     } }<\/code><\/pre>\n<h2>\u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 X509 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0435\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0448\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 X509_dup(), \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/x509v3.h> #include &lt;iostream>  int main() {     std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> certificate(X509_new(), ::X509_free);     if (certificate == nullptr) {         std::cerr &lt;&lt; \"Failed to create certificate\" &lt;&lt; std::endl;         return -1;     }        std::unique_ptr&lt;X509, decltype(&amp;::X509_free)> duplicate(X509_dup(certificate.get()), ::X509_free);     if (duplicate == nullptr) {         std::cerr &lt;&lt; \"Failed to duplicate certificate\" &lt;&lt; std::endl;         return -1;     } }<\/code><\/pre>\n<h2>\u0414\u043e\u0431\u0430\u0432\u0438\u043c Serial Number<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0443 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0435\u0439, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043d\u043e\u043c\u0435\u0440\u0430, \u043e\u043d \u0436\u0435 serial number. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e X509_get_serialNumber \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u044c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0430 \u0434\u0430\u043b\u0435\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e RAND_bytes \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0435\u0440\u0438\u0439\u043d\u044b\u0439 \u043d\u043e\u043c\u0435\u0440 \u0438 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f ASN1_STRING_set(). ASN1_STRING_set() \u0432\u0435\u0440\u043d\u0435\u0442 1 \u043d\u0430 \u0443\u0434\u0430\u0447\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432, 0 \u043d\u0430 \u0437\u0430\u0432\u0430\u043b\u0438\u0432\u0448\u0438\u0439\u0441\u044f. \u042d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u043c.<\/p>\n<pre><code class=\"cpp\">bool setSerialNumber(X509* cert, uint32_t bytes) {     bool result = false;     ASN1_STRING* serialNumber = X509_get_serialNumber(cert);     if (serialNumber != nullptr &amp;&amp; bytes != 0) {        std::vector&lt;unsigned char> serial(bytes);         RAND_bytes(serial.data(), static_cast&lt;int>(serial.size()));         if (ASN1_STRING_set(serialNumber, serial.data(), static_cast&lt;int>(serial.size())) == 1) {             result = true;         }     }     return result; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0435\u0440\u0441\u0438\u044e<\/h2>\n<p>\u0412\u044b\u0441\u0442\u0430\u0432\u0438\u043c \u0432\u0435\u0440\u0441\u0438\u044e \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e X509_set_version(). \u041d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>0x0<\/p>\n<\/li>\n<li>\n<p>0x1<\/p>\n<\/li>\n<li>\n<p>0x2<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"cpp\">bool setVersion(X509* cert, long version) {     return X509_set_version(cert, version) == 1; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c Subject Name<\/h2>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e X509_get_subject_name \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 subject name \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0430 \u0432\u044b\u0437\u0432\u0430\u0432 X509_NAME_add_entry_by_txt(), \u043c\u043e\u0436\u0435\u043c \u0435\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">bool updateSubjectName(X509* cert, const char* key, const char* value) {     bool result = false;     X509_NAME* subjectName = X509_get_subject_name(cert);     if (subjectName != nullptr) {         const int res = X509_NAME_add_entry_by_txt(subjectName, key, MBSTRING_ASC, (unsigned char*)value, -1, -1, 0);         result = res == 1;     }     return result; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0440\u043e\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u0440\u043e\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0441 \u043a\u0430\u043a\u043e\u0439 \u043f\u043e \u043a\u0430\u043a\u0443\u044e \u0434\u0430\u0442\u0443 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 X509_getm_notAfter() \u0438 X509_getm_notBefore(). \u0414\u0430\u043b\u044c\u0448\u0435 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u044f \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043e\u043a, \u043d\u043e \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0435\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435.<\/p>\n<pre><code class=\"cpp\">bool setNotAfter(X509* cert, uint32_t y, uint32_t m, uint32_t d, int32_t offset_days) {     struct tm base;     memset(&amp;base, 0, sizeof(base));     base.tm_year = y - 1900;     base.tm_mon = m - 1;     base.tm_mday = d;     time_t tm = mktime(&amp;base);      bool result = false;     ASN1_STRING* notAfter = X509_getm_notAfter(cert);     if (notAfter != nullptr) {         X509_time_adj(notAfter, 86400L * offset_days, &amp;tm);         result = true;     }     return result; }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0443\u044e \u0442\u043e\u0447\u043a\u0443. \u041a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u0436\u0435, \u043a\u0430\u043a \u0438 \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0438\u043c\u0435\u043d\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0438 &#8212; X509_getm_notBefore()<\/p>\n<pre><code class=\"cpp\">bool setNotBefore(X509* cert, uint32_t y, uint32_t m, uint32_t d, int32_t offset_days) {     struct tm base;     memset(&amp;base, 0, sizeof(base));     base.tm_year = y - 1900;     base.tm_mon = m - 1;     base.tm_mday = d;     time_t tm = mktime(&amp;base);      bool result = false;     ASN1_STRING* notBefore = X509_getm_notBefore(cert);     if (notBefore != nullptr) {         X509_time_adj(notBefore, 86400L * offset_days, &amp;tm);         result = true;     }     return result; }<\/code><\/pre>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c Issuer<\/h2>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0432\u044b\u0441\u0442\u0430\u0432\u0438\u0442\u044c Issuer \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430, \u0442\u043e\u0442 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d \u043d\u0430\u0448. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c X509_set_issuer_name().<\/p>\n<pre><code class=\"cpp\">bool setIssuer(X509* cert, X509* issuer) {     bool result = false;     X509_NAME* subjectName = X509_get_subject_name(issuer);     if (subjectName != nullptr) {         result = X509_set_issuer_name(cert, subjectName) == 1;     }     return result; }<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432 \u043f\u043e\u043b\u0435 Issuer \u043d\u0430\u0448\u0435\u043c\u0443 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0443 \u0442\u043e\u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e, \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u0443\u044e \u043d\u0430\u043c X509_NAME_add_entry_by_txt():<\/p>\n<pre><code class=\"cpp\">bool addIssuerInfo(X509* cert, const char* key, const char* value) {     bool result = false;     X509_NAME* issuerName = X509_get_issuer_name(cert);     if (issuerName != nullptr) {         result = X509_NAME_add_entry_by_txt(issuerName, key, MBSTRING_ASC, (unsigned char*)value, -1, -1, 0) == 1;     }     return result; }<\/code><\/pre>\n<h2>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f<\/h2>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u0440\u0443 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 (standart extensions) \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430. \u0414\u043b\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u0432 OpenSSL \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 ID (nid). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f Basic Constraints  &#8212; \u044d\u0442\u043e NID_basic_constraints, \u0430 \u0434\u043b\u044f Key Usage &#8212; NID_key_usage. \u042d\u0442\u0438 \u0430\u0439\u0434\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b, \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0437\u0430\u0434\u0430\u0442\u044c \u0442\u0435 \u0438\u043b\u0438 \u0438\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430.<\/p>\n<pre><code class=\"cpp\">bool addStandardExtension(X509* cert, X509* issuer, int nid, const char* value) {     X509V3_CTX ctx; \/\/ create context     X509V3_set_ctx_nodb(&amp;ctx); \/\/ init context     X509V3_set_ctx(&amp;ctx, issuer, cert, nullptr, nullptr, 0); \/\/ set context      std::unique_ptr&lt;X509_EXTENSION, decltype(&amp;::X509_EXTENSION_free)> ex(X509V3_EXT_conf_nid(nullptr, &amp;ctx, nid, value), ::X509_EXTENSION_free);     if (ex != nullptr) {         return X509_add_ext(cert, ex.get(), -1) == 1;     }     return false; }<\/code><\/pre>\n<h2>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f<\/h2>\n<p>\u042d\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u0430. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043d\u043e \u0442\u043e\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c\u043e. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 OpenSSL, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0434\u043d\u0435\u0435 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u043d\u0430\u0448\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043b\u044e\u0447 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442. <\/p>\n<pre><code class=\"cpp\">bool addCustomExtension(X509* cert, const char* key, const char* value, bool critical) {     const int nid = OBJ_create(key, value, nullptr);      std::unique_ptr&lt;ASN1_OCTET_STRING, decltype(&amp;::ASN1_OCTET_STRING_free)> data(ASN1_OCTET_STRING_new(), ::ASN1_OCTET_STRING_free);     int ret = ASN1_OCTET_STRING_set(data.get(), reinterpret_cast&lt;unsigned const char*>(value), strlen(value));     if (ret != 1) {         return false;     }      std::unique_ptr&lt;X509_EXTENSION, decltype(&amp;::X509_EXTENSION_free)> ex(X509_EXTENSION_create_by_NID(nullptr, nid, critical, data.get()), ::X509_EXTENSION_free);     return X509_add_ext(cert, ex.get(), -1) == 1; }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0438 \u0434\u043e\u043b\u0433\u0438\u0439 \u043f\u0443\u0442\u044c, \u043d\u043e, \u0440\u0430\u0431\u043e\u0447\u0438\u0439, \u043a\u0430\u043a \u044f \u0432\u044b\u044f\u0441\u043d\u0438\u043b \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u041d\u043e \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0438\u043b\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 &#8212; \u043f\u0440\u043e\u0448\u0443 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438.<\/p>\n<p>\u041d\u0443 \u0438 \u043a\u0430\u043a\u043e\u0439 \u0436\u0435 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u0431\u0435\u0437 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c X509_set_pubkey() \u0438 \u0433\u043e\u0442\u043e\u0432\u043e. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0438 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u043c \u043a\u043b\u044e\u0447\u0430\u043c\u0438 \u0432 OpenSSL, \u0442\u043e \u043f\u0438\u0448\u0438\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438.<\/p>\n<h2>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447<\/h2>\n<p>\u0422\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e, \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447\u0435\u0439 &#8212; \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u0438 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f EVP_RSA_gen(). \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0439 \u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043d\u0438\u043c\u0438 \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u0412\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043b\u044e\u0447 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e X509_set_pubkey()<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/evp.h> \/\/\/ std::unique_ptr&lt;EVP_PKEY, decltype(&amp;::EVP_PKEY_free)> generateKeyPair(int32_t bits) {     std::unique_ptr&lt;EVP_PKEY, decltype(&amp;::EVP_PKEY_free)> key(EVP_RSA_gen(bits), ::EVP_PKEY_free);     return std::move(key); } \/\/\/ bool setPublicKey(X509* cert, EVP_PKEY* key) {     return X509_set_pubkey(cert, key) == 1; }<\/code><\/pre>\n<h2>\u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442<\/h2>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0448 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442. \u0415\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u0438, \u043d\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 &#8212; \u044d\u0442\u043e SHA256, \u043e\u043d \u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435. \u041f\u043e \u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0430 \u043f\u043e \u0435\u0433\u043e \u0438\u043c\u0435\u043d\u0438 \u043d\u0430\u0439\u0442\u0438 \u043d\u0443\u0436\u043d\u044b\u0439 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435 OpenSSL.<\/p>\n<pre><code class=\"cpp\">signCert(certificate.get(), keyPair.get(), EVP_sha256()); \/\/\/ bool signCert(X509* cert, EVP_PKEY* key, const EVP_MD* algo) {     return X509_sign(cert, key, algo) != 0; }<\/code><\/pre>\n<h2>\u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u0432 \u0444\u0430\u0439\u043b<\/h2>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 BIO \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0438\u043e BIO_new(BIO_s_file(), \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 BIO_write_filename \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f PEM_write_bio_X509() \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u0432 \u0444\u0430\u0439\u043b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 PEM.<\/p>\n<pre><code class=\"cpp\">#include &lt;openssl\/pem.h> \/\/\/ bool saveCertToPemFile(X509* cert, const std::string&amp; file) {     bool result = false;     std::unique_ptr&lt;BIO, decltype(&amp;::BIO_free)> bio(BIO_new(BIO_s_file()), ::BIO_free);     if (bio != nullptr) {         if (BIO_write_filename(bio.get(), const_cast&lt;char*>(file.c_str())) > 0) {             result = PEM_write_bio_X509(bio.get(), cert) == 1;         }     }     return result; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"cpp\">#include &lt;openssl\/x509v3.h><\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-390325","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/390325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=390325"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/390325\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=390325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=390325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=390325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}