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