http_curl.c 41 KB

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