e94b18b0b4ff3fe7f0986daeb2c45890cf5a3bb4
[openssl.git] / test / x509_check_cert_pkey_test.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <openssl/pem.h>
14 #include <openssl/x509.h>
15 #include "testutil.h"
16
17 /*
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.
22  */
23 static int test_x509_check_cert_pkey(const char *c, const char *k,
24     const char *t, const char *e)
25 {
26     BIO *bio = NULL;
27     X509 *x509 = NULL;
28     X509_REQ *x509_req = NULL;
29     EVP_PKEY *pkey = NULL;
30     int ret = 0, type = 0, expected = 0, result = 0;
31
32     /*
33      * we check them first thus if fails we don't need to do
34      * those PEM parsing operations.
35      */
36     if (strcmp(t, "cert") == 0) {
37         type = 1;
38     } else if (strcmp(t, "req") == 0) {
39         type = 2;
40     } else {
41         TEST_error("invalid 'type'");
42         goto failed;
43     }
44
45     if (strcmp(e, "ok") == 0) {
46         expected = 1;
47     } else if (strcmp(e, "failed") == 0) {
48         expected = 2;
49     } else {
50         TEST_error("invalid 'expected'");
51         goto failed;
52     }
53
54     /* process private key */
55     bio = BIO_new_file(k, "r");
56     if (bio == NULL) {
57         TEST_error("create BIO for private key failed");
58         goto failed;
59     }
60
61     pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
62     if (pkey == NULL) {
63         TEST_error("read PEM private key failed");
64         goto failed;
65     }
66
67     BIO_free(bio);
68
69     /* process cert or cert request, use the same local var */
70     bio = BIO_new_file(c, "r");
71     if (bio == NULL) {
72         TEST_error("create BIO for cert or cert req failed");
73         goto failed;
74     }
75
76     switch (type) {
77         case 1:
78             x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
79             if (x509 == NULL) {
80                 TEST_error("read PEM x509 failed");
81                 goto failed;
82             }
83
84             result = X509_check_private_key(x509, pkey);
85             break;
86         case 2:
87             x509_req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL);
88             if (x509_req == NULL) {
89                 TEST_error("read PEM x509 req failed");
90                 goto failed;
91             }
92
93             result = X509_REQ_check_private_key(x509_req, pkey);
94             break;
95         default:
96             /* should never be here */
97             break;
98     }
99
100     if (expected == 1) {
101         /* expected == 1 means we expect an "ok" */
102         if (!TEST_int_eq(result, 1)) {
103             TEST_error("check private key: expected: 1, got: %d", result);
104             goto failed;
105         }
106     } else {
107         if (!TEST_int_eq(result, 0)) {
108             TEST_error("check private key: expected: 0, got: %d", result);
109             goto failed;
110         }
111     }
112
113 out:
114     if (bio)
115         BIO_free(bio);
116     if (x509)
117         X509_free(x509);
118     if (x509_req)
119         X509_REQ_free(x509_req);
120     if (pkey)
121         EVP_PKEY_free(pkey);
122     return ret;
123
124 failed:
125     ret = 1;
126     goto out;
127 }
128
129 int test_main(int argc, char **argv)
130 {
131     if (!TEST_int_eq(argc, 5)) {
132         TEST_info("usage: x509_check_cert_pkey cert.pem|cert.req"
133                   " key.pem cert|req <expected>");
134         return 1;
135     }
136
137     return test_x509_check_cert_pkey(argv[1], argv[2], argv[3], argv[4]);
138 }