OpenCPN Partial API docs
Loading...
Searching...
No Matches
certificates.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2022 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include "config.h"
25
26#include <cstdio>
27#include <iostream>
28#include <string.h>
29
30#include <openssl/err.h>
31#include <openssl/evp.h>
32#include <openssl/pem.h>
33#include <openssl/x509.h>
34#include <openssl/x509v3.h>
35
36#ifdef __MSVC__
37#include "openssl/applink.c"
38#endif
39
40#ifdef HAVE_OCPN_LOG
41#include "model/logger.h"
42#define cerr ERROR_LOG
43#endif
44
45using namespace std;
46
47/* Generates a 2048-bit RSA key. */
48EVP_PKEY *generate_key() {
49 /* Allocate memory for the EVP_PKEY structure. */
50 EVP_PKEY *pkey = EVP_PKEY_new();
51 if (!pkey) {
52 cerr << "Unable to create EVP_PKEY structure." << endl;
53 return NULL;
54 }
55
56 /* Generate the RSA key and assign it to pkey. */
57 RSA *rsa = RSA_generate_key(2048, RSA_F4, NULL, NULL);
58 if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
59 cerr << "Unable to generate 2048-bit RSA key." << endl;
60 EVP_PKEY_free(pkey);
61 return NULL;
62 }
63
64 /* The key has been generated, return it. */
65 return pkey;
66}
67
68int cs_cert_set_subject_alt_name(X509 *x509_cert, string name) {
69 const char *subject_alt_name = name.c_str(); //"IP: 192.168.1.1";
70 X509_EXTENSION *extension_san = NULL;
71 ASN1_OCTET_STRING *subject_alt_name_ASN1 = NULL;
72 int ret = -1;
73
74 subject_alt_name_ASN1 = ASN1_OCTET_STRING_new();
75 if (!subject_alt_name_ASN1) {
76 goto err;
77 }
78 ASN1_OCTET_STRING_set(subject_alt_name_ASN1,
79 (unsigned char *)subject_alt_name,
80 strlen(subject_alt_name));
81 if (!X509_EXTENSION_create_by_NID(&extension_san, NID_subject_alt_name, 0,
82 subject_alt_name_ASN1)) {
83 goto err;
84 }
85 ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
86 ret = X509_add_ext(x509_cert, extension_san, -1);
87 if (!ret) {
88 goto err;
89 }
90 X509_EXTENSION_free(extension_san);
91 return 0;
92
93err:
94 if (subject_alt_name_ASN1) ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
95 if (extension_san) X509_EXTENSION_free(extension_san);
96 return -1;
97}
98
99/* Generates a self-signed x509 certificate. */
100X509 *generate_x509(EVP_PKEY *pkey, string ip_v4) {
101 /* Allocate memory for the X509 structure. */
102 X509 *x509 = X509_new();
103 if (!x509) {
104 cerr << "Unable to create X509 structure." << endl;
105 return NULL;
106 }
107
108 /* Set the serial number. */
109 ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
110
111 /* This certificate is valid from now until exactly one year from now. */
112
113 X509_gmtime_adj(X509_get_notBefore(x509), 0);
114 X509_gmtime_adj(X509_get_notAfter(x509), 31536000L);
115
116 /* Set the public key for our certificate. */
117 X509_set_pubkey(x509, pkey);
118
119 /* We want to copy the subject name to the issuer name. */
120 X509_NAME *name = X509_get_subject_name(x509);
121
122 /* Set the country code and common name. */
123 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"CA", -1,
124 -1, 0);
125 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
126 (unsigned char *)"MyCompany", -1, -1, 0);
127 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
128 (unsigned char *)"localhost", -1, -1, 0);
129 string ext_name("IP: ");
130 ext_name += ip_v4;
131 cs_cert_set_subject_alt_name(x509, ext_name);
132
133 /* Now set the issuer name. */
134 X509_set_issuer_name(x509, name);
135
136 /* Actually sign the certificate with our key. */
137#if OPENSSL_VERSION_MAJOR * 10 + OPENSSL_VERSION_MINOR >= 32
138 const EVP_MD *evp_md = EVP_MD_fetch(0, "SHA-256", 0);
139#else
140 const EVP_MD *evp_md = EVP_sha1();
141#endif
142 if (!X509_sign(x509, pkey, evp_md)) {
143 unsigned long error = ERR_peek_error();
144 char *errmsg = ERR_error_string(error, 0);
145 cerr << "Error signing certificate:" << errmsg << endl;
146 X509_free(x509);
147 return NULL;
148 }
149
150 return x509;
151}
152
153bool write_to_disk(EVP_PKEY *pkey, X509 *x509, string cert_directory) {
154 /* Open the PEM file for writing the key to disk. */
155 string key_file = cert_directory;
156 key_file += "key.pem";
157 FILE *pkey_file = fopen(key_file.c_str(), "wb");
158 if (!pkey_file) {
159 cerr << "Unable to open \"key.pem\" for writing." << endl;
160 return false;
161 }
162
163 /* Write the key to disk. */
164 bool ret = PEM_write_PrivateKey(pkey_file, pkey, NULL, NULL, 0, NULL, NULL);
165 fclose(pkey_file);
166
167 if (!ret) {
168 cerr << "Unable to write private key to disk." << endl;
169 return false;
170 }
171
172 /* Open the PEM file for writing the certificate to disk. */
173 string cert_file = cert_directory;
174 cert_file += "cert.pem";
175
176 FILE *x509_file = fopen(cert_file.c_str(), "wb");
177 if (!x509_file) {
178 cerr << "Unable to open \"cert.pem\" for writing." << endl;
179 return false;
180 }
181
182 /* Write the certificate to disk. */
183 ret = PEM_write_X509(x509_file, x509);
184 fclose(x509_file);
185
186 if (!ret) {
187 cerr << "Unable to write certificate to disk." << endl;
188 return false;
189 }
190
191 return true;
192}
193
194int make_certificate(string ipv4, string destination_dir) {
195 /* Generate the key. */
196 if (getenv("OCPN_DEBUG_CERT")) cout << "Generating RSA key..." << endl;
197
198 EVP_PKEY *pkey = generate_key();
199 if (!pkey) return 1;
200
201 /* Generate the certificate. */
202 if (getenv("OCPN_DEBUG_CERT"))
203 cout << "Generating x509 certificate..." << endl;
204
205 X509 *x509 = generate_x509(pkey, ipv4);
206 if (!x509) {
207 EVP_PKEY_free(pkey);
208 return 1;
209 }
210
211 /* Write the private key and certificate out to disk. */
212 if (getenv("OCPN_DEBUG_CERT"))
213 cout << "Writing key and certificate to disk..." << endl;
214
215 bool ret = write_to_disk(pkey, x509, destination_dir);
216 EVP_PKEY_free(pkey);
217 X509_free(x509);
218
219 if (ret) {
220 if (getenv("OCPN_DEBUG_CERT")) cout << "Success!" << endl;
221 return 0;
222 } else
223 return 1;
224}
Enhanced logging interface on top of wx/log.h.