2 * Hotspot 2.0 OSU client - EST client
3 * Copyright (c) 2012-2014, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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>
19 #include <openssl/opensslv.h>
20 #ifdef OPENSSL_IS_BORINGSSL
21 #include <openssl/buf.h>
22 #endif /* OPENSSL_IS_BORINGSSL */
25 #include "utils/base64.h"
26 #include "utils/xml-utils.h"
27 #include "utils/http-utils.h"
28 #include "osu_client.h"
31 static int pkcs7_to_cert(struct hs20_osu_client *ctx, const u8 *pkcs7,
32 size_t len, char *pem_file, char *der_file)
34 #ifdef OPENSSL_IS_BORINGSSL
36 #else /* OPENSSL_IS_BORINGSSL */
38 const unsigned char *p = pkcs7;
39 #endif /* OPENSSL_IS_BORINGSSL */
40 STACK_OF(X509) *certs;
44 #ifdef OPENSSL_IS_BORINGSSL
45 certs = sk_X509_new_null();
48 CBS_init(&pkcs7_cbs, pkcs7, len);
49 if (!PKCS7_get_certificates(certs, &pkcs7_cbs)) {
50 wpa_printf(MSG_INFO, "Could not parse PKCS#7 object: %s",
51 ERR_error_string(ERR_get_error(), NULL));
52 write_result(ctx, "Could not parse PKCS#7 object from EST");
55 #else /* OPENSSL_IS_BORINGSSL */
56 p7 = d2i_PKCS7(NULL, &p, len);
58 wpa_printf(MSG_INFO, "Could not parse PKCS#7 object: %s",
59 ERR_error_string(ERR_get_error(), NULL));
60 write_result(ctx, "Could not parse PKCS#7 object from EST");
64 switch (OBJ_obj2nid(p7->type)) {
65 case NID_pkcs7_signed:
66 certs = p7->d.sign->cert;
68 case NID_pkcs7_signedAndEnveloped:
69 certs = p7->d.signed_and_enveloped->cert;
75 #endif /* OPENSSL_IS_BORINGSSL */
77 if (!certs || ((num = sk_X509_num(certs)) == 0)) {
78 wpa_printf(MSG_INFO, "No certificates found in PKCS#7 object");
79 write_result(ctx, "No certificates found in PKCS#7 object");
84 FILE *f = fopen(der_file, "wb");
87 i2d_X509_fp(f, sk_X509_value(certs, 0));
92 out = BIO_new(BIO_s_file());
94 BIO_write_filename(out, pem_file) <= 0)
97 for (i = 0; i < num; i++) {
98 X509 *cert = sk_X509_value(certs, i);
99 X509_print(out, cert);
100 PEM_write_bio_X509(out, cert);
108 #ifdef OPENSSL_IS_BORINGSSL
110 sk_X509_pop_free(certs, X509_free);
111 #else /* OPENSSL_IS_BORINGSSL */
113 #endif /* OPENSSL_IS_BORINGSSL */
121 int est_load_cacerts(struct hs20_osu_client *ctx, const char *url)
125 unsigned char *pkcs7;
126 size_t pkcs7_len, resp_len;
129 buflen = os_strlen(url) + 100;
130 buf = os_malloc(buflen);
134 os_snprintf(buf, buflen, "%s/cacerts", url);
135 wpa_printf(MSG_INFO, "Download EST cacerts from %s", buf);
136 write_summary(ctx, "Download EST cacerts from %s", buf);
137 ctx->no_osu_cert_validation = 1;
138 http_ocsp_set(ctx->http, 1);
139 res = http_download_file(ctx->http, buf, "Cert/est-cacerts.txt",
141 http_ocsp_set(ctx->http,
142 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
143 ctx->no_osu_cert_validation = 0;
145 wpa_printf(MSG_INFO, "Failed to download EST cacerts from %s",
147 write_result(ctx, "Failed to download EST cacerts from %s",
154 resp = os_readfile("Cert/est-cacerts.txt", &resp_len);
156 wpa_printf(MSG_INFO, "Could not read Cert/est-cacerts.txt");
157 write_result(ctx, "Could not read EST cacerts");
161 pkcs7 = base64_decode((unsigned char *) resp, resp_len, &pkcs7_len);
162 if (pkcs7 && pkcs7_len < resp_len / 2) {
163 wpa_printf(MSG_INFO, "Too short base64 decode (%u bytes; downloaded %u bytes) - assume this was binary",
164 (unsigned int) pkcs7_len, (unsigned int) resp_len);
169 wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
170 pkcs7 = os_malloc(resp_len);
172 os_memcpy(pkcs7, resp, resp_len);
173 pkcs7_len = resp_len;
179 wpa_printf(MSG_INFO, "Could not fetch PKCS7 cacerts");
180 write_result(ctx, "Could not fetch EST PKCS#7 cacerts");
184 res = pkcs7_to_cert(ctx, pkcs7, pkcs7_len, "Cert/est-cacerts.pem",
188 wpa_printf(MSG_INFO, "Could not parse CA certs from PKCS#7 cacerts response");
189 write_result(ctx, "Could not parse CA certs from EST PKCS#7 cacerts response");
192 unlink("Cert/est-cacerts.txt");
199 * CsrAttrs ::= SEQUENCE SIZE (0..MAX) OF AttrOrOID
201 * AttrOrOID ::= CHOICE {
202 * oid OBJECT IDENTIFIER,
203 * attribute Attribute }
205 * Attribute ::= SEQUENCE {
206 * type OBJECT IDENTIFIER,
207 * values SET SIZE(1..MAX) OF OBJECT IDENTIFIER }
212 STACK_OF(ASN1_OBJECT) *values;
219 Attribute *attribute;
223 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
224 DEFINE_STACK_OF(AttrOrOID)
229 STACK_OF(AttrOrOID) *attrs;
232 ASN1_SEQUENCE(Attribute) = {
233 ASN1_SIMPLE(Attribute, type, ASN1_OBJECT),
234 ASN1_SET_OF(Attribute, values, ASN1_OBJECT)
235 } ASN1_SEQUENCE_END(Attribute);
237 ASN1_CHOICE(AttrOrOID) = {
238 ASN1_SIMPLE(AttrOrOID, d.oid, ASN1_OBJECT),
239 ASN1_SIMPLE(AttrOrOID, d.attribute, Attribute)
240 } ASN1_CHOICE_END(AttrOrOID);
242 ASN1_CHOICE(CsrAttrs) = {
243 ASN1_SEQUENCE_OF(CsrAttrs, attrs, AttrOrOID)
244 } ASN1_CHOICE_END(CsrAttrs);
246 IMPLEMENT_ASN1_FUNCTIONS(CsrAttrs);
249 static void add_csrattrs_oid(struct hs20_osu_client *ctx, ASN1_OBJECT *oid,
250 STACK_OF(X509_EXTENSION) *exts)
258 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
259 if (res < 0 || res >= (int) sizeof(txt))
262 if (os_strcmp(txt, "1.2.840.113549.1.9.7") == 0) {
263 wpa_printf(MSG_INFO, "TODO: csrattr challengePassword");
264 } else if (os_strcmp(txt, "1.2.840.113549.1.1.11") == 0) {
265 wpa_printf(MSG_INFO, "csrattr sha256WithRSAEncryption");
267 wpa_printf(MSG_INFO, "Ignore unsupported csrattr oid %s", txt);
272 static void add_csrattrs_ext_req(struct hs20_osu_client *ctx,
273 STACK_OF(ASN1_OBJECT) *values,
274 STACK_OF(X509_EXTENSION) *exts)
279 num = sk_ASN1_OBJECT_num(values);
280 for (i = 0; i < num; i++) {
281 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(values, i);
283 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
284 if (res < 0 || res >= (int) sizeof(txt))
287 if (os_strcmp(txt, "1.3.6.1.1.1.1.22") == 0) {
288 wpa_printf(MSG_INFO, "TODO: extReq macAddress");
289 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.3") == 0) {
290 wpa_printf(MSG_INFO, "TODO: extReq imei");
291 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.4") == 0) {
292 wpa_printf(MSG_INFO, "TODO: extReq meid");
293 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.5") == 0) {
294 wpa_printf(MSG_INFO, "TODO: extReq DevId");
296 wpa_printf(MSG_INFO, "Ignore unsupported cstattr extensionsRequest %s",
303 static void add_csrattrs_attr(struct hs20_osu_client *ctx, Attribute *attr,
304 STACK_OF(X509_EXTENSION) *exts)
306 char txt[100], txt2[100];
309 if (!attr || !attr->type || !attr->values)
312 res = OBJ_obj2txt(txt, sizeof(txt), attr->type, 1);
313 if (res < 0 || res >= (int) sizeof(txt))
316 if (os_strcmp(txt, "1.2.840.113549.1.9.14") == 0) {
317 add_csrattrs_ext_req(ctx, attr->values, exts);
321 num = sk_ASN1_OBJECT_num(attr->values);
322 for (i = 0; i < num; i++) {
323 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(attr->values, i);
325 res = OBJ_obj2txt(txt2, sizeof(txt2), oid, 1);
326 if (res < 0 || res >= (int) sizeof(txt2))
329 wpa_printf(MSG_INFO, "Ignore unsupported cstattr::attr %s oid %s",
335 static void add_csrattrs(struct hs20_osu_client *ctx, CsrAttrs *csrattrs,
336 STACK_OF(X509_EXTENSION) *exts)
340 if (!csrattrs || ! csrattrs->attrs)
343 #ifdef OPENSSL_IS_BORINGSSL
344 num = sk_num(CHECKED_CAST(_STACK *, STACK_OF(AttrOrOID) *,
346 for (i = 0; i < num; i++) {
347 AttrOrOID *ao = sk_value(
348 CHECKED_CAST(_STACK *, const STACK_OF(AttrOrOID) *,
349 csrattrs->attrs), i);
352 add_csrattrs_oid(ctx, ao->d.oid, exts);
355 add_csrattrs_attr(ctx, ao->d.attribute, exts);
359 #else /* OPENSSL_IS_BORINGSSL */
360 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
361 num = sk_AttrOrOID_num(csrattrs->attrs);
363 num = SKM_sk_num(AttrOrOID, csrattrs->attrs);
365 for (i = 0; i < num; i++) {
366 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL)
367 AttrOrOID *ao = sk_AttrOrOID_value(csrattrs->attrs, i);
369 AttrOrOID *ao = SKM_sk_value(AttrOrOID, csrattrs->attrs, i);
373 add_csrattrs_oid(ctx, ao->d.oid, exts);
376 add_csrattrs_attr(ctx, ao->d.attribute, exts);
380 #endif /* OPENSSL_IS_BORINGSSL */
384 static int generate_csr(struct hs20_osu_client *ctx, char *key_pem,
385 char *csr_pem, char *est_req, char *old_cert,
388 EVP_PKEY_CTX *pctx = NULL;
389 EVP_PKEY *pkey = NULL;
391 X509_REQ *req = NULL;
394 X509_NAME *subj = NULL;
396 STACK_OF(X509_EXTENSION) *exts = NULL;
401 wpa_printf(MSG_INFO, "Generate RSA private key");
402 write_summary(ctx, "Generate RSA private key");
403 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
407 if (EVP_PKEY_keygen_init(pctx) <= 0)
410 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, 2048) <= 0)
413 if (EVP_PKEY_keygen(pctx, &pkey) <= 0)
415 EVP_PKEY_CTX_free(pctx);
418 rsa = EVP_PKEY_get1_RSA(pkey);
423 FILE *f = fopen(key_pem, "wb");
426 if (!PEM_write_RSAPrivateKey(f, rsa, NULL, NULL, 0, NULL,
428 wpa_printf(MSG_INFO, "Could not write private key: %s",
429 ERR_error_string(ERR_get_error(), NULL));
436 wpa_printf(MSG_INFO, "Generate CSR");
437 write_summary(ctx, "Generate CSR");
438 req = X509_REQ_new();
447 f = fopen(old_cert, "r");
450 cert = PEM_read_X509(f, NULL, NULL, NULL);
455 res = X509_REQ_set_subject_name(req,
456 X509_get_subject_name(cert));
461 os_get_random((u8 *) &val, sizeof(val));
462 os_snprintf(name, sizeof(name), "cert-user-%u", val);
463 subj = X509_NAME_new();
465 !X509_NAME_add_entry_by_txt(subj, "CN", MBSTRING_ASC,
466 (unsigned char *) name,
468 !X509_REQ_set_subject_name(req, subj))
470 X509_NAME_free(subj);
474 if (!X509_REQ_set_pubkey(req, pkey))
477 exts = sk_X509_EXTENSION_new_null();
481 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_basic_constraints,
484 !sk_X509_EXTENSION_push(exts, ex))
487 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_key_usage,
488 "nonRepudiation,digitalSignature,keyEncipherment");
490 !sk_X509_EXTENSION_push(exts, ex))
493 ex = X509V3_EXT_nconf_nid(ctmp, NULL, NID_ext_key_usage,
494 "1.3.6.1.4.1.40808.1.1.2");
496 !sk_X509_EXTENSION_push(exts, ex))
499 add_csrattrs(ctx, csrattrs, exts);
501 if (!X509_REQ_add_extensions(req, exts))
503 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
506 if (!X509_REQ_sign(req, pkey, EVP_sha256()))
509 out = BIO_new(BIO_s_mem());
514 #if !defined(ANDROID) || !defined(OPENSSL_IS_BORINGSSL)
515 X509_REQ_print(out, req);
517 rlen = BIO_ctrl_pending(out);
518 txt = os_malloc(rlen + 1);
520 int res = BIO_read(out, txt, rlen);
523 wpa_printf(MSG_MSGDUMP, "OpenSSL: Certificate request:\n%s",
532 FILE *f = fopen(csr_pem, "w");
535 #if !defined(ANDROID) || !defined(OPENSSL_IS_BORINGSSL)
536 X509_REQ_print_fp(f, req);
538 if (!PEM_write_X509_REQ(f, req)) {
546 BIO *mem = BIO_new(BIO_s_mem());
548 char *pos, *end, *buf_end;
553 if (!PEM_write_bio_X509_REQ(mem, req)) {
558 BIO_get_mem_ptr(mem, &ptr);
560 buf_end = pos + ptr->length;
562 /* Remove START/END lines */
563 while (pos < buf_end && *pos != '\n')
565 if (pos == buf_end) {
572 while (end < buf_end && *end != '-')
575 f = fopen(est_req, "w");
580 fwrite(pos, end - pos, 1, f);
589 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
591 X509_NAME_free(subj);
597 EVP_PKEY_CTX_free(pctx);
602 int est_build_csr(struct hs20_osu_client *ctx, const char *url)
607 char old_cert_buf[200];
608 char *old_cert = NULL;
609 CsrAttrs *csrattrs = NULL;
611 buflen = os_strlen(url) + 100;
612 buf = os_malloc(buflen);
616 os_snprintf(buf, buflen, "%s/csrattrs", url);
617 wpa_printf(MSG_INFO, "Download csrattrs from %s", buf);
618 write_summary(ctx, "Download EST csrattrs from %s", buf);
619 ctx->no_osu_cert_validation = 1;
620 http_ocsp_set(ctx->http, 1);
621 res = http_download_file(ctx->http, buf, "Cert/est-csrattrs.txt",
623 http_ocsp_set(ctx->http,
624 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
625 ctx->no_osu_cert_validation = 0;
628 wpa_printf(MSG_INFO, "Failed to download EST csrattrs - assume no extra attributes are needed");
632 unsigned char *attrs;
633 const unsigned char *pos;
636 resp = os_readfile("Cert/est-csrattrs.txt", &resp_len);
638 wpa_printf(MSG_INFO, "Could not read csrattrs");
642 attrs = base64_decode((unsigned char *) resp, resp_len,
647 wpa_printf(MSG_INFO, "Could not base64 decode csrattrs");
650 unlink("Cert/est-csrattrs.txt");
653 csrattrs = d2i_CsrAttrs(NULL, &pos, attrs_len);
655 if (csrattrs == NULL) {
656 wpa_printf(MSG_INFO, "Failed to parse csrattrs ASN.1");
657 /* Continue assuming no additional requirements */
661 if (ctx->client_cert_present) {
662 os_snprintf(old_cert_buf, sizeof(old_cert_buf),
663 "SP/%s/client-cert.pem", ctx->fqdn);
664 old_cert = old_cert_buf;
667 res = generate_csr(ctx, "Cert/privkey-plain.pem", "Cert/est-req.pem",
668 "Cert/est-req.b64", old_cert, csrattrs);
670 CsrAttrs_free(csrattrs);
676 int est_simple_enroll(struct hs20_osu_client *ctx, const char *url,
677 const char *user, const char *pw)
679 char *buf, *resp, *req, *req2;
680 size_t buflen, resp_len, len, pkcs7_len;
681 unsigned char *pkcs7;
682 char client_cert_buf[200];
683 char client_key_buf[200];
684 const char *client_cert = NULL, *client_key = NULL;
687 req = os_readfile("Cert/est-req.b64", &len);
689 wpa_printf(MSG_INFO, "Could not read Cert/req.b64");
692 req2 = os_realloc(req, len + 1);
699 wpa_printf(MSG_DEBUG, "EST simpleenroll request: %s", req);
701 buflen = os_strlen(url) + 100;
702 buf = os_malloc(buflen);
708 if (ctx->client_cert_present) {
709 os_snprintf(buf, buflen, "%s/simplereenroll", url);
710 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
711 "SP/%s/client-cert.pem", ctx->fqdn);
712 client_cert = client_cert_buf;
713 os_snprintf(client_key_buf, sizeof(client_key_buf),
714 "SP/%s/client-key.pem", ctx->fqdn);
715 client_key = client_key_buf;
717 os_snprintf(buf, buflen, "%s/simpleenroll", url);
718 wpa_printf(MSG_INFO, "EST simpleenroll URL: %s", buf);
719 write_summary(ctx, "EST simpleenroll URL: %s", buf);
720 ctx->no_osu_cert_validation = 1;
721 http_ocsp_set(ctx->http, 1);
722 resp = http_post(ctx->http, buf, req, "application/pkcs10",
723 "Content-Transfer-Encoding: base64",
724 ctx->ca_fname, user, pw, client_cert, client_key,
726 http_ocsp_set(ctx->http,
727 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
728 ctx->no_osu_cert_validation = 0;
731 wpa_printf(MSG_INFO, "EST certificate enrollment failed");
732 write_result(ctx, "EST certificate enrollment failed");
735 wpa_printf(MSG_DEBUG, "EST simpleenroll response: %s", resp);
737 pkcs7 = base64_decode((unsigned char *) resp, resp_len, &pkcs7_len);
739 wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
740 pkcs7 = os_malloc(resp_len);
742 os_memcpy(pkcs7, resp, resp_len);
743 pkcs7_len = resp_len;
749 wpa_printf(MSG_INFO, "Failed to parse simpleenroll base64 response");
750 write_result(ctx, "Failed to parse EST simpleenroll base64 response");
754 res = pkcs7_to_cert(ctx, pkcs7, pkcs7_len, "Cert/est_cert.pem",
755 "Cert/est_cert.der");
759 wpa_printf(MSG_INFO, "EST: Failed to extract certificate from PKCS7 file");
760 write_result(ctx, "EST: Failed to extract certificate from EST PKCS7 file");
764 wpa_printf(MSG_INFO, "EST simple%senroll completed successfully",
765 ctx->client_cert_present ? "re" : "");
766 write_summary(ctx, "EST simple%senroll completed successfully",
767 ctx->client_cert_present ? "re" : "");