30#include <openssl/pem.h>
31#include <openssl/x509.h>
32#include <openssl/x509v3.h>
35#include "openssl/applink.c"
46EVP_PKEY *generate_key() {
48 EVP_PKEY *pkey = EVP_PKEY_new();
50 cerr <<
"Unable to create EVP_PKEY structure." << endl;
55 RSA *rsa = RSA_generate_key(2048, RSA_F4, NULL, NULL);
56 if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
57 cerr <<
"Unable to generate 2048-bit RSA key." << endl;
66int cs_cert_set_subject_alt_name(X509 *x509_cert,
string name) {
67 const char *subject_alt_name = name.c_str();
68 X509_EXTENSION *extension_san = NULL;
69 ASN1_OCTET_STRING *subject_alt_name_ASN1 = NULL;
72 subject_alt_name_ASN1 = ASN1_OCTET_STRING_new();
73 if (!subject_alt_name_ASN1) {
76 ASN1_OCTET_STRING_set(subject_alt_name_ASN1,
77 (
unsigned char *)subject_alt_name,
78 strlen(subject_alt_name));
79 if (!X509_EXTENSION_create_by_NID(&extension_san, NID_subject_alt_name, 0,
80 subject_alt_name_ASN1)) {
83 ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
84 ret = X509_add_ext(x509_cert, extension_san, -1);
88 X509_EXTENSION_free(extension_san);
92 if (subject_alt_name_ASN1) ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
93 if (extension_san) X509_EXTENSION_free(extension_san);
98X509 *generate_x509(EVP_PKEY *pkey,
string ip_v4) {
100 X509 *x509 = X509_new();
102 cerr <<
"Unable to create X509 structure." << endl;
107 ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
111 X509_gmtime_adj(X509_get_notBefore(x509), 0);
112 X509_gmtime_adj(X509_get_notAfter(x509), 31536000L);
115 X509_set_pubkey(x509, pkey);
118 X509_NAME *name = X509_get_subject_name(x509);
121 X509_NAME_add_entry_by_txt(name,
"C", MBSTRING_ASC, (
unsigned char *)
"CA", -1,
123 X509_NAME_add_entry_by_txt(name,
"O", MBSTRING_ASC,
124 (
unsigned char *)
"MyCompany", -1, -1, 0);
125 X509_NAME_add_entry_by_txt(name,
"CN", MBSTRING_ASC,
126 (
unsigned char *)
"localhost", -1, -1, 0);
127 string ext_name(
"IP: ");
129 cs_cert_set_subject_alt_name(x509, ext_name);
132 X509_set_issuer_name(x509, name);
135 if (!X509_sign(x509, pkey, EVP_sha1())) {
136 cerr <<
"Error signing certificate." << endl;
144bool write_to_disk(EVP_PKEY *pkey, X509 *x509,
string cert_directory) {
146 string key_file = cert_directory;
147 key_file +=
"key.pem";
148 FILE *pkey_file = fopen(key_file.c_str(),
"wb");
150 cerr <<
"Unable to open \"key.pem\" for writing." << endl;
155 bool ret = PEM_write_PrivateKey(pkey_file, pkey, NULL, NULL, 0, NULL, NULL);
159 cerr <<
"Unable to write private key to disk." << endl;
164 string cert_file = cert_directory;
165 cert_file +=
"cert.pem";
167 FILE *x509_file = fopen(cert_file.c_str(),
"wb");
169 cerr <<
"Unable to open \"cert.pem\" for writing." << endl;
174 ret = PEM_write_X509(x509_file, x509);
178 cerr <<
"Unable to write certificate to disk." << endl;
185int make_certificate(
string ipv4,
string destination_dir) {
187 if (getenv(
"OCPN_DEBUG_CERT")) cout <<
"Generating RSA key..." << endl;
189 EVP_PKEY *pkey = generate_key();
193 if (getenv(
"OCPN_DEBUG_CERT"))
194 cout <<
"Generating x509 certificate..." << endl;
196 X509 *x509 = generate_x509(pkey, ipv4);
203 if (getenv(
"OCPN_DEBUG_CERT"))
204 cout <<
"Writing key and certificate to disk..." << endl;
206 bool ret = write_to_disk(pkey, x509, destination_dir);
211 if (getenv(
"OCPN_DEBUG_CERT")) cout <<
"Success!" << endl;
Enhanced logging interface on top of wx/log.h.