Cumulative patch from commit 20ff2642e192437245422c9654463bcebf65b277

20ff264 WPS: Clear WPS data on init failure
f19e370 WPS: Do not advertise WPA/WPA2-Enterprise Auth Type Flags
1b5df9e nl80211: Do not indicate scan started event on scan_for_auth
bb23826 HS 2.0R2: Clean up debug log during exit path
48408fc HS 2.0R2: Do not mandate OCSP response for EST operations
8f60293 HS 2.0R2: Do not use OSU cert validation for EST
40bdcea HS 2.0R2: Configure OSU client trust root more consistently
4d65ded HS 2.0R2: Clean up debug from libcurl

Change-Id: I44aa20ac92abf03ea7b47f1ecad72dc07a0870d9
Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
diff --git a/hs20/client/osu_client.c b/hs20/client/osu_client.c
index 2175fc3..ea269ab 100644
--- a/hs20/client/osu_client.c
+++ b/hs20/client/osu_client.c
@@ -1,6 +1,6 @@
 /*
  * Hotspot 2.0 OSU client
- * Copyright (c) 2012-2013, Qualcomm Atheros, Inc.
+ * Copyright (c) 2012-2014, Qualcomm Atheros, Inc.
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
@@ -302,7 +302,12 @@
 	xml_node_get_text_free(ctx->xml, hash);
 
 	write_summary(ctx, "Download certificate from %s", url);
+	ctx->no_osu_cert_validation = 1;
+	http_ocsp_set(ctx->http, 1);
 	res = http_download_file(ctx->http, url, TMP_CERT_DL_FILE, NULL);
+	http_ocsp_set(ctx->http,
+		      (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
+	ctx->no_osu_cert_validation = 0;
 	xml_node_get_text_free(ctx->xml, url);
 	if (res < 0)
 		return -1;
@@ -1993,7 +1998,7 @@
 
 
 static int osu_connect(struct hs20_osu_client *ctx, const char *bssid,
-		       const char *ssid, const char *url, const char *ca_fname,
+		       const char *ssid, const char *url,
 		       unsigned int methods, int no_prod_assoc,
 		       const char *osu_nai)
 {
@@ -2068,9 +2073,9 @@
 
 	ctx->no_reconnect = 1;
 	if (methods & 0x02)
-		res = cmd_prov(ctx, url, ca_fname);
+		res = cmd_prov(ctx, url);
 	else if (methods & 0x01)
-		res = cmd_oma_dm_prov(ctx, url, ca_fname);
+		res = cmd_oma_dm_prov(ctx, url);
 
 	wpa_printf(MSG_INFO, "Remove OSU network connection");
 	write_summary(ctx, "Remove OSU network connection");
@@ -2093,7 +2098,7 @@
 
 
 static int cmd_osu_select(struct hs20_osu_client *ctx, const char *dir,
-			  int connect, const char *ca_fname, int no_prod_assoc,
+			  int connect, int no_prod_assoc,
 			  const char *friendly_name)
 {
 	char fname[255];
@@ -2264,14 +2269,14 @@
 
 		if (connect == 2) {
 			if (last->methods & 0x02)
-				ret = cmd_prov(ctx, last->url, ca_fname);
+				ret = cmd_prov(ctx, last->url);
 			else if (last->methods & 0x01)
-				ret = cmd_oma_dm_prov(ctx, last->url, ca_fname);
+				ret = cmd_oma_dm_prov(ctx, last->url);
 			else
 				ret = -1;
 		} else if (connect)
 			ret = osu_connect(ctx, last->bssid, last->osu_ssid,
-					  last->url, ca_fname, last->methods,
+					  last->url, last->methods,
 					  no_prod_assoc, last->osu_nai);
 	} else
 		ret = -1;
@@ -2282,8 +2287,8 @@
 }
 
 
-static int cmd_signup(struct hs20_osu_client *ctx, const char *ca_fname,
-		      int no_prod_assoc, const char *friendly_name)
+static int cmd_signup(struct hs20_osu_client *ctx, int no_prod_assoc,
+		      const char *friendly_name)
 {
 	char dir[255];
 	char fname[300], buf[400];
@@ -2334,8 +2339,7 @@
 	}
 	wpa_printf(MSG_INFO, "OSU provider fetch completed");
 
-	return cmd_osu_select(ctx, fname, 1, ca_fname, no_prod_assoc,
-			      friendly_name);
+	return cmd_osu_select(ctx, fname, 1, no_prod_assoc, friendly_name);
 }
 
 
@@ -2354,8 +2358,6 @@
 	char *client_key = NULL;
 	int spp;
 
-	ctx->ca_fname = ca_fname;
-
 	wpa_printf(MSG_INFO, "Subscription remediation requested with Server URL: %s",
 		   address);
 
@@ -2399,6 +2401,7 @@
 		return;
 	}
 	wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
+	ctx->ca_fname = ca_fname;
 
 	pps = node_from_file(ctx->xml, pps_fname);
 	if (pps == NULL) {
@@ -2482,11 +2485,11 @@
 	}
 
 	if (spp)
-		spp_sub_rem(ctx, address, pps_fname, ca_fname,
+		spp_sub_rem(ctx, address, pps_fname,
 			    client_cert, client_key,
 			    cred_username, cred_password, pps);
 	else
-		oma_dm_sub_rem(ctx, address, pps_fname, ca_fname,
+		oma_dm_sub_rem(ctx, address, pps_fname,
 			       client_cert, client_key,
 			       cred_username, cred_password, pps);
 
@@ -2555,6 +2558,7 @@
 		return -1;
 	}
 	wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
+	ctx->ca_fname = ca_fname;
 
 	pps = node_from_file(ctx->xml, pps_fname);
 	if (pps == NULL) {
@@ -2628,11 +2632,11 @@
 	}
 
 	if (spp)
-		spp_pol_upd(ctx, address, pps_fname, ca_fname,
+		spp_pol_upd(ctx, address, pps_fname,
 			    client_cert, client_key,
 			    cred_username, cred_password, pps);
 	else
-		oma_dm_pol_upd(ctx, address, pps_fname, ca_fname,
+		oma_dm_pol_upd(ctx, address, pps_fname,
 			       client_cert, client_key,
 			       cred_username, cred_password, pps);
 
@@ -2693,7 +2697,8 @@
 	int found;
 	char *host = NULL;
 
-	wpa_printf(MSG_INFO, "osu_cert_cb");
+	wpa_printf(MSG_INFO, "osu_cert_cb(osu_cert_validation=%d)",
+		   !ctx->no_osu_cert_validation);
 
 	host = get_hostname(ctx->server_url);
 
@@ -2737,7 +2742,8 @@
 		}
 	}
 
-	for (j = 0; j < ctx->friendly_name_count; j++) {
+	for (j = 0; !ctx->no_osu_cert_validation &&
+		     j < ctx->friendly_name_count; j++) {
 		int found = 0;
 		for (i = 0; i < cert->num_othername; i++) {
 			if (os_strcmp(cert->othername[i].oid,
@@ -2776,7 +2782,7 @@
 				  logo->hash, logo->hash_len);
 	}
 
-	for (j = 0; j < ctx->icon_count; j++) {
+	for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
 		int found = 0;
 		char *name = ctx->icon_filename[j];
 		size_t name_len = os_strlen(name);
@@ -2812,7 +2818,7 @@
 		}
 	}
 
-	for (j = 0; j < ctx->icon_count; j++) {
+	for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
 		int found = 0;
 
 		for (i = 0; i < cert->num_logo; i++) {
@@ -3077,13 +3083,15 @@
 			usage();
 			exit(0);
 		}
-		cmd_prov(&ctx, argv[optind + 1], argv[optind + 2]);
+		ctx.ca_fname = argv[optind + 2];
+		cmd_prov(&ctx, argv[optind + 1]);
 	} else if (strcmp(argv[optind], "sim_prov") == 0) {
 		if (argc - optind < 2) {
 			usage();
 			exit(0);
 		}
-		cmd_sim_prov(&ctx, argv[optind + 1], argv[optind + 2]);
+		ctx.ca_fname = argv[optind + 2];
+		cmd_sim_prov(&ctx, argv[optind + 1]);
 	} else if (strcmp(argv[optind], "dl_osu_ca") == 0) {
 		if (argc - optind < 2) {
 			usage();
@@ -3107,13 +3115,11 @@
 			usage();
 			exit(0);
 		}
-		cmd_osu_select(&ctx, argv[optind + 1], 2,
-			       argc > optind + 2 ? argv[optind + 2] : NULL,
-			       1, NULL);
+		ctx.ca_fname = argc > optind + 2 ? argv[optind + 2] : NULL;
+		cmd_osu_select(&ctx, argv[optind + 1], 2, 1, NULL);
 	} else if (strcmp(argv[optind], "signup") == 0) {
-		ret = cmd_signup(&ctx,
-				 argc > optind + 1 ? argv[optind + 1] : NULL,
-				 no_prod_assoc, friendly_name);
+		ctx.ca_fname = argc > optind + 1 ? argv[optind + 1] : NULL;
+		ret = cmd_signup(&ctx, no_prod_assoc, friendly_name);
 	} else if (strcmp(argv[optind], "set_pps") == 0) {
 		if (argc - optind < 2) {
 			usage();
@@ -3131,14 +3137,15 @@
 			usage();
 			exit(0);
 		}
-		cmd_oma_dm_prov(&ctx, argv[optind + 1], argv[optind + 2]);
+		ctx.ca_fname = argv[optind + 2];
+		cmd_oma_dm_prov(&ctx, argv[optind + 1]);
 	} else if (strcmp(argv[optind], "oma_dm_sim_prov") == 0) {
 		if (argc - optind < 2) {
 			usage();
 			exit(0);
 		}
-		if (cmd_oma_dm_sim_prov(&ctx, argv[optind + 1],
-					argv[optind + 2]) < 0) {
+		ctx.ca_fname = argv[optind + 2];
+		if (cmd_oma_dm_sim_prov(&ctx, argv[optind + 1]) < 0) {
 			write_summary(&ctx, "Failed to complete OMA DM SIM provisioning");
 			return -1;
 		}
@@ -3186,11 +3193,11 @@
 		wpa_printf(MSG_INFO, "Unknown command '%s'", argv[optind]);
 	}
 
+	deinit_ctx(&ctx);
 	wpa_printf(MSG_DEBUG,
 		   "===[hs20-osu-client END ]======================");
 
 	wpa_debug_close_file();
-	deinit_ctx(&ctx);
 
 	return ret;
 }