blob: c3f27e1e95a2dd5fe65e3b6dec6fa8ce38f2e197 [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
Hai Shalom74f70d42019-02-11 14:42:39 -0800221DEFINE_STACK_OF(AttrOrOID)
Hai Shalom74f70d42019-02-11 14:42:39 -0800222
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700223typedef struct {
224 int type;
225 STACK_OF(AttrOrOID) *attrs;
226} CsrAttrs;
227
228ASN1_SEQUENCE(Attribute) = {
229 ASN1_SIMPLE(Attribute, type, ASN1_OBJECT),
230 ASN1_SET_OF(Attribute, values, ASN1_OBJECT)
231} ASN1_SEQUENCE_END(Attribute);
232
233ASN1_CHOICE(AttrOrOID) = {
234 ASN1_SIMPLE(AttrOrOID, d.oid, ASN1_OBJECT),
235 ASN1_SIMPLE(AttrOrOID, d.attribute, Attribute)
236} ASN1_CHOICE_END(AttrOrOID);
237
238ASN1_CHOICE(CsrAttrs) = {
239 ASN1_SEQUENCE_OF(CsrAttrs, attrs, AttrOrOID)
240} ASN1_CHOICE_END(CsrAttrs);
241
242IMPLEMENT_ASN1_FUNCTIONS(CsrAttrs);
243
244
245static void add_csrattrs_oid(struct hs20_osu_client *ctx, ASN1_OBJECT *oid,
246 STACK_OF(X509_EXTENSION) *exts)
247{
248 char txt[100];
249 int res;
250
251 if (!oid)
252 return;
253
254 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
255 if (res < 0 || res >= (int) sizeof(txt))
256 return;
257
258 if (os_strcmp(txt, "1.2.840.113549.1.9.7") == 0) {
259 wpa_printf(MSG_INFO, "TODO: csrattr challengePassword");
260 } else if (os_strcmp(txt, "1.2.840.113549.1.1.11") == 0) {
261 wpa_printf(MSG_INFO, "csrattr sha256WithRSAEncryption");
262 } else {
263 wpa_printf(MSG_INFO, "Ignore unsupported csrattr oid %s", txt);
264 }
265}
266
267
268static void add_csrattrs_ext_req(struct hs20_osu_client *ctx,
269 STACK_OF(ASN1_OBJECT) *values,
270 STACK_OF(X509_EXTENSION) *exts)
271{
272 char txt[100];
273 int i, num, res;
274
275 num = sk_ASN1_OBJECT_num(values);
276 for (i = 0; i < num; i++) {
277 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(values, i);
278
279 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
280 if (res < 0 || res >= (int) sizeof(txt))
281 continue;
282
283 if (os_strcmp(txt, "1.3.6.1.1.1.1.22") == 0) {
284 wpa_printf(MSG_INFO, "TODO: extReq macAddress");
285 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.3") == 0) {
286 wpa_printf(MSG_INFO, "TODO: extReq imei");
287 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.4") == 0) {
288 wpa_printf(MSG_INFO, "TODO: extReq meid");
289 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.5") == 0) {
290 wpa_printf(MSG_INFO, "TODO: extReq DevId");
291 } else {
292 wpa_printf(MSG_INFO, "Ignore unsupported cstattr extensionsRequest %s",
293 txt);
294 }
295 }
296}
297
298
299static void add_csrattrs_attr(struct hs20_osu_client *ctx, Attribute *attr,
300 STACK_OF(X509_EXTENSION) *exts)
301{
302 char txt[100], txt2[100];
303 int i, num, res;
304
305 if (!attr || !attr->type || !attr->values)
306 return;
307
308 res = OBJ_obj2txt(txt, sizeof(txt), attr->type, 1);
309 if (res < 0 || res >= (int) sizeof(txt))
310 return;
311
312 if (os_strcmp(txt, "1.2.840.113549.1.9.14") == 0) {
313 add_csrattrs_ext_req(ctx, attr->values, exts);
314 return;
315 }
316
317 num = sk_ASN1_OBJECT_num(attr->values);
318 for (i = 0; i < num; i++) {
319 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(attr->values, i);
320
321 res = OBJ_obj2txt(txt2, sizeof(txt2), oid, 1);
322 if (res < 0 || res >= (int) sizeof(txt2))
323 continue;
324
325 wpa_printf(MSG_INFO, "Ignore unsupported cstattr::attr %s oid %s",
326 txt, txt2);
327 }
328}
329
330
331static void add_csrattrs(struct hs20_osu_client *ctx, CsrAttrs *csrattrs,
332 STACK_OF(X509_EXTENSION) *exts)
333{
334 int i, num;
335
336 if (!csrattrs || ! csrattrs->attrs)
337 return;
338
David Benjamin92c34502022-06-13 12:22:05 -0400339#if OPENSSL_VERSION_NUMBER >= 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -0800340 num = sk_AttrOrOID_num(csrattrs->attrs);
341#else
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700342 num = SKM_sk_num(AttrOrOID, csrattrs->attrs);
Hai Shalom74f70d42019-02-11 14:42:39 -0800343#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700344 for (i = 0; i < num; i++) {
David Benjamin92c34502022-06-13 12:22:05 -0400345#if OPENSSL_VERSION_NUMBER >= 0x10100000L
Hai Shalom74f70d42019-02-11 14:42:39 -0800346 AttrOrOID *ao = sk_AttrOrOID_value(csrattrs->attrs, i);
347#else
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700348 AttrOrOID *ao = SKM_sk_value(AttrOrOID, csrattrs->attrs, i);
Hai Shalom74f70d42019-02-11 14:42:39 -0800349#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700350 switch (ao->type) {
351 case 0:
352 add_csrattrs_oid(ctx, ao->d.oid, exts);
353 break;
354 case 1:
355 add_csrattrs_attr(ctx, ao->d.attribute, exts);
356 break;
357 }
358 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800359}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800360
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700361
362static int generate_csr(struct hs20_osu_client *ctx, char *key_pem,
363 char *csr_pem, char *est_req, char *old_cert,
364 CsrAttrs *csrattrs)
365{
366 EVP_PKEY_CTX *pctx = NULL;
367 EVP_PKEY *pkey = NULL;
368 RSA *rsa;
369 X509_REQ *req = NULL;
370 int ret = -1;
371 unsigned int val;
372 X509_NAME *subj = NULL;
373 char name[100];
374 STACK_OF(X509_EXTENSION) *exts = NULL;
375 X509_EXTENSION *ex;
376 BIO *out;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800377 CONF *ctmp = NULL;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700378
379 wpa_printf(MSG_INFO, "Generate RSA private key");
380 write_summary(ctx, "Generate RSA private key");
381 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
382 if (!pctx)
383 return -1;
384
385 if (EVP_PKEY_keygen_init(pctx) <= 0)
386 goto fail;
387
388 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, 2048) <= 0)
389 goto fail;
390
391 if (EVP_PKEY_keygen(pctx, &pkey) <= 0)
392 goto fail;
393 EVP_PKEY_CTX_free(pctx);
394 pctx = NULL;
395
396 rsa = EVP_PKEY_get1_RSA(pkey);
397 if (rsa == NULL)
398 goto fail;
399
400 if (key_pem) {
401 FILE *f = fopen(key_pem, "wb");
402 if (f == NULL)
403 goto fail;
404 if (!PEM_write_RSAPrivateKey(f, rsa, NULL, NULL, 0, NULL,
405 NULL)) {
406 wpa_printf(MSG_INFO, "Could not write private key: %s",
407 ERR_error_string(ERR_get_error(), NULL));
408 fclose(f);
409 goto fail;
410 }
411 fclose(f);
412 }
413
414 wpa_printf(MSG_INFO, "Generate CSR");
415 write_summary(ctx, "Generate CSR");
416 req = X509_REQ_new();
417 if (req == NULL)
418 goto fail;
419
420 if (old_cert) {
421 FILE *f;
422 X509 *cert;
423 int res;
424
425 f = fopen(old_cert, "r");
426 if (f == NULL)
427 goto fail;
428 cert = PEM_read_X509(f, NULL, NULL, NULL);
429 fclose(f);
430
431 if (cert == NULL)
432 goto fail;
433 res = X509_REQ_set_subject_name(req,
434 X509_get_subject_name(cert));
435 X509_free(cert);
436 if (!res)
437 goto fail;
438 } else {
439 os_get_random((u8 *) &val, sizeof(val));
440 os_snprintf(name, sizeof(name), "cert-user-%u", val);
441 subj = X509_NAME_new();
442 if (subj == NULL ||
443 !X509_NAME_add_entry_by_txt(subj, "CN", MBSTRING_ASC,
444 (unsigned char *) name,
445 -1, -1, 0) ||
446 !X509_REQ_set_subject_name(req, subj))
447 goto fail;
448 X509_NAME_free(subj);
449 subj = NULL;
450 }
451
452 if (!X509_REQ_set_pubkey(req, pkey))
453 goto fail;
454
455 exts = sk_X509_EXTENSION_new_null();
456 if (!exts)
457 goto fail;
458
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800459 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_basic_constraints,
460 "CA:FALSE");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700461 if (ex == NULL ||
462 !sk_X509_EXTENSION_push(exts, ex))
463 goto fail;
464
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800465 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_key_usage,
466 "nonRepudiation,digitalSignature,keyEncipherment");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700467 if (ex == NULL ||
468 !sk_X509_EXTENSION_push(exts, ex))
469 goto fail;
470
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800471 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_ext_key_usage,
472 "1.3.6.1.4.1.40808.1.1.2");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700473 if (ex == NULL ||
474 !sk_X509_EXTENSION_push(exts, ex))
475 goto fail;
476
477 add_csrattrs(ctx, csrattrs, exts);
478
479 if (!X509_REQ_add_extensions(req, exts))
480 goto fail;
481 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
482 exts = NULL;
483
484 if (!X509_REQ_sign(req, pkey, EVP_sha256()))
485 goto fail;
486
487 out = BIO_new(BIO_s_mem());
488 if (out) {
489 char *txt;
490 size_t rlen;
491
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800492#if !defined(ANDROID) || !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700493 X509_REQ_print(out, req);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800494#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700495 rlen = BIO_ctrl_pending(out);
496 txt = os_malloc(rlen + 1);
497 if (txt) {
498 int res = BIO_read(out, txt, rlen);
499 if (res > 0) {
500 txt[res] = '\0';
501 wpa_printf(MSG_MSGDUMP, "OpenSSL: Certificate request:\n%s",
502 txt);
503 }
504 os_free(txt);
505 }
506 BIO_free(out);
507 }
508
509 if (csr_pem) {
510 FILE *f = fopen(csr_pem, "w");
511 if (f == NULL)
512 goto fail;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800513#if !defined(ANDROID) || !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700514 X509_REQ_print_fp(f, req);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800515#endif
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700516 if (!PEM_write_X509_REQ(f, req)) {
517 fclose(f);
518 goto fail;
519 }
520 fclose(f);
521 }
522
523 if (est_req) {
524 BIO *mem = BIO_new(BIO_s_mem());
525 BUF_MEM *ptr;
526 char *pos, *end, *buf_end;
527 FILE *f;
528
529 if (mem == NULL)
530 goto fail;
531 if (!PEM_write_bio_X509_REQ(mem, req)) {
532 BIO_free(mem);
533 goto fail;
534 }
535
536 BIO_get_mem_ptr(mem, &ptr);
537 pos = ptr->data;
538 buf_end = pos + ptr->length;
539
540 /* Remove START/END lines */
541 while (pos < buf_end && *pos != '\n')
542 pos++;
543 if (pos == buf_end) {
544 BIO_free(mem);
545 goto fail;
546 }
547 pos++;
548
549 end = pos;
550 while (end < buf_end && *end != '-')
551 end++;
552
553 f = fopen(est_req, "w");
554 if (f == NULL) {
555 BIO_free(mem);
556 goto fail;
557 }
558 fwrite(pos, end - pos, 1, f);
559 fclose(f);
560
561 BIO_free(mem);
562 }
563
564 ret = 0;
565fail:
566 if (exts)
567 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
568 if (subj)
569 X509_NAME_free(subj);
570 if (req)
571 X509_REQ_free(req);
572 if (pkey)
573 EVP_PKEY_free(pkey);
574 if (pctx)
575 EVP_PKEY_CTX_free(pctx);
576 return ret;
577}
578
579
580int est_build_csr(struct hs20_osu_client *ctx, const char *url)
581{
582 char *buf;
583 size_t buflen;
584 int res;
585 char old_cert_buf[200];
586 char *old_cert = NULL;
587 CsrAttrs *csrattrs = NULL;
588
589 buflen = os_strlen(url) + 100;
590 buf = os_malloc(buflen);
591 if (buf == NULL)
592 return -1;
593
594 os_snprintf(buf, buflen, "%s/csrattrs", url);
595 wpa_printf(MSG_INFO, "Download csrattrs from %s", buf);
596 write_summary(ctx, "Download EST csrattrs from %s", buf);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700597 ctx->no_osu_cert_validation = 1;
598 http_ocsp_set(ctx->http, 1);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700599 res = http_download_file(ctx->http, buf, "Cert/est-csrattrs.txt",
600 ctx->ca_fname);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700601 http_ocsp_set(ctx->http,
602 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
603 ctx->no_osu_cert_validation = 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700604 os_free(buf);
605 if (res < 0) {
606 wpa_printf(MSG_INFO, "Failed to download EST csrattrs - assume no extra attributes are needed");
607 } else {
608 size_t resp_len;
609 char *resp;
610 unsigned char *attrs;
611 const unsigned char *pos;
612 size_t attrs_len;
613
614 resp = os_readfile("Cert/est-csrattrs.txt", &resp_len);
615 if (resp == NULL) {
616 wpa_printf(MSG_INFO, "Could not read csrattrs");
617 return -1;
618 }
619
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800620 attrs = base64_decode(resp, resp_len, &attrs_len);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700621 os_free(resp);
622
623 if (attrs == NULL) {
624 wpa_printf(MSG_INFO, "Could not base64 decode csrattrs");
625 return -1;
626 }
627 unlink("Cert/est-csrattrs.txt");
628
629 pos = attrs;
630 csrattrs = d2i_CsrAttrs(NULL, &pos, attrs_len);
631 os_free(attrs);
632 if (csrattrs == NULL) {
633 wpa_printf(MSG_INFO, "Failed to parse csrattrs ASN.1");
634 /* Continue assuming no additional requirements */
635 }
636 }
637
638 if (ctx->client_cert_present) {
639 os_snprintf(old_cert_buf, sizeof(old_cert_buf),
640 "SP/%s/client-cert.pem", ctx->fqdn);
641 old_cert = old_cert_buf;
642 }
643
644 res = generate_csr(ctx, "Cert/privkey-plain.pem", "Cert/est-req.pem",
645 "Cert/est-req.b64", old_cert, csrattrs);
646 if (csrattrs)
647 CsrAttrs_free(csrattrs);
648
649 return res;
650}
651
652
653int est_simple_enroll(struct hs20_osu_client *ctx, const char *url,
654 const char *user, const char *pw)
655{
656 char *buf, *resp, *req, *req2;
657 size_t buflen, resp_len, len, pkcs7_len;
658 unsigned char *pkcs7;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700659 char client_cert_buf[200];
660 char client_key_buf[200];
661 const char *client_cert = NULL, *client_key = NULL;
662 int res;
663
664 req = os_readfile("Cert/est-req.b64", &len);
665 if (req == NULL) {
666 wpa_printf(MSG_INFO, "Could not read Cert/req.b64");
667 return -1;
668 }
669 req2 = os_realloc(req, len + 1);
670 if (req2 == NULL) {
671 os_free(req);
672 return -1;
673 }
674 req2[len] = '\0';
675 req = req2;
676 wpa_printf(MSG_DEBUG, "EST simpleenroll request: %s", req);
677
678 buflen = os_strlen(url) + 100;
679 buf = os_malloc(buflen);
680 if (buf == NULL) {
681 os_free(req);
682 return -1;
683 }
684
685 if (ctx->client_cert_present) {
686 os_snprintf(buf, buflen, "%s/simplereenroll", url);
687 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
688 "SP/%s/client-cert.pem", ctx->fqdn);
689 client_cert = client_cert_buf;
690 os_snprintf(client_key_buf, sizeof(client_key_buf),
691 "SP/%s/client-key.pem", ctx->fqdn);
692 client_key = client_key_buf;
693 } else
694 os_snprintf(buf, buflen, "%s/simpleenroll", url);
695 wpa_printf(MSG_INFO, "EST simpleenroll URL: %s", buf);
696 write_summary(ctx, "EST simpleenroll URL: %s", buf);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700697 ctx->no_osu_cert_validation = 1;
698 http_ocsp_set(ctx->http, 1);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700699 resp = http_post(ctx->http, buf, req, "application/pkcs10",
700 "Content-Transfer-Encoding: base64",
701 ctx->ca_fname, user, pw, client_cert, client_key,
702 &resp_len);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700703 http_ocsp_set(ctx->http,
704 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
705 ctx->no_osu_cert_validation = 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700706 os_free(buf);
707 if (resp == NULL) {
708 wpa_printf(MSG_INFO, "EST certificate enrollment failed");
709 write_result(ctx, "EST certificate enrollment failed");
710 return -1;
711 }
712 wpa_printf(MSG_DEBUG, "EST simpleenroll response: %s", resp);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700713
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800714 pkcs7 = base64_decode(resp, resp_len, &pkcs7_len);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700715 if (pkcs7 == NULL) {
716 wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
717 pkcs7 = os_malloc(resp_len);
718 if (pkcs7) {
719 os_memcpy(pkcs7, resp, resp_len);
720 pkcs7_len = resp_len;
721 }
722 }
723 os_free(resp);
724
725 if (pkcs7 == NULL) {
726 wpa_printf(MSG_INFO, "Failed to parse simpleenroll base64 response");
727 write_result(ctx, "Failed to parse EST simpleenroll base64 response");
728 return -1;
729 }
730
731 res = pkcs7_to_cert(ctx, pkcs7, pkcs7_len, "Cert/est_cert.pem",
732 "Cert/est_cert.der");
733 os_free(pkcs7);
734
735 if (res < 0) {
736 wpa_printf(MSG_INFO, "EST: Failed to extract certificate from PKCS7 file");
737 write_result(ctx, "EST: Failed to extract certificate from EST PKCS7 file");
738 return -1;
739 }
740
741 wpa_printf(MSG_INFO, "EST simple%senroll completed successfully",
742 ctx->client_cert_present ? "re" : "");
743 write_summary(ctx, "EST simple%senroll completed successfully",
744 ctx->client_cert_present ? "re" : "");
745
746 return 0;
747}