est.c 17 KB

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