-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcert.c
180 lines (139 loc) · 3.45 KB
/
cert.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
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <re.h>
#include "avs_log.h"
#include "avs_cert.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bn.h>
/* note: shadow struct */
struct tls {
SSL_CTX *ctx;
X509 *cert;
char *pass; /* password for private key */
};
int cert_tls_set_selfsigned_ecdsa(struct tls *tls, const char *curve_name)
{
X509_NAME *subj = NULL;
EVP_PKEY *key = NULL;
EC_KEY *ec_key = NULL;
X509 *cert = NULL;
int r, err = ENOMEM;
const char *cn = "[email protected]";
int eccgrp;
if (!tls || !cn)
return EINVAL;
key = EVP_PKEY_new();
if (!key)
goto out;
eccgrp = OBJ_txt2nid(curve_name);
if (eccgrp == NID_undef) {
warning("curve not supported: %s\n");
return ENOTSUP;
}
/* ECDSA */
ec_key = EC_KEY_new_by_curve_name(eccgrp);
if (!ec_key) {
warning("EC_KEY_new_by_curve_name error\n");
goto out;
}
/* NOTE: important */
EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
if (!EC_KEY_generate_key(ec_key)) {
warning("EC_KEY_generate_key error\n");
goto out;
}
if (!EVP_PKEY_assign_EC_KEY(key, ec_key)) {
warning("EVP_PKEY_assign_EC_KEY error\n");
goto out;
}
/* ownership of ec_key struct was assigned, don't free it. */
cert = X509_new();
if (!cert)
goto out;
if (!X509_set_version(cert, 2))
goto out;
if (!ASN1_INTEGER_set(X509_get_serialNumber(cert), rand_u32()))
goto out;
subj = X509_NAME_new();
if (!subj)
goto out;
if (!X509_NAME_add_entry_by_txt(subj, "CN", MBSTRING_ASC,
(unsigned char *)cn,
(int)str_len(cn), -1, 0))
goto out;
if (!X509_set_issuer_name(cert, subj) ||
!X509_set_subject_name(cert, subj))
goto out;
if (!X509_gmtime_adj(X509_get_notBefore(cert), -3600*24*365) ||
!X509_gmtime_adj(X509_get_notAfter(cert), 3600*24*365*10))
goto out;
if (!X509_set_pubkey(cert, key))
goto out;
if (!X509_sign(cert, key, EVP_sha1()))
goto out;
r = SSL_CTX_use_certificate(tls->ctx, cert);
if (r != 1)
goto out;
r = SSL_CTX_use_PrivateKey(tls->ctx, key);
if (r != 1) {
warning("SSL_CTX_use_PrivateKey error\n");
ERR_print_errors_fp(stderr);
goto out;
}
if (tls->cert)
X509_free(tls->cert);
tls->cert = cert;
cert = NULL;
#if 0
X509_print_fp(stderr, tls->cert);
#endif
err = 0;
out:
if (subj)
X509_NAME_free(subj);
if (cert)
X509_free(cert);
if (key)
EVP_PKEY_free(key);
if (err)
ERR_clear_error();
return err;
}
/**
* Enable ECDH (Elliptic Curve Diffie-Hellmann) on the TLS context
*
* This is valid for a TLS/DTLS server only.
*/
int cert_enable_ecdh(struct tls *tls)
{
SSL_CTX *ctx;
if (!tls)
return EINVAL;
ctx = tls_openssl_context(tls);
if (!ctx) {
warning("cert: no openssl context\n");
return ENOENT;
}
if (!SSL_CTX_set_ecdh_auto(ctx, 1)) {
warning("cert: failed to enable ECDH auto\n");
ERR_clear_error();
return EPROTO;
}
return 0;
}