-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsm2cert.cpp
399 lines (334 loc) · 10.7 KB
/
sm2cert.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "sm2cert.h"
#include "ui_sm2cert.h"
#include <openssl/asn1.h>
Sm2Cert::Sm2Cert(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Sm2Cert)
{
ui->setupUi(this);
}
Sm2Cert::~Sm2Cert()
{
delete ui;
}
static char *opt_getprog(void)
{
return (char *) "";
}
/*
* name is expected to be in the format /type0=value0/type1=value1/type2=...
* where + can be used instead of / to form multi-valued RDNs if canmulti
* and characters may be escaped by \
*/
static X509_NAME *parse_name(const char *cp, int chtype, int canmulti, const char *desc)
{
int nextismulti = 0;
char *work;
X509_NAME *n;
if (*cp++ != '/') {
BIO_printf(bio_err,
"%s: %s name is expected to be in the format "
"/type0=value0/type1=value1/type2=... where characters may "
"be escaped by \\. This name is not in that format: '%s'\n",
opt_getprog(),
desc,
--cp);
return NULL;
}
n = X509_NAME_new();
if (n == NULL) {
BIO_printf(bio_err, "%s: Out of memory\n", opt_getprog());
return NULL;
}
work = OPENSSL_strdup(cp);
if (work == NULL) {
BIO_printf(bio_err, "%s: Error copying %s name input\n", opt_getprog(), desc);
goto err;
}
while (*cp != '\0') {
char *bp = work;
char *typestr = bp;
unsigned char *valstr;
int nid;
int ismulti = nextismulti;
nextismulti = 0;
/* Collect the type */
while (*cp != '\0' && *cp != '=')
*bp++ = *cp++;
*bp++ = '\0';
if (*cp == '\0') {
BIO_printf(bio_err,
"%s: Missing '=' after RDN type string '%s' in %s name string\n",
opt_getprog(),
typestr,
desc);
goto err;
}
++cp;
/* Collect the value. */
valstr = (unsigned char *) bp;
for (; *cp != '\0' && *cp != '/'; *bp++ = *cp++) {
/* unescaped '+' symbol string signals further member of multiRDN */
if (canmulti && *cp == '+') {
nextismulti = 1;
break;
}
if (*cp == '\\' && *++cp == '\0') {
BIO_printf(bio_err,
"%s: Escape character at end of %s name string\n",
opt_getprog(),
desc);
goto err;
}
}
*bp++ = '\0';
/* If not at EOS (must be + or /), move forward. */
if (*cp != '\0')
++cp;
/* Parse */
nid = OBJ_txt2nid(typestr);
if (nid == NID_undef) {
BIO_printf(bio_err,
"%s: Skipping unknown %s name attribute \"%s\"\n",
opt_getprog(),
desc,
typestr);
if (ismulti)
BIO_printf(bio_err,
"Hint: a '+' in a value string needs be escaped using '\\' else a new "
"member of a multi-valued RDN is expected\n");
continue;
}
if (*valstr == '\0') {
BIO_printf(bio_err,
"%s: No value provided for %s name attribute \"%s\", skipped\n",
opt_getprog(),
desc,
typestr);
continue;
}
if (!X509_NAME_add_entry_by_NID(
n, nid, chtype, valstr, strlen((char *) valstr), -1, ismulti ? -1 : 0)) {
ERR_print_errors(bio_err);
BIO_printf(bio_err,
"%s: Error adding %s name attribute \"/%s=%s\"\n",
opt_getprog(),
desc,
typestr,
valstr);
goto err;
}
}
OPENSSL_free(work);
return n;
err:
X509_NAME_free(n);
OPENSSL_free(work);
return NULL;
}
static X509 *genCert(int type,
X509 *midCA,
EVP_PKEY *midcaPkey,
QString subj,
QString days,
char **key,
size_t *keylen)
{
X509_NAME *name = NULL;
X509 *userCer = NULL;
std::string str;
long len;
BIO *out = NULL;
X509_EXTENSION *cert_ex = NULL;
X509_REQ *userReq = NULL;
ASN1_INTEGER *aserial = NULL;
const X509_NAME *rootCAname;
time_t curTime;
ASN1_TIME *rootBeforeTime = NULL;
ASN1_TIME *rootAfterTime = NULL;
EVP_PKEY *userKey = EVP_PKEY_Q_keygen(NULL, NULL, "SM2");
if (userKey == NULL) {
printTSError();
return NULL;
}
out = BIO_new(BIO_s_mem());
if (out == NULL)
goto end;
if (!PEM_write_bio_PrivateKey(out, userKey, NULL, NULL, 0, NULL, NULL)) {
printTSError();
goto end;
}
len = BIO_get_mem_data(out, NULL);
if (len <= 0)
goto end;
*key = (char *) malloc(len);
if (*key == NULL)
goto end;
if (BIO_read(out, *key, len) != len)
goto end;
*keylen = len;
userReq = X509_REQ_new();
if (userReq == NULL)
goto end;
X509_REQ_set_pubkey(userReq, userKey);
if (!subj.isEmpty()) {
name = parse_name(subj.toStdString().c_str(), MBSTRING_ASC, 1, "subject");
if (!name) {
return NULL;
}
X509_REQ_set_subject_name(userReq, name);
}
if (!X509_REQ_set_version(userReq, X509_VERSION_3)
|| !X509_REQ_sign(userReq, userKey, EVP_sm3()) || !X509_REQ_verify(userReq, userKey))
goto end;
if (type == 0) {
str = "Key Encipherment, Data Encipherment";
} else {
str = "Digital Signature";
}
userCer = X509_new();
if (userCer == NULL)
goto end;
cert_ex = X509V3_EXT_conf_nid(NULL, NULL, NID_key_usage, str.c_str());
if (cert_ex == NULL)
goto end;
if (!X509_add_ext(userCer, cert_ex, -1) || !X509_set_version(userCer, X509_VERSION_3)
|| !X509_set_pubkey(userCer, userKey))
goto end;
aserial = ASN1_INTEGER_new();
if (!ASN1_INTEGER_set(aserial, 0))
goto end;
if (!X509_set_serialNumber(userCer, aserial) || !X509_set_subject_name(userCer, name))
goto end;
rootCAname = X509_get_subject_name(midCA);
if (!X509_set_issuer_name(userCer, rootCAname))
goto end;
curTime = time(NULL);
rootBeforeTime = ASN1_TIME_new();
rootAfterTime = ASN1_TIME_adj(NULL, curTime, 0, days.toInt() * 60 * 60 * 24);
if (!ASN1_TIME_set(rootBeforeTime, curTime) || !X509_set_notBefore(userCer, rootBeforeTime)
|| !X509_set_notAfter(userCer, rootAfterTime))
goto end;
if (!X509_sign(userCer, midcaPkey, EVP_sm3()))
goto end;
end:
ASN1_TIME_free(rootAfterTime);
ASN1_TIME_free(rootBeforeTime);
ASN1_INTEGER_free(aserial);
X509_EXTENSION_free(cert_ex);
X509_REQ_free(userReq);
BIO_free(out);
EVP_PKEY_free(userKey);
return userCer;
}
void Sm2Cert::on_pushButtonGen_clicked()
{
QString subj = this->ui->lineEditSubj->text();
QString days = this->ui->lineEditDays->text();
QFile fsubca(":/certs/subca.pem");
QFile fpkey(":/certs/subca.key");
X509 *userSignCer = NULL, *userEncryptCer = NULL;
char *signKey = NULL, *encKey = NULL;
size_t signKeyLen, encKeyLen;
QString subcaQstr, pkeyQstr;
X509 *subca = NULL;
EVP_PKEY *pkey = NULL;
long len;
char *buf = NULL;
BIO *out = NULL;
if (subj.isEmpty()) {
QMessageBox::warning(NULL,
"warning",
QString("请输入主体名称!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
if (days.isEmpty()) {
QMessageBox::warning(NULL,
"warning",
QString("请输入有效期!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
if (!fsubca.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(NULL,
"warning",
QString("subca.pem打开失败!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
if (!fpkey.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(NULL,
"warning",
QString("subca.key打开失败!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
QTextStream subcaInput(&fsubca);
QTextStream pkeyInput(&fpkey);
subcaQstr = subcaInput.readAll();
pkeyQstr = pkeyInput.readAll();
out = BIO_new(BIO_s_mem());
if (out == NULL)
goto end;
if (BIO_write(out, subcaQstr.toStdString().c_str(), subcaQstr.size()) != subcaQstr.size())
goto end;
subca = PEM_read_bio_X509(out, NULL, NULL, NULL);
if (subca == NULL) {
this->ui->textBrowserSignKey->setText(subcaQstr);
printTSError();
goto end;
}
fsubca.close();
BIO_reset(out);
if (BIO_write(out, pkeyQstr.toStdString().c_str(), pkeyQstr.size()) != pkeyQstr.size())
goto end;
pkey = PEM_read_bio_PrivateKey(out, NULL, NULL, NULL);
if (pkey == NULL) {
printTSError();
goto end;
}
fpkey.close();
userSignCer = genCert(1, subca, pkey, subj, days, &signKey, &signKeyLen);
if (userSignCer == NULL) {
printTSError();
goto end;
}
userEncryptCer = genCert(0, subca, pkey, subj, days, &encKey, &encKeyLen);
if (userEncryptCer == NULL) {
printTSError();
goto end;
}
this->ui->textBrowserSignKey->setText(QString::fromStdString(std::string(signKey, signKeyLen)));
this->ui->textBrowserEncryKey->setText(QString::fromStdString(std::string(encKey, encKeyLen)));
BIO_reset(out);
if (!PEM_write_bio_X509(out, userSignCer)) {
printTSError();
goto end;
}
len = BIO_get_mem_data(out, &buf);
if (len <= 0)
goto end;
this->ui->textBrowserSignOutput->setPlainText(QString::fromStdString(std::string(buf, len)));
BIO_reset(out);
if (!PEM_write_bio_X509(out, userEncryptCer)) {
printTSError();
goto end;
}
len = BIO_get_mem_data(out, &buf);
if (len <= 0)
goto end;
this->ui->textBrowserEncryptOutput->setPlainText(QString::fromStdString(std::string(buf, len)));
end:
EVP_PKEY_free(pkey);
X509_free(subca);
BIO_free(out);
free(signKey);
free(encKey);
X509_free(userSignCer);
X509_free(userEncryptCer);
}