http_curl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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. return 0;
  714. if (0) {
  715. BIO *out;
  716. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  717. X509_print_ex(out, cert, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  718. BIO_free(out);
  719. }
  720. parse_cert(ctx, &hcert, cert, &names);
  721. ret = ctx->cert_cb(ctx->cert_cb_ctx, &hcert);
  722. parse_cert_free(&hcert, names);
  723. return ret;
  724. }
  725. void http_parse_x509_certificate(struct http_ctx *ctx, const char *fname)
  726. {
  727. BIO *in, *out;
  728. X509 *cert;
  729. GENERAL_NAMES *names;
  730. struct http_cert hcert;
  731. unsigned int i;
  732. in = BIO_new_file(fname, "r");
  733. if (in == NULL) {
  734. wpa_printf(MSG_ERROR, "Could not read '%s'", fname);
  735. return;
  736. }
  737. cert = d2i_X509_bio(in, NULL);
  738. BIO_free(in);
  739. if (cert == NULL) {
  740. wpa_printf(MSG_ERROR, "Could not parse certificate");
  741. return;
  742. }
  743. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  744. if (out) {
  745. X509_print_ex(out, cert, XN_FLAG_COMPAT,
  746. X509_FLAG_COMPAT);
  747. BIO_free(out);
  748. }
  749. wpa_printf(MSG_INFO, "Additional parsing information:");
  750. parse_cert(ctx, &hcert, cert, &names);
  751. for (i = 0; i < hcert.num_othername; i++) {
  752. if (os_strcmp(hcert.othername[i].oid,
  753. "1.3.6.1.4.1.40808.1.1.1") == 0) {
  754. char *name = os_zalloc(hcert.othername[i].len + 1);
  755. if (name) {
  756. os_memcpy(name, hcert.othername[i].data,
  757. hcert.othername[i].len);
  758. wpa_printf(MSG_INFO,
  759. "id-wfa-hotspot-friendlyName: %s",
  760. name);
  761. os_free(name);
  762. }
  763. wpa_hexdump_ascii(MSG_INFO,
  764. "id-wfa-hotspot-friendlyName",
  765. hcert.othername[i].data,
  766. hcert.othername[i].len);
  767. } else {
  768. wpa_printf(MSG_INFO, "subjAltName[othername]: oid=%s",
  769. hcert.othername[i].oid);
  770. wpa_hexdump_ascii(MSG_INFO, "unknown othername",
  771. hcert.othername[i].data,
  772. hcert.othername[i].len);
  773. }
  774. }
  775. parse_cert_free(&hcert, names);
  776. X509_free(cert);
  777. }
  778. static int curl_cb_ssl_verify(int preverify_ok, X509_STORE_CTX *x509_ctx)
  779. {
  780. struct http_ctx *ctx;
  781. X509 *cert;
  782. int err, depth;
  783. char buf[256];
  784. X509_NAME *name;
  785. const char *err_str;
  786. SSL *ssl;
  787. SSL_CTX *ssl_ctx;
  788. ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
  789. SSL_get_ex_data_X509_STORE_CTX_idx());
  790. ssl_ctx = ssl->ctx;
  791. ctx = SSL_CTX_get_app_data(ssl_ctx);
  792. wpa_printf(MSG_DEBUG, "curl_cb_ssl_verify");
  793. err = X509_STORE_CTX_get_error(x509_ctx);
  794. err_str = X509_verify_cert_error_string(err);
  795. depth = X509_STORE_CTX_get_error_depth(x509_ctx);
  796. cert = X509_STORE_CTX_get_current_cert(x509_ctx);
  797. if (!cert) {
  798. wpa_printf(MSG_INFO, "No server certificate available");
  799. ctx->last_err = "No server certificate available";
  800. return 0;
  801. }
  802. if (depth == 0)
  803. ctx->peer_cert = cert;
  804. else if (depth == 1)
  805. ctx->peer_issuer = cert;
  806. else if (depth == 2)
  807. ctx->peer_issuer_issuer = cert;
  808. name = X509_get_subject_name(cert);
  809. X509_NAME_oneline(name, buf, sizeof(buf));
  810. wpa_printf(MSG_INFO, "Server certificate chain - depth=%d err=%d (%s) subject=%s",
  811. depth, err, err_str, buf);
  812. debug_dump_cert("Server certificate chain - certificate", cert);
  813. if (depth == 0 && preverify_ok && validate_server_cert(ctx, cert) < 0)
  814. return 0;
  815. if (!preverify_ok)
  816. ctx->last_err = "TLS validation failed";
  817. return preverify_ok;
  818. }
  819. #ifdef HAVE_OCSP
  820. static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
  821. {
  822. BIO *out;
  823. size_t rlen;
  824. char *txt;
  825. int res;
  826. out = BIO_new(BIO_s_mem());
  827. if (!out)
  828. return;
  829. OCSP_RESPONSE_print(out, rsp, 0);
  830. rlen = BIO_ctrl_pending(out);
  831. txt = os_malloc(rlen + 1);
  832. if (!txt) {
  833. BIO_free(out);
  834. return;
  835. }
  836. res = BIO_read(out, txt, rlen);
  837. if (res > 0) {
  838. txt[res] = '\0';
  839. wpa_printf(MSG_MSGDUMP, "OpenSSL: OCSP Response\n%s", txt);
  840. }
  841. os_free(txt);
  842. BIO_free(out);
  843. }
  844. static void tls_show_errors(const char *func, const char *txt)
  845. {
  846. unsigned long err;
  847. wpa_printf(MSG_DEBUG, "OpenSSL: %s - %s %s",
  848. func, txt, ERR_error_string(ERR_get_error(), NULL));
  849. while ((err = ERR_get_error())) {
  850. wpa_printf(MSG_DEBUG, "OpenSSL: pending error: %s",
  851. ERR_error_string(err, NULL));
  852. }
  853. }
  854. static int ocsp_resp_cb(SSL *s, void *arg)
  855. {
  856. struct http_ctx *ctx = arg;
  857. const unsigned char *p;
  858. int len, status, reason;
  859. OCSP_RESPONSE *rsp;
  860. OCSP_BASICRESP *basic;
  861. OCSP_CERTID *id;
  862. ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
  863. X509_STORE *store;
  864. STACK_OF(X509) *certs = NULL;
  865. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  866. if (!p) {
  867. wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
  868. if (ctx->ocsp == MANDATORY_OCSP)
  869. ctx->last_err = "No OCSP response received";
  870. return (ctx->ocsp == MANDATORY_OCSP) ? 0 : 1;
  871. }
  872. wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
  873. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  874. if (!rsp) {
  875. wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
  876. ctx->last_err = "Failed to parse OCSP response";
  877. return 0;
  878. }
  879. ocsp_debug_print_resp(rsp);
  880. status = OCSP_response_status(rsp);
  881. if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
  882. wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
  883. status, OCSP_response_status_str(status));
  884. ctx->last_err = "OCSP responder error";
  885. return 0;
  886. }
  887. basic = OCSP_response_get1_basic(rsp);
  888. if (!basic) {
  889. wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
  890. ctx->last_err = "Could not find BasicOCSPResponse";
  891. return 0;
  892. }
  893. store = SSL_CTX_get_cert_store(s->ctx);
  894. if (ctx->peer_issuer) {
  895. wpa_printf(MSG_DEBUG, "OpenSSL: Add issuer");
  896. debug_dump_cert("OpenSSL: Issuer certificate",
  897. ctx->peer_issuer);
  898. if (X509_STORE_add_cert(store, ctx->peer_issuer) != 1) {
  899. tls_show_errors(__func__,
  900. "OpenSSL: Could not add issuer to certificate store\n");
  901. }
  902. certs = sk_X509_new_null();
  903. if (certs) {
  904. X509 *cert;
  905. cert = X509_dup(ctx->peer_issuer);
  906. if (cert && !sk_X509_push(certs, cert)) {
  907. tls_show_errors(
  908. __func__,
  909. "OpenSSL: Could not add issuer to OCSP responder trust store\n");
  910. X509_free(cert);
  911. sk_X509_free(certs);
  912. certs = NULL;
  913. }
  914. if (ctx->peer_issuer_issuer) {
  915. cert = X509_dup(ctx->peer_issuer_issuer);
  916. if (cert && !sk_X509_push(certs, cert)) {
  917. tls_show_errors(
  918. __func__,
  919. "OpenSSL: Could not add issuer to OCSP responder trust store\n");
  920. X509_free(cert);
  921. }
  922. }
  923. }
  924. }
  925. status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
  926. sk_X509_pop_free(certs, X509_free);
  927. if (status <= 0) {
  928. tls_show_errors(__func__,
  929. "OpenSSL: OCSP response failed verification");
  930. OCSP_BASICRESP_free(basic);
  931. OCSP_RESPONSE_free(rsp);
  932. ctx->last_err = "OCSP response failed verification";
  933. return 0;
  934. }
  935. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
  936. if (!ctx->peer_cert) {
  937. wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
  938. OCSP_BASICRESP_free(basic);
  939. OCSP_RESPONSE_free(rsp);
  940. ctx->last_err = "Peer certificate not available for OCSP status check";
  941. return 0;
  942. }
  943. if (!ctx->peer_issuer) {
  944. wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
  945. OCSP_BASICRESP_free(basic);
  946. OCSP_RESPONSE_free(rsp);
  947. ctx->last_err = "Peer issuer certificate not available for OCSP status check";
  948. return 0;
  949. }
  950. id = OCSP_cert_to_id(NULL, ctx->peer_cert, ctx->peer_issuer);
  951. if (!id) {
  952. wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier");
  953. OCSP_BASICRESP_free(basic);
  954. OCSP_RESPONSE_free(rsp);
  955. ctx->last_err = "Could not create OCSP certificate identifier";
  956. return 0;
  957. }
  958. if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
  959. &this_update, &next_update)) {
  960. wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
  961. (ctx->ocsp == MANDATORY_OCSP) ? "" :
  962. " (OCSP not required)");
  963. OCSP_BASICRESP_free(basic);
  964. OCSP_RESPONSE_free(rsp);
  965. if (ctx->ocsp == MANDATORY_OCSP)
  966. ctx->last_err = "Could not find current server certificate from OCSP response";
  967. return (ctx->ocsp == MANDATORY_OCSP) ? 0 : 1;
  968. }
  969. if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
  970. tls_show_errors(__func__, "OpenSSL: OCSP status times invalid");
  971. OCSP_BASICRESP_free(basic);
  972. OCSP_RESPONSE_free(rsp);
  973. ctx->last_err = "OCSP status times invalid";
  974. return 0;
  975. }
  976. OCSP_BASICRESP_free(basic);
  977. OCSP_RESPONSE_free(rsp);
  978. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
  979. OCSP_cert_status_str(status));
  980. if (status == V_OCSP_CERTSTATUS_GOOD)
  981. return 1;
  982. if (status == V_OCSP_CERTSTATUS_REVOKED) {
  983. ctx->last_err = "Server certificate has been revoked";
  984. return 0;
  985. }
  986. if (ctx->ocsp == MANDATORY_OCSP) {
  987. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
  988. ctx->last_err = "OCSP status unknown";
  989. return 0;
  990. }
  991. wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
  992. return 1;
  993. }
  994. static SSL_METHOD patch_ssl_method;
  995. static const SSL_METHOD *real_ssl_method;
  996. static int curl_patch_ssl_new(SSL *s)
  997. {
  998. SSL_CTX *ssl = s->ctx;
  999. int ret;
  1000. ssl->method = real_ssl_method;
  1001. s->method = real_ssl_method;
  1002. ret = s->method->ssl_new(s);
  1003. SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp);
  1004. return ret;
  1005. }
  1006. #endif /* HAVE_OCSP */
  1007. static CURLcode curl_cb_ssl(CURL *curl, void *sslctx, void *parm)
  1008. {
  1009. struct http_ctx *ctx = parm;
  1010. SSL_CTX *ssl = sslctx;
  1011. wpa_printf(MSG_DEBUG, "curl_cb_ssl");
  1012. SSL_CTX_set_app_data(ssl, ctx);
  1013. SSL_CTX_set_verify(ssl, SSL_VERIFY_PEER, curl_cb_ssl_verify);
  1014. #ifdef HAVE_OCSP
  1015. if (ctx->ocsp != NO_OCSP) {
  1016. SSL_CTX_set_tlsext_status_cb(ssl, ocsp_resp_cb);
  1017. SSL_CTX_set_tlsext_status_arg(ssl, ctx);
  1018. /*
  1019. * Use a temporary SSL_METHOD to get a callback on SSL_new()
  1020. * from libcurl since there is no proper callback registration
  1021. * available for this.
  1022. */
  1023. os_memset(&patch_ssl_method, 0, sizeof(patch_ssl_method));
  1024. patch_ssl_method.ssl_new = curl_patch_ssl_new;
  1025. real_ssl_method = ssl->method;
  1026. ssl->method = &patch_ssl_method;
  1027. }
  1028. #endif /* HAVE_OCSP */
  1029. return CURLE_OK;
  1030. }
  1031. #endif /* EAP_TLS_OPENSSL */
  1032. static CURL * setup_curl_post(struct http_ctx *ctx, const char *address,
  1033. const char *ca_fname, const char *username,
  1034. const char *password, const char *client_cert,
  1035. const char *client_key)
  1036. {
  1037. CURL *curl;
  1038. wpa_printf(MSG_DEBUG, "Start HTTP client: address=%s ca_fname=%s "
  1039. "username=%s", address, ca_fname, username);
  1040. curl = curl_easy_init();
  1041. if (curl == NULL)
  1042. return NULL;
  1043. curl_easy_setopt(curl, CURLOPT_URL, address);
  1044. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  1045. if (ca_fname) {
  1046. curl_easy_setopt(curl, CURLOPT_CAINFO, ca_fname);
  1047. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  1048. #ifdef EAP_TLS_OPENSSL
  1049. curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_cb_ssl);
  1050. curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, ctx);
  1051. #endif /* EAP_TLS_OPENSSL */
  1052. } else {
  1053. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  1054. }
  1055. if (client_cert && client_key) {
  1056. curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert);
  1057. curl_easy_setopt(curl, CURLOPT_SSLKEY, client_key);
  1058. }
  1059. /* TODO: use curl_easy_getinfo() with CURLINFO_CERTINFO to fetch
  1060. * information about the server certificate */
  1061. curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
  1062. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_cb_debug);
  1063. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, ctx);
  1064. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_cb_write);
  1065. curl_easy_setopt(curl, CURLOPT_WRITEDATA, ctx);
  1066. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  1067. if (username) {
  1068. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
  1069. curl_easy_setopt(curl, CURLOPT_USERNAME, username);
  1070. curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
  1071. }
  1072. return curl;
  1073. }
  1074. static int post_init_client(struct http_ctx *ctx, const char *address,
  1075. const char *ca_fname, const char *username,
  1076. const char *password, const char *client_cert,
  1077. const char *client_key)
  1078. {
  1079. char *pos;
  1080. int count;
  1081. clone_str(&ctx->svc_address, address);
  1082. clone_str(&ctx->svc_ca_fname, ca_fname);
  1083. clone_str(&ctx->svc_username, username);
  1084. clone_str(&ctx->svc_password, password);
  1085. clone_str(&ctx->svc_client_cert, client_cert);
  1086. clone_str(&ctx->svc_client_key, client_key);
  1087. /*
  1088. * Workaround for Apache "Hostname 'FOO' provided via SNI and hostname
  1089. * 'foo' provided via HTTP are different.
  1090. */
  1091. for (count = 0, pos = ctx->svc_address; count < 3 && pos && *pos;
  1092. pos++) {
  1093. if (*pos == '/')
  1094. count++;
  1095. *pos = tolower(*pos);
  1096. }
  1097. ctx->curl = setup_curl_post(ctx, ctx->svc_address, ca_fname, username,
  1098. password, client_cert, client_key);
  1099. if (ctx->curl == NULL)
  1100. return -1;
  1101. return 0;
  1102. }
  1103. int soap_init_client(struct http_ctx *ctx, const char *address,
  1104. const char *ca_fname, const char *username,
  1105. const char *password, const char *client_cert,
  1106. const char *client_key)
  1107. {
  1108. if (post_init_client(ctx, address, ca_fname, username, password,
  1109. client_cert, client_key) < 0)
  1110. return -1;
  1111. ctx->curl_hdr = curl_slist_append(ctx->curl_hdr,
  1112. "Content-Type: application/soap+xml");
  1113. ctx->curl_hdr = curl_slist_append(ctx->curl_hdr, "SOAPAction: ");
  1114. ctx->curl_hdr = curl_slist_append(ctx->curl_hdr, "Expect:");
  1115. curl_easy_setopt(ctx->curl, CURLOPT_HTTPHEADER, ctx->curl_hdr);
  1116. return 0;
  1117. }
  1118. int soap_reinit_client(struct http_ctx *ctx)
  1119. {
  1120. char *address = NULL;
  1121. char *ca_fname = NULL;
  1122. char *username = NULL;
  1123. char *password = NULL;
  1124. char *client_cert = NULL;
  1125. char *client_key = NULL;
  1126. int ret;
  1127. clear_curl(ctx);
  1128. clone_str(&address, ctx->svc_address);
  1129. clone_str(&ca_fname, ctx->svc_ca_fname);
  1130. clone_str(&username, ctx->svc_username);
  1131. clone_str(&password, ctx->svc_password);
  1132. clone_str(&client_cert, ctx->svc_client_cert);
  1133. clone_str(&client_key, ctx->svc_client_key);
  1134. ret = soap_init_client(ctx, address, ca_fname, username, password,
  1135. client_cert, client_key);
  1136. os_free(address);
  1137. os_free(ca_fname);
  1138. str_clear_free(username);
  1139. str_clear_free(password);
  1140. os_free(client_cert);
  1141. os_free(client_key);
  1142. return ret;
  1143. }
  1144. static void free_curl_buf(struct http_ctx *ctx)
  1145. {
  1146. os_free(ctx->curl_buf);
  1147. ctx->curl_buf = NULL;
  1148. ctx->curl_buf_len = 0;
  1149. }
  1150. xml_node_t * soap_send_receive(struct http_ctx *ctx, xml_node_t *node)
  1151. {
  1152. char *str;
  1153. xml_node_t *envelope, *ret, *resp, *n;
  1154. CURLcode res;
  1155. long http = 0;
  1156. ctx->last_err = NULL;
  1157. wpa_printf(MSG_DEBUG, "SOAP: Sending message");
  1158. envelope = soap_build_envelope(ctx->xml, node);
  1159. str = xml_node_to_str(ctx->xml, envelope);
  1160. xml_node_free(ctx->xml, envelope);
  1161. wpa_printf(MSG_MSGDUMP, "SOAP[%s]", str);
  1162. curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDS, str);
  1163. free_curl_buf(ctx);
  1164. res = curl_easy_perform(ctx->curl);
  1165. if (res != CURLE_OK) {
  1166. if (!ctx->last_err)
  1167. ctx->last_err = curl_easy_strerror(res);
  1168. wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
  1169. ctx->last_err);
  1170. os_free(str);
  1171. free_curl_buf(ctx);
  1172. return NULL;
  1173. }
  1174. os_free(str);
  1175. curl_easy_getinfo(ctx->curl, CURLINFO_RESPONSE_CODE, &http);
  1176. wpa_printf(MSG_DEBUG, "SOAP: Server response code %ld", http);
  1177. if (http != 200) {
  1178. ctx->last_err = "HTTP download failed";
  1179. wpa_printf(MSG_INFO, "HTTP download failed - code %ld", http);
  1180. free_curl_buf(ctx);
  1181. return NULL;
  1182. }
  1183. if (ctx->curl_buf == NULL)
  1184. return NULL;
  1185. wpa_printf(MSG_MSGDUMP, "Server response:\n%s", ctx->curl_buf);
  1186. resp = xml_node_from_buf(ctx->xml, ctx->curl_buf);
  1187. free_curl_buf(ctx);
  1188. if (resp == NULL) {
  1189. wpa_printf(MSG_INFO, "Could not parse SOAP response");
  1190. ctx->last_err = "Could not parse SOAP response";
  1191. return NULL;
  1192. }
  1193. ret = soap_get_body(ctx->xml, resp);
  1194. if (ret == NULL) {
  1195. wpa_printf(MSG_INFO, "Could not get SOAP body");
  1196. ctx->last_err = "Could not get SOAP body";
  1197. return NULL;
  1198. }
  1199. wpa_printf(MSG_DEBUG, "SOAP body localname: '%s'",
  1200. xml_node_get_localname(ctx->xml, ret));
  1201. n = xml_node_copy(ctx->xml, ret);
  1202. xml_node_free(ctx->xml, resp);
  1203. return n;
  1204. }
  1205. struct http_ctx * http_init_ctx(void *upper_ctx, struct xml_node_ctx *xml_ctx)
  1206. {
  1207. struct http_ctx *ctx;
  1208. ctx = os_zalloc(sizeof(*ctx));
  1209. if (ctx == NULL)
  1210. return NULL;
  1211. ctx->ctx = upper_ctx;
  1212. ctx->xml = xml_ctx;
  1213. ctx->ocsp = OPTIONAL_OCSP;
  1214. curl_global_init(CURL_GLOBAL_ALL);
  1215. return ctx;
  1216. }
  1217. void http_ocsp_set(struct http_ctx *ctx, int val)
  1218. {
  1219. if (val == 0)
  1220. ctx->ocsp = NO_OCSP;
  1221. else if (val == 1)
  1222. ctx->ocsp = OPTIONAL_OCSP;
  1223. if (val == 2)
  1224. ctx->ocsp = MANDATORY_OCSP;
  1225. }
  1226. void http_deinit_ctx(struct http_ctx *ctx)
  1227. {
  1228. clear_curl(ctx);
  1229. os_free(ctx->curl_buf);
  1230. curl_global_cleanup();
  1231. os_free(ctx->svc_address);
  1232. os_free(ctx->svc_ca_fname);
  1233. str_clear_free(ctx->svc_username);
  1234. str_clear_free(ctx->svc_password);
  1235. os_free(ctx->svc_client_cert);
  1236. os_free(ctx->svc_client_key);
  1237. os_free(ctx);
  1238. }
  1239. int http_download_file(struct http_ctx *ctx, const char *url,
  1240. const char *fname, const char *ca_fname)
  1241. {
  1242. CURL *curl;
  1243. FILE *f;
  1244. CURLcode res;
  1245. long http = 0;
  1246. ctx->last_err = NULL;
  1247. wpa_printf(MSG_DEBUG, "curl: Download file from %s to %s (ca=%s)",
  1248. url, fname, ca_fname);
  1249. curl = curl_easy_init();
  1250. if (curl == NULL)
  1251. return -1;
  1252. f = fopen(fname, "wb");
  1253. if (f == NULL) {
  1254. curl_easy_cleanup(curl);
  1255. return -1;
  1256. }
  1257. curl_easy_setopt(curl, CURLOPT_URL, url);
  1258. if (ca_fname) {
  1259. curl_easy_setopt(curl, CURLOPT_CAINFO, ca_fname);
  1260. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  1261. curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
  1262. } else {
  1263. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  1264. }
  1265. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_cb_debug);
  1266. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, ctx);
  1267. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  1268. curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
  1269. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  1270. res = curl_easy_perform(curl);
  1271. if (res != CURLE_OK) {
  1272. if (!ctx->last_err)
  1273. ctx->last_err = curl_easy_strerror(res);
  1274. wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
  1275. ctx->last_err);
  1276. curl_easy_cleanup(curl);
  1277. fclose(f);
  1278. return -1;
  1279. }
  1280. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http);
  1281. wpa_printf(MSG_DEBUG, "curl: Server response code %ld", http);
  1282. if (http != 200) {
  1283. ctx->last_err = "HTTP download failed";
  1284. wpa_printf(MSG_INFO, "HTTP download failed - code %ld", http);
  1285. curl_easy_cleanup(curl);
  1286. fclose(f);
  1287. return -1;
  1288. }
  1289. curl_easy_cleanup(curl);
  1290. fclose(f);
  1291. return 0;
  1292. }
  1293. char * http_post(struct http_ctx *ctx, const char *url, const char *data,
  1294. const char *content_type, const char *ext_hdr,
  1295. const char *ca_fname,
  1296. const char *username, const char *password,
  1297. const char *client_cert, const char *client_key,
  1298. size_t *resp_len)
  1299. {
  1300. long http = 0;
  1301. CURLcode res;
  1302. char *ret;
  1303. CURL *curl;
  1304. struct curl_slist *curl_hdr = NULL;
  1305. ctx->last_err = NULL;
  1306. wpa_printf(MSG_DEBUG, "curl: HTTP POST to %s", url);
  1307. curl = setup_curl_post(ctx, url, ca_fname, username, password,
  1308. client_cert, client_key);
  1309. if (curl == NULL)
  1310. return NULL;
  1311. if (content_type) {
  1312. char ct[200];
  1313. snprintf(ct, sizeof(ct), "Content-Type: %s", content_type);
  1314. curl_hdr = curl_slist_append(curl_hdr, ct);
  1315. }
  1316. if (ext_hdr)
  1317. curl_hdr = curl_slist_append(curl_hdr, ext_hdr);
  1318. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_hdr);
  1319. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  1320. free_curl_buf(ctx);
  1321. res = curl_easy_perform(curl);
  1322. if (res != CURLE_OK) {
  1323. if (!ctx->last_err)
  1324. ctx->last_err = curl_easy_strerror(res);
  1325. wpa_printf(MSG_ERROR, "curl_easy_perform() failed: %s",
  1326. ctx->last_err);
  1327. free_curl_buf(ctx);
  1328. return NULL;
  1329. }
  1330. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http);
  1331. wpa_printf(MSG_DEBUG, "curl: Server response code %ld", http);
  1332. if (http != 200) {
  1333. ctx->last_err = "HTTP POST failed";
  1334. wpa_printf(MSG_INFO, "HTTP POST failed - code %ld", http);
  1335. free_curl_buf(ctx);
  1336. return NULL;
  1337. }
  1338. if (ctx->curl_buf == NULL)
  1339. return NULL;
  1340. ret = ctx->curl_buf;
  1341. if (resp_len)
  1342. *resp_len = ctx->curl_buf_len;
  1343. ctx->curl_buf = NULL;
  1344. ctx->curl_buf_len = 0;
  1345. wpa_printf(MSG_MSGDUMP, "Server response:\n%s", ret);
  1346. return ret;
  1347. }
  1348. void http_set_cert_cb(struct http_ctx *ctx,
  1349. int (*cb)(void *ctx, struct http_cert *cert),
  1350. void *cb_ctx)
  1351. {
  1352. ctx->cert_cb = cb;
  1353. ctx->cert_cb_ctx = cb_ctx;
  1354. }
  1355. const char * http_get_err(struct http_ctx *ctx)
  1356. {
  1357. return ctx->last_err;
  1358. }