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>
21 #include "utils/base64.h"
22 #include "utils/xml-utils.h"
23 #include "utils/http-utils.h"
24 #include "osu_client.h"
27 static int pkcs7_to_cert(struct hs20_osu_client *ctx, const u8 *pkcs7,
28 size_t len, char *pem_file, char *der_file)
31 const unsigned char *p = pkcs7;
32 STACK_OF(X509) *certs;
36 p7 = d2i_PKCS7(NULL, &p, len);
38 wpa_printf(MSG_INFO, "Could not parse PKCS#7 object: %s",
39 ERR_error_string(ERR_get_error(), NULL));
40 write_result(ctx, "Could not parse PKCS#7 object from EST");
44 switch (OBJ_obj2nid(p7->type)) {
45 case NID_pkcs7_signed:
46 certs = p7->d.sign->cert;
48 case NID_pkcs7_signedAndEnveloped:
49 certs = p7->d.signed_and_enveloped->cert;
56 if (!certs || ((num = sk_X509_num(certs)) == 0)) {
57 wpa_printf(MSG_INFO, "No certificates found in PKCS#7 object");
58 write_result(ctx, "No certificates found in PKCS#7 object");
63 FILE *f = fopen(der_file, "wb");
66 i2d_X509_fp(f, sk_X509_value(certs, 0));
71 out = BIO_new(BIO_s_file());
73 BIO_write_filename(out, pem_file) <= 0)
76 for (i = 0; i < num; i++) {
77 X509 *cert = sk_X509_value(certs, i);
78 X509_print(out, cert);
79 PEM_write_bio_X509(out, cert);
95 int est_load_cacerts(struct hs20_osu_client *ctx, const char *url)
100 size_t pkcs7_len, resp_len;
103 buflen = os_strlen(url) + 100;
104 buf = os_malloc(buflen);
108 os_snprintf(buf, buflen, "%s/cacerts", url);
109 wpa_printf(MSG_INFO, "Download EST cacerts from %s", buf);
110 write_summary(ctx, "Download EST cacerts from %s", buf);
111 ctx->no_osu_cert_validation = 1;
112 http_ocsp_set(ctx->http, 1);
113 res = http_download_file(ctx->http, buf, "Cert/est-cacerts.txt",
115 http_ocsp_set(ctx->http,
116 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
117 ctx->no_osu_cert_validation = 0;
119 wpa_printf(MSG_INFO, "Failed to download EST cacerts from %s",
121 write_result(ctx, "Failed to download EST cacerts from %s",
128 resp = os_readfile("Cert/est-cacerts.txt", &resp_len);
130 wpa_printf(MSG_INFO, "Could not read Cert/est-cacerts.txt");
131 write_result(ctx, "Could not read EST cacerts");
135 pkcs7 = base64_decode((unsigned char *) resp, resp_len, &pkcs7_len);
136 if (pkcs7 && pkcs7_len < resp_len / 2) {
137 wpa_printf(MSG_INFO, "Too short base64 decode (%u bytes; downloaded %u bytes) - assume this was binary",
138 (unsigned int) pkcs7_len, (unsigned int) resp_len);
143 wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
144 pkcs7 = os_malloc(resp_len);
146 os_memcpy(pkcs7, resp, resp_len);
147 pkcs7_len = resp_len;
153 wpa_printf(MSG_INFO, "Could not fetch PKCS7 cacerts");
154 write_result(ctx, "Could not fetch EST PKCS#7 cacerts");
158 res = pkcs7_to_cert(ctx, pkcs7, pkcs7_len, "Cert/est-cacerts.pem",
162 wpa_printf(MSG_INFO, "Could not parse CA certs from PKCS#7 cacerts response");
163 write_result(ctx, "Could not parse CA certs from EST PKCS#7 cacerts response");
166 unlink("Cert/est-cacerts.txt");
173 * CsrAttrs ::= SEQUENCE SIZE (0..MAX) OF AttrOrOID
175 * AttrOrOID ::= CHOICE {
176 * oid OBJECT IDENTIFIER,
177 * attribute Attribute }
179 * Attribute ::= SEQUENCE {
180 * type OBJECT IDENTIFIER,
181 * values SET SIZE(1..MAX) OF OBJECT IDENTIFIER }
186 STACK_OF(ASN1_OBJECT) *values;
193 Attribute *attribute;
199 STACK_OF(AttrOrOID) *attrs;
202 ASN1_SEQUENCE(Attribute) = {
203 ASN1_SIMPLE(Attribute, type, ASN1_OBJECT),
204 ASN1_SET_OF(Attribute, values, ASN1_OBJECT)
205 } ASN1_SEQUENCE_END(Attribute);
207 ASN1_CHOICE(AttrOrOID) = {
208 ASN1_SIMPLE(AttrOrOID, d.oid, ASN1_OBJECT),
209 ASN1_SIMPLE(AttrOrOID, d.attribute, Attribute)
210 } ASN1_CHOICE_END(AttrOrOID);
212 ASN1_CHOICE(CsrAttrs) = {
213 ASN1_SEQUENCE_OF(CsrAttrs, attrs, AttrOrOID)
214 } ASN1_CHOICE_END(CsrAttrs);
216 IMPLEMENT_ASN1_FUNCTIONS(CsrAttrs);
219 static void add_csrattrs_oid(struct hs20_osu_client *ctx, ASN1_OBJECT *oid,
220 STACK_OF(X509_EXTENSION) *exts)
228 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
229 if (res < 0 || res >= (int) sizeof(txt))
232 if (os_strcmp(txt, "1.2.840.113549.1.9.7") == 0) {
233 wpa_printf(MSG_INFO, "TODO: csrattr challengePassword");
234 } else if (os_strcmp(txt, "1.2.840.113549.1.1.11") == 0) {
235 wpa_printf(MSG_INFO, "csrattr sha256WithRSAEncryption");
237 wpa_printf(MSG_INFO, "Ignore unsupported csrattr oid %s", txt);
242 static void add_csrattrs_ext_req(struct hs20_osu_client *ctx,
243 STACK_OF(ASN1_OBJECT) *values,
244 STACK_OF(X509_EXTENSION) *exts)
249 num = sk_ASN1_OBJECT_num(values);
250 for (i = 0; i < num; i++) {
251 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(values, i);
253 res = OBJ_obj2txt(txt, sizeof(txt), oid, 1);
254 if (res < 0 || res >= (int) sizeof(txt))
257 if (os_strcmp(txt, "1.3.6.1.1.1.1.22") == 0) {
258 wpa_printf(MSG_INFO, "TODO: extReq macAddress");
259 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.3") == 0) {
260 wpa_printf(MSG_INFO, "TODO: extReq imei");
261 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.4") == 0) {
262 wpa_printf(MSG_INFO, "TODO: extReq meid");
263 } else if (os_strcmp(txt, "1.3.6.1.4.1.40808.1.1.5") == 0) {
264 wpa_printf(MSG_INFO, "TODO: extReq DevId");
266 wpa_printf(MSG_INFO, "Ignore unsupported cstattr extensionsRequest %s",
273 static void add_csrattrs_attr(struct hs20_osu_client *ctx, Attribute *attr,
274 STACK_OF(X509_EXTENSION) *exts)
276 char txt[100], txt2[100];
279 if (!attr || !attr->type || !attr->values)
282 res = OBJ_obj2txt(txt, sizeof(txt), attr->type, 1);
283 if (res < 0 || res >= (int) sizeof(txt))
286 if (os_strcmp(txt, "1.2.840.113549.1.9.14") == 0) {
287 add_csrattrs_ext_req(ctx, attr->values, exts);
291 num = sk_ASN1_OBJECT_num(attr->values);
292 for (i = 0; i < num; i++) {
293 ASN1_OBJECT *oid = sk_ASN1_OBJECT_value(attr->values, i);
295 res = OBJ_obj2txt(txt2, sizeof(txt2), oid, 1);
296 if (res < 0 || res >= (int) sizeof(txt2))
299 wpa_printf(MSG_INFO, "Ignore unsupported cstattr::attr %s oid %s",
305 static void add_csrattrs(struct hs20_osu_client *ctx, CsrAttrs *csrattrs,
306 STACK_OF(X509_EXTENSION) *exts)
310 if (!csrattrs || ! csrattrs->attrs)
313 num = SKM_sk_num(AttrOrOID, csrattrs->attrs);
314 for (i = 0; i < num; i++) {
315 AttrOrOID *ao = SKM_sk_value(AttrOrOID, csrattrs->attrs, i);
318 add_csrattrs_oid(ctx, ao->d.oid, exts);
321 add_csrattrs_attr(ctx, ao->d.attribute, exts);
328 static int generate_csr(struct hs20_osu_client *ctx, char *key_pem,
329 char *csr_pem, char *est_req, char *old_cert,
332 EVP_PKEY_CTX *pctx = NULL;
333 EVP_PKEY *pkey = NULL;
335 X509_REQ *req = NULL;
338 X509_NAME *subj = NULL;
340 STACK_OF(X509_EXTENSION) *exts = NULL;
344 wpa_printf(MSG_INFO, "Generate RSA private key");
345 write_summary(ctx, "Generate RSA private key");
346 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
350 if (EVP_PKEY_keygen_init(pctx) <= 0)
353 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, 2048) <= 0)
356 if (EVP_PKEY_keygen(pctx, &pkey) <= 0)
358 EVP_PKEY_CTX_free(pctx);
361 rsa = EVP_PKEY_get1_RSA(pkey);
366 FILE *f = fopen(key_pem, "wb");
369 if (!PEM_write_RSAPrivateKey(f, rsa, NULL, NULL, 0, NULL,
371 wpa_printf(MSG_INFO, "Could not write private key: %s",
372 ERR_error_string(ERR_get_error(), NULL));
379 wpa_printf(MSG_INFO, "Generate CSR");
380 write_summary(ctx, "Generate CSR");
381 req = X509_REQ_new();
390 f = fopen(old_cert, "r");
393 cert = PEM_read_X509(f, NULL, NULL, NULL);
398 res = X509_REQ_set_subject_name(req,
399 X509_get_subject_name(cert));
404 os_get_random((u8 *) &val, sizeof(val));
405 os_snprintf(name, sizeof(name), "cert-user-%u", val);
406 subj = X509_NAME_new();
408 !X509_NAME_add_entry_by_txt(subj, "CN", MBSTRING_ASC,
409 (unsigned char *) name,
411 !X509_REQ_set_subject_name(req, subj))
413 X509_NAME_free(subj);
417 if (!X509_REQ_set_pubkey(req, pkey))
420 exts = sk_X509_EXTENSION_new_null();
424 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints,
427 !sk_X509_EXTENSION_push(exts, ex))
430 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_key_usage,
431 "nonRepudiation,digitalSignature,keyEncipherment");
433 !sk_X509_EXTENSION_push(exts, ex))
436 ex = X509V3_EXT_conf_nid(NULL, NULL, NID_ext_key_usage,
437 "1.3.6.1.4.1.40808.1.1.2");
439 !sk_X509_EXTENSION_push(exts, ex))
442 add_csrattrs(ctx, csrattrs, exts);
444 if (!X509_REQ_add_extensions(req, exts))
446 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
449 if (!X509_REQ_sign(req, pkey, EVP_sha256()))
452 out = BIO_new(BIO_s_mem());
457 X509_REQ_print(out, req);
458 rlen = BIO_ctrl_pending(out);
459 txt = os_malloc(rlen + 1);
461 int res = BIO_read(out, txt, rlen);
464 wpa_printf(MSG_MSGDUMP, "OpenSSL: Certificate request:\n%s",
473 FILE *f = fopen(csr_pem, "w");
476 X509_REQ_print_fp(f, req);
477 if (!PEM_write_X509_REQ(f, req)) {
485 BIO *mem = BIO_new(BIO_s_mem());
487 char *pos, *end, *buf_end;
492 if (!PEM_write_bio_X509_REQ(mem, req)) {
497 BIO_get_mem_ptr(mem, &ptr);
499 buf_end = pos + ptr->length;
501 /* Remove START/END lines */
502 while (pos < buf_end && *pos != '\n')
504 if (pos == buf_end) {
511 while (end < buf_end && *end != '-')
514 f = fopen(est_req, "w");
519 fwrite(pos, end - pos, 1, f);
528 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
530 X509_NAME_free(subj);
536 EVP_PKEY_CTX_free(pctx);
541 int est_build_csr(struct hs20_osu_client *ctx, const char *url)
546 char old_cert_buf[200];
547 char *old_cert = NULL;
548 CsrAttrs *csrattrs = NULL;
550 buflen = os_strlen(url) + 100;
551 buf = os_malloc(buflen);
555 os_snprintf(buf, buflen, "%s/csrattrs", url);
556 wpa_printf(MSG_INFO, "Download csrattrs from %s", buf);
557 write_summary(ctx, "Download EST csrattrs from %s", buf);
558 ctx->no_osu_cert_validation = 1;
559 http_ocsp_set(ctx->http, 1);
560 res = http_download_file(ctx->http, buf, "Cert/est-csrattrs.txt",
562 http_ocsp_set(ctx->http,
563 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
564 ctx->no_osu_cert_validation = 0;
567 wpa_printf(MSG_INFO, "Failed to download EST csrattrs - assume no extra attributes are needed");
571 unsigned char *attrs;
572 const unsigned char *pos;
575 resp = os_readfile("Cert/est-csrattrs.txt", &resp_len);
577 wpa_printf(MSG_INFO, "Could not read csrattrs");
581 attrs = base64_decode((unsigned char *) resp, resp_len,
586 wpa_printf(MSG_INFO, "Could not base64 decode csrattrs");
589 unlink("Cert/est-csrattrs.txt");
592 csrattrs = d2i_CsrAttrs(NULL, &pos, attrs_len);
594 if (csrattrs == NULL) {
595 wpa_printf(MSG_INFO, "Failed to parse csrattrs ASN.1");
596 /* Continue assuming no additional requirements */
600 if (ctx->client_cert_present) {
601 os_snprintf(old_cert_buf, sizeof(old_cert_buf),
602 "SP/%s/client-cert.pem", ctx->fqdn);
603 old_cert = old_cert_buf;
606 res = generate_csr(ctx, "Cert/privkey-plain.pem", "Cert/est-req.pem",
607 "Cert/est-req.b64", old_cert, csrattrs);
609 CsrAttrs_free(csrattrs);
615 int est_simple_enroll(struct hs20_osu_client *ctx, const char *url,
616 const char *user, const char *pw)
618 char *buf, *resp, *req, *req2;
619 size_t buflen, resp_len, len, pkcs7_len;
620 unsigned char *pkcs7;
622 char client_cert_buf[200];
623 char client_key_buf[200];
624 const char *client_cert = NULL, *client_key = NULL;
627 req = os_readfile("Cert/est-req.b64", &len);
629 wpa_printf(MSG_INFO, "Could not read Cert/req.b64");
632 req2 = os_realloc(req, len + 1);
639 wpa_printf(MSG_DEBUG, "EST simpleenroll request: %s", req);
641 buflen = os_strlen(url) + 100;
642 buf = os_malloc(buflen);
648 if (ctx->client_cert_present) {
649 os_snprintf(buf, buflen, "%s/simplereenroll", url);
650 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
651 "SP/%s/client-cert.pem", ctx->fqdn);
652 client_cert = client_cert_buf;
653 os_snprintf(client_key_buf, sizeof(client_key_buf),
654 "SP/%s/client-key.pem", ctx->fqdn);
655 client_key = client_key_buf;
657 os_snprintf(buf, buflen, "%s/simpleenroll", url);
658 wpa_printf(MSG_INFO, "EST simpleenroll URL: %s", buf);
659 write_summary(ctx, "EST simpleenroll URL: %s", buf);
660 ctx->no_osu_cert_validation = 1;
661 http_ocsp_set(ctx->http, 1);
662 resp = http_post(ctx->http, buf, req, "application/pkcs10",
663 "Content-Transfer-Encoding: base64",
664 ctx->ca_fname, user, pw, client_cert, client_key,
666 http_ocsp_set(ctx->http,
667 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
668 ctx->no_osu_cert_validation = 0;
671 wpa_printf(MSG_INFO, "EST certificate enrollment failed");
672 write_result(ctx, "EST certificate enrollment failed");
675 wpa_printf(MSG_DEBUG, "EST simpleenroll response: %s", resp);
676 f = fopen("Cert/est-resp.raw", "w");
678 fwrite(resp, resp_len, 1, f);
682 pkcs7 = base64_decode((unsigned char *) resp, resp_len, &pkcs7_len);
684 wpa_printf(MSG_INFO, "EST workaround - Could not decode base64, assume this is DER encoded PKCS7");
685 pkcs7 = os_malloc(resp_len);
687 os_memcpy(pkcs7, resp, resp_len);
688 pkcs7_len = resp_len;
694 wpa_printf(MSG_INFO, "Failed to parse simpleenroll base64 response");
695 write_result(ctx, "Failed to parse EST simpleenroll base64 response");
699 res = pkcs7_to_cert(ctx, pkcs7, pkcs7_len, "Cert/est_cert.pem",
700 "Cert/est_cert.der");
704 wpa_printf(MSG_INFO, "EST: Failed to extract certificate from PKCS7 file");
705 write_result(ctx, "EST: Failed to extract certificate from EST PKCS7 file");
709 wpa_printf(MSG_INFO, "EST simple%senroll completed successfully",
710 ctx->client_cert_present ? "re" : "");
711 write_summary(ctx, "EST simple%senroll completed successfully",
712 ctx->client_cert_present ? "re" : "");