2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <boost/filesystem.hpp>
23 #include <boost/algorithm/string.hpp>
24 #include <openssl/sha.h>
25 #include <openssl/bio.h>
26 #include <openssl/evp.h>
28 #include "signer_chain.h"
29 #include "exceptions.h"
35 using std::stringstream;
38 static void command (string cmd)
41 /* We need to use CreateProcessW on Windows so that the UTF-8/16 mess
44 int const wn = MultiByteToWideChar (CP_UTF8, 0, cmd.c_str(), -1, 0, 0);
45 wchar_t* buffer = new wchar_t[wn];
46 if (MultiByteToWideChar (CP_UTF8, 0, cmd.c_str(), -1, buffer, wn) == 0) {
53 STARTUPINFOW startup_info;
54 memset (&startup_info, 0, sizeof (startup_info));
55 startup_info.cb = sizeof (startup_info);
56 PROCESS_INFORMATION process_info;
58 /* XXX: this doesn't actually seem to work; failing commands end up with
61 if (CreateProcessW (0, buffer, 0, 0, FALSE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
62 WaitForSingleObject (process_info.hProcess, INFINITE);
64 if (GetExitCodeProcess (process_info.hProcess, &c)) {
67 CloseHandle (process_info.hProcess);
68 CloseHandle (process_info.hThread);
73 cmd += " 2> /dev/null";
74 int const r = system (cmd.c_str ());
75 int const code = WEXITSTATUS (r);
79 s << "error " << code << " in " << cmd << " within " << boost::filesystem::current_path();
80 throw libdcp::MiscError (s.str());
84 /** Extract a public key from a private key and create a SHA1 digest of it.
85 * @param key Private key
86 * @param openssl openssl binary name (or full path if openssl is not on the system path).
87 * @return SHA1 digest of corresponding public key, with escaped / characters.
91 public_key_digest (boost::filesystem::path private_key, boost::filesystem::path openssl)
93 boost::filesystem::path public_name = private_key.string() + ".public";
95 /* Create the public key from the private key */
97 s << "\"" << openssl.string() << "\" rsa -outform PEM -pubout -in " << private_key.string() << " -out " << public_name.string ();
98 command (s.str().c_str ());
100 /* Read in the public key from the file */
103 ifstream f (public_name.string().c_str ());
105 throw libdcp::MiscError ("public key not found");
112 if (line.length() >= 10 && line.substr(0, 10) == "-----BEGIN") {
114 } else if (line.length() >= 8 && line.substr(0, 8) == "-----END") {
121 /* Decode the base64 of the public key */
123 unsigned char buffer[512];
124 int const N = libdcp::base64_decode (pub, buffer, 1024);
126 /* Hash it with SHA1 (without the first 24 bytes, for reasons that are not entirely clear) */
129 if (!SHA1_Init (&context)) {
130 throw libdcp::MiscError ("could not init SHA1 context");
133 if (!SHA1_Update (&context, buffer + 24, N - 24)) {
134 throw libdcp::MiscError ("could not update SHA1 digest");
137 unsigned char digest[SHA_DIGEST_LENGTH];
138 if (!SHA1_Final (digest, &context)) {
139 throw libdcp::MiscError ("could not finish SHA1 digest");
142 char digest_base64[64];
143 string dig = Kumu::base64encode (digest, SHA_DIGEST_LENGTH, digest_base64, 64);
144 #ifdef LIBDCP_WINDOWS
145 boost::replace_all (dig, "/", "\\/");
147 boost::replace_all (dig, "/", "\\\\/");
153 libdcp::make_signer_chain (boost::filesystem::path directory, boost::filesystem::path openssl)
155 boost::filesystem::path const cwd = boost::filesystem::current_path ();
157 string quoted_openssl = "\"" + openssl.string() + "\"";
159 boost::filesystem::current_path (directory);
160 command (quoted_openssl + " genrsa -out ca.key 2048");
163 ofstream f ("ca.cnf");
165 << "distinguished_name = req_distinguished_name\n"
166 << "x509_extensions = v3_ca\n"
168 << "basicConstraints = critical,CA:true,pathlen:3\n"
169 << "keyUsage = keyCertSign,cRLSign\n"
170 << "subjectKeyIdentifier = hash\n"
171 << "authorityKeyIdentifier = keyid:always,issuer:always\n"
172 << "[ req_distinguished_name ]\n"
173 << "O = Unique organization name\n"
174 << "OU = Organization unit\n"
175 << "CN = Entity and dnQualifier\n";
178 string const ca_subject = "/O=example.org/OU=example.org/CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION/dnQualifier=" + public_key_digest ("ca.key", openssl);
183 << " req -new -x509 -sha256 -config ca.cnf -days 3650 -set_serial 5"
184 << " -subj " << ca_subject << " -key ca.key -outform PEM -out ca.self-signed.pem";
185 command (c.str().c_str());
188 command (quoted_openssl + " genrsa -out intermediate.key 2048");
191 ofstream f ("intermediate.cnf");
193 << "distinguished_name = req_distinguished_name\n"
194 << "x509_extensions = v3_ca\n"
196 << "basicConstraints = critical,CA:true,pathlen:2\n"
197 << "keyUsage = keyCertSign,cRLSign\n"
198 << "subjectKeyIdentifier = hash\n"
199 << "authorityKeyIdentifier = keyid:always,issuer:always\n"
200 << "[ req_distinguished_name ]\n"
201 << "O = Unique organization name\n"
202 << "OU = Organization unit\n"
203 << "CN = Entity and dnQualifier\n";
206 string const inter_subject = "/O=example.org/OU=example.org/CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION/dnQualifier="
207 + public_key_digest ("intermediate.key", openssl);
212 << " req -new -config intermediate.cnf -days 3649 -subj " << inter_subject << " -key intermediate.key -out intermediate.csr";
213 command (s.str().c_str());
219 " x509 -req -sha256 -days 3649 -CA ca.self-signed.pem -CAkey ca.key -set_serial 6"
220 " -in intermediate.csr -extfile intermediate.cnf -extensions v3_ca -out intermediate.signed.pem"
223 command (quoted_openssl + " genrsa -out leaf.key 2048");
226 ofstream f ("leaf.cnf");
228 << "distinguished_name = req_distinguished_name\n"
229 << "x509_extensions = v3_ca\n"
231 << "basicConstraints = critical,CA:false\n"
232 << "keyUsage = digitalSignature,keyEncipherment\n"
233 << "subjectKeyIdentifier = hash\n"
234 << "authorityKeyIdentifier = keyid,issuer:always\n"
235 << "[ req_distinguished_name ]\n"
236 << "O = Unique organization name\n"
237 << "OU = Organization unit\n"
238 << "CN = Entity and dnQualifier\n";
241 string const leaf_subject = "/O=example.org/OU=example.org/CN=CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION/dnQualifier="
242 + public_key_digest ("leaf.key", openssl);
246 s << quoted_openssl << " req -new -config leaf.cnf -days 3648 -subj " << leaf_subject << " -key leaf.key -outform PEM -out leaf.csr";
247 command (s.str().c_str());
252 " x509 -req -sha256 -days 3648 -CA intermediate.signed.pem -CAkey intermediate.key"
253 " -set_serial 7 -in leaf.csr -extfile leaf.cnf -extensions v3_ca -out leaf.signed.pem"
256 boost::filesystem::current_path (cwd);