est.c 18 KB

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