2 * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/e_os2.h>
15 #ifdef OPENSSL_SYS_UNIX
16 # include <sys/stat.h>
17 # include <sys/resource.h>
18 # include <openssl/pem.h>
19 # include <openssl/x509.h>
20 # include <openssl/err.h>
21 # include <openssl/bio.h>
22 # include "internal/e_os.h"
23 # if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
26 /* struct timeval * subtraction; a must be greater than or equal to b */
27 # define timersub(a, b, res) \
29 (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
30 if ((a)->tv_usec < (b)->tv_usec) { \
31 (res)->tv_usec = (a)->tv_usec + 1000000 - (b)->tv_usec; \
34 (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
41 static void readx509(const char *contents, int size)
44 BIO *b = BIO_new_mem_buf(contents, size);
47 ERR_print_errors_fp(stderr);
50 PEM_read_bio_X509(b, &x, 0, NULL);
52 ERR_print_errors_fp(stderr);
59 static void readpkey(const char *contents, int size)
61 BIO *b = BIO_new_mem_buf(contents, size);
65 ERR_print_errors_fp(stderr);
68 pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
70 ERR_print_errors_fp(stderr);
78 static void print_timeval(const char *what, struct timeval *tp)
80 printf("%s %d sec %d microsec\n", what, (int)tp->tv_sec, (int)tp->tv_usec);
83 static void usage(void)
85 fprintf(stderr, "Usage: %s [flags] pem-file\n", prog);
86 fprintf(stderr, "Flags, with the default being '-wc':\n");
87 fprintf(stderr, " -c # Repeat count\n");
88 fprintf(stderr, " -d Debugging output (minimal)\n");
89 fprintf(stderr, " -w<T> What to load T is a single character:\n");
90 fprintf(stderr, " c for cert\n");
91 fprintf(stderr, " p for private key\n");
97 int main(int ac, char **av)
99 #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
100 int i, debug = 0, count = 100, what = 'c';
104 struct rusage start, end, elapsed;
105 struct timeval e_start, e_end, e_elapsed;
109 while ((i = getopt(ac, av, "c:dw:")) != EOF) {
115 if ((count = atoi(optarg)) < 0)
122 if (optarg[1] != '\0')
139 /* Read input file. */
142 if (stat(av[0], &sb) < 0) {
146 contents = OPENSSL_malloc(sb.st_size + 1);
147 if (contents == NULL) {
151 fp = fopen(av[0], "r");
152 if ((long)fread(contents, 1, sb.st_size, fp) != sb.st_size) {
156 contents[sb.st_size] = '\0';
159 printf(">%s<\n", contents);
161 /* Try to prep system cache, etc. */
162 for (i = 10; i > 0; i--) {
165 readx509(contents, (int)sb.st_size);
168 readpkey(contents, (int)sb.st_size);
173 if (gettimeofday(&e_start, NULL) < 0) {
174 perror("elapsed start");
177 if (getrusage(RUSAGE_SELF, &start) < 0) {
181 for (i = count; i > 0; i--) {
184 readx509(contents, (int)sb.st_size);
187 readpkey(contents, (int)sb.st_size);
191 if (getrusage(RUSAGE_SELF, &end) < 0) {
195 if (gettimeofday(&e_end, NULL) < 0) {
196 perror("gettimeofday");
200 timersub(&end.ru_utime, &start.ru_stime, &elapsed.ru_stime);
201 timersub(&end.ru_utime, &start.ru_utime, &elapsed.ru_utime);
202 timersub(&e_end, &e_start, &e_elapsed);
203 print_timeval("user ", &elapsed.ru_utime);
204 print_timeval("sys ", &elapsed.ru_stime);
206 print_timeval("elapsed??", &e_elapsed);
208 OPENSSL_free(contents);
212 "This tool is not supported on this platform for lack of POSIX1.2001 support\n");