http_curl.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /*
  2. * HTTP wrapper for libcurl
  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 <curl/curl.h>
  10. #ifdef EAP_TLS_OPENSSL
  11. #include <openssl/ssl.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509v3.h>
  15. #ifdef SSL_set_tlsext_status_type
  16. #ifndef OPENSSL_NO_TLSEXT
  17. #define HAVE_OCSP
  18. #include <openssl/err.h>
  19. #include <openssl/ocsp.h>
  20. #endif /* OPENSSL_NO_TLSEXT */
  21. #endif /* SSL_set_tlsext_status_type */
  22. #endif /* EAP_TLS_OPENSSL */
  23. #include "common.h"
  24. #include "xml-utils.h"
  25. #include "http-utils.h"
  26. #ifdef EAP_TLS_OPENSSL
  27. #include "crypto/tls_openssl.h"
  28. #endif /* EAP_TLS_OPENSSL */
  29. struct http_ctx {
  30. void *ctx;
  31. struct xml_node_ctx *xml;
  32. CURL *curl;
  33. struct curl_slist *curl_hdr;
  34. char *svc_address;
  35. char *svc_ca_fname;
  36. char *svc_username;
  37. char *svc_password;
  38. char *svc_client_cert;
  39. char *svc_client_key;
  40. char *curl_buf;
  41. size_t curl_buf_len;
  42. int (*cert_cb)(void *ctx, struct http_cert *cert);
  43. void *cert_cb_ctx;
  44. enum {
  45. NO_OCSP, OPTIONAL_OCSP, MANDATORY_OCSP
  46. } ocsp;
  47. X509 *peer_cert;
  48. X509 *peer_issuer;
  49. X509 *peer_issuer_issuer;
  50. const char *last_err;
  51. };
  52. static void clear_curl(struct http_ctx *ctx)
  53. {
  54. if (ctx->curl) {
  55. curl_easy_cleanup(ctx->curl);
  56. ctx->curl = NULL;
  57. }
  58. if (ctx->curl_hdr) {
  59. curl_slist_free_all(ctx->curl_hdr);
  60. ctx->curl_hdr = NULL;
  61. }
  62. }
  63. static void clone_str(char **dst, const char *src)
  64. {
  65. os_free(*dst);
  66. if (src)
  67. *dst = os_strdup(src);
  68. else
  69. *dst = NULL;
  70. }
  71. static void debug_dump(struct http_ctx *ctx, const char *title,
  72. const char *buf, size_t len)
  73. {
  74. char *txt;
  75. size_t i;
  76. for (i = 0; i < len; i++) {
  77. if (buf[i] < 32 && buf[i] != '\t' && buf[i] != '\n' &&
  78. buf[i] != '\r') {
  79. wpa_hexdump_ascii(MSG_MSGDUMP, title, buf, len);
  80. return;
  81. }
  82. }
  83. txt = os_malloc(len + 1);
  84. if (txt == NULL)
  85. return;
  86. os_memcpy(txt, buf, len);
  87. txt[len] = '\0';
  88. while (len > 0) {
  89. len--;
  90. if (txt[len] == '\n' || txt[len] == '\r')
  91. txt[len] = '\0';
  92. else
  93. break;
  94. }
  95. wpa_printf(MSG_MSGDUMP, "%s[%s]", title, txt);
  96. os_free(txt);
  97. }
  98. static int curl_cb_debug(CURL *curl, curl_infotype info, char *buf, size_t len,
  99. void *userdata)
  100. {
  101. struct http_ctx *ctx = userdata;
  102. switch (info) {
  103. case CURLINFO_TEXT:
  104. debug_dump(ctx, "CURLINFO_TEXT", buf, len);
  105. break;
  106. case CURLINFO_HEADER_IN:
  107. debug_dump(ctx, "CURLINFO_HEADER_IN", buf, len);
  108. break;
  109. case CURLINFO_HEADER_OUT:
  110. debug_dump(ctx, "CURLINFO_HEADER_OUT", buf, len);
  111. break;
  112. case CURLINFO_DATA_IN:
  113. debug_dump(ctx, "CURLINFO_DATA_IN", buf, len);
  114. break;
  115. case CURLINFO_DATA_OUT:
  116. debug_dump(ctx, "CURLINFO_DATA_OUT", buf, len);
  117. break;
  118. case CURLINFO_SSL_DATA_IN:
  119. wpa_printf(MSG_DEBUG, "debug - CURLINFO_SSL_DATA_IN - %d",
  120. (int) len);
  121. break;
  122. case CURLINFO_SSL_DATA_OUT:
  123. wpa_printf(MSG_DEBUG, "debug - CURLINFO_SSL_DATA_OUT - %d",
  124. (int) len);
  125. break;
  126. case CURLINFO_END:
  127. wpa_printf(MSG_DEBUG, "debug - CURLINFO_END - %d",
  128. (int) len);
  129. break;
  130. }
  131. return 0;
  132. }
  133. static size_t curl_cb_write(void *ptr, size_t size, size_t nmemb,
  134. void *userdata)
  135. {
  136. struct http_ctx *ctx = userdata;
  137. char *n;
  138. n = os_realloc(ctx->curl_buf, ctx->curl_buf_len + size * nmemb + 1);
  139. if (n == NULL)
  140. return 0;
  141. ctx->curl_buf = n;
  142. os_memcpy(n + ctx->curl_buf_len, ptr, size * nmemb);
  143. n[ctx->curl_buf_len + size * nmemb] = '\0';
  144. ctx->curl_buf_len += size * nmemb;
  145. return size * nmemb;
  146. }
  147. #ifdef EAP_TLS_OPENSSL
  148. static void debug_dump_cert(const char *title, X509 *cert)
  149. {
  150. BIO *out;
  151. char *txt;
  152. size_t rlen;
  153. out = BIO_new(BIO_s_mem());
  154. if (!out)
  155. return;
  156. X509_print_ex(out, cert, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  157. rlen = BIO_ctrl_pending(out);
  158. txt = os_malloc(rlen + 1);
  159. if (txt) {
  160. int res = BIO_read(out, txt, rlen);
  161. if (res > 0) {
  162. txt[res] = '\0';
  163. wpa_printf(MSG_MSGDUMP, "%s:\n%s", title, txt);
  164. }
  165. os_free(txt);
  166. }
  167. BIO_free(out);
  168. }
  169. static void add_alt_name_othername(struct http_ctx *ctx, struct http_cert *cert,
  170. OTHERNAME *o)
  171. {
  172. char txt[100];
  173. int res;
  174. struct http_othername *on;
  175. ASN1_TYPE *val;
  176. on = os_realloc_array(cert->othername, cert->num_othername + 1,
  177. sizeof(struct http_othername));
  178. if (on == NULL)
  179. return;
  180. cert->othername = on;
  181. on = &on[cert->num_othername];
  182. os_memset(on, 0, sizeof(*on));
  183. res = OBJ_obj2txt(txt, sizeof(txt), o->type_id, 1);
  184. if (res < 0 || res >= (int) sizeof(txt))
  185. return;
  186. on->oid = os_strdup(txt);
  187. if (on->oid == NULL)
  188. return;
  189. val = o->value;
  190. on->data = val->value.octet_string->data;
  191. on->len = val->value.octet_string->length;
  192. cert->num_othername++;
  193. }
  194. static void add_alt_name_dns(struct http_ctx *ctx, struct http_cert *cert,
  195. ASN1_STRING *name)
  196. {
  197. char *buf;
  198. char **n;
  199. buf = NULL;
  200. if (ASN1_STRING_to_UTF8((unsigned char **) &buf, name) < 0)
  201. return;
  202. n = os_realloc_array(cert->dnsname, cert->num_dnsname + 1,
  203. sizeof(char *));
  204. if (n == NULL)
  205. return;
  206. cert->dnsname = n;
  207. n[cert->num_dnsname] = buf;
  208. cert->num_dnsname++;
  209. }
  210. static void add_alt_name(struct http_ctx *ctx, struct http_cert *cert,
  211. const GENERAL_NAME *name)
  212. {
  213. switch (name->type) {
  214. case GEN_OTHERNAME:
  215. add_alt_name_othername(ctx, cert, name->d.otherName);
  216. break;
  217. case GEN_DNS:
  218. add_alt_name_dns(ctx, cert, name->d.dNSName);
  219. break;
  220. }
  221. }
  222. static void add_alt_names(struct http_ctx *ctx, struct http_cert *cert,
  223. GENERAL_NAMES *names)
  224. {
  225. int num, i;
  226. num = sk_GENERAL_NAME_num(names);
  227. for (i = 0; i < num; i++) {
  228. const GENERAL_NAME *name;
  229. name = sk_GENERAL_NAME_value(names, i);
  230. add_alt_name(ctx, cert, name);
  231. }
  232. }
  233. /* RFC 3709 */
  234. typedef struct {
  235. X509_ALGOR *hashAlg;
  236. ASN1_OCTET_STRING *hashValue;
  237. } HashAlgAndValue;
  238. typedef struct {
  239. STACK_OF(HashAlgAndValue) *refStructHash;
  240. STACK_OF(ASN1_IA5STRING) *refStructURI;
  241. } LogotypeReference;
  242. typedef struct {
  243. ASN1_IA5STRING *mediaType;
  244. STACK_OF(HashAlgAndValue) *logotypeHash;
  245. STACK_OF(ASN1_IA5STRING) *logotypeURI;
  246. } LogotypeDetails;
  247. typedef struct {
  248. int type;
  249. union {
  250. ASN1_INTEGER *numBits;
  251. ASN1_INTEGER *tableSize;
  252. } d;
  253. } LogotypeImageResolution;
  254. typedef struct {
  255. ASN1_INTEGER *type; /* LogotypeImageType ::= INTEGER */
  256. ASN1_INTEGER *fileSize;
  257. ASN1_INTEGER *xSize;
  258. ASN1_INTEGER *ySize;
  259. LogotypeImageResolution *resolution;
  260. ASN1_IA5STRING *language;
  261. } LogotypeImageInfo;
  262. typedef struct {
  263. LogotypeDetails *imageDetails;
  264. LogotypeImageInfo *imageInfo;
  265. } LogotypeImage;
  266. typedef struct {
  267. ASN1_INTEGER *fileSize;
  268. ASN1_INTEGER *playTime;
  269. ASN1_INTEGER *channels;
  270. ASN1_INTEGER *sampleRate;
  271. ASN1_IA5STRING *language;
  272. } LogotypeAudioInfo;
  273. typedef struct {
  274. LogotypeDetails *audioDetails;
  275. LogotypeAudioInfo *audioInfo;
  276. } LogotypeAudio;
  277. typedef struct {
  278. STACK_OF(LogotypeImage) *image;
  279. STACK_OF(LogotypeAudio) *audio;
  280. } LogotypeData;
  281. typedef struct {
  282. int type;
  283. union {
  284. LogotypeData *direct;
  285. LogotypeReference *indirect;
  286. } d;
  287. } LogotypeInfo;
  288. typedef struct {
  289. ASN1_OBJECT *logotypeType;
  290. LogotypeInfo *info;
  291. } OtherLogotypeInfo;
  292. typedef struct {
  293. STACK_OF(LogotypeInfo) *communityLogos;
  294. LogotypeInfo *issuerLogo;
  295. LogotypeInfo *subjectLogo;
  296. STACK_OF(OtherLogotypeInfo) *otherLogos;
  297. } LogotypeExtn;
  298. ASN1_SEQUENCE(HashAlgAndValue) = {
  299. ASN1_SIMPLE(HashAlgAndValue, hashAlg, X509_ALGOR),
  300. ASN1_SIMPLE(HashAlgAndValue, hashValue, ASN1_OCTET_STRING)
  301. } ASN1_SEQUENCE_END(HashAlgAndValue);
  302. ASN1_SEQUENCE(LogotypeReference) = {
  303. ASN1_SEQUENCE_OF(LogotypeReference, refStructHash, HashAlgAndValue),
  304. ASN1_SEQUENCE_OF(LogotypeReference, refStructURI, ASN1_IA5STRING)
  305. } ASN1_SEQUENCE_END(LogotypeReference);
  306. ASN1_SEQUENCE(LogotypeDetails) = {
  307. ASN1_SIMPLE(LogotypeDetails, mediaType, ASN1_IA5STRING),
  308. ASN1_SEQUENCE_OF(LogotypeDetails, logotypeHash, HashAlgAndValue),
  309. ASN1_SEQUENCE_OF(LogotypeDetails, logotypeURI, ASN1_IA5STRING)
  310. } ASN1_SEQUENCE_END(LogotypeDetails);
  311. ASN1_CHOICE(LogotypeImageResolution) = {
  312. ASN1_IMP(LogotypeImageResolution, d.numBits, ASN1_INTEGER, 1),
  313. ASN1_IMP(LogotypeImageResolution, d.tableSize, ASN1_INTEGER, 2)
  314. } ASN1_CHOICE_END(LogotypeImageResolution);
  315. ASN1_SEQUENCE(LogotypeImageInfo) = {
  316. ASN1_IMP_OPT(LogotypeImageInfo, type, ASN1_INTEGER, 0),
  317. ASN1_SIMPLE(LogotypeImageInfo, fileSize, ASN1_INTEGER),
  318. ASN1_SIMPLE(LogotypeImageInfo, xSize, ASN1_INTEGER),
  319. ASN1_SIMPLE(LogotypeImageInfo, ySize, ASN1_INTEGER),
  320. ASN1_OPT(LogotypeImageInfo, resolution, LogotypeImageResolution),
  321. ASN1_IMP_OPT(LogotypeImageInfo, language, ASN1_IA5STRING, 4),
  322. } ASN1_SEQUENCE_END(LogotypeImageInfo);
  323. ASN1_SEQUENCE(LogotypeImage) = {
  324. ASN1_SIMPLE(LogotypeImage, imageDetails, LogotypeDetails),
  325. ASN1_OPT(LogotypeImage, imageInfo, LogotypeImageInfo)
  326. } ASN1_SEQUENCE_END(LogotypeImage);
  327. ASN1_SEQUENCE(LogotypeAudioInfo) = {
  328. ASN1_SIMPLE(LogotypeAudioInfo, fileSize, ASN1_INTEGER),
  329. ASN1_SIMPLE(LogotypeAudioInfo, playTime, ASN1_INTEGER),
  330. ASN1_SIMPLE(LogotypeAudioInfo, channels, ASN1_INTEGER),
  331. ASN1_IMP_OPT(LogotypeAudioInfo, sampleRate, ASN1_INTEGER, 3),
  332. ASN1_IMP_OPT(LogotypeAudioInfo, language, ASN1_IA5STRING, 4)
  333. } ASN1_SEQUENCE_END(LogotypeAudioInfo);
  334. ASN1_SEQUENCE(LogotypeAudio) = {
  335. ASN1_SIMPLE(LogotypeAudio, audioDetails, LogotypeDetails),
  336. ASN1_OPT(LogotypeAudio, audioInfo, LogotypeAudioInfo)
  337. } ASN1_SEQUENCE_END(LogotypeAudio);
  338. ASN1_SEQUENCE(LogotypeData) = {
  339. ASN1_SEQUENCE_OF_OPT(LogotypeData, image, LogotypeImage),
  340. ASN1_IMP_SEQUENCE_OF_OPT(LogotypeData, audio, LogotypeAudio, 1)
  341. } ASN1_SEQUENCE_END(LogotypeData);
  342. ASN1_CHOICE(LogotypeInfo) = {
  343. ASN1_IMP(LogotypeInfo, d.direct, LogotypeData, 0),
  344. ASN1_IMP(LogotypeInfo, d.indirect, LogotypeReference, 1)
  345. } ASN1_CHOICE_END(LogotypeInfo);
  346. ASN1_SEQUENCE(OtherLogotypeInfo) = {
  347. ASN1_SIMPLE(OtherLogotypeInfo, logotypeType, ASN1_OBJECT),
  348. ASN1_SIMPLE(OtherLogotypeInfo, info, LogotypeInfo)
  349. } ASN1_SEQUENCE_END(OtherLogotypeInfo);
  350. ASN1_SEQUENCE(LogotypeExtn) = {
  351. ASN1_EXP_SEQUENCE_OF_OPT(LogotypeExtn, communityLogos, LogotypeInfo, 0),
  352. ASN1_EXP_OPT(LogotypeExtn, issuerLogo, LogotypeInfo, 1),
  353. ASN1_EXP_OPT(LogotypeExtn, issuerLogo, LogotypeInfo, 2),
  354. ASN1_EXP_SEQUENCE_OF_OPT(LogotypeExtn, otherLogos, OtherLogotypeInfo, 3)
  355. } ASN1_SEQUENCE_END(LogotypeExtn);
  356. IMPLEMENT_ASN1_FUNCTIONS(LogotypeExtn);
  357. #ifdef OPENSSL_IS_BORINGSSL
  358. #define sk_LogotypeInfo_num(st) \
  359. sk_num(CHECKED_CAST(_STACK *, STACK_OF(LogotypeInfo) *, (st)))
  360. #define sk_LogotypeInfo_value(st, i) (LogotypeInfo *) \
  361. sk_value(CHECKED_CAST(_STACK *, const STACK_OF(LogotypeInfo) *, (st)), (i))
  362. #define sk_LogotypeImage_num(st) \
  363. sk_num(CHECKED_CAST(_STACK *, STACK_OF(LogotypeImage) *, (st)))
  364. #define sk_LogotypeImage_value(st, i) (LogotypeImage *) \
  365. sk_value(CHECKED_CAST(_STACK *, const STACK_OF(LogotypeImage) *, (st)), (i))
  366. #define sk_LogotypeAudio_num(st) \
  367. sk_num(CHECKED_CAST(_STACK *, STACK_OF(LogotypeAudio) *, (st)))
  368. #define sk_LogotypeAudio_value(st, i) (LogotypeAudio *) \
  369. sk_value(CHECK_CAST(_STACK *, const STACK_OF(LogotypeAudio) *, (st)), (i))
  370. #define sk_HashAlgAndValue_num(st) \
  371. sk_num(CHECKED_CAST(_STACK *, STACK_OF(HashAlgAndValue) *, (st)))
  372. #define sk_HashAlgAndValue_value(st, i) (HashAlgAndValue *) \
  373. sk_value(CHECKED_CAST(_STACK *, const STACK_OF(HashAlgAndValue) *, (st)), (i))
  374. #define sk_ASN1_IA5STRING_num(st) \
  375. sk_num(CHECKED_CAST(_STACK *, STACK_OF(ASN1_IA5STRING) *, (st)))
  376. #define sk_ASN1_IA5STRING_value(st, i) (ASN1_IA5STRING *) \
  377. sk_value(CHECKED_CAST(_STACK *, const STACK_OF(ASN1_IA5STRING) *, (st)), (i))
  378. #else /* OPENSSL_IS_BORINGSSL */
  379. #define sk_LogotypeInfo_num(st) SKM_sk_num(LogotypeInfo, (st))
  380. #define sk_LogotypeInfo_value(st, i) SKM_sk_value(LogotypeInfo, (st), (i))
  381. #define sk_LogotypeImage_num(st) SKM_sk_num(LogotypeImage, (st))
  382. #define sk_LogotypeImage_value(st, i) SKM_sk_value(LogotypeImage, (st), (i))
  383. #define sk_LogotypeAudio_num(st) SKM_sk_num(LogotypeAudio, (st))
  384. #define sk_LogotypeAudio_value(st, i) SKM_sk_value(LogotypeAudio, (st), (i))
  385. #define sk_HashAlgAndValue_num(st) SKM_sk_num(HashAlgAndValue, (st))
  386. #define sk_HashAlgAndValue_value(st, i) SKM_sk_value(HashAlgAndValue, (st), (i))
  387. #define sk_ASN1_IA5STRING_num(st) SKM_sk_num(ASN1_IA5STRING, (st))
  388. #define sk_ASN1_IA5STRING_value(st, i) SKM_sk_value(ASN1_IA5STRING, (st), (i))
  389. #endif /* OPENSSL_IS_BORINGSSL */
  390. static void add_logo(struct http_ctx *ctx, struct http_cert *hcert,
  391. HashAlgAndValue *hash, ASN1_IA5STRING *uri)
  392. {
  393. char txt[100];
  394. int res, len;
  395. struct http_logo *n;
  396. if (hash == NULL || uri == NULL)
  397. return;
  398. res = OBJ_obj2txt(txt, sizeof(txt), hash->hashAlg->algorithm, 1);
  399. if (res < 0 || res >= (int) sizeof(txt))
  400. return;
  401. n = os_realloc_array(hcert->logo, hcert->num_logo + 1,
  402. sizeof(struct http_logo));
  403. if (n == NULL)
  404. return;
  405. hcert->logo = n;
  406. n = &hcert->logo[hcert->num_logo];
  407. os_memset(n, 0, sizeof(*n));
  408. n->alg_oid = os_strdup(txt);
  409. if (n->alg_oid == NULL)
  410. return;
  411. n->hash_len = ASN1_STRING_length(hash->hashValue);
  412. n->hash = os_malloc(n->hash_len);
  413. if (n->hash == NULL) {
  414. os_free(n->alg_oid);
  415. return;
  416. }
  417. os_memcpy(n->hash, ASN1_STRING_data(hash->hashValue), n->hash_len);
  418. len = ASN1_STRING_length(uri);
  419. n->uri = os_malloc(len + 1);
  420. if (n->uri == NULL) {
  421. os_free(n->alg_oid);
  422. os_free(n->hash);
  423. return;
  424. }
  425. os_memcpy(n->uri, ASN1_STRING_data(uri), len);
  426. n->uri[len] = '\0';
  427. hcert->num_logo++;
  428. }
  429. static void add_logo_direct(struct http_ctx *ctx, struct http_cert *hcert,
  430. LogotypeData *data)
  431. {
  432. int i, num;
  433. if (data->image == NULL)
  434. return;
  435. num = sk_LogotypeImage_num(data->image);
  436. for (i = 0; i < num; i++) {
  437. LogotypeImage *image;
  438. LogotypeDetails *details;
  439. int j, hash_num, uri_num;
  440. HashAlgAndValue *found_hash = NULL;
  441. image = sk_LogotypeImage_value(data->image, i);
  442. if (image == NULL)
  443. continue;
  444. details = image->imageDetails;
  445. if (details == NULL)
  446. continue;
  447. hash_num = sk_HashAlgAndValue_num(details->logotypeHash);
  448. for (j = 0; j < hash_num; j++) {
  449. HashAlgAndValue *hash;
  450. char txt[100];
  451. int res;
  452. hash = sk_HashAlgAndValue_value(details->logotypeHash,
  453. j);
  454. if (hash == NULL)
  455. continue;
  456. res = OBJ_obj2txt(txt, sizeof(txt),
  457. hash->hashAlg->algorithm, 1);
  458. if (res < 0 || res >= (int) sizeof(txt))
  459. continue;
  460. if (os_strcmp(txt, "2.16.840.1.101.3.4.2.1") == 0) {
  461. found_hash = hash;
  462. break;
  463. }
  464. }
  465. if (!found_hash) {
  466. wpa_printf(MSG_DEBUG, "OpenSSL: No SHA256 hash found for the logo");
  467. continue;
  468. }
  469. uri_num = sk_ASN1_IA5STRING_num(details->logotypeURI);
  470. for (j = 0; j < uri_num; j++) {
  471. ASN1_IA5STRING *uri;
  472. uri = sk_ASN1_IA5STRING_value(details->logotypeURI, j);
  473. add_logo(ctx, hcert, found_hash, uri);
  474. }
  475. }
  476. }
  477. static void add_logo_indirect(struct http_ctx *ctx, struct http_cert *hcert,
  478. LogotypeReference *ref)
  479. {
  480. int j, hash_num, uri_num;
  481. hash_num = sk_HashAlgAndValue_num(ref->refStructHash);
  482. uri_num = sk_ASN1_IA5STRING_num(ref->refStructURI);
  483. if (hash_num != uri_num) {
  484. wpa_printf(MSG_INFO, "Unexpected LogotypeReference array size difference %d != %d",
  485. hash_num, uri_num);
  486. return;
  487. }
  488. for (j = 0; j < hash_num; j++) {
  489. HashAlgAndValue *hash;
  490. ASN1_IA5STRING *uri;
  491. hash = sk_HashAlgAndValue_value(ref->refStructHash, j);
  492. uri = sk_ASN1_IA5STRING_value(ref->refStructURI, j);
  493. add_logo(ctx, hcert, hash, uri);
  494. }
  495. }
  496. static void i2r_HashAlgAndValue(HashAlgAndValue *hash, BIO *out, int indent)
  497. {
  498. int i;
  499. const unsigned char *data;
  500. BIO_printf(out, "%*shashAlg: ", indent, "");
  501. i2a_ASN1_OBJECT(out, hash->hashAlg->algorithm);
  502. BIO_printf(out, "\n");
  503. BIO_printf(out, "%*shashValue: ", indent, "");
  504. data = hash->hashValue->data;
  505. for (i = 0; i < hash->hashValue->length; i++)
  506. BIO_printf(out, "%s%02x", i > 0 ? ":" : "", data[i]);
  507. BIO_printf(out, "\n");
  508. }
  509. static void i2r_LogotypeDetails(LogotypeDetails *details, BIO *out, int indent)
  510. {
  511. int i, num;
  512. BIO_printf(out, "%*sLogotypeDetails\n", indent, "");
  513. if (details->mediaType) {
  514. BIO_printf(out, "%*smediaType: ", indent, "");
  515. ASN1_STRING_print(out, details->mediaType);
  516. BIO_printf(out, "\n");
  517. }
  518. num = details->logotypeHash ?
  519. sk_HashAlgAndValue_num(details->logotypeHash) : 0;
  520. for (i = 0; i < num; i++) {
  521. HashAlgAndValue *hash;
  522. hash = sk_HashAlgAndValue_value(details->logotypeHash, i);
  523. i2r_HashAlgAndValue(hash, out, indent);
  524. }
  525. num = details->logotypeURI ?
  526. sk_ASN1_IA5STRING_num(details->logotypeURI) : 0;
  527. for (i = 0; i < num; i++) {
  528. ASN1_IA5STRING *uri;
  529. uri = sk_ASN1_IA5STRING_value(details->logotypeURI, i);
  530. BIO_printf(out, "%*slogotypeURI: ", indent, "");
  531. ASN1_STRING_print(out, uri);
  532. BIO_printf(out, "\n");
  533. }
  534. }
  535. static void i2r_LogotypeImageInfo(LogotypeImageInfo *info, BIO *out, int indent)
  536. {
  537. long val;
  538. BIO_printf(out, "%*sLogotypeImageInfo\n", indent, "");
  539. if (info->type) {
  540. val = ASN1_INTEGER_get(info->type);
  541. BIO_printf(out, "%*stype: %ld\n", indent, "", val);
  542. } else {
  543. BIO_printf(out, "%*stype: default (1)\n", indent, "");
  544. }
  545. val = ASN1_INTEGER_get(info->fileSize);
  546. BIO_printf(out, "%*sfileSize: %ld\n", indent, "", val);
  547. val = ASN1_INTEGER_get(info->xSize);
  548. BIO_printf(out, "%*sxSize: %ld\n", indent, "", val);
  549. val = ASN1_INTEGER_get(info->ySize);
  550. BIO_printf(out, "%*sySize: %ld\n", indent, "", val);
  551. if (info->resolution) {
  552. BIO_printf(out, "%*sresolution [%d]\n", indent, "",
  553. info->resolution->type);
  554. switch (info->resolution->type) {
  555. case 0:
  556. val = ASN1_INTEGER_get(info->resolution->d.numBits);
  557. BIO_printf(out, "%*snumBits: %ld\n", indent, "", val);
  558. break;
  559. case 1:
  560. val = ASN1_INTEGER_get(info->resolution->d.tableSize);
  561. BIO_printf(out, "%*stableSize: %ld\n", indent, "", val);
  562. break;
  563. }
  564. }
  565. if (info->language) {
  566. BIO_printf(out, "%*slanguage: ", indent, "");
  567. ASN1_STRING_print(out, info->language);
  568. BIO_printf(out, "\n");
  569. }
  570. }
  571. static void i2r_LogotypeImage(LogotypeImage *image, BIO *out, int indent)
  572. {
  573. BIO_printf(out, "%*sLogotypeImage\n", indent, "");
  574. if (image->imageDetails) {
  575. i2r_LogotypeDetails(image->imageDetails, out, indent + 4);
  576. }
  577. if (image->imageInfo) {
  578. i2r_LogotypeImageInfo(image->imageInfo, out, indent + 4);
  579. }
  580. }
  581. static void i2r_LogotypeData(LogotypeData *data, const char *title, BIO *out,
  582. int indent)
  583. {
  584. int i, num;
  585. BIO_printf(out, "%*s%s - LogotypeData\n", indent, "", title);
  586. num = data->image ? sk_LogotypeImage_num(data->image) : 0;
  587. for (i = 0; i < num; i++) {
  588. LogotypeImage *image = sk_LogotypeImage_value(data->image, i);
  589. i2r_LogotypeImage(image, out, indent + 4);
  590. }
  591. num = data->audio ? sk_LogotypeAudio_num(data->audio) : 0;
  592. for (i = 0; i < num; i++) {
  593. BIO_printf(out, "%*saudio: TODO\n", indent, "");
  594. }
  595. }
  596. static void i2r_LogotypeReference(LogotypeReference *ref, const char *title,
  597. BIO *out, int indent)
  598. {
  599. int i, hash_num, uri_num;
  600. BIO_printf(out, "%*s%s - LogotypeReference\n", indent, "", title);
  601. hash_num = ref->refStructHash ?
  602. sk_HashAlgAndValue_num(ref->refStructHash) : 0;
  603. uri_num = ref->refStructURI ?
  604. sk_ASN1_IA5STRING_num(ref->refStructURI) : 0;
  605. if (hash_num != uri_num) {
  606. BIO_printf(out, "%*sUnexpected LogotypeReference array size difference %d != %d\n",
  607. indent, "", hash_num, uri_num);
  608. return;
  609. }
  610. for (i = 0; i < hash_num; i++) {
  611. HashAlgAndValue *hash;
  612. ASN1_IA5STRING *uri;
  613. hash = sk_HashAlgAndValue_value(ref->refStructHash, i);
  614. i2r_HashAlgAndValue(hash, out, indent);
  615. uri = sk_ASN1_IA5STRING_value(ref->refStructURI, i);
  616. BIO_printf(out, "%*srefStructURI: ", indent, "");
  617. ASN1_STRING_print(out, uri);
  618. BIO_printf(out, "\n");
  619. }
  620. }
  621. static void i2r_LogotypeInfo(LogotypeInfo *info, const char *title, BIO *out,
  622. int indent)
  623. {
  624. switch (info->type) {
  625. case 0:
  626. i2r_LogotypeData(info->d.direct, title, out, indent);
  627. break;
  628. case 1:
  629. i2r_LogotypeReference(info->d.indirect, title, out, indent);
  630. break;
  631. }
  632. }
  633. static void debug_print_logotypeext(LogotypeExtn *logo)
  634. {
  635. BIO *out;
  636. int i, num;
  637. int indent = 0;
  638. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  639. if (out == NULL)
  640. return;
  641. if (logo->communityLogos) {
  642. num = sk_LogotypeInfo_num(logo->communityLogos);
  643. for (i = 0; i < num; i++) {
  644. LogotypeInfo *info;
  645. info = sk_LogotypeInfo_value(logo->communityLogos, i);
  646. i2r_LogotypeInfo(info, "communityLogo", out, indent);
  647. }
  648. }
  649. if (logo->issuerLogo) {
  650. i2r_LogotypeInfo(logo->issuerLogo, "issuerLogo", out, indent );
  651. }
  652. if (logo->subjectLogo) {
  653. i2r_LogotypeInfo(logo->subjectLogo, "subjectLogo", out, indent);
  654. }
  655. if (logo->otherLogos) {
  656. BIO_printf(out, "%*sotherLogos - TODO\n", indent, "");
  657. }
  658. BIO_free(out);
  659. }
  660. static void add_logotype_ext(struct http_ctx *ctx, struct http_cert *hcert,
  661. X509 *cert)
  662. {
  663. ASN1_OBJECT *obj;
  664. int pos;
  665. X509_EXTENSION *ext;
  666. ASN1_OCTET_STRING *os;
  667. LogotypeExtn *logo;
  668. const unsigned char *data;
  669. int i, num;
  670. obj = OBJ_txt2obj("1.3.6.1.5.5.7.1.12", 0);
  671. if (obj == NULL)
  672. return;
  673. pos = X509_get_ext_by_OBJ(cert, obj, -1);
  674. if (pos < 0) {
  675. wpa_printf(MSG_INFO, "No logotype extension included");
  676. return;
  677. }
  678. wpa_printf(MSG_INFO, "Parsing logotype extension");
  679. ext = X509_get_ext(cert, pos);
  680. if (!ext) {
  681. wpa_printf(MSG_INFO, "Could not get logotype extension");
  682. return;
  683. }
  684. os = X509_EXTENSION_get_data(ext);
  685. if (os == NULL) {
  686. wpa_printf(MSG_INFO, "Could not get logotype extension data");
  687. return;
  688. }
  689. wpa_hexdump(MSG_DEBUG, "logotypeExtn",
  690. ASN1_STRING_data(os), ASN1_STRING_length(os));
  691. data = ASN1_STRING_data(os);
  692. logo = d2i_LogotypeExtn(NULL, &data, ASN1_STRING_length(os));
  693. if (logo == NULL) {
  694. wpa_printf(MSG_INFO, "Failed to parse logotypeExtn");
  695. return;
  696. }
  697. if (wpa_debug_level < MSG_INFO)
  698. debug_print_logotypeext(logo);
  699. if (!logo->communityLogos) {
  700. wpa_printf(MSG_INFO, "No communityLogos included");
  701. LogotypeExtn_free(logo);
  702. return;
  703. }
  704. num = sk_LogotypeInfo_num(logo->communityLogos);
  705. for (i = 0; i < num; i++) {
  706. LogotypeInfo *info;
  707. info = sk_LogotypeInfo_value(logo->communityLogos, i);
  708. switch (info->type) {
  709. case 0:
  710. add_logo_direct(ctx, hcert, info->d.direct);
  711. break;
  712. case 1:
  713. add_logo_indirect(ctx, hcert, info->d.indirect);
  714. break;
  715. }
  716. }
  717. LogotypeExtn_free(logo);
  718. }
  719. static void parse_cert(struct http_ctx *ctx, struct http_cert *hcert,
  720. X509 *cert, GENERAL_NAMES **names)
  721. {
  722. os_memset(hcert, 0, sizeof(*hcert));
  723. *names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
  724. if (*names) {
  725. add_alt_names(ctx, hcert, *names);
  726. sk_GENERAL_NAME_pop_free(*names, GENERAL_NAME_free);
  727. }
  728. add_logotype_ext(ctx, hcert, cert);
  729. }
  730. static void parse_cert_free(struct http_cert *hcert, GENERAL_NAMES *names)
  731. {
  732. unsigned int i;
  733. for (i = 0; i < hcert->num_dnsname; i++)
  734. OPENSSL_free(hcert->dnsname[i]);
  735. os_free(hcert->dnsname);
  736. for (i = 0; i < hcert->num_othername; i++)
  737. os_free(hcert->othername[i].oid);
  738. os_free(hcert->othername);
  739. for (i = 0; i < hcert->num_logo; i++) {
  740. os_free(hcert->logo[i].alg_oid);
  741. os_free(hcert->logo[i].hash);
  742. os_free(hcert->logo[i].uri);
  743. }
  744. os_free(hcert->logo);
  745. sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
  746. }
  747. static int validate_server_cert(struct http_ctx *ctx, X509 *cert)
  748. {
  749. GENERAL_NAMES *names;
  750. struct http_cert hcert;
  751. int ret;
  752. if (ctx->cert_cb == NULL) {
  753. wpa_printf(MSG_DEBUG, "%s: no cert_cb configured", __func__);
  754. return 0;
  755. }
  756. if (0) {
  757. BIO *out;
  758. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  759. X509_print_ex(out, cert, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  760. BIO_free(out);
  761. }
  762. parse_cert(ctx, &hcert, cert, &names);
  763. ret = ctx->cert_cb(ctx->cert_cb_ctx, &hcert);
  764. parse_cert_free(&hcert, names);
  765. return ret;
  766. }
  767. void http_parse_x509_certificate(struct http_ctx *ctx, const char *fname)
  768. {
  769. BIO *in, *out;
  770. X509 *cert;
  771. GENERAL_NAMES *names;
  772. struct http_cert hcert;
  773. unsigned int i;
  774. in = BIO_new_file(fname, "r");
  775. if (in == NULL) {
  776. wpa_printf(MSG_ERROR, "Could not read '%s'", fname);
  777. return;
  778. }
  779. cert = d2i_X509_bio(in, NULL);
  780. BIO_free(in);
  781. if (cert == NULL) {
  782. wpa_printf(MSG_ERROR, "Could not parse certificate");
  783. return;
  784. }
  785. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  786. if (out) {
  787. X509_print_ex(out, cert, XN_FLAG_COMPAT,
  788. X509_FLAG_COMPAT);
  789. BIO_free(out);
  790. }
  791. wpa_printf(MSG_INFO, "Additional parsing information:");
  792. parse_cert(ctx, &hcert, cert, &names);
  793. for (i = 0; i < hcert.num_othername; i++) {
  794. if (os_strcmp(hcert.othername[i].oid,
  795. "1.3.6.1.4.1.40808.1.1.1") == 0) {
  796. char *name = os_zalloc(hcert.othername[i].len + 1);
  797. if (name) {
  798. os_memcpy(name, hcert.othername[i].data,
  799. hcert.othername[i].len);
  800. wpa_printf(MSG_INFO,
  801. "id-wfa-hotspot-friendlyName: %s",
  802. name);
  803. os_free(name);
  804. }
  805. wpa_hexdump_ascii(MSG_INFO,
  806. "id-wfa-hotspot-friendlyName",
  807. hcert.othername[i].data,
  808. hcert.othername[i].len);
  809. } else {
  810. wpa_printf(MSG_INFO, "subjAltName[othername]: oid=%s",
  811. hcert.othername[i].oid);
  812. wpa_hexdump_ascii(MSG_INFO, "unknown othername",
  813. hcert.othername[i].data,
  814. hcert.othername[i].len);
  815. }
  816. }
  817. parse_cert_free(&hcert, names);
  818. X509_free(cert);
  819. }
  820. static int curl_cb_ssl_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
  821. {
  822. struct http_ctx *ctx;
  823. X509 *cert;
  824. int err, depth;
  825. char buf[256];
  826. X509_NAME *name;
  827. const char *err_str;
  828. SSL *ssl;
  829. SSL_CTX *ssl_ctx;
  830. ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
  831. SSL_get_ex_data_X509_STORE_CTX_idx());
  832. ssl_ctx = ssl->ctx;
  833. ctx = SSL_CTX_get_app_data(ssl_ctx);
  834. wpa_printf(MSG_DEBUG, "curl_cb_ssl_verify, preverify_ok: %d",
  835. preverify_ok);
  836. err = X509_STORE_CTX_get_error(x509_ctx);
  837. err_str = X509_verify_cert_error_string(err);
  838. depth = X509_STORE_CTX_get_error_depth(x509_ctx);
  839. cert = X509_STORE_CTX_get_current_cert(x509_ctx);
  840. if (!cert) {
  841. wpa_printf(MSG_INFO, "No server certificate available");
  842. ctx->last_err = "No server certificate available";
  843. return 0;
  844. }
  845. if (depth == 0)
  846. ctx->peer_cert = cert;
  847. else if (depth == 1)
  848. ctx->peer_issuer = cert;
  849. else if (depth == 2)
  850. ctx->peer_issuer_issuer = cert;
  851. name = X509_get_subject_name(cert);
  852. X509_NAME_oneline(name, buf, sizeof(buf));
  853. wpa_printf(MSG_INFO, "Server certificate chain - depth=%d err=%d (%s) subject=%s",
  854. depth, err, err_str, buf);
  855. debug_dump_cert("Server certificate chain - certificate", cert);
  856. if (depth == 0 && preverify_ok && validate_server_cert(ctx, cert) < 0)
  857. return 0;
  858. #ifdef OPENSSL_IS_BORINGSSL
  859. if (depth == 0 && ctx->ocsp != NO_OCSP && preverify_ok) {
  860. enum ocsp_result res;
  861. res = check_ocsp_resp(ssl_ctx, ssl, cert, ctx->peer_issuer,
  862. ctx->peer_issuer_issuer);
  863. if (res == OCSP_REVOKED) {
  864. preverify_ok = 0;
  865. wpa_printf(MSG_INFO, "OCSP: certificate revoked");
  866. if (err == X509_V_OK)
  867. X509_STORE_CTX_set_error(
  868. x509_ctx, X509_V_ERR_CERT_REVOKED);
  869. } else if (res != OCSP_GOOD && (ctx->ocsp == MANDATORY_OCSP)) {
  870. preverify_ok = 0;
  871. wpa_printf(MSG_INFO,
  872. "OCSP: bad certificate status response");
  873. }
  874. }
  875. #endif /* OPENSSL_IS_BORINGSSL */
  876. if (!preverify_ok)
  877. ctx->last_err = "TLS validation failed";
  878. return preverify_ok;
  879. }
  880. #ifdef HAVE_OCSP
  881. static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
  882. {
  883. BIO *out;
  884. size_t rlen;
  885. char *txt;
  886. int res;
  887. out = BIO_new(BIO_s_mem());
  888. if (!out)
  889. return;
  890. OCSP_RESPONSE_print(out, rsp, 0);
  891. rlen = BIO_ctrl_pending(out);
  892. txt = os_malloc(rlen + 1);
  893. if (!txt) {
  894. BIO_free(out);
  895. return;
  896. }
  897. res = BIO_read(out, txt, rlen);
  898. if (res > 0) {
  899. txt[res] = '\0';
  900. wpa_printf(MSG_MSGDUMP, "OpenSSL: OCSP Response\n%s", txt);
  901. }
  902. os_free(txt);
  903. BIO_free(out);
  904. }
  905. static void tls_show_errors(const char *func, const char *txt)
  906. {
  907. unsigned long err;
  908. wpa_printf(MSG_DEBUG, "OpenSSL: %s - %s %s",
  909. func, txt, ERR_error_string(ERR_get_error(), NULL));
  910. while ((err = ERR_get_error())) {
  911. wpa_printf(MSG_DEBUG, "OpenSSL: pending error: %s",
  912. ERR_error_string(err, NULL));
  913. }
  914. }
  915. static int ocsp_resp_cb(SSL *s, void *arg)
  916. {
  917. struct http_ctx *ctx = arg;
  918. const unsigned char *p;
  919. int len, status, reason;
  920. OCSP_RESPONSE *rsp;
  921. OCSP_BASICRESP *basic;
  922. OCSP_CERTID *id;
  923. ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
  924. X509_STORE *store;
  925. STACK_OF(X509) *certs = NULL;
  926. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  927. if (!p) {
  928. wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
  929. if (ctx->ocsp == MANDATORY_OCSP)
  930. ctx->last_err = "No OCSP response received";
  931. return (ctx->ocsp == MANDATORY_OCSP) ? 0 : 1;
  932. }
  933. wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
  934. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  935. if (!rsp) {
  936. wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
  937. ctx->last_err = "Failed to parse OCSP response";
  938. return 0;
  939. }
  940. ocsp_debug_print_resp(rsp);
  941. status = OCSP_response_status(rsp);
  942. if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
  943. wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
  944. status, OCSP_response_status_str(status));
  945. ctx->last_err = "OCSP responder error";
  946. return 0;
  947. }
  948. basic = OCSP_response_get1_basic(rsp);
  949. if (!basic) {
  950. wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
  951. ctx->last_err = "Could not find BasicOCSPResponse";
  952. return 0;
  953. }
  954. store = SSL_CTX_get_cert_store(s->ctx);
  955. if (ctx->peer_issuer) {
  956. wpa_printf(MSG_DEBUG, "OpenSSL: Add issuer");
  957. debug_dump_cert("OpenSSL: Issuer certificate",
  958. ctx->peer_issuer);
  959. if (X509_STORE_add_cert(store, ctx->peer_issuer) != 1) {
  960. tls_show_errors(__func__,
  961. "OpenSSL: Could not add issuer to certificate store");
  962. }
  963. certs = sk_X509_new_null();
  964. if (certs) {
  965. X509 *cert;
  966. cert = X509_dup(ctx->peer_issuer);
  967. if (cert && !sk_X509_push(certs, cert)) {
  968. tls_show_errors(
  969. __func__,
  970. "OpenSSL: Could not add issuer to OCSP responder trust store");
  971. X509_free(cert);
  972. sk_X509_free(certs);
  973. certs = NULL;
  974. }
  975. if (certs && ctx->peer_issuer_issuer) {
  976. cert = X509_dup(ctx->peer_issuer_issuer);
  977. if (cert && !sk_X509_push(certs, cert)) {
  978. tls_show_errors(
  979. __func__,
  980. "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
  981. X509_free(cert);
  982. }
  983. }
  984. }
  985. }
  986. status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
  987. sk_X509_pop_free(certs, X509_free);
  988. if (status <= 0) {
  989. tls_show_errors(__func__,
  990. "OpenSSL: OCSP response failed verification");
  991. OCSP_BASICRESP_free(basic);
  992. OCSP_RESPONSE_free(rsp);
  993. ctx->last_err = "OCSP response failed verification";
  994. return 0;
  995. }
  996. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
  997. if (!ctx->peer_cert) {
  998. wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
  999. OCSP_BASICRESP_free(basic);
  1000. OCSP_RESPONSE_free(rsp);
  1001. ctx->last_err = "Peer certificate not available for OCSP status check";
  1002. return 0;
  1003. }
  1004. if (!ctx->peer_issuer) {
  1005. wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
  1006. OCSP_BASICRESP_free(basic);
  1007. OCSP_RESPONSE_free(rsp);
  1008. ctx->last_err = "Peer issuer certificate not available for OCSP status check";
  1009. return 0;
  1010. }
  1011. id = OCSP_cert_to_id(NULL, ctx->peer_cert, ctx->peer_issuer);
  1012. if (!id) {
  1013. wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier");
  1014. OCSP_BASICRESP_free(basic);
  1015. OCSP_RESPONSE_free(rsp);
  1016. ctx->last_err = "Could not create OCSP certificate identifier";
  1017. return 0;
  1018. }
  1019. if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
  1020. &this_update, &next_update)) {
  1021. wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
  1022. (ctx->ocsp == MANDATORY_OCSP) ? "" :
  1023. " (OCSP not required)");
  1024. OCSP_CERTID_free(id);
  1025. OCSP_BASICRESP_free(basic);
  1026. OCSP_RESPONSE_free(rsp);
  1027. if (ctx->ocsp == MANDATORY_OCSP)
  1028. ctx->last_err = "Could not find current server certificate from OCSP response";
  1029. return (ctx->ocsp == MANDATORY_OCSP) ? 0 : 1;
  1030. }
  1031. OCSP_CERTID_free(id);
  1032. if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
  1033. tls_show_errors(__func__, "OpenSSL: OCSP status times invalid");
  1034. OCSP_BASICRESP_free(basic);
  1035. OCSP_RESPONSE_free(rsp);
  1036. ctx->last_err = "OCSP status times invalid";
  1037. return 0;
  1038. }
  1039. OCSP_BASICRESP_free(basic);
  1040. OCSP_RESPONSE_free(rsp);
  1041. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
  1042. OCSP_cert_status_str(status));
  1043. if (status == V_OCSP_CERTSTATUS_GOOD)
  1044. return 1;
  1045. if (status == V_OCSP_CERTSTATUS_REVOKED) {
  1046. ctx->last_err = "Server certificate has been revoked";
  1047. return 0;
  1048. }
  1049. if (ctx->ocsp == MANDATORY_OCSP) {
  1050. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
  1051. ctx->last_err = "OCSP status unknown";
  1052. return 0;
  1053. }
  1054. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
  1055. return 1;
  1056. }
  1057. static SSL_METHOD patch_ssl_method;
  1058. static const SSL_METHOD *real_ssl_method;
  1059. static int curl_patch_ssl_new(SSL *s)
  1060. {
  1061. SSL_CTX *ssl = s->ctx;
  1062. int ret;
  1063. ssl->method = real_ssl_method;
  1064. s->method = real_ssl_method;
  1065. ret = s->method->ssl_new(s);
  1066. SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp);
  1067. return ret;
  1068. }
  1069. #endif /* HAVE_OCSP */
  1070. static CURLcode curl_cb_ssl(CURL *curl, void *sslctx, void *parm)
  1071. {
  1072. struct http_ctx *ctx = parm;
  1073. SSL_CTX *ssl = sslctx;
  1074. wpa_printf(MSG_DEBUG, "curl_cb_ssl");
  1075. SSL_CTX_set_app_data(ssl, ctx);
  1076. SSL_CTX_set_verify(ssl, SSL_VERIFY_PEER, curl_cb_ssl_verify);
  1077. #ifdef HAVE_OCSP
  1078. if (ctx->ocsp != NO_OCSP) {
  1079. SSL_CTX_set_tlsext_status_cb(ssl, ocsp_resp_cb);
  1080. SSL_CTX_set_tlsext_status_arg(ssl, ctx);
  1081. /*
  1082. * Use a temporary SSL_METHOD to get a callback on SSL_new()
  1083. * from libcurl since there is no proper callback registration
  1084. * available for this.
  1085. */
  1086. os_memset(&patch_ssl_method, 0, sizeof(patch_ssl_method));
  1087. patch_ssl_method.ssl_new = curl_patch_ssl_new;
  1088. real_ssl_method = ssl->method;
  1089. ssl->method = &patch_ssl_method;
  1090. }
  1091. #endif /* HAVE_OCSP */
  1092. return CURLE_OK;
  1093. }
  1094. #endif /* EAP_TLS_OPENSSL */
  1095. static CURL * setup_curl_post(struct http_ctx *ctx, const char *address,
  1096. const char *ca_fname, const char *username,
  1097. const char *password, const char *client_cert,
  1098. const char *client_key)
  1099. {
  1100. CURL *curl;
  1101. #ifdef EAP_TLS_OPENSSL
  1102. const char *extra = " tls=openssl";
  1103. #else /* EAP_TLS_OPENSSL */
  1104. const char *extra = "";
  1105. #endif /* EAP_TLS_OPENSSL */
  1106. wpa_printf(MSG_DEBUG, "Start HTTP client: address=%s ca_fname=%s "
  1107. "username=%s%s", address, ca_fname, username, extra);
  1108. curl = curl_easy_init();
  1109. if (curl == NULL)
  1110. return NULL;
  1111. curl_easy_setopt(curl, CURLOPT_URL, address);
  1112. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  1113. if (ca_fname) {
  1114. curl_easy_setopt(curl, CURLOPT_CAINFO, ca_fname);
  1115. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  1116. #ifdef EAP_TLS_OPENSSL
  1117. curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_cb_ssl);
  1118. curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, ctx);
  1119. #ifdef OPENSSL_IS_BORINGSSL
  1120. /* For now, using the CURLOPT_SSL_VERIFYSTATUS option only
  1121. * with BoringSSL since the OpenSSL specific callback hack to
  1122. * enable OCSP is not available with BoringSSL. The OCSP
  1123. * implementation within libcurl is not sufficient for the
  1124. * Hotspot 2.0 OSU needs, so cannot use this with OpenSSL.
  1125. */
  1126. if (ctx->ocsp != NO_OCSP)
  1127. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1L);
  1128. #endif /* OPENSSL_IS_BORINGSSL */
  1129. #endif /* EAP_TLS_OPENSSL */
  1130. } else {
  1131. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  1132. }
  1133. if (client_cert && client_key) {
  1134. curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert);
  1135. curl_easy_setopt(curl, CURLOPT_SSLKEY, client_key);
  1136. }
  1137. /* TODO: use curl_easy_getinfo() with CURLINFO_CERTINFO to fetch
  1138. * information about the server certificate */
  1139. curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
  1140. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_cb_debug);
  1141. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, ctx);
  1142. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_cb_write);
  1143. curl_easy_setopt(curl, CURLOPT_WRITEDATA, ctx);
  1144. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  1145. if (username) {
  1146. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
  1147. curl_easy_setopt(curl, CURLOPT_USERNAME, username);
  1148. curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
  1149. }
  1150. return curl;
  1151. }
  1152. static int post_init_client(struct http_ctx *ctx, const char *address,
  1153. const char *ca_fname, const char *username,
  1154. const char *password, const char *client_cert,
  1155. const char *client_key)
  1156. {
  1157. char *pos;
  1158. int count;
  1159. clone_str(&ctx->svc_address, address);
  1160. clone_str(&ctx->svc_ca_fname, ca_fname);
  1161. clone_str(&ctx->svc_username, username);
  1162. clone_str(&ctx->svc_password, password);
  1163. clone_str(&ctx->svc_client_cert, client_cert);
  1164. clone_str(&ctx->svc_client_key, client_key);
  1165. /*
  1166. * Workaround for Apache "Hostname 'FOO' provided via SNI and hostname
  1167. * 'foo' provided via HTTP are different.
  1168. */
  1169. for (count = 0, pos = ctx->svc_address; count < 3 && pos && *pos;
  1170. pos++) {
  1171. if (*pos == '/')
  1172. count++;
  1173. *pos = tolower(*pos);
  1174. }
  1175. ctx->curl = setup_curl_post(ctx, ctx->svc_address, ca_fname, username,
  1176. password, client_cert, client_key);
  1177. if (ctx->curl == NULL)
  1178. return -1;
  1179. return 0;
  1180. }
  1181. int soap_init_client(struct http_ctx *ctx, const char *address,
  1182. const char *ca_fname, const char *username,
  1183. const char *password, const char *client_cert,
  1184. const char *client_key)
  1185. {
  1186. if (post_init_client(ctx, address, ca_fname, username, password,
  1187. client_cert, client_key) < 0)
  1188. return -1;
  1189. ctx->curl_hdr = curl_slist_append(ctx->curl_hdr,
  1190. "Content-Type: application/soap+xml");
  1191. ctx->curl_hdr = curl_slist_append(ctx->curl_hdr, "SOAPAction: ");
  1192. ctx->curl_hdr = curl_slist_append(ctx->curl_hdr, "Expect:");
  1193. curl_easy_setopt(ctx->curl, CURLOPT_HTTPHEADER, ctx->curl_hdr);
  1194. return 0;
  1195. }
  1196. int soap_reinit_client(struct http_ctx *ctx)
  1197. {
  1198. char *address = NULL;
  1199. char *ca_fname = NULL;
  1200. char *username = NULL;
  1201. char *password = NULL;
  1202. char *client_cert = NULL;
  1203. char *client_key = NULL;
  1204. int ret;
  1205. clear_curl(ctx);
  1206. clone_str(&address, ctx->svc_address);
  1207. clone_str(&ca_fname, ctx->svc_ca_fname);
  1208. clone_str(&username, ctx->svc_username);
  1209. clone_str(&password, ctx->svc_password);
  1210. clone_str(&client_cert, ctx->svc_client_cert);
  1211. clone_str(&client_key, ctx->svc_client_key);
  1212. ret = soap_init_client(ctx, address, ca_fname, username, password,
  1213. client_cert, client_key);
  1214. os_free(address);
  1215. os_free(ca_fname);
  1216. str_clear_free(username);
  1217. str_clear_free(password);
  1218. os_free(client_cert);
  1219. os_free(client_key);
  1220. return ret;
  1221. }
  1222. static void free_curl_buf(struct http_ctx *ctx)
  1223. {
  1224. os_free(ctx->curl_buf);
  1225. ctx->curl_buf = NULL;
  1226. ctx->curl_buf_len = 0;
  1227. }
  1228. xml_node_t * soap_send_receive(struct http_ctx *ctx, xml_node_t *node)
  1229. {
  1230. char *str;
  1231. xml_node_t *envelope, *ret, *resp, *n;
  1232. CURLcode res;
  1233. long http = 0;
  1234. ctx->last_err = NULL;
  1235. wpa_printf(MSG_DEBUG, "SOAP: Sending message");
  1236. envelope = soap_build_envelope(ctx->xml, node);
  1237. str = xml_node_to_str(ctx->xml, envelope);
  1238. xml_node_free(ctx->xml, envelope);
  1239. wpa_printf(MSG_MSGDUMP, "SOAP[%s]", str);
  1240. curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDS, str);
  1241. free_curl_buf(ctx);
  1242. res = curl_easy_perform(ctx->curl);
  1243. if (res != CURLE_OK) {
  1244. if (!ctx->last_err)
  1245. ctx->last_err = curl_easy_strerror(res);
  1246. wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
  1247. ctx->last_err);
  1248. os_free(str);
  1249. free_curl_buf(ctx);
  1250. return NULL;
  1251. }
  1252. os_free(str);
  1253. curl_easy_getinfo(ctx->curl, CURLINFO_RESPONSE_CODE, &http);
  1254. wpa_printf(MSG_DEBUG, "SOAP: Server response code %ld", http);
  1255. if (http != 200) {
  1256. ctx->last_err = "HTTP download failed";
  1257. wpa_printf(MSG_INFO, "HTTP download failed - code %ld", http);
  1258. free_curl_buf(ctx);
  1259. return NULL;
  1260. }
  1261. if (ctx->curl_buf == NULL)
  1262. return NULL;
  1263. wpa_printf(MSG_MSGDUMP, "Server response:\n%s", ctx->curl_buf);
  1264. resp = xml_node_from_buf(ctx->xml, ctx->curl_buf);
  1265. free_curl_buf(ctx);
  1266. if (resp == NULL) {
  1267. wpa_printf(MSG_INFO, "Could not parse SOAP response");
  1268. ctx->last_err = "Could not parse SOAP response";
  1269. return NULL;
  1270. }
  1271. ret = soap_get_body(ctx->xml, resp);
  1272. if (ret == NULL) {
  1273. wpa_printf(MSG_INFO, "Could not get SOAP body");
  1274. ctx->last_err = "Could not get SOAP body";
  1275. return NULL;
  1276. }
  1277. wpa_printf(MSG_DEBUG, "SOAP body localname: '%s'",
  1278. xml_node_get_localname(ctx->xml, ret));
  1279. n = xml_node_copy(ctx->xml, ret);
  1280. xml_node_free(ctx->xml, resp);
  1281. return n;
  1282. }
  1283. struct http_ctx * http_init_ctx(void *upper_ctx, struct xml_node_ctx *xml_ctx)
  1284. {
  1285. struct http_ctx *ctx;
  1286. ctx = os_zalloc(sizeof(*ctx));
  1287. if (ctx == NULL)
  1288. return NULL;
  1289. ctx->ctx = upper_ctx;
  1290. ctx->xml = xml_ctx;
  1291. ctx->ocsp = OPTIONAL_OCSP;
  1292. curl_global_init(CURL_GLOBAL_ALL);
  1293. return ctx;
  1294. }
  1295. void http_ocsp_set(struct http_ctx *ctx, int val)
  1296. {
  1297. if (val == 0)
  1298. ctx->ocsp = NO_OCSP;
  1299. else if (val == 1)
  1300. ctx->ocsp = OPTIONAL_OCSP;
  1301. if (val == 2)
  1302. ctx->ocsp = MANDATORY_OCSP;
  1303. }
  1304. void http_deinit_ctx(struct http_ctx *ctx)
  1305. {
  1306. clear_curl(ctx);
  1307. os_free(ctx->curl_buf);
  1308. curl_global_cleanup();
  1309. os_free(ctx->svc_address);
  1310. os_free(ctx->svc_ca_fname);
  1311. str_clear_free(ctx->svc_username);
  1312. str_clear_free(ctx->svc_password);
  1313. os_free(ctx->svc_client_cert);
  1314. os_free(ctx->svc_client_key);
  1315. os_free(ctx);
  1316. }
  1317. int http_download_file(struct http_ctx *ctx, const char *url,
  1318. const char *fname, const char *ca_fname)
  1319. {
  1320. CURL *curl;
  1321. FILE *f;
  1322. CURLcode res;
  1323. long http = 0;
  1324. ctx->last_err = NULL;
  1325. wpa_printf(MSG_DEBUG, "curl: Download file from %s to %s (ca=%s)",
  1326. url, fname, ca_fname);
  1327. curl = curl_easy_init();
  1328. if (curl == NULL)
  1329. return -1;
  1330. f = fopen(fname, "wb");
  1331. if (f == NULL) {
  1332. curl_easy_cleanup(curl);
  1333. return -1;
  1334. }
  1335. curl_easy_setopt(curl, CURLOPT_URL, url);
  1336. if (ca_fname) {
  1337. curl_easy_setopt(curl, CURLOPT_CAINFO, ca_fname);
  1338. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  1339. curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
  1340. } else {
  1341. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  1342. }
  1343. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_cb_debug);
  1344. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, ctx);
  1345. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  1346. curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
  1347. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  1348. res = curl_easy_perform(curl);
  1349. if (res != CURLE_OK) {
  1350. if (!ctx->last_err)
  1351. ctx->last_err = curl_easy_strerror(res);
  1352. wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
  1353. ctx->last_err);
  1354. curl_easy_cleanup(curl);
  1355. fclose(f);
  1356. return -1;
  1357. }
  1358. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http);
  1359. wpa_printf(MSG_DEBUG, "curl: Server response code %ld", http);
  1360. if (http != 200) {
  1361. ctx->last_err = "HTTP download failed";
  1362. wpa_printf(MSG_INFO, "HTTP download failed - code %ld", http);
  1363. curl_easy_cleanup(curl);
  1364. fclose(f);
  1365. return -1;
  1366. }
  1367. curl_easy_cleanup(curl);
  1368. fclose(f);
  1369. return 0;
  1370. }
  1371. char * http_post(struct http_ctx *ctx, const char *url, const char *data,
  1372. const char *content_type, const char *ext_hdr,
  1373. const char *ca_fname,
  1374. const char *username, const char *password,
  1375. const char *client_cert, const char *client_key,
  1376. size_t *resp_len)
  1377. {
  1378. long http = 0;
  1379. CURLcode res;
  1380. char *ret;
  1381. CURL *curl;
  1382. struct curl_slist *curl_hdr = NULL;
  1383. ctx->last_err = NULL;
  1384. wpa_printf(MSG_DEBUG, "curl: HTTP POST to %s", url);
  1385. curl = setup_curl_post(ctx, url, ca_fname, username, password,
  1386. client_cert, client_key);
  1387. if (curl == NULL)
  1388. return NULL;
  1389. if (content_type) {
  1390. char ct[200];
  1391. snprintf(ct, sizeof(ct), "Content-Type: %s", content_type);
  1392. curl_hdr = curl_slist_append(curl_hdr, ct);
  1393. }
  1394. if (ext_hdr)
  1395. curl_hdr = curl_slist_append(curl_hdr, ext_hdr);
  1396. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_hdr);
  1397. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  1398. free_curl_buf(ctx);
  1399. res = curl_easy_perform(curl);
  1400. if (res != CURLE_OK) {
  1401. if (!ctx->last_err)
  1402. ctx->last_err = curl_easy_strerror(res);
  1403. wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
  1404. ctx->last_err);
  1405. free_curl_buf(ctx);
  1406. return NULL;
  1407. }
  1408. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http);
  1409. wpa_printf(MSG_DEBUG, "curl: Server response code %ld", http);
  1410. if (http != 200) {
  1411. ctx->last_err = "HTTP POST failed";
  1412. wpa_printf(MSG_INFO, "HTTP POST failed - code %ld", http);
  1413. free_curl_buf(ctx);
  1414. return NULL;
  1415. }
  1416. if (ctx->curl_buf == NULL)
  1417. return NULL;
  1418. ret = ctx->curl_buf;
  1419. if (resp_len)
  1420. *resp_len = ctx->curl_buf_len;
  1421. ctx->curl_buf = NULL;
  1422. ctx->curl_buf_len = 0;
  1423. wpa_printf(MSG_MSGDUMP, "Server response:\n%s", ret);
  1424. return ret;
  1425. }
  1426. void http_set_cert_cb(struct http_ctx *ctx,
  1427. int (*cb)(void *ctx, struct http_cert *cert),
  1428. void *cb_ctx)
  1429. {
  1430. ctx->cert_cb = cb;
  1431. ctx->cert_cb_ctx = cb_ctx;
  1432. }
  1433. const char * http_get_err(struct http_ctx *ctx)
  1434. {
  1435. return ctx->last_err;
  1436. }