File: timing_load_creds.c

package info (click to toggle)
openssl 3.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 147,036 kB
  • sloc: ansic: 652,495; perl: 247,867; asm: 6,332; sh: 1,681; pascal: 997; python: 648; makefile: 551; lisp: 35; ruby: 16; cpp: 10; sed: 6
file content (256 lines) | stat: -rw-r--r-- 7,139 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
 *
 * 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
 */

#include <stdio.h>
#include <stdlib.h>

#include <openssl/e_os2.h>

#ifdef OPENSSL_SYS_UNIX
#include <sys/stat.h>
#include <sys/resource.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include "internal/e_os.h"
#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L

#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) {                          \
            (res)->tv_usec = (a)->tv_usec + 1000000 - (b)->tv_usec; \
            --(res)->tv_sec;                                        \
        } else {                                                    \
            (res)->tv_usec = (a)->tv_usec - (b)->tv_usec;           \
        }                                                           \
    } while (0)
#endif

static char *prog;

static void readx509(const char *contents, int size)
{
    X509 *x = NULL;
    BIO *b = BIO_new_mem_buf(contents, size);

    if (b == NULL) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }
    PEM_read_bio_X509(b, &x, 0, NULL);
    if (x == NULL) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }
    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;

    if (b == NULL) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }
    pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
    if (pkey == NULL) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }

    EVP_PKEY_free(pkey);
    BIO_free(b);
}

static void print_timeval(const char *what, struct timeval *tp)
{
    printf("%s %d sec %d microsec\n", what, (int)tp->tv_sec, (int)tp->tv_usec);
}

static void usage(void)
{
    fprintf(stderr, "Usage: %s [flags] pem-file\n", prog);
    fprintf(stderr, "Flags, with the default being '-wc':\n");
    fprintf(stderr, "  -c #  Repeat count\n");
    fprintf(stderr, "  -d    Debugging output (minimal)\n");
    fprintf(stderr, "  -w<T> What to load T is a single character:\n");
    fprintf(stderr, "          c for cert\n");
    fprintf(stderr, "          p for private key\n");
    exit(EXIT_FAILURE);
}
#endif
#endif

#if !defined(RUSAGE_SELF) && defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
#include <sys/times.h>
#endif
int main(int ac, char **av)
{
#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
    int i, debug = 0, count = 100, what = 'c';
    struct stat sb;
    FILE *fp;
    char *contents;
#if !defined(RUSAGE_SELF)
    struct tms rus;
    struct timeval u_start, u_end, u_elapsed;
    struct timeval s_start, s_end, s_elapsed;
#else
    struct rusage start, end, elapsed;
#endif
    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();
            break;
        case 'c':
            if ((count = atoi(optarg)) < 0)
                usage();
            break;
        case 'd':
            debug = 1;
            break;
        case 'w':
            if (optarg[1] != '\0')
                usage();
            switch (*optarg) {
            default:
                usage();
                break;
            case 'c':
            case 'p':
                what = *optarg;
                break;
            }
            break;
        }
    }
    ac -= optind;
    av += optind;

    /* Read input file. */
    if (av[0] == NULL)
        usage();
    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);
    }
    fp = fopen(av[0], "r");
    if ((long)fread(contents, 1, sb.st_size, fp) != sb.st_size) {
        fclose(fp);
        OPENSSL_free(contents);
        perror("fread");
        exit(EXIT_FAILURE);
    }
    contents[sb.st_size] = '\0';
    fclose(fp);
    if (debug)
        printf(">%s<\n", contents);

    /* Try to prep system cache, etc. */
    for (i = 10; i > 0; i--) {
        switch (what) {
        case 'c':
            readx509(contents, (int)sb.st_size);
            break;
        case 'p':
            readpkey(contents, (int)sb.st_size);
            break;
        }
    }

    if (gettimeofday(&e_start, NULL) < 0) {
        OPENSSL_free(contents);
        perror("elapsed start");
        exit(EXIT_FAILURE);
    }
#if !defined(RUSAGE_SELF)
    times(&rus);
    u_start.tv_sec = rus.tms_utime / CLOCKS_PER_SEC;
    u_start.tv_usec = (rus.tms_utime * 1000000) / CLOCKS_PER_SEC;
    s_start.tv_sec = rus.tms_stime / CLOCKS_PER_SEC;
    s_start.tv_usec = (rus.tms_stime * 1000000) / CLOCKS_PER_SEC;
#else
    if (getrusage(RUSAGE_SELF, &start) < 0) {
        OPENSSL_free(contents);
        perror("start");
        exit(EXIT_FAILURE);
    }
#endif
    for (i = count; i > 0; i--) {
        switch (what) {
        case 'c':
            readx509(contents, (int)sb.st_size);
            break;
        case 'p':
            readpkey(contents, (int)sb.st_size);
            break;
        }
    }
#if !defined(RUSAGE_SELF)
    times(&rus);
    u_end.tv_sec = rus.tms_utime / CLOCKS_PER_SEC;
    u_end.tv_usec = (rus.tms_utime * 1000000) / CLOCKS_PER_SEC;
    s_end.tv_sec = rus.tms_stime / CLOCKS_PER_SEC;
    s_end.tv_usec = (rus.tms_stime * 1000000) / CLOCKS_PER_SEC;
#else
    if (getrusage(RUSAGE_SELF, &end) < 0) {
        OPENSSL_free(contents);
        perror("getrusage");
        exit(EXIT_FAILURE);
    }
#endif
    if (gettimeofday(&e_end, NULL) < 0) {
        OPENSSL_free(contents);
        perror("gettimeofday");
        exit(EXIT_FAILURE);
    }

#if !defined(RUSAGE_SELF)
    timersub(&u_end, &u_start, &u_elapsed);
    timersub(&s_end, &s_start, &s_elapsed);
#else
    timersub(&end.ru_utime, &start.ru_stime, &elapsed.ru_stime);
    timersub(&end.ru_utime, &start.ru_utime, &elapsed.ru_utime);
#endif
    timersub(&e_end, &e_start, &e_elapsed);
#if !defined(RUSAGE_SELF)
    print_timeval("user     ", &u_elapsed);
    print_timeval("sys      ", &s_elapsed);
#else
    print_timeval("user     ", &elapsed.ru_utime);
    print_timeval("sys      ", &elapsed.ru_stime);
#endif
    if (debug)
        print_timeval("elapsed??", &e_elapsed);

    OPENSSL_free(contents);
    return EXIT_SUCCESS;
#else
    fprintf(stderr,
        "This tool is not supported on this platform for lack of POSIX1.2001 support\n");
    exit(EXIT_FAILURE);
#endif
}