lib678.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, 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. ***************************************************************************/
  22. #include "test.h"
  23. #include "testutil.h"
  24. #include "warnless.h"
  25. #include "memdebug.h"
  26. static int loadfile(const char *filename, void **filedata, size_t *filesize)
  27. {
  28. size_t datasize = 0;
  29. void *data = NULL;
  30. if(filename) {
  31. FILE *fInCert = fopen(filename, "rb");
  32. if(fInCert) {
  33. long cert_tell = 0;
  34. bool continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
  35. if(continue_reading)
  36. cert_tell = ftell(fInCert);
  37. if(cert_tell < 0)
  38. continue_reading = FALSE;
  39. else
  40. datasize = (size_t)cert_tell;
  41. if(continue_reading)
  42. continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
  43. if(continue_reading)
  44. data = malloc(datasize + 1);
  45. if((!data) ||
  46. ((int)fread(data, datasize, 1, fInCert) != 1))
  47. continue_reading = FALSE;
  48. fclose(fInCert);
  49. if(!continue_reading) {
  50. free(data);
  51. datasize = 0;
  52. data = NULL;
  53. }
  54. }
  55. }
  56. *filesize = datasize;
  57. *filedata = data;
  58. return data ? 1 : 0;
  59. }
  60. static int test_cert_blob(const char *url, const char *cafile)
  61. {
  62. CURLcode code = CURLE_OUT_OF_MEMORY;
  63. CURL *curl;
  64. struct curl_blob blob;
  65. size_t certsize;
  66. void *certdata;
  67. curl = curl_easy_init();
  68. if(!curl) {
  69. fprintf(stderr, "curl_easy_init() failed\n");
  70. return CURLE_FAILED_INIT;
  71. }
  72. if(loadfile(cafile, &certdata, &certsize)) {
  73. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  74. curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
  75. curl_easy_setopt(curl, CURLOPT_URL, url);
  76. curl_easy_setopt(curl, CURLOPT_USERAGENT, "CURLOPT_CAINFO_BLOB");
  77. curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS,
  78. CURLSSLOPT_REVOKE_BEST_EFFORT);
  79. blob.data = certdata;
  80. blob.len = certsize;
  81. blob.flags = CURL_BLOB_COPY;
  82. curl_easy_setopt(curl, CURLOPT_CAINFO_BLOB, &blob);
  83. free(certdata);
  84. code = curl_easy_perform(curl);
  85. }
  86. curl_easy_cleanup(curl);
  87. return (int)code;
  88. }
  89. int test(char *URL)
  90. {
  91. int res = 0;
  92. curl_global_init(CURL_GLOBAL_DEFAULT);
  93. if(!strcmp("check", URL)) {
  94. CURL *e;
  95. CURLcode w = CURLE_OK;
  96. struct curl_blob blob = {0};
  97. e = curl_easy_init();
  98. if(e) {
  99. w = curl_easy_setopt(e, CURLOPT_CAINFO_BLOB, &blob);
  100. if(w)
  101. printf("CURLOPT_CAINFO_BLOB is not supported\n");
  102. curl_easy_cleanup(e);
  103. }
  104. res = (int)w;
  105. }
  106. else
  107. res = test_cert_blob(URL, libtest_arg2);
  108. curl_global_cleanup();
  109. return res;
  110. }