est.c 16 KB

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