timing_load_creds.c 5.1 KB

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