2
0

lib678.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #include "testutil.h"
  26. #include "warnless.h"
  27. #include "memdebug.h"
  28. static int loadfile(const char *filename, void **filedata, size_t *filesize)
  29. {
  30. size_t datasize = 0;
  31. void *data = NULL;
  32. if(filename) {
  33. FILE *fInCert = fopen(filename, "rb");
  34. if(fInCert) {
  35. long cert_tell = 0;
  36. bool continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
  37. if(continue_reading)
  38. cert_tell = ftell(fInCert);
  39. if(cert_tell < 0)
  40. continue_reading = FALSE;
  41. else
  42. datasize = (size_t)cert_tell;
  43. if(continue_reading)
  44. continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
  45. if(continue_reading)
  46. data = malloc(datasize + 1);
  47. if((!data) ||
  48. ((int)fread(data, datasize, 1, fInCert) != 1))
  49. continue_reading = FALSE;
  50. fclose(fInCert);
  51. if(!continue_reading) {
  52. free(data);
  53. datasize = 0;
  54. data = NULL;
  55. }
  56. }
  57. }
  58. *filesize = datasize;
  59. *filedata = data;
  60. return data ? 1 : 0;
  61. }
  62. static CURLcode test_cert_blob(const char *url, const char *cafile)
  63. {
  64. CURLcode code = CURLE_OUT_OF_MEMORY;
  65. CURL *curl;
  66. struct curl_blob blob;
  67. size_t certsize;
  68. void *certdata;
  69. curl = curl_easy_init();
  70. if(!curl) {
  71. fprintf(stderr, "curl_easy_init() failed\n");
  72. return CURLE_FAILED_INIT;
  73. }
  74. if(loadfile(cafile, &certdata, &certsize)) {
  75. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  76. curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
  77. curl_easy_setopt(curl, CURLOPT_URL, url);
  78. curl_easy_setopt(curl, CURLOPT_USERAGENT, "CURLOPT_CAINFO_BLOB");
  79. curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS,
  80. CURLSSLOPT_REVOKE_BEST_EFFORT);
  81. blob.data = certdata;
  82. blob.len = certsize;
  83. blob.flags = CURL_BLOB_COPY;
  84. curl_easy_setopt(curl, CURLOPT_CAINFO_BLOB, &blob);
  85. free(certdata);
  86. code = curl_easy_perform(curl);
  87. }
  88. curl_easy_cleanup(curl);
  89. return code;
  90. }
  91. CURLcode test(char *URL)
  92. {
  93. CURLcode res = CURLE_OK;
  94. curl_global_init(CURL_GLOBAL_DEFAULT);
  95. if(!strcmp("check", URL)) {
  96. CURL *e;
  97. CURLcode w = CURLE_OK;
  98. struct curl_blob blob = {0};
  99. e = curl_easy_init();
  100. if(e) {
  101. w = curl_easy_setopt(e, CURLOPT_CAINFO_BLOB, &blob);
  102. if(w)
  103. printf("CURLOPT_CAINFO_BLOB is not supported\n");
  104. curl_easy_cleanup(e);
  105. }
  106. res = w;
  107. }
  108. else
  109. res = test_cert_blob(URL, libtest_arg2);
  110. curl_global_cleanup();
  111. return res;
  112. }