blob: 5c6e2f67dda9ccdc3600a14f90007ceb4985fa3a [file] [log] [blame]
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001/*
2 * Hotspot 2.0 OSU client - EST client
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003 * Copyright (c) 2012-2014, Qualcomm Atheros, Inc.
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07004 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10#include <openssl/err.h>
11#include <openssl/evp.h>
12#include <openssl/pem.h>
13#include <openssl/pkcs7.h>
14#include <openssl/rsa.h>
15#include <openssl/asn1.h>
16#include <openssl/asn1t.h>
17#include <openssl/x509.h>
18#include <openssl/x509v3.h>
Hai Shalom74f70d42019-02-11 14:42:39 -080019#include <openssl/opensslv.h>
David Benjamin92c34502022-06-13 12:22:05 -040020#include <openssl/buffer.h>
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070021
22#include "common.h"
23#include "utils/base64.h"
24#include "utils/xml-utils.h"
25#include "utils/http-utils.h"
26#include "osu_client.h"
27
28
29static int pkcs7_to_cert(struct hs20_osu_client *ctx, const u8 *pkcs7,
30 size_t len, char *pem_file, char *der_file)
31{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080032#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080033 CBS pkcs7_cbs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080034#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070035 PKCS7 *p7 = NULL;
36 const unsigned char *p = pkcs7;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080037#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070038 STACK_OF(X509) *certs;
39 int i, num, ret = -1;
40 BIO *out = NULL;
41
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080042#ifdef OPENSSL_IS_BORINGSSL
43 certs = sk_X509_new_null();
44 if (!certs)
45 goto fail;
46 CBS_init(&pkcs7_cbs, pkcs7, len);
47 if (!PKCS7_get_certificates(certs, &pkcs7_cbs)) {
48 wpa_printf(MSG_INFO, "Could not parse PKCS#7 object: %s",
49 ERR_error_string(ERR_get_error(), NULL));
50 write_result(ctx, "Could not parse PKCS#7 object from EST");
51 goto fail;
52 }
53#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070054 p7 = d2i_PKCS7(NULL, &p, len);
55 if (p7 == NULL) {
56 wpa_printf(MSG_INFO, "Could not parse PKCS#7 object: %s",
57 ERR_error_string(ERR_get_error(), NULL));
58 write_result(ctx, "Could not parse PKCS#7 object from EST");
59 goto fail;
60 }
61
62 switch (OBJ_obj2nid(p7->type)) {
63 case NID_pkcs7_signed:
64 certs = p7->d.sign->cert;
65 break;
66 case NID_pkcs7_signedAndEnveloped:
67 certs = p7->d.signed_and_enveloped->cert;
68 break;
69 default:
70 certs = NULL;
71 break;
72 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080073#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070074
75 if (!certs || ((num = sk_X509_num(certs)) == 0)) {
76 wpa_printf(MSG_INFO, "No certificates found in PKCS#7 object");
77 write_result(ctx, "No certificates found in PKCS#7 object");
78 goto fail;
79 }
80
81 if (der_file) {
82 FILE *f = fopen(der_file, "wb");
83 if (f == NULL)
84 goto fail;
85 i2d_X509_fp(f, sk_X509_value(certs, 0));
86 fclose(f);
87 }
88
89 if (pem_file) {
90 out = BIO_new(BIO_s_file());
91 if (out == NULL ||
92 BIO_write_filename(out, pem_file) <= 0)
93 goto fail;
94
95 for (i = 0; i < num; i++) {
96 X509 *cert = sk_X509_value(certs, i);
97 X509_print(out, cert);
98 PEM_write_bio_X509(out, cert);
99 BIO_puts(out, "\n");
100 }
101 }
102
103 ret = 0;
104
105fail:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800106#ifdef OPENSSL_IS_BORINGSSL
107 if (certs)
108 sk_X509_pop_free(certs, X509_free);
109#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700110 PKCS7_free(p7);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800111#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700112 if (out)
113 BIO_free_all(out);
114
115 return ret;
116}
117
118
119int est_load_cacerts(struct hs20_osu_client *ctx, const char *url)
120{
121 char *buf, *resp;
122 size_t buflen;
123 unsigned char *pkcs7;
124 size_t pkcs7_len, resp_len;
125 int res;
126
127 buflen = os_strlen(url) + 100;
128 buf = os_malloc(buflen);
129 if (buf == NULL)
130 return -1;
131
132 os_snprintf(buf, buflen, "%s/cacerts", url);
133 wpa_printf(MSG_INFO, "Download EST cacerts from %s", buf);
134 write_summary(ctx, "Download EST cacerts from %s", buf);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700135 ctx->no_osu_cert_validation = 1;
136 http_ocsp_set(ctx->http, 1);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700137 res = http_download_file(ctx->http, buf, "Cert/est-cacerts.txt",
138 ctx->ca_fname);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700139 http_ocsp_set(ctx->http,
140 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
141 ctx->no_osu_cert_validation = 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700142 if (res < 0) {
143 wpa_printf(MSG_INFO, "Failed to download EST cacerts from %s",
144 buf);
145 write_result(ctx, "Failed to download EST cacerts from %s",
146 buf);
147 os_free(buf);
148 return -1;
149 }
150 os_free(buf);
151
152 resp = os_readfile("Cert/est-cacerts.txt", &resp_len);
153 if (resp == NULL) {
154 wpa_printf(MSG_INFO, "Could not read Cert/est-cacerts.txt");
155 write_result(ctx, "Could not read EST cacerts");
156 return -1;
157 }
158
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800159 pkcs7 = base64_decode(resp, resp_len, &pkcs7_len);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700160 if (pkcs7 && pkcs7_len < resp_len / 2) {
161 wpa_printf(MSG_INFO, "Too short base64 decode (%u bytes; downloaded %u bytes) - assume this was binary",
162 (unsigned int) pkcs7_len, (unsigned int) resp_len);
163 os_free(pkcs7);
164 pkcs7 = NULL;
165 }
166 if (pkcs7 == NULL) {
167 wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
168 pkcs7 = os_malloc(resp_len);
169 if (pkcs7) {
170 os_memcpy(pkcs7, resp, resp_len);
171 pkcs7_len = resp_len;
172 }
173 }
174 os_free(resp);
175
176 if (pkcs7 == NULL) {
177 wpa_printf(MSG_INFO, "Could not fetch PKCS7 cacerts");
178 write_result(ctx, "Could not fetch EST PKCS#7 cacerts");
179 return -1;
180 }
181
182 res = pkcs7_to_cert(ctx, pkcs7, pkcs7_len, "Cert/est-cacerts.pem",
183 NULL);
184 os_free(pkcs7);
185 if (res < 0) {
186 wpa_printf(MSG_INFO, "Could not parse CA certs from PKCS#7 cacerts response");
187 write_result(ctx, "Could not parse CA certs from EST PKCS#7 cacerts response");
188 return -1;
189 }
190 unlink("Cert/est-cacerts.txt");
191
192 return 0;
193}
194
195
196/*
197 * CsrAttrs ::= SEQUENCE SIZE (0..MAX) OF AttrOrOID
198 *
199 * AttrOrOID ::= CHOICE {
200 * oid OBJECT IDENTIFIER,
201 * attribute Attribute }
202 *
203 * Attribute ::= SEQUENCE {
204 * type OBJECT IDENTIFIER,
205 * values SET SIZE(1..MAX) OF OBJECT IDENTIFIER }
206 */
207
208typedef struct {
209 ASN1_OBJECT *type;
210 STACK_OF(ASN1_OBJECT) *values;
211} Attribute;
212
213typedef struct {
214 int type;
215 union {
216 ASN1_OBJECT *oid;
217 Attribute *attribute;
218 } d;
219} AttrOrOID;
220
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000221#if OPENSSL_VERSION_NUMBER >= 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -0800222DEFINE_STACK_OF(AttrOrOID)
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000223#endif
Hai Shalom74f70d42019-02-11 14:42:39 -0800224
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700225typedef struct {
226 int type;
227 STACK_OF(AttrOrOID) *attrs;
228} CsrAttrs;
229
230ASN1_SEQUENCE(Attribute) = {
231 ASN1_SIMPLE(Attribute, type, ASN1_OBJECT),
232 ASN1_SET_OF(Attribute, values, ASN1_OBJECT)
233} ASN1_SEQUENCE_END(Attribute);
234
235ASN1_CHOICE(AttrOrOID) = {
236 ASN1_SIMPLE(AttrOrOID, d.oid, ASN1_OBJECT),
237 ASN1_SIMPLE(AttrOrOID, d.attribute, Attribute)
238} ASN1_CHOICE_END(AttrOrOID);
239
240ASN1_CHOICE(CsrAttrs) = {
241 ASN1_SEQUENCE_OF(CsrAttrs, attrs, AttrOrOID)
242} ASN1_CHOICE_END(CsrAttrs);
243
244IMPLEMENT_ASN1_FUNCTIONS(CsrAttrs);
245
246
247static void add_csrattrs_oid(struct hs20_osu_client *ctx, ASN1_OBJECT *oid,
248 STACK_OF(X509_EXTENSION) *exts)
249{
250 char txt[100];
251 int res;
252
253 if (!oid)
254 return;
255
256 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
257 if (res < 0 || res >= (int) sizeof(txt))
258 return;
259
260 if (os_strcmp(txt, "1.2.840.113549.1.9.7") == 0) {
261 wpa_printf(MSG_INFO, "TODO: csrattr challengePassword");
262 } else if (os_strcmp(txt, "1.2.840.113549.1.1.11") == 0) {
263 wpa_printf(MSG_INFO, "csrattr sha256WithRSAEncryption");
264 } else {
265 wpa_printf(MSG_INFO, "Ignore unsupported csrattr oid %s", txt);
266 }
267}
268
269
270static void add_csrattrs_ext_req(struct hs20_osu_client *ctx,
271 STACK_OF(ASN1_OBJECT) *values,
272 STACK_OF(X509_EXTENSION) *exts)
273{
274 char txt[100];
275 int i, num, res;
276
277 num = sk_ASN1_OBJECT_num(values);
278 for (i = 0; i < num; i++) {
279 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(values, i);
280
281 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
282 if (res < 0 || res >= (int) sizeof(txt))
283 continue;
284
285 if (os_strcmp(txt, "1.3.6.1.1.1.1.22") == 0) {
286 wpa_printf(MSG_INFO, "TODO: extReq macAddress");
287 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.3") == 0) {
288 wpa_printf(MSG_INFO, "TODO: extReq imei");
289 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.4") == 0) {
290 wpa_printf(MSG_INFO, "TODO: extReq meid");
291 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.5") == 0) {
292 wpa_printf(MSG_INFO, "TODO: extReq DevId");
293 } else {
294 wpa_printf(MSG_INFO, "Ignore unsupported cstattr extensionsRequest %s",
295 txt);
296 }
297 }
298}
299
300
301static void add_csrattrs_attr(struct hs20_osu_client *ctx, Attribute *attr,
302 STACK_OF(X509_EXTENSION) *exts)
303{
304 char txt[100], txt2[100];
305 int i, num, res;
306
307 if (!attr || !attr->type || !attr->values)
308 return;
309
310 res = OBJ_obj2txt(txt, sizeof(txt), attr->type, 1);
311 if (res < 0 || res >= (int) sizeof(txt))
312 return;
313
314 if (os_strcmp(txt, "1.2.840.113549.1.9.14") == 0) {
315 add_csrattrs_ext_req(ctx, attr->values, exts);
316 return;
317 }
318
319 num = sk_ASN1_OBJECT_num(attr->values);
320 for (i = 0; i < num; i++) {
321 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(attr->values, i);
322
323 res = OBJ_obj2txt(txt2, sizeof(txt2), oid, 1);
324 if (res < 0 || res >= (int) sizeof(txt2))
325 continue;
326
327 wpa_printf(MSG_INFO, "Ignore unsupported cstattr::attr %s oid %s",
328 txt, txt2);
329 }
330}
331
332
333static void add_csrattrs(struct hs20_osu_client *ctx, CsrAttrs *csrattrs,
334 STACK_OF(X509_EXTENSION) *exts)
335{
336 int i, num;
337
338 if (!csrattrs || ! csrattrs->attrs)
339 return;
340
David Benjamin92c34502022-06-13 12:22:05 -0400341#if OPENSSL_VERSION_NUMBER >= 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -0800342 num = sk_AttrOrOID_num(csrattrs->attrs);
343#else
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700344 num = SKM_sk_num(AttrOrOID, csrattrs->attrs);
Hai Shalom74f70d42019-02-11 14:42:39 -0800345#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700346 for (i = 0; i < num; i++) {
David Benjamin92c34502022-06-13 12:22:05 -0400347#if OPENSSL_VERSION_NUMBER >= 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -0800348 AttrOrOID *ao = sk_AttrOrOID_value(csrattrs->attrs, i);
349#else
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700350 AttrOrOID *ao = SKM_sk_value(AttrOrOID, csrattrs->attrs, i);
Hai Shalom74f70d42019-02-11 14:42:39 -0800351#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700352 switch (ao->type) {
353 case 0:
354 add_csrattrs_oid(ctx, ao->d.oid, exts);
355 break;
356 case 1:
357 add_csrattrs_attr(ctx, ao->d.attribute, exts);
358 break;
359 }
360 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800361}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800362
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700363
364static int generate_csr(struct hs20_osu_client *ctx, char *key_pem,
365 char *csr_pem, char *est_req, char *old_cert,
366 CsrAttrs *csrattrs)
367{
368 EVP_PKEY_CTX *pctx = NULL;
369 EVP_PKEY *pkey = NULL;
370 RSA *rsa;
371 X509_REQ *req = NULL;
372 int ret = -1;
373 unsigned int val;
374 X509_NAME *subj = NULL;
375 char name[100];
376 STACK_OF(X509_EXTENSION) *exts = NULL;
377 X509_EXTENSION *ex;
378 BIO *out;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800379 CONF *ctmp = NULL;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700380
381 wpa_printf(MSG_INFO, "Generate RSA private key");
382 write_summary(ctx, "Generate RSA private key");
383 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
384 if (!pctx)
385 return -1;
386
387 if (EVP_PKEY_keygen_init(pctx) <= 0)
388 goto fail;
389
390 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, 2048) <= 0)
391 goto fail;
392
393 if (EVP_PKEY_keygen(pctx, &pkey) <= 0)
394 goto fail;
395 EVP_PKEY_CTX_free(pctx);
396 pctx = NULL;
397
398 rsa = EVP_PKEY_get1_RSA(pkey);
399 if (rsa == NULL)
400 goto fail;
401
402 if (key_pem) {
403 FILE *f = fopen(key_pem, "wb");
404 if (f == NULL)
405 goto fail;
406 if (!PEM_write_RSAPrivateKey(f, rsa, NULL, NULL, 0, NULL,
407 NULL)) {
408 wpa_printf(MSG_INFO, "Could not write private key: %s",
409 ERR_error_string(ERR_get_error(), NULL));
410 fclose(f);
411 goto fail;
412 }
413 fclose(f);
414 }
415
416 wpa_printf(MSG_INFO, "Generate CSR");
417 write_summary(ctx, "Generate CSR");
418 req = X509_REQ_new();
419 if (req == NULL)
420 goto fail;
421
422 if (old_cert) {
423 FILE *f;
424 X509 *cert;
425 int res;
426
427 f = fopen(old_cert, "r");
428 if (f == NULL)
429 goto fail;
430 cert = PEM_read_X509(f, NULL, NULL, NULL);
431 fclose(f);
432
433 if (cert == NULL)
434 goto fail;
435 res = X509_REQ_set_subject_name(req,
436 X509_get_subject_name(cert));
437 X509_free(cert);
438 if (!res)
439 goto fail;
440 } else {
441 os_get_random((u8 *) &val, sizeof(val));
442 os_snprintf(name, sizeof(name), "cert-user-%u", val);
443 subj = X509_NAME_new();
444 if (subj == NULL ||
445 !X509_NAME_add_entry_by_txt(subj, "CN", MBSTRING_ASC,
446 (unsigned char *) name,
447 -1, -1, 0) ||
448 !X509_REQ_set_subject_name(req, subj))
449 goto fail;
450 X509_NAME_free(subj);
451 subj = NULL;
452 }
453
454 if (!X509_REQ_set_pubkey(req, pkey))
455 goto fail;
456
457 exts = sk_X509_EXTENSION_new_null();
458 if (!exts)
459 goto fail;
460
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800461 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_basic_constraints,
462 "CA:FALSE");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700463 if (ex == NULL ||
464 !sk_X509_EXTENSION_push(exts, ex))
465 goto fail;
466
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800467 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_key_usage,
468 "nonRepudiation,digitalSignature,keyEncipherment");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700469 if (ex == NULL ||
470 !sk_X509_EXTENSION_push(exts, ex))
471 goto fail;
472
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800473 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_ext_key_usage,
474 "1.3.6.1.4.1.40808.1.1.2");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700475 if (ex == NULL ||
476 !sk_X509_EXTENSION_push(exts, ex))
477 goto fail;
478
479 add_csrattrs(ctx, csrattrs, exts);
480
481 if (!X509_REQ_add_extensions(req, exts))
482 goto fail;
483 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
484 exts = NULL;
485
486 if (!X509_REQ_sign(req, pkey, EVP_sha256()))
487 goto fail;
488
489 out = BIO_new(BIO_s_mem());
490 if (out) {
491 char *txt;
492 size_t rlen;
493
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800494#if !defined(ANDROID) || !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700495 X509_REQ_print(out, req);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800496#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700497 rlen = BIO_ctrl_pending(out);
498 txt = os_malloc(rlen + 1);
499 if (txt) {
500 int res = BIO_read(out, txt, rlen);
501 if (res > 0) {
502 txt[res] = '\0';
503 wpa_printf(MSG_MSGDUMP, "OpenSSL: Certificate request:\n%s",
504 txt);
505 }
506 os_free(txt);
507 }
508 BIO_free(out);
509 }
510
511 if (csr_pem) {
512 FILE *f = fopen(csr_pem, "w");
513 if (f == NULL)
514 goto fail;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800515#if !defined(ANDROID) || !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700516 X509_REQ_print_fp(f, req);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800517#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700518 if (!PEM_write_X509_REQ(f, req)) {
519 fclose(f);
520 goto fail;
521 }
522 fclose(f);
523 }
524
525 if (est_req) {
526 BIO *mem = BIO_new(BIO_s_mem());
527 BUF_MEM *ptr;
528 char *pos, *end, *buf_end;
529 FILE *f;
530
531 if (mem == NULL)
532 goto fail;
533 if (!PEM_write_bio_X509_REQ(mem, req)) {
534 BIO_free(mem);
535 goto fail;
536 }
537
538 BIO_get_mem_ptr(mem, &ptr);
539 pos = ptr->data;
540 buf_end = pos + ptr->length;
541
542 /* Remove START/END lines */
543 while (pos < buf_end && *pos != '\n')
544 pos++;
545 if (pos == buf_end) {
546 BIO_free(mem);
547 goto fail;
548 }
549 pos++;
550
551 end = pos;
552 while (end < buf_end && *end != '-')
553 end++;
554
555 f = fopen(est_req, "w");
556 if (f == NULL) {
557 BIO_free(mem);
558 goto fail;
559 }
560 fwrite(pos, end - pos, 1, f);
561 fclose(f);
562
563 BIO_free(mem);
564 }
565
566 ret = 0;
567fail:
568 if (exts)
569 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
570 if (subj)
571 X509_NAME_free(subj);
572 if (req)
573 X509_REQ_free(req);
574 if (pkey)
575 EVP_PKEY_free(pkey);
576 if (pctx)
577 EVP_PKEY_CTX_free(pctx);
578 return ret;
579}
580
581
582int est_build_csr(struct hs20_osu_client *ctx, const char *url)
583{
584 char *buf;
585 size_t buflen;
586 int res;
587 char old_cert_buf[200];
588 char *old_cert = NULL;
589 CsrAttrs *csrattrs = NULL;
590
591 buflen = os_strlen(url) + 100;
592 buf = os_malloc(buflen);
593 if (buf == NULL)
594 return -1;
595
596 os_snprintf(buf, buflen, "%s/csrattrs", url);
597 wpa_printf(MSG_INFO, "Download csrattrs from %s", buf);
598 write_summary(ctx, "Download EST csrattrs from %s", buf);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700599 ctx->no_osu_cert_validation = 1;
600 http_ocsp_set(ctx->http, 1);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700601 res = http_download_file(ctx->http, buf, "Cert/est-csrattrs.txt",
602 ctx->ca_fname);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700603 http_ocsp_set(ctx->http,
604 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
605 ctx->no_osu_cert_validation = 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700606 os_free(buf);
607 if (res < 0) {
608 wpa_printf(MSG_INFO, "Failed to download EST csrattrs - assume no extra attributes are needed");
609 } else {
610 size_t resp_len;
611 char *resp;
612 unsigned char *attrs;
613 const unsigned char *pos;
614 size_t attrs_len;
615
616 resp = os_readfile("Cert/est-csrattrs.txt", &resp_len);
617 if (resp == NULL) {
618 wpa_printf(MSG_INFO, "Could not read csrattrs");
619 return -1;
620 }
621
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800622 attrs = base64_decode(resp, resp_len, &attrs_len);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700623 os_free(resp);
624
625 if (attrs == NULL) {
626 wpa_printf(MSG_INFO, "Could not base64 decode csrattrs");
627 return -1;
628 }
629 unlink("Cert/est-csrattrs.txt");
630
631 pos = attrs;
632 csrattrs = d2i_CsrAttrs(NULL, &pos, attrs_len);
633 os_free(attrs);
634 if (csrattrs == NULL) {
635 wpa_printf(MSG_INFO, "Failed to parse csrattrs ASN.1");
636 /* Continue assuming no additional requirements */
637 }
638 }
639
640 if (ctx->client_cert_present) {
641 os_snprintf(old_cert_buf, sizeof(old_cert_buf),
642 "SP/%s/client-cert.pem", ctx->fqdn);
643 old_cert = old_cert_buf;
644 }
645
646 res = generate_csr(ctx, "Cert/privkey-plain.pem", "Cert/est-req.pem",
647 "Cert/est-req.b64", old_cert, csrattrs);
648 if (csrattrs)
649 CsrAttrs_free(csrattrs);
650
651 return res;
652}
653
654
655int est_simple_enroll(struct hs20_osu_client *ctx, const char *url,
656 const char *user, const char *pw)
657{
658 char *buf, *resp, *req, *req2;
659 size_t buflen, resp_len, len, pkcs7_len;
660 unsigned char *pkcs7;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700661 char client_cert_buf[200];
662 char client_key_buf[200];
663 const char *client_cert = NULL, *client_key = NULL;
664 int res;
665
666 req = os_readfile("Cert/est-req.b64", &len);
667 if (req == NULL) {
668 wpa_printf(MSG_INFO, "Could not read Cert/req.b64");
669 return -1;
670 }
671 req2 = os_realloc(req, len + 1);
672 if (req2 == NULL) {
673 os_free(req);
674 return -1;
675 }
676 req2[len] = '\0';
677 req = req2;
678 wpa_printf(MSG_DEBUG, "EST simpleenroll request: %s", req);
679
680 buflen = os_strlen(url) + 100;
681 buf = os_malloc(buflen);
682 if (buf == NULL) {
683 os_free(req);
684 return -1;
685 }
686
687 if (ctx->client_cert_present) {
688 os_snprintf(buf, buflen, "%s/simplereenroll", url);
689 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
690 "SP/%s/client-cert.pem", ctx->fqdn);
691 client_cert = client_cert_buf;
692 os_snprintf(client_key_buf, sizeof(client_key_buf),
693 "SP/%s/client-key.pem", ctx->fqdn);
694 client_key = client_key_buf;
695 } else
696 os_snprintf(buf, buflen, "%s/simpleenroll", url);
697 wpa_printf(MSG_INFO, "EST simpleenroll URL: %s", buf);
698 write_summary(ctx, "EST simpleenroll URL: %s", buf);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700699 ctx->no_osu_cert_validation = 1;
700 http_ocsp_set(ctx->http, 1);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700701 resp = http_post(ctx->http, buf, req, "application/pkcs10",
702 "Content-Transfer-Encoding: base64",
703 ctx->ca_fname, user, pw, client_cert, client_key,
704 &resp_len);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700705 http_ocsp_set(ctx->http,
706 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
707 ctx->no_osu_cert_validation = 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700708 os_free(buf);
709 if (resp == NULL) {
710 wpa_printf(MSG_INFO, "EST certificate enrollment failed");
711 write_result(ctx, "EST certificate enrollment failed");
712 return -1;
713 }
714 wpa_printf(MSG_DEBUG, "EST simpleenroll response: %s", resp);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700715
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800716 pkcs7 = base64_decode(resp, resp_len, &pkcs7_len);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700717 if (pkcs7 == NULL) {
718 wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
719 pkcs7 = os_malloc(resp_len);
720 if (pkcs7) {
721 os_memcpy(pkcs7, resp, resp_len);
722 pkcs7_len = resp_len;
723 }
724 }
725 os_free(resp);
726
727 if (pkcs7 == NULL) {
728 wpa_printf(MSG_INFO, "Failed to parse simpleenroll base64 response");
729 write_result(ctx, "Failed to parse EST simpleenroll base64 response");
730 return -1;
731 }
732
733 res = pkcs7_to_cert(ctx, pkcs7, pkcs7_len, "Cert/est_cert.pem",
734 "Cert/est_cert.der");
735 os_free(pkcs7);
736
737 if (res < 0) {
738 wpa_printf(MSG_INFO, "EST: Failed to extract certificate from PKCS7 file");
739 write_result(ctx, "EST: Failed to extract certificate from EST PKCS7 file");
740 return -1;
741 }
742
743 wpa_printf(MSG_INFO, "EST simple%senroll completed successfully",
744 ctx->client_cert_present ? "re" : "");
745 write_summary(ctx, "EST simple%senroll completed successfully",
746 ctx->client_cert_present ? "re" : "");
747
748 return 0;
749}