-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsslcert.cc
231 lines (191 loc) · 6.75 KB
/
sslcert.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright 2012 James Peach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* sslcert.cc: Dump the subject CN from a SSL certificate */
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/asn1.h>
#include <openssl/ts.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
// To create a self-signed certificate that contains subjectAltNames, most
// online resources assume that you need to set up your own CA. However,
// http://www.verynoisy.com/blog/2010/jan/25/create-self-signed-certificates-multiple-domains/
// has the most straigtforward trick, which is to use an extension file when
// you create the certificate.
//
// A basic, self-signed certificate for test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gTHRkMSYwJAYDVQQDEx10ZXN0LnRyYWZm\n"
"aWNzZXJ2ZXIuYXBhY2hlLm9yZzErMCkGCSqGSIb3DQEJARYcZGV2QHRyYWZmaWNz\n"
"ZXJ2ZXIuYXBhY2hlLm9yZ4IJAJYJC9kk4SILMAwGA1UdEwQFMAMBAf8wDQYJKoZI\n"
"hvcNAQEFBQADgYEAMfZe5tXJhTPqjCJlhbzUPvDzCrwjRMnVSiyDuhJXOF72xlWC\n"
"g4ccKUAdXbmV5sQHrISaTJD4FbuKJmFOatHnm0ZBKIT9bxqvfelFy5kWatUY2+g1\n"
"JPLZRg0M4JbOYzPyPGhckFD1ZPpdXNgOJIQzoRzzcJsGULMlQUhJoC0zCFY=\n"
"-----END CERTIFICATE-----\n"
;
// A self-signed certificate with altSubjectNames.
static const char alt_certificate[] =
"-----BEGIN CERTIFICATE-----\n"
"MIICazCCAdSgAwIBAgIJAIszqFE3DLgXMA0GCSqGSIb3DQEBBQUAMF8xCzAJBgNV\n"
"BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX\n"
"aWRnaXRzIFB0eSBMdGQxGDAWBgNVBAMTD2Zvby5leGFtcGxlLm9yZzAeFw0xMjAz\n"
"MDUwNDI0MTJaFw0zOTA3MjIwNDI0MTJaMF8xCzAJBgNVBAYTAkFVMRMwEQYDVQQI\n"
"EwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQx\n"
"GDAWBgNVBAMTD2Zvby5leGFtcGxlLm9yZzCBnzANBgkqhkiG9w0BAQEFAAOBjQAw\n"
"gYkCgYEAtcgbFIdAdg0eKvznAUfvoiKlUkhK2TMBjlbwt3ej88vl0dE3G1swAgZj\n"
"NPw1U5P00olGdOE6nDS7g2RgCSBWblA23jVBc0Y2AHqB0On2wj126DHiY88uVOmz\n"
"L2mkvqrxuY1drWZTgtzxSXRNE6yNWBvrqZxFz5oXvs83AuLrNusCAwEAAaMvMC0w\n"
"KwYDVR0RBCQwIoIPYmFyLmV4YW1wbGUub3Jngg9iYXouZXhhbXBsZS5vcmcwDQYJ\n"
"KoZIhvcNAQEFBQADgYEAmXnezfpxh/04U2X+e8PvPEZhsCpk26FSrvWfV7LBBtZF\n"
"gmoKIj2s2dKKWrXSTqWtJ2xecz9A9rKKLY8qjYKc/f+q3ALDz940lwyT6Fjz3c8u\n"
"Fsz7z9d6+vWjTTW1snFPCVpVJl7uTZZgbqnYNYW/TVkbVd5aFNl6s5tJnP99lvw=\n"
"-----END CERTIFICATE-----\n"
;
struct scoped_file_bio
{
scoped_file_bio(BIO * b) : bio(b) {
}
~scoped_file_bio() {
(void)BIO_set_close(bio, BIO_CLOSE);
BIO_free(bio);
}
operator bool() const {
return bio != nullptr;
}
BIO * bio;
private:
scoped_file_bio(const scoped_file_bio&);
scoped_file_bio& operator=(const scoped_file_bio&);
};
char *
asn1_strdup(ASN1_STRING * s)
{
// Make sure we have an 8-bit encoding.
assert(ASN1_STRING_type(s) == V_ASN1_IA5STRING ||
ASN1_STRING_type(s) == V_ASN1_UTF8STRING ||
ASN1_STRING_type(s) == V_ASN1_PRINTABLESTRING);
return strndup((const char *)ASN1_STRING_data(s), ASN1_STRING_length(s));
}
char *
x509_copy_subject_alt_name_cn(X509 * x509)
{
GENERAL_NAMES * names;
names = (GENERAL_NAMES *)X509_get_ext_d2i(x509, NID_subject_alt_name, nullptr, nullptr);
if (!names) {
printf("no subjectAltName entries\n");
return NULL;
}
unsigned count = sk_GENERAL_NAME_num(names);
for (unsigned i = 0; i < count; ++i) {
GENERAL_NAME * name;
name = sk_GENERAL_NAME_value(names, i);
switch (name->type) {
case GEN_DNS: {
char * dns;
dns = asn1_strdup(name->d.dNSName);
printf("alt name: %s\n", dns);
free(dns);
break;
}
default:
printf("ignoring subjectAltName type %d\n",
name->type);
}
}
if (names) {
GENERAL_NAMES_free(names);
}
return NULL;
}
char *
x509_copy_subject_cn(X509 * x509)
{
X509_NAME * subject;
subject = X509_get_subject_name(x509);
for (int pos = -1; ;) {
pos = X509_NAME_get_index_by_NID(subject, NID_commonName, pos);
if (pos == -1) {
break;
}
X509_NAME_ENTRY * e = X509_NAME_get_entry(subject, pos);
ASN1_STRING * cn = X509_NAME_ENTRY_get_data(e);
return asn1_strdup(cn);
}
return NULL;
}
static void
leaks(void)
{
char cmd[64];
snprintf(cmd, sizeof(cmd), "leaks %lu", (unsigned long)getpid());
system(cmd);
}
static void
extract_x509_names(BIO * bio)
{
X509 * cert;
char * subject;
// Read the cert from the BIO.
cert = PEM_read_bio_X509_AUX(bio, nullptr, nullptr, nullptr);
// Copy the subject.
subject = x509_copy_subject_cn(cert);
assert(subject);
printf("subject: %s\n", subject);
// Extract the altSubjectNames.
x509_copy_subject_alt_name_cn(cert);
X509_free(cert);
free(subject);
}
int main(int argc, const char ** argv)
{
BIO * b1;
BIO * b2;
b1 = BIO_new_mem_buf((void *)basic_certificate, sizeof(basic_certificate));
b2 = BIO_new_mem_buf((void *)alt_certificate, sizeof(alt_certificate));
printf("checking basic certificate\n");
extract_x509_names(b1);
printf("checking altSubjectName certificate\n");
extract_x509_names(b2);
BIO_free(b1);
BIO_free(b2);
for (int i = 1; i < argc; ++i) {
printf("checking %s\n", argv[i]);
scoped_file_bio bio(BIO_new_file(argv[i], "r"));
extract_x509_names(bio.bio);
}
leaks();
return 0;
}
/* vim: set sw=4 ts=5 cindent : */