timing_load_creds.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #ifndef _WIN32
  12. # include <sys/stat.h>
  13. # include <openssl/pem.h>
  14. # include <openssl/x509.h>
  15. # include <openssl/err.h>
  16. # include <openssl/bio.h>
  17. # include <../include/internal/e_os.h>
  18. # if defined(OPENSSL_SYS_UNIX)
  19. # include <sys/resource.h>
  20. # endif
  21. static char *prog;
  22. static void readx509(const char *contents, int size)
  23. {
  24. X509 *x = NULL;
  25. BIO *b = BIO_new_mem_buf(contents, size);
  26. if (b == NULL) {
  27. ERR_print_errors_fp(stderr);
  28. exit(EXIT_FAILURE);
  29. }
  30. PEM_read_bio_X509(b, &x, 0, NULL);
  31. if (x == NULL) {
  32. ERR_print_errors_fp(stderr);
  33. exit(EXIT_FAILURE);
  34. }
  35. X509_free(x);
  36. BIO_free(b);
  37. }
  38. static void readpkey(const char *contents, int size)
  39. {
  40. BIO *b = BIO_new_mem_buf(contents, size);
  41. EVP_PKEY *pkey;
  42. if (b == NULL) {
  43. ERR_print_errors_fp(stderr);
  44. exit(EXIT_FAILURE);
  45. }
  46. pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
  47. if (pkey == NULL) {
  48. ERR_print_errors_fp(stderr);
  49. exit(EXIT_FAILURE);
  50. }
  51. EVP_PKEY_free(pkey);
  52. BIO_free(b);
  53. }
  54. static void print_timeval(const char *what, struct timeval *tp)
  55. {
  56. printf("%s %d sec %d microsec\n", what, (int)tp->tv_sec, (int)tp->tv_usec);
  57. }
  58. static void usage(void)
  59. {
  60. fprintf(stderr, "Usage: %s [flags] pem-file\n", prog);
  61. fprintf(stderr, "Flags, with the default being '-wc':\n");
  62. fprintf(stderr, " -c # Repeat count\n");
  63. fprintf(stderr, " -d Debugging output (minimal)\n");
  64. fprintf(stderr, " -w<T> What to load T is a single character:\n");
  65. fprintf(stderr, " c for cert\n");
  66. fprintf(stderr, " p for private key\n");
  67. exit(EXIT_FAILURE);
  68. }
  69. #endif
  70. int main(int ac, char **av)
  71. {
  72. #ifndef _WIN32
  73. int i, debug = 0, count = 100, what = 'c';
  74. struct stat sb;
  75. FILE *fp;
  76. char *contents;
  77. struct rusage start, end, elapsed;
  78. struct timeval e_start, e_end, e_elapsed;
  79. /* Parse JCL. */
  80. prog = av[0];
  81. while ((i = getopt(ac, av, "c:dw:")) != EOF) {
  82. switch (i) {
  83. default:
  84. usage();
  85. break;
  86. case 'c':
  87. if ((count = atoi(optarg)) < 0)
  88. usage();
  89. break;
  90. case 'd':
  91. debug = 1;
  92. break;
  93. case 'w':
  94. if (optarg[1] != '\0')
  95. usage();
  96. switch (*optarg) {
  97. default:
  98. usage();
  99. break;
  100. case 'c':
  101. case 'p':
  102. what = *optarg;
  103. break;
  104. }
  105. break;
  106. }
  107. }
  108. ac -= optind;
  109. av += optind;
  110. /* Read input file. */
  111. if (av[0] == NULL)
  112. usage();
  113. if (stat(av[0], &sb) < 0) {
  114. perror(av[0]);
  115. exit(EXIT_FAILURE);
  116. }
  117. contents = OPENSSL_malloc(sb.st_size + 1);
  118. if (contents == NULL) {
  119. perror("malloc");
  120. exit(EXIT_FAILURE);
  121. }
  122. fp = fopen(av[0], "r");
  123. if ((long)fread(contents, 1, sb.st_size, fp) != sb.st_size) {
  124. perror("fread");
  125. exit(EXIT_FAILURE);
  126. }
  127. contents[sb.st_size] = '\0';
  128. fclose(fp);
  129. if (debug)
  130. printf(">%s<\n", contents);
  131. /* Try to prep system cache, etc. */
  132. for (i = 10; i > 0; i--) {
  133. switch (what) {
  134. case 'c':
  135. readx509(contents, (int)sb.st_size);
  136. break;
  137. case 'p':
  138. readpkey(contents, (int)sb.st_size);
  139. break;
  140. }
  141. }
  142. if (gettimeofday(&e_start, NULL) < 0) {
  143. perror("elapsed start");
  144. exit(EXIT_FAILURE);
  145. }
  146. if (getrusage(RUSAGE_SELF, &start) < 0) {
  147. perror("start");
  148. exit(EXIT_FAILURE);
  149. }
  150. for (i = count; i > 0; i--) {
  151. switch (what) {
  152. case 'c':
  153. readx509(contents, (int)sb.st_size);
  154. break;
  155. case 'p':
  156. readpkey(contents, (int)sb.st_size);
  157. break;
  158. }
  159. }
  160. if (getrusage(RUSAGE_SELF, &end) < 0) {
  161. perror("getrusage");
  162. exit(EXIT_FAILURE);
  163. }
  164. if (gettimeofday(&e_end, NULL) < 0) {
  165. perror("gettimeofday");
  166. exit(EXIT_FAILURE);
  167. }
  168. timersub(&end.ru_utime, &start.ru_stime, &elapsed.ru_stime);
  169. timersub(&end.ru_utime, &start.ru_utime, &elapsed.ru_utime);
  170. timersub(&e_end, &e_start, &e_elapsed);
  171. print_timeval("user ", &elapsed.ru_utime);
  172. print_timeval("sys ", &elapsed.ru_stime);
  173. if (debug)
  174. print_timeval("elapsed??", &e_elapsed);
  175. OPENSSL_free(contents);
  176. return EXIT_SUCCESS;
  177. #else
  178. fprintf(stderr, "This tool is not supported on Windows\n");
  179. exit(EXIT_FAILURE);
  180. #endif
  181. }