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/pem.h>
31#include <openssl/x509.h>
32#include <openssl/x509v3.h>
33
34#ifdef __MSVC__
35#include "openssl/applink.c"
36#endif
37
38#ifdef HAVE_OCPN_LOG
39#include "model/logger.h"
40#define cerr ERROR_LOG
41#endif
42
43using namespace std;
44
45/* Generates a 2048-bit RSA key. */
46EVP_PKEY *generate_key() {
47 /* Allocate memory for the EVP_PKEY structure. */
48 EVP_PKEY *pkey = EVP_PKEY_new();
49 if (!pkey) {
50 cerr << "Unable to create EVP_PKEY structure." << endl;
51 return NULL;
52 }
53
54 /* Generate the RSA key and assign it to pkey. */
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;
58 EVP_PKEY_free(pkey);
59 return NULL;
60 }
61
62 /* The key has been generated, return it. */
63 return pkey;
64}
65
66int cs_cert_set_subject_alt_name(X509 *x509_cert, string name) {
67 const char *subject_alt_name = name.c_str(); //"IP: 192.168.1.1";
68 X509_EXTENSION *extension_san = NULL;
69 ASN1_OCTET_STRING *subject_alt_name_ASN1 = NULL;
70 int ret = -1;
71
72 subject_alt_name_ASN1 = ASN1_OCTET_STRING_new();
73 if (!subject_alt_name_ASN1) {
74 goto err;
75 }
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)) {
81 goto err;
82 }
83 ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
84 ret = X509_add_ext(x509_cert, extension_san, -1);
85 if (!ret) {
86 goto err;
87 }
88 X509_EXTENSION_free(extension_san);
89 return 0;
90
91err:
92 if (subject_alt_name_ASN1) ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
93 if (extension_san) X509_EXTENSION_free(extension_san);
94 return -1;
95}
96
97/* Generates a self-signed x509 certificate. */
98X509 *generate_x509(EVP_PKEY *pkey, string ip_v4) {
99 /* Allocate memory for the X509 structure. */
100 X509 *x509 = X509_new();
101 if (!x509) {
102 cerr << "Unable to create X509 structure." << endl;
103 return NULL;
104 }
105
106 /* Set the serial number. */
107 ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
108
109 /* This certificate is valid from now until exactly one year from now. */
110
111 X509_gmtime_adj(X509_get_notBefore(x509), 0);
112 X509_gmtime_adj(X509_get_notAfter(x509), 31536000L);
113
114 /* Set the public key for our certificate. */
115 X509_set_pubkey(x509, pkey);
116
117 /* We want to copy the subject name to the issuer name. */
118 X509_NAME *name = X509_get_subject_name(x509);
119
120 /* Set the country code and common name. */
121 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"CA", -1,
122 -1, 0);
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: ");
128 ext_name += ip_v4;
129 cs_cert_set_subject_alt_name(x509, ext_name);
130
131 /* Now set the issuer name. */
132 X509_set_issuer_name(x509, name);
133
134 /* Actually sign the certificate with our key. */
135 if (!X509_sign(x509, pkey, EVP_sha1())) {
136 cerr << "Error signing certificate." << endl;
137 X509_free(x509);
138 return NULL;
139 }
140
141 return x509;
142}
143
144bool write_to_disk(EVP_PKEY *pkey, X509 *x509, string cert_directory) {
145 /* Open the PEM file for writing the key to disk. */
146 string key_file = cert_directory;
147 key_file += "key.pem";
148 FILE *pkey_file = fopen(key_file.c_str(), "wb");
149 if (!pkey_file) {
150 cerr << "Unable to open \"key.pem\" for writing." << endl;
151 return false;
152 }
153
154 /* Write the key to disk. */
155 bool ret = PEM_write_PrivateKey(pkey_file, pkey, NULL, NULL, 0, NULL, NULL);
156 fclose(pkey_file);
157
158 if (!ret) {
159 cerr << "Unable to write private key to disk." << endl;
160 return false;
161 }
162
163 /* Open the PEM file for writing the certificate to disk. */
164 string cert_file = cert_directory;
165 cert_file += "cert.pem";
166
167 FILE *x509_file = fopen(cert_file.c_str(), "wb");
168 if (!x509_file) {
169 cerr << "Unable to open \"cert.pem\" for writing." << endl;
170 return false;
171 }
172
173 /* Write the certificate to disk. */
174 ret = PEM_write_X509(x509_file, x509);
175 fclose(x509_file);
176
177 if (!ret) {
178 cerr << "Unable to write certificate to disk." << endl;
179 return false;
180 }
181
182 return true;
183}
184
185int make_certificate(string ipv4, string destination_dir) {
186 /* Generate the key. */
187 if (getenv("OCPN_DEBUG_CERT")) cout << "Generating RSA key..." << endl;
188
189 EVP_PKEY *pkey = generate_key();
190 if (!pkey) return 1;
191
192 /* Generate the certificate. */
193 if (getenv("OCPN_DEBUG_CERT"))
194 cout << "Generating x509 certificate..." << endl;
195
196 X509 *x509 = generate_x509(pkey, ipv4);
197 if (!x509) {
198 EVP_PKEY_free(pkey);
199 return 1;
200 }
201
202 /* Write the private key and certificate out to disk. */
203 if (getenv("OCPN_DEBUG_CERT"))
204 cout << "Writing key and certificate to disk..." << endl;
205
206 bool ret = write_to_disk(pkey, x509, destination_dir);
207 EVP_PKEY_free(pkey);
208 X509_free(x509);
209
210 if (ret) {
211 if (getenv("OCPN_DEBUG_CERT")) cout << "Success!" << endl;
212 return 0;
213 } else
214 return 1;
215}
Enhanced logging interface on top of wx/log.h.