11#include " cert.h"
22
33#include < cstring>
4+ #include < stdexcept>
5+
6+ #include < openssl/bio.h>
47
58// X509_new
69namespace hcpp
@@ -77,9 +80,8 @@ namespace hcpp
7780
7881 // Issuer
7982 // https://www.rfc-editor.org/rfc/rfc5280#section-4.1.2.4
80- void add_issuer (X509 *cert, std::string_view name2 )
83+ void set_issuer (X509 *cert, const std::vector<name_entry> &ne )
8184 {
82- // auto issuer = X509_NAME_new();
8385 // * country,
8486 // * organization,
8587 // * organizational unit,
@@ -89,24 +91,10 @@ namespace hcpp
8991 // x serial number.
9092
9193 // * locality
92- struct name_entry
93- {
94- int nid_;
95- const char *nid_val_;
96- int nid_val_len_;
97- };
98- name_entry issuer[] = {
99- {NID_countryName, " CN" , sizeof (" CN" ) - 1 },
100- {NID_organizationName, " NoBody" , sizeof (" NoBody" ) - 1 },
101- {NID_organizationalUnitName, " XX" , sizeof (" XX" ) - 1 },
102- {NID_stateOrProvinceName, " JS" , sizeof (" JS" ) - 1 },
103- {NID_commonName, " SelfCA" , sizeof (" SelfCA" ) - 1 },
104- {NID_localityName, " SQ" , sizeof (" SQ" ) - 1 },
105- };
10694
10795 auto name = X509_NAME_new ();
10896
109- for (auto &&i : issuer )
97+ for (auto &&i : ne )
11098 {
11199 X509_NAME_add_entry_by_NID (name, i.nid_ , MBSTRING_ASC , (const unsigned char *)i.nid_val_ , i.nid_val_len_ , -1 , 0 );
112100 }
@@ -132,10 +120,18 @@ namespace hcpp
132120
133121 // Subject
134122 // https://www.rfc-editor.org/rfc/rfc5280#section-4.1.2.6
135- void set_subject (X509 *cert)
123+ void set_subject (X509 *cert, const std::vector<name_entry> &ne )
136124 {
125+ auto name = X509_NAME_new ();
126+
127+ for (auto &&i : ne)
128+ {
129+ X509_NAME_add_entry_by_NID (name, i.nid_ , MBSTRING_ASC , (const unsigned char *)i.nid_val_ , i.nid_val_len_ , -1 , 0 );
130+ }
131+ X509_set_subject_name (cert, name);
132+ X509_NAME_free (name);
137133 // X509_set_subject_name get X509_NAME hashes or get and set issuer or subject names
138- X509_set_subject_name (cert, X509_get_issuer_name (cert));
134+ // X509_set_subject_name(cert, X509_get_issuer_name(cert));
139135 }
140136
141137 // Subject Public Key Info
@@ -207,15 +203,22 @@ namespace hcpp
207203
208204 // Subject Alternative Name
209205 // https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.6
210- void add_DNS_SAN (X509 *caCert, std::string_view dns_name )
206+ void add_DNS_SAN (X509 *caCert, const std::vector<std::string> &dns_names )
211207 {
208+ if (dns_names.empty ())
209+ {
210+ return ;
211+ }
212212 GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null ();
213213
214- GENERAL_NAME *gen_dns = GENERAL_NAME_new ();
215- ASN1_IA5STRING *ia5 = ASN1_IA5STRING_new ();
216- ASN1_STRING_set (ia5, dns_name.data (), dns_name.length ());
217- GENERAL_NAME_set0_value (gen_dns, GEN_DNS , ia5);
218- sk_GENERAL_NAME_push (gens, gen_dns);
214+ for (auto &dns_name : dns_names)
215+ {
216+ GENERAL_NAME *gen_dns = GENERAL_NAME_new ();
217+ ASN1_IA5STRING *ia5 = ASN1_IA5STRING_new ();
218+ ASN1_STRING_set (ia5, dns_name.data (), dns_name.length ());
219+ GENERAL_NAME_set0_value (gen_dns, GEN_DNS , ia5);
220+ sk_GENERAL_NAME_push (gens, gen_dns);
221+ }
219222
220223 // in_addr_t ipv4 = inet_addr("10.0.0.1");
221224 // GENERAL_NAME *gen_ip = GENERAL_NAME_new();
@@ -272,14 +275,51 @@ namespace hcpp
272275 return pkey_array;
273276 }
274277
278+ X509 *make_x509 (const std::string_view cert)
279+ {
280+ auto bp = BIO_new_mem_buf (cert.data (), cert.size ());
281+ if (bp == nullptr )
282+ {
283+ throw std::runtime_error (" bio 打开失败" );
284+ }
285+ auto c = PEM_read_bio_X509 (bp, NULL , 0 , NULL );
286+ if (c == nullptr )
287+ {
288+ throw std::runtime_error (" make_x509 failed" );
289+ }
290+ BIO_free (bp);
291+ return c;
292+ }
293+
294+ EVP_PKEY *make_pkey (const std::string_view pkey)
295+ {
296+ auto bp = BIO_new_mem_buf (pkey.data (), pkey.size ());
297+ if (bp == nullptr )
298+ {
299+ throw std::runtime_error (" bio 打开失败" );
300+ }
301+ auto p = PEM_read_bio_PrivateKey (bp, nullptr , nullptr , nullptr );
302+ if (p == nullptr )
303+ {
304+ throw std::runtime_error (" 读取密钥错误" );
305+ }
306+ BIO_free (bp);
307+ return p;
308+ }
309+
310+ void add_issuer (X509 *cert, X509 *ca_cert)
311+ {
312+ X509_set_issuer_name (cert, X509_get_subject_name (ca_cert));
313+ }
314+
275315 void test ()
276316 {
277317
278318 auto cert = X509_new ();
279319
280320 auto pkey = generate_pkey ();
281321 // if (pkey == NULL)
282- // CHECK(false);
322+ // CHECK(false);
283323 // print_key(pkey);
284324
285325 // set_version(cert);
0 commit comments