est.c 18 KB

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