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