Panda3D
Loading...
Searching...
No Matches
make_ca_bundle.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file make_ca_bundle.cxx
10 * @author drose
11 * @date 2009-10-07
12 */
13
14#include "pandabase.h"
15#include "openSSLWrapper.h"
16#include <stdio.h>
17
18using std::cerr;
19using std::stringstream;
20using std::string;
21
22static const char *source_filename = "ca-bundle.crt";
23static const char *target_filename = "ca_bundle_data_src.c";
24
25int
26main(int argc, char *argv[]) {
27 FILE *fin = fopen(source_filename, "r");
28 if (fin == nullptr) {
29 cerr << "Couldn't open " << source_filename << " for reading.\n";
30 return 1;
31 }
32
33 // Initialize OpenSSL.
34 OpenSSLWrapper::get_global_ptr();
35
36 // We have to be sure and clear the OpenSSL error state before we call this
37 // function, or it will get confused.
38 ERR_clear_error();
39 STACK_OF(X509_INFO) *inf;
40 inf = PEM_X509_INFO_read(fin, nullptr, nullptr, nullptr);
41
42 if (!inf) {
43 // Could not scan certificates.
44 cerr << "Couldn't read PEM file in " << source_filename << "\n";
45 return 0;
46 }
47
48 cerr << "PEM_X509_INFO_read() found " << sk_X509_INFO_num(inf)
49 << " entries.\n";
50
51 // Now convert the certificates to DER form.
52 std::stringstream der_stream;
53
54 int cert_count = 0;
55 int num_entries = sk_X509_INFO_num(inf);
56 for (int i = 0; i < num_entries; i++) {
57 X509_INFO *itmp = sk_X509_INFO_value(inf, i);
58
59 if (itmp->x509) {
60 X509 *cert = itmp->x509;
61
62 int der_len = i2d_X509(cert, nullptr);
63 unsigned char *der_buf = new unsigned char[der_len];
64 unsigned char *p = der_buf;
65 i2d_X509(cert, &p);
66 der_stream.write((const char *)der_buf, der_len);
67 delete[] der_buf;
68 cert_count++;
69 }
70 }
71 sk_X509_INFO_pop_free(inf, X509_INFO_free);
72
73 fclose(fin);
74
75 // Now write the data to the .c file, in a compilable form, similar to
76 // bin2c.
77 std::ofstream out;
78 Filename target = Filename::text_filename(string(target_filename));
79 if (!target.open_write(out)) {
80 cerr << "Couldn't open " << target_filename << " for writing.\n";
81 return (1);
82 }
83
84 der_stream.seekg(0);
85 std::istream &in = der_stream;
86
87 string table_type = "const unsigned char ";
88 string length_type = "const int ";
89 string table_name = "ca_bundle_data";
90 string static_keyword = "static ";
91 static const int col_width = 11;
92
93 out << "\n"
94 << "/*\n"
95 << " * This table was generated by the command:\n"
96 << " *\n"
97 << " * make_ca_bundle\n"
98 << " *\n"
99 << " * which is a \"test\" program in the express directory; it reads\n"
100 << " * ca-bundle.crt and produces this file.\n"
101 << " *\n"
102 << " * This file represents the set of well-known certificate authorities\n"
103 << " * in DER form, for compiling into OpenSSLWrapper.\n"
104 << " */\n\n"
105 << static_keyword << table_type << table_name << "[] = {";
106 out << std::hex << std::setfill('0');
107 int count = 0;
108 int col = 0;
109 unsigned int ch;
110 ch = in.get();
111 while (!in.fail() && ch != EOF) {
112 if (col == 0) {
113 out << "\n ";
114 } else if (col == col_width) {
115 out << ",\n ";
116 col = 0;
117 } else {
118 out << ", ";
119 }
120 out << "0x" << std::setw(2) << ch;
121 col++;
122 count++;
123 ch = in.get();
124 }
125 out << "\n};\n\n"
126 << static_keyword << length_type << table_name << "_len = "
127 << std::dec << count << ";\n\n";
128
129 cerr << "Wrote " << cert_count << " certificates to "
130 << target_filename << "\n";
131 return 0;
132}
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
bool open_write(std::ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.