2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include <openssl/pem.h>
14 #include <openssl/x509.h>
18 * c: path of a cert in PEM format
19 * k: path of a key in PEM format
20 * t: API type, "cert" for X509_ and "req" for X509_REQ_ APIs.
21 * e: expected, "ok" for success, "failed" for what should fail.
28 static int test_x509_check_cert_pkey(void)
32 X509_REQ *x509_req = NULL;
33 EVP_PKEY *pkey = NULL;
34 int ret = 0, type = 0, expected = 0, result = 0;
37 * we check them first thus if fails we don't need to do
38 * those PEM parsing operations.
40 if (strcmp(t, "cert") == 0) {
42 } else if (strcmp(t, "req") == 0) {
45 TEST_error("invalid 'type'");
49 if (strcmp(e, "ok") == 0) {
51 } else if (strcmp(e, "failed") == 0) {
54 TEST_error("invalid 'expected'");
58 /* process private key */
59 if (!TEST_ptr(bio = BIO_new_file(k, "r")))
62 if (!TEST_ptr(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)))
67 /* process cert or cert request, use the same local var */
68 if (!TEST_ptr(bio = BIO_new_file(c, "r")))
73 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
75 TEST_error("read PEM x509 failed");
79 result = X509_check_private_key(x509, pkey);
82 x509_req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL);
83 if (x509_req == NULL) {
84 TEST_error("read PEM x509 req failed");
88 result = X509_REQ_check_private_key(x509_req, pkey);
91 /* should never be here */
95 if (!TEST_int_eq(result, expected)) {
96 TEST_error("check private key: expected: %d, got: %d", expected, result);
104 X509_REQ_free(x509_req);
109 int setup_tests(void)
111 if (!TEST_ptr(c = test_get_argument(0))
112 || !TEST_ptr(k = test_get_argument(1))
113 || !TEST_ptr(t = test_get_argument(2))
114 || !TEST_ptr(e = test_get_argument(3))) {
115 TEST_note("usage: x509_check_cert_pkey cert.pem|cert.req"
116 " key.pem cert|req <expected>");
120 ADD_TEST(test_x509_check_cert_pkey);