Implement riscv_vlen_asm for riscv32
[openssl.git] / test / testutil / load.c
1 /*
2  * Copyright 2020-2021 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
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include <openssl/x509.h>
14 #include <openssl/pem.h>
15
16 #include "../testutil.h"
17
18 X509 *load_cert_pem(const char *file, OSSL_LIB_CTX *libctx)
19 {
20     X509 *cert = NULL;
21     BIO *bio = NULL;
22
23     if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
24         return NULL;
25     if (TEST_int_gt(BIO_read_filename(bio, file), 0)
26             && TEST_ptr(cert = X509_new_ex(libctx, NULL)))
27         (void)TEST_ptr(cert = PEM_read_bio_X509(bio, &cert, NULL, NULL));
28
29     BIO_free(bio);
30     return cert;
31 }
32
33 STACK_OF(X509) *load_certs_pem(const char *filename)
34 {
35     STACK_OF(X509) *certs;
36     BIO *bio;
37     X509 *x;
38
39     bio = BIO_new_file(filename, "r");
40
41     if (bio == NULL) {
42         return NULL;
43     }
44
45     certs = sk_X509_new_null();
46     if (certs == NULL) {
47         BIO_free(bio);
48         return NULL;
49     }
50
51     ERR_set_mark();
52     do {
53         x = PEM_read_bio_X509(bio, NULL, 0, NULL);
54         if (x != NULL && !sk_X509_push(certs, x)) {
55             sk_X509_pop_free(certs, X509_free);
56             BIO_free(bio);
57             return NULL;
58         } else if (x == NULL) {
59             /*
60              * We probably just ran out of certs, so ignore any errors
61              * generated
62              */
63             ERR_pop_to_mark();
64         }
65     } while (x != NULL);
66
67     BIO_free(bio);
68
69     return certs;
70 }
71
72 EVP_PKEY *load_pkey_pem(const char *file, OSSL_LIB_CTX *libctx)
73 {
74     EVP_PKEY *key = NULL;
75     BIO *bio = NULL;
76
77     if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
78         return NULL;
79     if (TEST_int_gt(BIO_read_filename(bio, file), 0))
80         (void)TEST_ptr(key = PEM_read_bio_PrivateKey_ex(bio, NULL, NULL, NULL,
81                                                         libctx, NULL));
82
83     BIO_free(bio);
84     return key;
85 }
86
87 X509_REQ *load_csr_der(const char *file)
88 {
89     X509_REQ *csr = NULL;
90     BIO *bio = NULL;
91
92     if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new_file(file, "rb")))
93         return NULL;
94     (void)TEST_ptr(csr = d2i_X509_REQ_bio(bio, NULL));
95     BIO_free(bio);
96     return csr;
97 }