1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <stdlib.h>
12*e0c4386eSCy Schubert
13*e0c4386eSCy Schubert #include <openssl/x509.h>
14*e0c4386eSCy Schubert #include <openssl/pem.h>
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include "../testutil.h"
17*e0c4386eSCy Schubert
load_cert_pem(const char * file,OSSL_LIB_CTX * libctx)18*e0c4386eSCy Schubert X509 *load_cert_pem(const char *file, OSSL_LIB_CTX *libctx)
19*e0c4386eSCy Schubert {
20*e0c4386eSCy Schubert X509 *cert = NULL;
21*e0c4386eSCy Schubert BIO *bio = NULL;
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubert if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new(BIO_s_file())))
24*e0c4386eSCy Schubert return NULL;
25*e0c4386eSCy Schubert if (TEST_int_gt(BIO_read_filename(bio, file), 0)
26*e0c4386eSCy Schubert && TEST_ptr(cert = X509_new_ex(libctx, NULL)))
27*e0c4386eSCy Schubert (void)TEST_ptr(cert = PEM_read_bio_X509(bio, &cert, NULL, NULL));
28*e0c4386eSCy Schubert
29*e0c4386eSCy Schubert BIO_free(bio);
30*e0c4386eSCy Schubert return cert;
31*e0c4386eSCy Schubert }
32*e0c4386eSCy Schubert
STACK_OF(X509)33*e0c4386eSCy Schubert STACK_OF(X509) *load_certs_pem(const char *file)
34*e0c4386eSCy Schubert {
35*e0c4386eSCy Schubert STACK_OF(X509) *certs;
36*e0c4386eSCy Schubert BIO *bio;
37*e0c4386eSCy Schubert X509 *x;
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubert if (!TEST_ptr(file) || (bio = BIO_new_file(file, "r")) == NULL)
40*e0c4386eSCy Schubert return NULL;
41*e0c4386eSCy Schubert
42*e0c4386eSCy Schubert certs = sk_X509_new_null();
43*e0c4386eSCy Schubert if (certs == NULL) {
44*e0c4386eSCy Schubert BIO_free(bio);
45*e0c4386eSCy Schubert return NULL;
46*e0c4386eSCy Schubert }
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubert ERR_set_mark();
49*e0c4386eSCy Schubert do {
50*e0c4386eSCy Schubert x = PEM_read_bio_X509(bio, NULL, 0, NULL);
51*e0c4386eSCy Schubert if (x != NULL && !sk_X509_push(certs, x)) {
52*e0c4386eSCy Schubert sk_X509_pop_free(certs, X509_free);
53*e0c4386eSCy Schubert BIO_free(bio);
54*e0c4386eSCy Schubert return NULL;
55*e0c4386eSCy Schubert } else if (x == NULL) {
56*e0c4386eSCy Schubert /*
57*e0c4386eSCy Schubert * We probably just ran out of certs, so ignore any errors
58*e0c4386eSCy Schubert * generated
59*e0c4386eSCy Schubert */
60*e0c4386eSCy Schubert ERR_pop_to_mark();
61*e0c4386eSCy Schubert }
62*e0c4386eSCy Schubert } while (x != NULL);
63*e0c4386eSCy Schubert
64*e0c4386eSCy Schubert BIO_free(bio);
65*e0c4386eSCy Schubert
66*e0c4386eSCy Schubert return certs;
67*e0c4386eSCy Schubert }
68*e0c4386eSCy Schubert
load_pkey_pem(const char * file,OSSL_LIB_CTX * libctx)69*e0c4386eSCy Schubert EVP_PKEY *load_pkey_pem(const char *file, OSSL_LIB_CTX *libctx)
70*e0c4386eSCy Schubert {
71*e0c4386eSCy Schubert EVP_PKEY *key = NULL;
72*e0c4386eSCy Schubert BIO *bio = NULL;
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new(BIO_s_file())))
75*e0c4386eSCy Schubert return NULL;
76*e0c4386eSCy Schubert if (TEST_int_gt(BIO_read_filename(bio, file), 0)) {
77*e0c4386eSCy Schubert unsigned long err = ERR_peek_error();
78*e0c4386eSCy Schubert
79*e0c4386eSCy Schubert if (TEST_ptr(key = PEM_read_bio_PrivateKey_ex(bio, NULL, NULL, NULL,
80*e0c4386eSCy Schubert libctx, NULL))
81*e0c4386eSCy Schubert && err != ERR_peek_error()) {
82*e0c4386eSCy Schubert TEST_info("Spurious error from reading PEM");
83*e0c4386eSCy Schubert EVP_PKEY_free(key);
84*e0c4386eSCy Schubert key = NULL;
85*e0c4386eSCy Schubert }
86*e0c4386eSCy Schubert }
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubert BIO_free(bio);
89*e0c4386eSCy Schubert return key;
90*e0c4386eSCy Schubert }
91*e0c4386eSCy Schubert
load_csr_der(const char * file,OSSL_LIB_CTX * libctx)92*e0c4386eSCy Schubert X509_REQ *load_csr_der(const char *file, OSSL_LIB_CTX *libctx)
93*e0c4386eSCy Schubert {
94*e0c4386eSCy Schubert X509_REQ *csr = NULL;
95*e0c4386eSCy Schubert BIO *bio = NULL;
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubert if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new_file(file, "rb")))
98*e0c4386eSCy Schubert return NULL;
99*e0c4386eSCy Schubert
100*e0c4386eSCy Schubert csr = X509_REQ_new_ex(libctx, NULL);
101*e0c4386eSCy Schubert if (TEST_ptr(csr))
102*e0c4386eSCy Schubert (void)TEST_ptr(d2i_X509_REQ_bio(bio, &csr));
103*e0c4386eSCy Schubert BIO_free(bio);
104*e0c4386eSCy Schubert return csr;
105*e0c4386eSCy Schubert }
106