est.c 17 KB

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