-
Notifications
You must be signed in to change notification settings - Fork 21
/
example_httpsrv_tls_cert_auth.c
220 lines (211 loc) · 7.59 KB
/
example_httpsrv_tls_cert_auth.c
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
/* _
* ___ __ _ __ _ _ _(_)
* / __|/ _` |/ _` | | | | |
* \__ \ (_| | (_| | |_| | |
* |___/\__,_|\__, |\__,_|_|
* |___/
*
* Cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (C) 2016-2019 Silvio Clecio <[email protected]>
*
* Sagui library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Sagui library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sagui library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <sagui.h>
/* NOTE: Error checking has been omitted to make it clear. */
/*
* Simple example using TLS client authentication.
*
* Client example using cURL:
*
* curl -k --cert certs/client.p12 --pass abc123 --cert-type p12 https://localhost:<PORT>
*
* Certificate generation:
*
* ## CA
* certtool --generate-privkey --outfile ca.key
* echo 'cn = GnuTLS test CA' > ca.tmpl
* echo 'ca' >> ca.tmpl
* echo 'cert_signing_key' >> ca.tmpl
* echo 'expiration_days = 3650' >> ca.tmpl
* certtool --generate-self-signed --load-privkey ca.key --template ca.tmpl --outfile ca.pem
*
* ## Server
* certtool --generate-privkey --outfile server.key
* echo 'organization = GnuTLS test server' > server.tmpl
* echo 'cn = test.gnutls.org' >> server.tmpl
* echo 'tls_www_server' >> server.tmpl
* echo 'expiration_days = 3650' >> server.tmpl
* certtool --generate-certificate --load-ca-privkey ca.key --load-ca-certificate ca.pem --load-privkey server.key --template server.tmpl --outfile server.pem
*
* ## Client
* certtool --generate-privkey --outfile client.key
* echo 'cn = GnuTLS test client' > client.tmpl
* echo 'tls_www_client' >> client.tmpl
* echo 'expiration_days = 3650' >> client.tmpl
* certtool --generate-certificate --load-ca-certificate ca.pem --load-ca-privkey ca.key --load-privkey client.key --template client.tmpl --outfile client.pem
* certtool --to-p12 --p12-name=MyKey --password=abc123 --load-ca-certificate ca.pem --load-privkey client.key --load-certificate client.pem --outder --outfile client.p12
*/
#define KEY_FILE SG_EXAMPLES_CERTS_DIR "/server.key"
#define CERT_FILE SG_EXAMPLES_CERTS_DIR "/server.pem"
#define CA_FILE SG_EXAMPLES_CERTS_DIR "/ca.pem"
#define ERR_SIZE 256
#define PAGE_FMT \
"<html><head><title>Hello world</title></head><body><font " \
"color=\"%s\">%s</font></font></body></html>"
#define SECRET_MSG "Secret"
static void concat(char *s1, ...) {
va_list ap;
const char *s;
va_start(ap, s1);
while ((s = va_arg(ap, const char *)))
strcat(s1, s);
va_end(ap);
}
static bool sess_verify_cert(gnutls_session_t tls_session,
const char *line_break, char *err) {
gnutls_x509_crt_t cert = NULL;
const gnutls_datum_t *certs;
size_t len;
unsigned int status, certs_size;
int ret;
if (!tls_session || !line_break || !err) {
sg_strerror(EINVAL, err, ERR_SIZE);
return false;
}
if ((ret = gnutls_certificate_verify_peers2(tls_session, &status)) !=
GNUTLS_E_SUCCESS) {
concat(err, "Error verifying peers: ", gnutls_strerror(ret), line_break,
NULL);
goto error;
}
if (status & GNUTLS_CERT_INVALID)
concat(err, "The certificate is not trusted", line_break, NULL);
if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
concat(err, "The certificate has not got a known issuer", line_break, NULL);
if (status & GNUTLS_CERT_REVOKED)
concat(err, "The certificate has been revoked", line_break, NULL);
if (gnutls_certificate_type_get(tls_session) != GNUTLS_CRT_X509) {
concat(err, "The certificate type is not X.509", line_break, NULL);
goto error;
}
if ((ret = gnutls_x509_crt_init(&cert)) != GNUTLS_E_SUCCESS) {
concat(err,
"Error in the certificate initialization: ", gnutls_strerror(ret),
line_break, NULL);
goto error;
}
if (!(certs = gnutls_certificate_get_peers(tls_session, &certs_size))) {
concat(err, "No certificate was found", line_break, NULL);
goto error;
}
if ((ret = gnutls_x509_crt_import(cert, &certs[0], GNUTLS_X509_FMT_DER)) !=
GNUTLS_E_SUCCESS) {
concat(err, "Error parsing certificate: ", gnutls_strerror(ret), line_break,
NULL);
goto error;
}
if (gnutls_x509_crt_get_expiration_time(cert) < time(NULL)) {
concat(err, "The certificate has expired", line_break, NULL);
goto error;
}
if (gnutls_x509_crt_get_activation_time(cert) > time(NULL)) {
concat(err, "The certificate has not been activated yet", line_break, NULL);
goto error;
}
error:
len = strlen(err);
err[len - strlen("<br>")] = '\0';
gnutls_x509_crt_deinit(cert);
return len == 0;
}
static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req,
struct sg_httpres *res) {
char msg[ERR_SIZE];
char *color, *page;
size_t page_size;
unsigned int status;
if (sess_verify_cert(sg_httpreq_tls_session(req), "<br>", msg)) {
strcpy(msg, SECRET_MSG);
color = "green";
status = 200;
} else {
color = "red";
status = 500;
}
page_size = (size_t) snprintf(NULL, 0, PAGE_FMT, color, msg);
page = sg_alloc(page_size);
snprintf(page, page_size, PAGE_FMT, color, msg);
sg_httpres_send(res, page, "text/html; charset=utf-8", status);
sg_free(page);
}
int main(int argc, const char *argv[]) {
struct sg_httpsrv *srv;
gnutls_datum_t key_file, cert_file, ca_file;
int ret, status;
uint16_t port;
if (argc != 2) {
printf("%s <PORT>\n", argv[0]);
return EXIT_FAILURE;
}
port = strtol(argv[1], NULL, 10);
status = EXIT_FAILURE;
srv = sg_httpsrv_new(req_cb, NULL);
memset(&key_file, 0, sizeof(gnutls_datum_t));
memset(&cert_file, 0, sizeof(gnutls_datum_t));
memset(&ca_file, 0, sizeof(gnutls_datum_t));
if ((ret = gnutls_load_file(KEY_FILE, &key_file)) != GNUTLS_E_SUCCESS) {
fprintf(stderr, "Error loading the private key \"%s\": %s\n", KEY_FILE,
gnutls_strerror(ret));
fflush(stdout);
goto error;
}
if ((ret = gnutls_load_file(CERT_FILE, &cert_file)) != GNUTLS_E_SUCCESS) {
fprintf(stderr, "Error loading the certificate \"%s\": %s\n", CERT_FILE,
gnutls_strerror(ret));
fflush(stdout);
goto error;
}
if ((ret = gnutls_load_file(CA_FILE, &ca_file)) != GNUTLS_E_SUCCESS) {
fprintf(stderr, "Error loading the CA \"%s\": %s\n", CA_FILE,
gnutls_strerror(ret));
fflush(stdout);
goto error;
}
if (sg_httpsrv_tls_listen2(srv, (const char *) key_file.data, NULL,
(const char *) cert_file.data,
(const char *) ca_file.data, NULL, port, false)) {
status = EXIT_SUCCESS;
fprintf(stdout, "Server running at https://localhost:%d\n",
sg_httpsrv_port(srv));
fflush(stdout);
getchar();
}
error:
sg_httpsrv_free(srv);
if (key_file.size > 0)
gnutls_free(key_file.data);
if (cert_file.size > 0)
gnutls_free(cert_file.data);
if (ca_file.size > 0)
gnutls_free(ca_file.data);
return status;
}