2021-04-30 01:48:45 +08:00
|
|
|
/*
|
2022-07-25 15:19:40 +08:00
|
|
|
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2021-04-30 01:48:45 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
2022-07-25 15:19:40 +08:00
|
|
|
|
2021-04-30 01:48:45 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-07-25 15:19:40 +08:00
|
|
|
|
2022-11-19 20:05:19 +08:00
|
|
|
#include <openssl/e_os2.h>
|
|
|
|
|
|
|
|
#ifdef OPENSSL_SYS_UNIX
|
2022-07-25 15:19:40 +08:00
|
|
|
# include <sys/stat.h>
|
2022-11-19 20:05:19 +08:00
|
|
|
# include <sys/resource.h>
|
2022-07-25 15:19:40 +08:00
|
|
|
# include <openssl/pem.h>
|
|
|
|
# include <openssl/x509.h>
|
|
|
|
# include <openssl/err.h>
|
|
|
|
# include <openssl/bio.h>
|
2022-11-19 20:05:19 +08:00
|
|
|
# include "internal/e_os.h"
|
2022-12-05 22:38:03 +08:00
|
|
|
# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
|
2021-04-30 01:48:45 +08:00
|
|
|
|
2022-12-02 16:00:33 +08:00
|
|
|
# ifndef timersub
|
|
|
|
/* struct timeval * subtraction; a must be greater than or equal to b */
|
|
|
|
# define timersub(a, b, res) \
|
|
|
|
do { \
|
|
|
|
(res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
|
|
|
|
if ((a)->tv_usec < (b)->tv_usec) { \
|
2022-12-12 18:49:21 +08:00
|
|
|
(res)->tv_usec = (a)->tv_usec + 1000000 - (b)->tv_usec; \
|
2022-12-02 16:00:33 +08:00
|
|
|
--(res)->tv_sec; \
|
|
|
|
} else { \
|
2022-12-12 18:49:21 +08:00
|
|
|
(res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
|
2022-12-02 16:00:33 +08:00
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
# endif
|
|
|
|
|
2022-07-19 02:26:57 +08:00
|
|
|
static char *prog;
|
2021-04-30 01:48:45 +08:00
|
|
|
|
|
|
|
static void readx509(const char *contents, int size)
|
|
|
|
{
|
|
|
|
X509 *x = NULL;
|
|
|
|
BIO *b = BIO_new_mem_buf(contents, size);
|
|
|
|
|
2022-07-25 15:19:40 +08:00
|
|
|
if (b == NULL) {
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-04-30 01:48:45 +08:00
|
|
|
PEM_read_bio_X509(b, &x, 0, NULL);
|
2022-07-25 15:19:40 +08:00
|
|
|
if (x == NULL) {
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-04-30 01:48:45 +08:00
|
|
|
X509_free(x);
|
|
|
|
BIO_free(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readpkey(const char *contents, int size)
|
|
|
|
{
|
|
|
|
BIO *b = BIO_new_mem_buf(contents, size);
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
2022-07-25 15:19:40 +08:00
|
|
|
if (b == NULL) {
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-04-30 01:48:45 +08:00
|
|
|
pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
|
2022-07-25 15:19:40 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-04-30 01:48:45 +08:00
|
|
|
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
BIO_free(b);
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:19:40 +08:00
|
|
|
static void print_timeval(const char *what, struct timeval *tp)
|
2021-04-30 01:48:45 +08:00
|
|
|
{
|
2022-07-25 15:19:40 +08:00
|
|
|
printf("%s %d sec %d microsec\n", what, (int)tp->tv_sec, (int)tp->tv_usec);
|
2021-04-30 01:48:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [flags] pem-file\n", prog);
|
2022-07-25 15:19:40 +08:00
|
|
|
fprintf(stderr, "Flags, with the default being '-wc':\n");
|
2021-04-30 01:48:45 +08:00
|
|
|
fprintf(stderr, " -c # Repeat count\n");
|
|
|
|
fprintf(stderr, " -d Debugging output (minimal)\n");
|
2022-07-25 15:19:40 +08:00
|
|
|
fprintf(stderr, " -w<T> What to load T is a single character:\n");
|
|
|
|
fprintf(stderr, " c for cert\n");
|
2021-04-30 01:48:45 +08:00
|
|
|
fprintf(stderr, " p for private key\n");
|
2022-07-25 15:19:40 +08:00
|
|
|
exit(EXIT_FAILURE);
|
2021-04-30 01:48:45 +08:00
|
|
|
}
|
2022-12-05 22:38:03 +08:00
|
|
|
# endif
|
2022-07-25 15:19:40 +08:00
|
|
|
#endif
|
2021-04-30 01:48:45 +08:00
|
|
|
|
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
2022-12-05 22:38:03 +08:00
|
|
|
#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
|
2021-04-30 01:48:45 +08:00
|
|
|
int i, debug = 0, count = 100, what = 'c';
|
|
|
|
struct stat sb;
|
|
|
|
FILE *fp;
|
|
|
|
char *contents;
|
2022-07-25 15:19:40 +08:00
|
|
|
struct rusage start, end, elapsed;
|
2021-04-30 01:48:45 +08:00
|
|
|
struct timeval e_start, e_end, e_elapsed;
|
|
|
|
|
|
|
|
/* Parse JCL. */
|
|
|
|
prog = av[0];
|
|
|
|
while ((i = getopt(ac, av, "c:dw:")) != EOF) {
|
|
|
|
switch (i) {
|
|
|
|
default:
|
|
|
|
usage();
|
2022-07-25 15:19:40 +08:00
|
|
|
break;
|
2021-04-30 01:48:45 +08:00
|
|
|
case 'c':
|
|
|
|
if ((count = atoi(optarg)) < 0)
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debug = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
if (optarg[1] != '\0')
|
2022-07-25 15:19:40 +08:00
|
|
|
usage();
|
2021-04-30 01:48:45 +08:00
|
|
|
switch (*optarg) {
|
|
|
|
default:
|
|
|
|
usage();
|
2022-07-25 15:19:40 +08:00
|
|
|
break;
|
2021-04-30 01:48:45 +08:00
|
|
|
case 'c':
|
|
|
|
case 'p':
|
|
|
|
what = *optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ac -= optind;
|
|
|
|
av += optind;
|
|
|
|
|
|
|
|
/* Read input file. */
|
|
|
|
if (av[0] == NULL)
|
|
|
|
usage();
|
2022-07-25 15:19:40 +08:00
|
|
|
if (stat(av[0], &sb) < 0) {
|
|
|
|
perror(av[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
contents = OPENSSL_malloc(sb.st_size + 1);
|
|
|
|
if (contents == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-04-30 01:48:45 +08:00
|
|
|
fp = fopen(av[0], "r");
|
2022-07-25 15:19:40 +08:00
|
|
|
if ((long)fread(contents, 1, sb.st_size, fp) != sb.st_size) {
|
|
|
|
perror("fread");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-04-30 01:48:45 +08:00
|
|
|
contents[sb.st_size] = '\0';
|
|
|
|
fclose(fp);
|
|
|
|
if (debug)
|
|
|
|
printf(">%s<\n", contents);
|
|
|
|
|
|
|
|
/* Try to prep system cache, etc. */
|
2022-07-25 15:19:40 +08:00
|
|
|
for (i = 10; i > 0; i--) {
|
2021-04-30 01:48:45 +08:00
|
|
|
switch (what) {
|
|
|
|
case 'c':
|
|
|
|
readx509(contents, (int)sb.st_size);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
readpkey(contents, (int)sb.st_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:19:40 +08:00
|
|
|
if (gettimeofday(&e_start, NULL) < 0) {
|
|
|
|
perror("elapsed start");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (getrusage(RUSAGE_SELF, &start) < 0) {
|
|
|
|
perror("start");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
for (i = count; i > 0; i--) {
|
2021-04-30 01:48:45 +08:00
|
|
|
switch (what) {
|
|
|
|
case 'c':
|
|
|
|
readx509(contents, (int)sb.st_size);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
readpkey(contents, (int)sb.st_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 15:19:40 +08:00
|
|
|
if (getrusage(RUSAGE_SELF, &end) < 0) {
|
|
|
|
perror("getrusage");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (gettimeofday(&e_end, NULL) < 0) {
|
|
|
|
perror("gettimeofday");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-04-30 01:48:45 +08:00
|
|
|
|
|
|
|
timersub(&end.ru_utime, &start.ru_stime, &elapsed.ru_stime);
|
|
|
|
timersub(&end.ru_utime, &start.ru_utime, &elapsed.ru_utime);
|
|
|
|
timersub(&e_end, &e_start, &e_elapsed);
|
2022-07-25 15:19:40 +08:00
|
|
|
print_timeval("user ", &elapsed.ru_utime);
|
|
|
|
print_timeval("sys ", &elapsed.ru_stime);
|
2021-04-30 01:48:45 +08:00
|
|
|
if (debug)
|
2022-07-25 15:19:40 +08:00
|
|
|
print_timeval("elapsed??", &e_elapsed);
|
|
|
|
|
|
|
|
OPENSSL_free(contents);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
#else
|
2022-12-05 22:38:03 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"This tool is not supported on this platform for lack of POSIX1.2001 support\n");
|
2022-07-25 15:19:40 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
#endif
|
2021-04-30 01:48:45 +08:00
|
|
|
}
|