2 * HTTP wrapper for libcurl
3 * Copyright (c) 2012-2014, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #include <curl/curl.h>
11 #ifdef EAP_TLS_OPENSSL
12 #include <openssl/ssl.h>
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/x509v3.h>
17 #ifdef SSL_set_tlsext_status_type
18 #ifndef OPENSSL_NO_TLSEXT
20 #include <openssl/err.h>
21 #include <openssl/ocsp.h>
22 #endif /* OPENSSL_NO_TLSEXT */
23 #endif /* SSL_set_tlsext_status_type */
24 #endif /* EAP_TLS_OPENSSL */
27 #include "xml-utils.h"
28 #include "http-utils.h"
29 #ifdef EAP_TLS_OPENSSL
30 #include "crypto/tls_openssl.h"
31 #endif /* EAP_TLS_OPENSSL */
36 struct xml_node_ctx *xml;
38 struct curl_slist *curl_hdr;
43 char *svc_client_cert;
48 int (*cert_cb)(void *ctx, struct http_cert *cert);
52 NO_OCSP, OPTIONAL_OCSP, MANDATORY_OCSP
56 X509 *peer_issuer_issuer;
62 static void clear_curl(struct http_ctx *ctx)
65 curl_easy_cleanup(ctx->curl);
69 curl_slist_free_all(ctx->curl_hdr);
75 static void clone_str(char **dst, const char *src)
79 *dst = os_strdup(src);
85 static void debug_dump(struct http_ctx *ctx, const char *title,
86 const char *buf, size_t len)
91 for (i = 0; i < len; i++) {
92 if (buf[i] < 32 && buf[i] != '\t' && buf[i] != '\n' &&
94 wpa_hexdump_ascii(MSG_MSGDUMP, title, buf, len);
99 txt = os_malloc(len + 1);
102 os_memcpy(txt, buf, len);
106 if (txt[len] == '\n' || txt[len] == '\r')
111 wpa_printf(MSG_MSGDUMP, "%s[%s]", title, txt);
116 static int curl_cb_debug(CURL *curl, curl_infotype info, char *buf, size_t len,
119 struct http_ctx *ctx = userdata;
122 debug_dump(ctx, "CURLINFO_TEXT", buf, len);
124 case CURLINFO_HEADER_IN:
125 debug_dump(ctx, "CURLINFO_HEADER_IN", buf, len);
127 case CURLINFO_HEADER_OUT:
128 debug_dump(ctx, "CURLINFO_HEADER_OUT", buf, len);
130 case CURLINFO_DATA_IN:
131 debug_dump(ctx, "CURLINFO_DATA_IN", buf, len);
133 case CURLINFO_DATA_OUT:
134 debug_dump(ctx, "CURLINFO_DATA_OUT", buf, len);
136 case CURLINFO_SSL_DATA_IN:
137 wpa_printf(MSG_DEBUG, "debug - CURLINFO_SSL_DATA_IN - %d",
140 case CURLINFO_SSL_DATA_OUT:
141 wpa_printf(MSG_DEBUG, "debug - CURLINFO_SSL_DATA_OUT - %d",
145 wpa_printf(MSG_DEBUG, "debug - CURLINFO_END - %d",
153 static size_t curl_cb_write(void *ptr, size_t size, size_t nmemb,
156 struct http_ctx *ctx = userdata;
158 n = os_realloc(ctx->curl_buf, ctx->curl_buf_len + size * nmemb + 1);
162 os_memcpy(n + ctx->curl_buf_len, ptr, size * nmemb);
163 n[ctx->curl_buf_len + size * nmemb] = '\0';
164 ctx->curl_buf_len += size * nmemb;
169 #ifdef EAP_TLS_OPENSSL
171 static void debug_dump_cert(const char *title, X509 *cert)
177 out = BIO_new(BIO_s_mem());
181 X509_print_ex(out, cert, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
182 rlen = BIO_ctrl_pending(out);
183 txt = os_malloc(rlen + 1);
185 int res = BIO_read(out, txt, rlen);
188 wpa_printf(MSG_MSGDUMP, "%s:\n%s", title, txt);
196 static void add_alt_name_othername(struct http_ctx *ctx, struct http_cert *cert,
201 struct http_othername *on;
204 on = os_realloc_array(cert->othername, cert->num_othername + 1,
205 sizeof(struct http_othername));
208 cert->othername = on;
209 on = &on[cert->num_othername];
210 os_memset(on, 0, sizeof(*on));
212 res = OBJ_obj2txt(txt, sizeof(txt), o->type_id, 1);
213 if (res < 0 || res >= (int) sizeof(txt))
216 on->oid = os_strdup(txt);
221 on->data = val->value.octet_string->data;
222 on->len = val->value.octet_string->length;
224 cert->num_othername++;
228 static void add_alt_name_dns(struct http_ctx *ctx, struct http_cert *cert,
235 if (ASN1_STRING_to_UTF8((unsigned char **) &buf, name) < 0)
238 n = os_realloc_array(cert->dnsname, cert->num_dnsname + 1,
244 n[cert->num_dnsname] = buf;
249 static void add_alt_name(struct http_ctx *ctx, struct http_cert *cert,
250 const GENERAL_NAME *name)
252 switch (name->type) {
254 add_alt_name_othername(ctx, cert, name->d.otherName);
257 add_alt_name_dns(ctx, cert, name->d.dNSName);
263 static void add_alt_names(struct http_ctx *ctx, struct http_cert *cert,
264 GENERAL_NAMES *names)
268 num = sk_GENERAL_NAME_num(names);
269 for (i = 0; i < num; i++) {
270 const GENERAL_NAME *name;
271 name = sk_GENERAL_NAME_value(names, i);
272 add_alt_name(ctx, cert, name);
281 ASN1_OCTET_STRING *hashValue;
285 STACK_OF(HashAlgAndValue) *refStructHash;
286 STACK_OF(ASN1_IA5STRING) *refStructURI;
290 ASN1_IA5STRING *mediaType;
291 STACK_OF(HashAlgAndValue) *logotypeHash;
292 STACK_OF(ASN1_IA5STRING) *logotypeURI;
298 ASN1_INTEGER *numBits;
299 ASN1_INTEGER *tableSize;
301 } LogotypeImageResolution;
304 ASN1_INTEGER *type; /* LogotypeImageType ::= INTEGER */
305 ASN1_INTEGER *fileSize;
308 LogotypeImageResolution *resolution;
309 ASN1_IA5STRING *language;
313 LogotypeDetails *imageDetails;
314 LogotypeImageInfo *imageInfo;
318 ASN1_INTEGER *fileSize;
319 ASN1_INTEGER *playTime;
320 ASN1_INTEGER *channels;
321 ASN1_INTEGER *sampleRate;
322 ASN1_IA5STRING *language;
326 LogotypeDetails *audioDetails;
327 LogotypeAudioInfo *audioInfo;
331 STACK_OF(LogotypeImage) *image;
332 STACK_OF(LogotypeAudio) *audio;
338 LogotypeData *direct;
339 LogotypeReference *indirect;
344 ASN1_OBJECT *logotypeType;
349 STACK_OF(LogotypeInfo) *communityLogos;
350 LogotypeInfo *issuerLogo;
351 LogotypeInfo *subjectLogo;
352 STACK_OF(OtherLogotypeInfo) *otherLogos;
355 ASN1_SEQUENCE(HashAlgAndValue) = {
356 ASN1_SIMPLE(HashAlgAndValue, hashAlg, X509_ALGOR),
357 ASN1_SIMPLE(HashAlgAndValue, hashValue, ASN1_OCTET_STRING)
358 } ASN1_SEQUENCE_END(HashAlgAndValue);
360 ASN1_SEQUENCE(LogotypeReference) = {
361 ASN1_SEQUENCE_OF(LogotypeReference, refStructHash, HashAlgAndValue),
362 ASN1_SEQUENCE_OF(LogotypeReference, refStructURI, ASN1_IA5STRING)
363 } ASN1_SEQUENCE_END(LogotypeReference);
365 ASN1_SEQUENCE(LogotypeDetails) = {
366 ASN1_SIMPLE(LogotypeDetails, mediaType, ASN1_IA5STRING),
367 ASN1_SEQUENCE_OF(LogotypeDetails, logotypeHash, HashAlgAndValue),
368 ASN1_SEQUENCE_OF(LogotypeDetails, logotypeURI, ASN1_IA5STRING)
369 } ASN1_SEQUENCE_END(LogotypeDetails);
371 ASN1_CHOICE(LogotypeImageResolution) = {
372 ASN1_IMP(LogotypeImageResolution, d.numBits, ASN1_INTEGER, 1),
373 ASN1_IMP(LogotypeImageResolution, d.tableSize, ASN1_INTEGER, 2)
374 } ASN1_CHOICE_END(LogotypeImageResolution);
376 ASN1_SEQUENCE(LogotypeImageInfo) = {
377 ASN1_IMP_OPT(LogotypeImageInfo, type, ASN1_INTEGER, 0),
378 ASN1_SIMPLE(LogotypeImageInfo, fileSize, ASN1_INTEGER),
379 ASN1_SIMPLE(LogotypeImageInfo, xSize, ASN1_INTEGER),
380 ASN1_SIMPLE(LogotypeImageInfo, ySize, ASN1_INTEGER),
381 ASN1_OPT(LogotypeImageInfo, resolution, LogotypeImageResolution),
382 ASN1_IMP_OPT(LogotypeImageInfo, language, ASN1_IA5STRING, 4),
383 } ASN1_SEQUENCE_END(LogotypeImageInfo);
385 ASN1_SEQUENCE(LogotypeImage) = {
386 ASN1_SIMPLE(LogotypeImage, imageDetails, LogotypeDetails),
387 ASN1_OPT(LogotypeImage, imageInfo, LogotypeImageInfo)
388 } ASN1_SEQUENCE_END(LogotypeImage);
390 ASN1_SEQUENCE(LogotypeAudioInfo) = {
391 ASN1_SIMPLE(LogotypeAudioInfo, fileSize, ASN1_INTEGER),
392 ASN1_SIMPLE(LogotypeAudioInfo, playTime, ASN1_INTEGER),
393 ASN1_SIMPLE(LogotypeAudioInfo, channels, ASN1_INTEGER),
394 ASN1_IMP_OPT(LogotypeAudioInfo, sampleRate, ASN1_INTEGER, 3),
395 ASN1_IMP_OPT(LogotypeAudioInfo, language, ASN1_IA5STRING, 4)
396 } ASN1_SEQUENCE_END(LogotypeAudioInfo);
398 ASN1_SEQUENCE(LogotypeAudio) = {
399 ASN1_SIMPLE(LogotypeAudio, audioDetails, LogotypeDetails),
400 ASN1_OPT(LogotypeAudio, audioInfo, LogotypeAudioInfo)
401 } ASN1_SEQUENCE_END(LogotypeAudio);
403 ASN1_SEQUENCE(LogotypeData) = {
404 ASN1_SEQUENCE_OF_OPT(LogotypeData, image, LogotypeImage),
405 ASN1_IMP_SEQUENCE_OF_OPT(LogotypeData, audio, LogotypeAudio, 1)
406 } ASN1_SEQUENCE_END(LogotypeData);
408 ASN1_CHOICE(LogotypeInfo) = {
409 ASN1_IMP(LogotypeInfo, d.direct, LogotypeData, 0),
410 ASN1_IMP(LogotypeInfo, d.indirect, LogotypeReference, 1)
411 } ASN1_CHOICE_END(LogotypeInfo);
413 ASN1_SEQUENCE(OtherLogotypeInfo) = {
414 ASN1_SIMPLE(OtherLogotypeInfo, logotypeType, ASN1_OBJECT),
415 ASN1_SIMPLE(OtherLogotypeInfo, info, LogotypeInfo)
416 } ASN1_SEQUENCE_END(OtherLogotypeInfo);
418 ASN1_SEQUENCE(LogotypeExtn) = {
419 ASN1_EXP_SEQUENCE_OF_OPT(LogotypeExtn, communityLogos, LogotypeInfo, 0),
420 ASN1_EXP_OPT(LogotypeExtn, issuerLogo, LogotypeInfo, 1),
421 ASN1_EXP_OPT(LogotypeExtn, issuerLogo, LogotypeInfo, 2),
422 ASN1_EXP_SEQUENCE_OF_OPT(LogotypeExtn, otherLogos, OtherLogotypeInfo, 3)
423 } ASN1_SEQUENCE_END(LogotypeExtn);
425 IMPLEMENT_ASN1_FUNCTIONS(LogotypeExtn);
427 #ifdef OPENSSL_IS_BORINGSSL
428 #define sk_LogotypeInfo_num(st) \
429 sk_num(CHECKED_CAST(_STACK *, STACK_OF(LogotypeInfo) *, (st)))
430 #define sk_LogotypeInfo_value(st, i) (LogotypeInfo *) \
431 sk_value(CHECKED_CAST(_STACK *, const STACK_OF(LogotypeInfo) *, (st)), (i))
432 #define sk_LogotypeImage_num(st) \
433 sk_num(CHECKED_CAST(_STACK *, STACK_OF(LogotypeImage) *, (st)))
434 #define sk_LogotypeImage_value(st, i) (LogotypeImage *) \
435 sk_value(CHECKED_CAST(_STACK *, const STACK_OF(LogotypeImage) *, (st)), (i))
436 #define sk_LogotypeAudio_num(st) \
437 sk_num(CHECKED_CAST(_STACK *, STACK_OF(LogotypeAudio) *, (st)))
438 #define sk_LogotypeAudio_value(st, i) (LogotypeAudio *) \
439 sk_value(CHECK_CAST(_STACK *, const STACK_OF(LogotypeAudio) *, (st)), (i))
440 #define sk_HashAlgAndValue_num(st) \
441 sk_num(CHECKED_CAST(_STACK *, STACK_OF(HashAlgAndValue) *, (st)))
442 #define sk_HashAlgAndValue_value(st, i) (HashAlgAndValue *) \
443 sk_value(CHECKED_CAST(_STACK *, const STACK_OF(HashAlgAndValue) *, (st)), (i))
444 #define sk_ASN1_IA5STRING_num(st) \
445 sk_num(CHECKED_CAST(_STACK *, STACK_OF(ASN1_IA5STRING) *, (st)))
446 #define sk_ASN1_IA5STRING_value(st, i) (ASN1_IA5STRING *) \
447 sk_value(CHECKED_CAST(_STACK *, const STACK_OF(ASN1_IA5STRING) *, (st)), (i))
448 #else /* OPENSSL_IS_BORINGSSL */
449 #define sk_LogotypeInfo_num(st) SKM_sk_num(LogotypeInfo, (st))
450 #define sk_LogotypeInfo_value(st, i) SKM_sk_value(LogotypeInfo, (st), (i))
451 #define sk_LogotypeImage_num(st) SKM_sk_num(LogotypeImage, (st))
452 #define sk_LogotypeImage_value(st, i) SKM_sk_value(LogotypeImage, (st), (i))
453 #define sk_LogotypeAudio_num(st) SKM_sk_num(LogotypeAudio, (st))
454 #define sk_LogotypeAudio_value(st, i) SKM_sk_value(LogotypeAudio, (st), (i))
455 #define sk_HashAlgAndValue_num(st) SKM_sk_num(HashAlgAndValue, (st))
456 #define sk_HashAlgAndValue_value(st, i) SKM_sk_value(HashAlgAndValue, (st), (i))
457 #define sk_ASN1_IA5STRING_num(st) SKM_sk_num(ASN1_IA5STRING, (st))
458 #define sk_ASN1_IA5STRING_value(st, i) SKM_sk_value(ASN1_IA5STRING, (st), (i))
459 #endif /* OPENSSL_IS_BORINGSSL */
462 static void add_logo(struct http_ctx *ctx, struct http_cert *hcert,
463 HashAlgAndValue *hash, ASN1_IA5STRING *uri)
469 if (hash == NULL || uri == NULL)
472 res = OBJ_obj2txt(txt, sizeof(txt), hash->hashAlg->algorithm, 1);
473 if (res < 0 || res >= (int) sizeof(txt))
476 n = os_realloc_array(hcert->logo, hcert->num_logo + 1,
477 sizeof(struct http_logo));
481 n = &hcert->logo[hcert->num_logo];
482 os_memset(n, 0, sizeof(*n));
484 n->alg_oid = os_strdup(txt);
485 if (n->alg_oid == NULL)
488 n->hash_len = ASN1_STRING_length(hash->hashValue);
489 n->hash = os_memdup(ASN1_STRING_data(hash->hashValue), n->hash_len);
490 if (n->hash == NULL) {
495 len = ASN1_STRING_length(uri);
496 n->uri = os_malloc(len + 1);
497 if (n->uri == NULL) {
502 os_memcpy(n->uri, ASN1_STRING_data(uri), len);
509 static void add_logo_direct(struct http_ctx *ctx, struct http_cert *hcert,
514 if (data->image == NULL)
517 num = sk_LogotypeImage_num(data->image);
518 for (i = 0; i < num; i++) {
519 LogotypeImage *image;
520 LogotypeDetails *details;
521 int j, hash_num, uri_num;
522 HashAlgAndValue *found_hash = NULL;
524 image = sk_LogotypeImage_value(data->image, i);
528 details = image->imageDetails;
532 hash_num = sk_HashAlgAndValue_num(details->logotypeHash);
533 for (j = 0; j < hash_num; j++) {
534 HashAlgAndValue *hash;
537 hash = sk_HashAlgAndValue_value(details->logotypeHash,
541 res = OBJ_obj2txt(txt, sizeof(txt),
542 hash->hashAlg->algorithm, 1);
543 if (res < 0 || res >= (int) sizeof(txt))
545 if (os_strcmp(txt, "2.16.840.1.101.3.4.2.1") == 0) {
552 wpa_printf(MSG_DEBUG, "OpenSSL: No SHA256 hash found for the logo");
556 uri_num = sk_ASN1_IA5STRING_num(details->logotypeURI);
557 for (j = 0; j < uri_num; j++) {
559 uri = sk_ASN1_IA5STRING_value(details->logotypeURI, j);
560 add_logo(ctx, hcert, found_hash, uri);
566 static void add_logo_indirect(struct http_ctx *ctx, struct http_cert *hcert,
567 LogotypeReference *ref)
569 int j, hash_num, uri_num;
571 hash_num = sk_HashAlgAndValue_num(ref->refStructHash);
572 uri_num = sk_ASN1_IA5STRING_num(ref->refStructURI);
573 if (hash_num != uri_num) {
574 wpa_printf(MSG_INFO, "Unexpected LogotypeReference array size difference %d != %d",
579 for (j = 0; j < hash_num; j++) {
580 HashAlgAndValue *hash;
582 hash = sk_HashAlgAndValue_value(ref->refStructHash, j);
583 uri = sk_ASN1_IA5STRING_value(ref->refStructURI, j);
584 add_logo(ctx, hcert, hash, uri);
589 static void i2r_HashAlgAndValue(HashAlgAndValue *hash, BIO *out, int indent)
592 const unsigned char *data;
594 BIO_printf(out, "%*shashAlg: ", indent, "");
595 i2a_ASN1_OBJECT(out, hash->hashAlg->algorithm);
596 BIO_printf(out, "\n");
598 BIO_printf(out, "%*shashValue: ", indent, "");
599 data = hash->hashValue->data;
600 for (i = 0; i < hash->hashValue->length; i++)
601 BIO_printf(out, "%s%02x", i > 0 ? ":" : "", data[i]);
602 BIO_printf(out, "\n");
605 static void i2r_LogotypeDetails(LogotypeDetails *details, BIO *out, int indent)
609 BIO_printf(out, "%*sLogotypeDetails\n", indent, "");
610 if (details->mediaType) {
611 BIO_printf(out, "%*smediaType: ", indent, "");
612 ASN1_STRING_print(out, details->mediaType);
613 BIO_printf(out, "\n");
616 num = details->logotypeHash ?
617 sk_HashAlgAndValue_num(details->logotypeHash) : 0;
618 for (i = 0; i < num; i++) {
619 HashAlgAndValue *hash;
620 hash = sk_HashAlgAndValue_value(details->logotypeHash, i);
621 i2r_HashAlgAndValue(hash, out, indent);
624 num = details->logotypeURI ?
625 sk_ASN1_IA5STRING_num(details->logotypeURI) : 0;
626 for (i = 0; i < num; i++) {
628 uri = sk_ASN1_IA5STRING_value(details->logotypeURI, i);
629 BIO_printf(out, "%*slogotypeURI: ", indent, "");
630 ASN1_STRING_print(out, uri);
631 BIO_printf(out, "\n");
635 static void i2r_LogotypeImageInfo(LogotypeImageInfo *info, BIO *out, int indent)
639 BIO_printf(out, "%*sLogotypeImageInfo\n", indent, "");
641 val = ASN1_INTEGER_get(info->type);
642 BIO_printf(out, "%*stype: %ld\n", indent, "", val);
644 BIO_printf(out, "%*stype: default (1)\n", indent, "");
646 val = ASN1_INTEGER_get(info->fileSize);
647 BIO_printf(out, "%*sfileSize: %ld\n", indent, "", val);
648 val = ASN1_INTEGER_get(info->xSize);
649 BIO_printf(out, "%*sxSize: %ld\n", indent, "", val);
650 val = ASN1_INTEGER_get(info->ySize);
651 BIO_printf(out, "%*sySize: %ld\n", indent, "", val);
652 if (info->resolution) {
653 BIO_printf(out, "%*sresolution [%d]\n", indent, "",
654 info->resolution->type);
655 switch (info->resolution->type) {
657 val = ASN1_INTEGER_get(info->resolution->d.numBits);
658 BIO_printf(out, "%*snumBits: %ld\n", indent, "", val);
661 val = ASN1_INTEGER_get(info->resolution->d.tableSize);
662 BIO_printf(out, "%*stableSize: %ld\n", indent, "", val);
666 if (info->language) {
667 BIO_printf(out, "%*slanguage: ", indent, "");
668 ASN1_STRING_print(out, info->language);
669 BIO_printf(out, "\n");
673 static void i2r_LogotypeImage(LogotypeImage *image, BIO *out, int indent)
675 BIO_printf(out, "%*sLogotypeImage\n", indent, "");
676 if (image->imageDetails) {
677 i2r_LogotypeDetails(image->imageDetails, out, indent + 4);
679 if (image->imageInfo) {
680 i2r_LogotypeImageInfo(image->imageInfo, out, indent + 4);
684 static void i2r_LogotypeData(LogotypeData *data, const char *title, BIO *out,
689 BIO_printf(out, "%*s%s - LogotypeData\n", indent, "", title);
691 num = data->image ? sk_LogotypeImage_num(data->image) : 0;
692 for (i = 0; i < num; i++) {
693 LogotypeImage *image = sk_LogotypeImage_value(data->image, i);
694 i2r_LogotypeImage(image, out, indent + 4);
697 num = data->audio ? sk_LogotypeAudio_num(data->audio) : 0;
698 for (i = 0; i < num; i++) {
699 BIO_printf(out, "%*saudio: TODO\n", indent, "");
703 static void i2r_LogotypeReference(LogotypeReference *ref, const char *title,
704 BIO *out, int indent)
706 int i, hash_num, uri_num;
708 BIO_printf(out, "%*s%s - LogotypeReference\n", indent, "", title);
710 hash_num = ref->refStructHash ?
711 sk_HashAlgAndValue_num(ref->refStructHash) : 0;
712 uri_num = ref->refStructURI ?
713 sk_ASN1_IA5STRING_num(ref->refStructURI) : 0;
714 if (hash_num != uri_num) {
715 BIO_printf(out, "%*sUnexpected LogotypeReference array size difference %d != %d\n",
716 indent, "", hash_num, uri_num);
720 for (i = 0; i < hash_num; i++) {
721 HashAlgAndValue *hash;
724 hash = sk_HashAlgAndValue_value(ref->refStructHash, i);
725 i2r_HashAlgAndValue(hash, out, indent);
727 uri = sk_ASN1_IA5STRING_value(ref->refStructURI, i);
728 BIO_printf(out, "%*srefStructURI: ", indent, "");
729 ASN1_STRING_print(out, uri);
730 BIO_printf(out, "\n");
734 static void i2r_LogotypeInfo(LogotypeInfo *info, const char *title, BIO *out,
737 switch (info->type) {
739 i2r_LogotypeData(info->d.direct, title, out, indent);
742 i2r_LogotypeReference(info->d.indirect, title, out, indent);
747 static void debug_print_logotypeext(LogotypeExtn *logo)
753 out = BIO_new_fp(stdout, BIO_NOCLOSE);
757 if (logo->communityLogos) {
758 num = sk_LogotypeInfo_num(logo->communityLogos);
759 for (i = 0; i < num; i++) {
761 info = sk_LogotypeInfo_value(logo->communityLogos, i);
762 i2r_LogotypeInfo(info, "communityLogo", out, indent);
766 if (logo->issuerLogo) {
767 i2r_LogotypeInfo(logo->issuerLogo, "issuerLogo", out, indent );
770 if (logo->subjectLogo) {
771 i2r_LogotypeInfo(logo->subjectLogo, "subjectLogo", out, indent);
774 if (logo->otherLogos) {
775 BIO_printf(out, "%*sotherLogos - TODO\n", indent, "");
782 static void add_logotype_ext(struct http_ctx *ctx, struct http_cert *hcert,
788 ASN1_OCTET_STRING *os;
790 const unsigned char *data;
793 obj = OBJ_txt2obj("1.3.6.1.5.5.7.1.12", 0);
797 pos = X509_get_ext_by_OBJ(cert, obj, -1);
799 wpa_printf(MSG_INFO, "No logotype extension included");
803 wpa_printf(MSG_INFO, "Parsing logotype extension");
804 ext = X509_get_ext(cert, pos);
806 wpa_printf(MSG_INFO, "Could not get logotype extension");
810 os = X509_EXTENSION_get_data(ext);
812 wpa_printf(MSG_INFO, "Could not get logotype extension data");
816 wpa_hexdump(MSG_DEBUG, "logotypeExtn",
817 ASN1_STRING_data(os), ASN1_STRING_length(os));
819 data = ASN1_STRING_data(os);
820 logo = d2i_LogotypeExtn(NULL, &data, ASN1_STRING_length(os));
822 wpa_printf(MSG_INFO, "Failed to parse logotypeExtn");
826 if (wpa_debug_level < MSG_INFO)
827 debug_print_logotypeext(logo);
829 if (!logo->communityLogos) {
830 wpa_printf(MSG_INFO, "No communityLogos included");
831 LogotypeExtn_free(logo);
835 num = sk_LogotypeInfo_num(logo->communityLogos);
836 for (i = 0; i < num; i++) {
838 info = sk_LogotypeInfo_value(logo->communityLogos, i);
839 switch (info->type) {
841 add_logo_direct(ctx, hcert, info->d.direct);
844 add_logo_indirect(ctx, hcert, info->d.indirect);
849 LogotypeExtn_free(logo);
853 static void parse_cert(struct http_ctx *ctx, struct http_cert *hcert,
854 X509 *cert, GENERAL_NAMES **names)
856 os_memset(hcert, 0, sizeof(*hcert));
858 *names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
860 add_alt_names(ctx, hcert, *names);
862 add_logotype_ext(ctx, hcert, cert);
866 static void parse_cert_free(struct http_cert *hcert, GENERAL_NAMES *names)
870 for (i = 0; i < hcert->num_dnsname; i++)
871 OPENSSL_free(hcert->dnsname[i]);
872 os_free(hcert->dnsname);
874 for (i = 0; i < hcert->num_othername; i++)
875 os_free(hcert->othername[i].oid);
876 os_free(hcert->othername);
878 for (i = 0; i < hcert->num_logo; i++) {
879 os_free(hcert->logo[i].alg_oid);
880 os_free(hcert->logo[i].hash);
881 os_free(hcert->logo[i].uri);
883 os_free(hcert->logo);
885 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
889 static int validate_server_cert(struct http_ctx *ctx, X509 *cert)
891 GENERAL_NAMES *names;
892 struct http_cert hcert;
895 if (ctx->cert_cb == NULL) {
896 wpa_printf(MSG_DEBUG, "%s: no cert_cb configured", __func__);
902 out = BIO_new_fp(stdout, BIO_NOCLOSE);
903 X509_print_ex(out, cert, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
907 parse_cert(ctx, &hcert, cert, &names);
908 ret = ctx->cert_cb(ctx->cert_cb_ctx, &hcert);
909 parse_cert_free(&hcert, names);
915 void http_parse_x509_certificate(struct http_ctx *ctx, const char *fname)
919 GENERAL_NAMES *names;
920 struct http_cert hcert;
923 in = BIO_new_file(fname, "r");
925 wpa_printf(MSG_ERROR, "Could not read '%s'", fname);
929 cert = d2i_X509_bio(in, NULL);
933 wpa_printf(MSG_ERROR, "Could not parse certificate");
937 out = BIO_new_fp(stdout, BIO_NOCLOSE);
939 X509_print_ex(out, cert, XN_FLAG_COMPAT,
944 wpa_printf(MSG_INFO, "Additional parsing information:");
945 parse_cert(ctx, &hcert, cert, &names);
946 for (i = 0; i < hcert.num_othername; i++) {
947 if (os_strcmp(hcert.othername[i].oid,
948 "1.3.6.1.4.1.40808.1.1.1") == 0) {
949 char *name = os_zalloc(hcert.othername[i].len + 1);
951 os_memcpy(name, hcert.othername[i].data,
952 hcert.othername[i].len);
954 "id-wfa-hotspot-friendlyName: %s",
958 wpa_hexdump_ascii(MSG_INFO,
959 "id-wfa-hotspot-friendlyName",
960 hcert.othername[i].data,
961 hcert.othername[i].len);
963 wpa_printf(MSG_INFO, "subjAltName[othername]: oid=%s",
964 hcert.othername[i].oid);
965 wpa_hexdump_ascii(MSG_INFO, "unknown othername",
966 hcert.othername[i].data,
967 hcert.othername[i].len);
970 parse_cert_free(&hcert, names);
976 static int curl_cb_ssl_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
978 struct http_ctx *ctx;
987 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
988 SSL_get_ex_data_X509_STORE_CTX_idx());
989 ssl_ctx = SSL_get_SSL_CTX(ssl);
990 ctx = SSL_CTX_get_app_data(ssl_ctx);
992 wpa_printf(MSG_DEBUG, "curl_cb_ssl_verify, preverify_ok: %d",
995 err = X509_STORE_CTX_get_error(x509_ctx);
996 err_str = X509_verify_cert_error_string(err);
997 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
998 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
1000 wpa_printf(MSG_INFO, "No server certificate available");
1001 ctx->last_err = "No server certificate available";
1006 ctx->peer_cert = cert;
1007 else if (depth == 1)
1008 ctx->peer_issuer = cert;
1009 else if (depth == 2)
1010 ctx->peer_issuer_issuer = cert;
1012 name = X509_get_subject_name(cert);
1013 X509_NAME_oneline(name, buf, sizeof(buf));
1014 wpa_printf(MSG_INFO, "Server certificate chain - depth=%d err=%d (%s) subject=%s",
1015 depth, err, err_str, buf);
1016 debug_dump_cert("Server certificate chain - certificate", cert);
1018 if (depth == 0 && preverify_ok && validate_server_cert(ctx, cert) < 0)
1021 #ifdef OPENSSL_IS_BORINGSSL
1022 if (depth == 0 && ctx->ocsp != NO_OCSP && preverify_ok) {
1023 enum ocsp_result res;
1025 res = check_ocsp_resp(ssl_ctx, ssl, cert, ctx->peer_issuer,
1026 ctx->peer_issuer_issuer);
1027 if (res == OCSP_REVOKED) {
1029 wpa_printf(MSG_INFO, "OCSP: certificate revoked");
1030 if (err == X509_V_OK)
1031 X509_STORE_CTX_set_error(
1032 x509_ctx, X509_V_ERR_CERT_REVOKED);
1033 } else if (res != OCSP_GOOD && (ctx->ocsp == MANDATORY_OCSP)) {
1035 wpa_printf(MSG_INFO,
1036 "OCSP: bad certificate status response");
1039 #endif /* OPENSSL_IS_BORINGSSL */
1042 ctx->last_err = "TLS validation failed";
1044 return preverify_ok;
1050 static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
1057 out = BIO_new(BIO_s_mem());
1061 OCSP_RESPONSE_print(out, rsp, 0);
1062 rlen = BIO_ctrl_pending(out);
1063 txt = os_malloc(rlen + 1);
1069 res = BIO_read(out, txt, rlen);
1072 wpa_printf(MSG_MSGDUMP, "OpenSSL: OCSP Response\n%s", txt);
1079 static void tls_show_errors(const char *func, const char *txt)
1083 wpa_printf(MSG_DEBUG, "OpenSSL: %s - %s %s",
1084 func, txt, ERR_error_string(ERR_get_error(), NULL));
1086 while ((err = ERR_get_error())) {
1087 wpa_printf(MSG_DEBUG, "OpenSSL: pending error: %s",
1088 ERR_error_string(err, NULL));
1093 static int ocsp_resp_cb(SSL *s, void *arg)
1095 struct http_ctx *ctx = arg;
1096 const unsigned char *p;
1097 int len, status, reason, res;
1099 OCSP_BASICRESP *basic;
1101 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
1103 STACK_OF(X509) *certs = NULL;
1105 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
1107 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
1108 if (ctx->ocsp == MANDATORY_OCSP)
1109 ctx->last_err = "No OCSP response received";
1110 return (ctx->ocsp == MANDATORY_OCSP) ? 0 : 1;
1113 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
1115 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
1117 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
1118 ctx->last_err = "Failed to parse OCSP response";
1122 ocsp_debug_print_resp(rsp);
1124 status = OCSP_response_status(rsp);
1125 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
1126 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
1127 status, OCSP_response_status_str(status));
1128 ctx->last_err = "OCSP responder error";
1132 basic = OCSP_response_get1_basic(rsp);
1134 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
1135 ctx->last_err = "Could not find BasicOCSPResponse";
1139 store = SSL_CTX_get_cert_store(s->ctx);
1140 if (ctx->peer_issuer) {
1141 wpa_printf(MSG_DEBUG, "OpenSSL: Add issuer");
1142 debug_dump_cert("OpenSSL: Issuer certificate",
1145 if (X509_STORE_add_cert(store, ctx->peer_issuer) != 1) {
1146 tls_show_errors(__func__,
1147 "OpenSSL: Could not add issuer to certificate store");
1149 certs = sk_X509_new_null();
1152 cert = X509_dup(ctx->peer_issuer);
1153 if (cert && !sk_X509_push(certs, cert)) {
1156 "OpenSSL: Could not add issuer to OCSP responder trust store");
1158 sk_X509_free(certs);
1161 if (certs && ctx->peer_issuer_issuer) {
1162 cert = X509_dup(ctx->peer_issuer_issuer);
1163 if (cert && !sk_X509_push(certs, cert)) {
1166 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
1173 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
1174 sk_X509_pop_free(certs, X509_free);
1176 tls_show_errors(__func__,
1177 "OpenSSL: OCSP response failed verification");
1178 OCSP_BASICRESP_free(basic);
1179 OCSP_RESPONSE_free(rsp);
1180 ctx->last_err = "OCSP response failed verification";
1184 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
1186 if (!ctx->peer_cert) {
1187 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
1188 OCSP_BASICRESP_free(basic);
1189 OCSP_RESPONSE_free(rsp);
1190 ctx->last_err = "Peer certificate not available for OCSP status check";
1194 if (!ctx->peer_issuer) {
1195 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
1196 OCSP_BASICRESP_free(basic);
1197 OCSP_RESPONSE_free(rsp);
1198 ctx->last_err = "Peer issuer certificate not available for OCSP status check";
1202 id = OCSP_cert_to_id(EVP_sha256(), ctx->peer_cert, ctx->peer_issuer);
1204 wpa_printf(MSG_DEBUG,
1205 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
1206 OCSP_BASICRESP_free(basic);
1207 OCSP_RESPONSE_free(rsp);
1208 ctx->last_err = "Could not create OCSP certificate identifier";
1212 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
1213 &this_update, &next_update);
1215 id = OCSP_cert_to_id(NULL, ctx->peer_cert, ctx->peer_issuer);
1217 wpa_printf(MSG_DEBUG,
1218 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
1219 OCSP_BASICRESP_free(basic);
1220 OCSP_RESPONSE_free(rsp);
1222 "Could not create OCSP certificate identifier";
1226 res = OCSP_resp_find_status(basic, id, &status, &reason,
1227 &produced_at, &this_update,
1232 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
1233 (ctx->ocsp == MANDATORY_OCSP) ? "" :
1234 " (OCSP not required)");
1235 OCSP_CERTID_free(id);
1236 OCSP_BASICRESP_free(basic);
1237 OCSP_RESPONSE_free(rsp);
1238 if (ctx->ocsp == MANDATORY_OCSP)
1240 ctx->last_err = "Could not find current server certificate from OCSP response";
1241 return (ctx->ocsp == MANDATORY_OCSP) ? 0 : 1;
1243 OCSP_CERTID_free(id);
1245 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
1246 tls_show_errors(__func__, "OpenSSL: OCSP status times invalid");
1247 OCSP_BASICRESP_free(basic);
1248 OCSP_RESPONSE_free(rsp);
1249 ctx->last_err = "OCSP status times invalid";
1253 OCSP_BASICRESP_free(basic);
1254 OCSP_RESPONSE_free(rsp);
1256 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
1257 OCSP_cert_status_str(status));
1259 if (status == V_OCSP_CERTSTATUS_GOOD)
1261 if (status == V_OCSP_CERTSTATUS_REVOKED) {
1262 ctx->last_err = "Server certificate has been revoked";
1265 if (ctx->ocsp == MANDATORY_OCSP) {
1266 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
1267 ctx->last_err = "OCSP status unknown";
1270 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
1275 static SSL_METHOD patch_ssl_method;
1276 static const SSL_METHOD *real_ssl_method;
1278 static int curl_patch_ssl_new(SSL *s)
1280 SSL_CTX *ssl = s->ctx;
1283 ssl->method = real_ssl_method;
1284 s->method = real_ssl_method;
1286 ret = s->method->ssl_new(s);
1287 SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp);
1292 #endif /* HAVE_OCSP */
1295 static CURLcode curl_cb_ssl(CURL *curl, void *sslctx, void *parm)
1297 struct http_ctx *ctx = parm;
1298 SSL_CTX *ssl = sslctx;
1300 wpa_printf(MSG_DEBUG, "curl_cb_ssl");
1301 SSL_CTX_set_app_data(ssl, ctx);
1302 SSL_CTX_set_verify(ssl, SSL_VERIFY_PEER, curl_cb_ssl_verify);
1305 if (ctx->ocsp != NO_OCSP) {
1306 SSL_CTX_set_tlsext_status_cb(ssl, ocsp_resp_cb);
1307 SSL_CTX_set_tlsext_status_arg(ssl, ctx);
1310 * Use a temporary SSL_METHOD to get a callback on SSL_new()
1311 * from libcurl since there is no proper callback registration
1312 * available for this.
1314 os_memset(&patch_ssl_method, 0, sizeof(patch_ssl_method));
1315 patch_ssl_method.ssl_new = curl_patch_ssl_new;
1316 real_ssl_method = ssl->method;
1317 ssl->method = &patch_ssl_method;
1319 #endif /* HAVE_OCSP */
1324 #endif /* EAP_TLS_OPENSSL */
1327 static CURL * setup_curl_post(struct http_ctx *ctx, const char *address,
1328 const char *ca_fname, const char *username,
1329 const char *password, const char *client_cert,
1330 const char *client_key)
1333 #ifdef EAP_TLS_OPENSSL
1334 const char *extra = " tls=openssl";
1335 #else /* EAP_TLS_OPENSSL */
1336 const char *extra = "";
1337 #endif /* EAP_TLS_OPENSSL */
1339 wpa_printf(MSG_DEBUG, "Start HTTP client: address=%s ca_fname=%s "
1340 "username=%s%s", address, ca_fname, username, extra);
1342 curl = curl_easy_init();
1346 curl_easy_setopt(curl, CURLOPT_URL, address);
1347 curl_easy_setopt(curl, CURLOPT_POST, 1L);
1349 curl_easy_setopt(curl, CURLOPT_CAINFO, ca_fname);
1350 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1351 #ifdef EAP_TLS_OPENSSL
1352 curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_cb_ssl);
1353 curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, ctx);
1354 #ifdef OPENSSL_IS_BORINGSSL
1355 /* For now, using the CURLOPT_SSL_VERIFYSTATUS option only
1356 * with BoringSSL since the OpenSSL specific callback hack to
1357 * enable OCSP is not available with BoringSSL. The OCSP
1358 * implementation within libcurl is not sufficient for the
1359 * Hotspot 2.0 OSU needs, so cannot use this with OpenSSL.
1361 if (ctx->ocsp != NO_OCSP)
1362 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1L);
1363 #endif /* OPENSSL_IS_BORINGSSL */
1364 #endif /* EAP_TLS_OPENSSL */
1366 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
1368 if (client_cert && client_key) {
1369 curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert);
1370 curl_easy_setopt(curl, CURLOPT_SSLKEY, client_key);
1372 /* TODO: use curl_easy_getinfo() with CURLINFO_CERTINFO to fetch
1373 * information about the server certificate */
1374 curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
1375 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_cb_debug);
1376 curl_easy_setopt(curl, CURLOPT_DEBUGDATA, ctx);
1377 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_cb_write);
1378 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ctx);
1379 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
1381 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
1382 curl_easy_setopt(curl, CURLOPT_USERNAME, username);
1383 curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
1390 static int post_init_client(struct http_ctx *ctx, const char *address,
1391 const char *ca_fname, const char *username,
1392 const char *password, const char *client_cert,
1393 const char *client_key)
1398 clone_str(&ctx->svc_address, address);
1399 clone_str(&ctx->svc_ca_fname, ca_fname);
1400 clone_str(&ctx->svc_username, username);
1401 clone_str(&ctx->svc_password, password);
1402 clone_str(&ctx->svc_client_cert, client_cert);
1403 clone_str(&ctx->svc_client_key, client_key);
1406 * Workaround for Apache "Hostname 'FOO' provided via SNI and hostname
1407 * 'foo' provided via HTTP are different.
1409 for (count = 0, pos = ctx->svc_address; count < 3 && pos && *pos;
1413 *pos = tolower(*pos);
1416 ctx->curl = setup_curl_post(ctx, ctx->svc_address, ca_fname, username,
1417 password, client_cert, client_key);
1418 if (ctx->curl == NULL)
1425 int soap_init_client(struct http_ctx *ctx, const char *address,
1426 const char *ca_fname, const char *username,
1427 const char *password, const char *client_cert,
1428 const char *client_key)
1430 if (post_init_client(ctx, address, ca_fname, username, password,
1431 client_cert, client_key) < 0)
1434 ctx->curl_hdr = curl_slist_append(ctx->curl_hdr,
1435 "Content-Type: application/soap+xml");
1436 ctx->curl_hdr = curl_slist_append(ctx->curl_hdr, "SOAPAction: ");
1437 ctx->curl_hdr = curl_slist_append(ctx->curl_hdr, "Expect:");
1438 curl_easy_setopt(ctx->curl, CURLOPT_HTTPHEADER, ctx->curl_hdr);
1444 int soap_reinit_client(struct http_ctx *ctx)
1446 char *address = NULL;
1447 char *ca_fname = NULL;
1448 char *username = NULL;
1449 char *password = NULL;
1450 char *client_cert = NULL;
1451 char *client_key = NULL;
1456 clone_str(&address, ctx->svc_address);
1457 clone_str(&ca_fname, ctx->svc_ca_fname);
1458 clone_str(&username, ctx->svc_username);
1459 clone_str(&password, ctx->svc_password);
1460 clone_str(&client_cert, ctx->svc_client_cert);
1461 clone_str(&client_key, ctx->svc_client_key);
1463 ret = soap_init_client(ctx, address, ca_fname, username, password,
1464 client_cert, client_key);
1467 str_clear_free(username);
1468 str_clear_free(password);
1469 os_free(client_cert);
1470 os_free(client_key);
1475 static void free_curl_buf(struct http_ctx *ctx)
1477 os_free(ctx->curl_buf);
1478 ctx->curl_buf = NULL;
1479 ctx->curl_buf_len = 0;
1483 xml_node_t * soap_send_receive(struct http_ctx *ctx, xml_node_t *node)
1486 xml_node_t *envelope, *ret, *resp, *n;
1490 ctx->last_err = NULL;
1492 wpa_printf(MSG_DEBUG, "SOAP: Sending message");
1493 envelope = soap_build_envelope(ctx->xml, node);
1494 str = xml_node_to_str(ctx->xml, envelope);
1495 xml_node_free(ctx->xml, envelope);
1496 wpa_printf(MSG_MSGDUMP, "SOAP[%s]", str);
1498 curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDS, str);
1501 res = curl_easy_perform(ctx->curl);
1502 if (res != CURLE_OK) {
1504 ctx->last_err = curl_easy_strerror(res);
1505 wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
1513 curl_easy_getinfo(ctx->curl, CURLINFO_RESPONSE_CODE, &http);
1514 wpa_printf(MSG_DEBUG, "SOAP: Server response code %ld", http);
1516 ctx->last_err = "HTTP download failed";
1517 wpa_printf(MSG_INFO, "HTTP download failed - code %ld", http);
1522 if (ctx->curl_buf == NULL)
1525 wpa_printf(MSG_MSGDUMP, "Server response:\n%s", ctx->curl_buf);
1526 resp = xml_node_from_buf(ctx->xml, ctx->curl_buf);
1529 wpa_printf(MSG_INFO, "Could not parse SOAP response");
1530 ctx->last_err = "Could not parse SOAP response";
1534 ret = soap_get_body(ctx->xml, resp);
1536 wpa_printf(MSG_INFO, "Could not get SOAP body");
1537 ctx->last_err = "Could not get SOAP body";
1541 wpa_printf(MSG_DEBUG, "SOAP body localname: '%s'",
1542 xml_node_get_localname(ctx->xml, ret));
1543 n = xml_node_copy(ctx->xml, ret);
1544 xml_node_free(ctx->xml, resp);
1550 struct http_ctx * http_init_ctx(void *upper_ctx, struct xml_node_ctx *xml_ctx)
1552 struct http_ctx *ctx;
1554 ctx = os_zalloc(sizeof(*ctx));
1557 ctx->ctx = upper_ctx;
1559 ctx->ocsp = OPTIONAL_OCSP;
1561 curl_global_init(CURL_GLOBAL_ALL);
1567 void http_ocsp_set(struct http_ctx *ctx, int val)
1570 ctx->ocsp = NO_OCSP;
1572 ctx->ocsp = OPTIONAL_OCSP;
1574 ctx->ocsp = MANDATORY_OCSP;
1578 void http_deinit_ctx(struct http_ctx *ctx)
1581 os_free(ctx->curl_buf);
1582 curl_global_cleanup();
1584 os_free(ctx->svc_address);
1585 os_free(ctx->svc_ca_fname);
1586 str_clear_free(ctx->svc_username);
1587 str_clear_free(ctx->svc_password);
1588 os_free(ctx->svc_client_cert);
1589 os_free(ctx->svc_client_key);
1595 int http_download_file(struct http_ctx *ctx, const char *url,
1596 const char *fname, const char *ca_fname)
1603 ctx->last_err = NULL;
1605 wpa_printf(MSG_DEBUG, "curl: Download file from %s to %s (ca=%s)",
1606 url, fname, ca_fname);
1607 curl = curl_easy_init();
1611 f = fopen(fname, "wb");
1613 curl_easy_cleanup(curl);
1617 curl_easy_setopt(curl, CURLOPT_URL, url);
1619 curl_easy_setopt(curl, CURLOPT_CAINFO, ca_fname);
1620 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1621 curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
1623 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
1625 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_cb_debug);
1626 curl_easy_setopt(curl, CURLOPT_DEBUGDATA, ctx);
1627 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
1628 curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
1629 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
1631 res = curl_easy_perform(curl);
1632 if (res != CURLE_OK) {
1634 ctx->last_err = curl_easy_strerror(res);
1635 wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
1637 curl_easy_cleanup(curl);
1642 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http);
1643 wpa_printf(MSG_DEBUG, "curl: Server response code %ld", http);
1645 ctx->last_err = "HTTP download failed";
1646 wpa_printf(MSG_INFO, "HTTP download failed - code %ld", http);
1647 curl_easy_cleanup(curl);
1652 curl_easy_cleanup(curl);
1659 char * http_post(struct http_ctx *ctx, const char *url, const char *data,
1660 const char *content_type, const char *ext_hdr,
1661 const char *ca_fname,
1662 const char *username, const char *password,
1663 const char *client_cert, const char *client_key,
1670 struct curl_slist *curl_hdr = NULL;
1672 ctx->last_err = NULL;
1673 wpa_printf(MSG_DEBUG, "curl: HTTP POST to %s", url);
1674 curl = setup_curl_post(ctx, url, ca_fname, username, password,
1675 client_cert, client_key);
1681 snprintf(ct, sizeof(ct), "Content-Type: %s", content_type);
1682 curl_hdr = curl_slist_append(curl_hdr, ct);
1685 curl_hdr = curl_slist_append(curl_hdr, ext_hdr);
1686 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_hdr);
1688 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
1691 res = curl_easy_perform(curl);
1692 if (res != CURLE_OK) {
1694 ctx->last_err = curl_easy_strerror(res);
1695 wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
1701 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http);
1702 wpa_printf(MSG_DEBUG, "curl: Server response code %ld", http);
1704 ctx->last_err = "HTTP POST failed";
1705 wpa_printf(MSG_INFO, "HTTP POST failed - code %ld", http);
1710 if (ctx->curl_buf == NULL)
1713 ret = ctx->curl_buf;
1715 *resp_len = ctx->curl_buf_len;
1716 ctx->curl_buf = NULL;
1717 ctx->curl_buf_len = 0;
1719 wpa_printf(MSG_MSGDUMP, "Server response:\n%s", ret);
1725 void http_set_cert_cb(struct http_ctx *ctx,
1726 int (*cb)(void *ctx, struct http_cert *cert),
1730 ctx->cert_cb_ctx = cb_ctx;
1734 const char * http_get_err(struct http_ctx *ctx)
1736 return ctx->last_err;