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