xref: /freebsd/crypto/openssl/test/pem_read_depr_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 /*
11*e0c4386eSCy Schubert  * This file tests deprecated APIs. Therefore we need to suppress deprecation
12*e0c4386eSCy Schubert  * warnings.
13*e0c4386eSCy Schubert  */
14*e0c4386eSCy Schubert #define OPENSSL_SUPPRESS_DEPRECATED
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert #include <openssl/pem.h>
17*e0c4386eSCy Schubert #include <openssl/bio.h>
18*e0c4386eSCy Schubert #include <openssl/dh.h>
19*e0c4386eSCy Schubert #include <openssl/dsa.h>
20*e0c4386eSCy Schubert #include <openssl/rsa.h>
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert #include "testutil.h"
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert static const char *datadir;
25*e0c4386eSCy Schubert 
getfile(const char * filename)26*e0c4386eSCy Schubert static BIO *getfile(const char *filename)
27*e0c4386eSCy Schubert {
28*e0c4386eSCy Schubert     char *paramsfile = test_mk_file_path(datadir, filename);
29*e0c4386eSCy Schubert     BIO *infile = NULL;
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert     if (!TEST_ptr(paramsfile))
32*e0c4386eSCy Schubert         goto err;
33*e0c4386eSCy Schubert     infile = BIO_new_file(paramsfile, "r");
34*e0c4386eSCy Schubert 
35*e0c4386eSCy Schubert  err:
36*e0c4386eSCy Schubert     OPENSSL_free(paramsfile);
37*e0c4386eSCy Schubert     return infile;
38*e0c4386eSCy Schubert }
39*e0c4386eSCy Schubert 
40*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
test_read_dh_params(void)41*e0c4386eSCy Schubert static int test_read_dh_params(void)
42*e0c4386eSCy Schubert {
43*e0c4386eSCy Schubert     int testresult = 0;
44*e0c4386eSCy Schubert     BIO *infile = getfile("dhparams.pem");
45*e0c4386eSCy Schubert     DH *dh = NULL;
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert     if (!TEST_ptr(infile))
48*e0c4386eSCy Schubert         goto err;
49*e0c4386eSCy Schubert 
50*e0c4386eSCy Schubert     dh = PEM_read_bio_DHparams(infile, NULL, NULL, NULL);
51*e0c4386eSCy Schubert     if (!TEST_ptr(dh))
52*e0c4386eSCy Schubert         goto err;
53*e0c4386eSCy Schubert 
54*e0c4386eSCy Schubert     testresult = 1;
55*e0c4386eSCy Schubert 
56*e0c4386eSCy Schubert  err:
57*e0c4386eSCy Schubert     BIO_free(infile);
58*e0c4386eSCy Schubert     DH_free(dh);
59*e0c4386eSCy Schubert     return testresult;
60*e0c4386eSCy Schubert }
61*e0c4386eSCy Schubert 
test_read_dh_x942_params(void)62*e0c4386eSCy Schubert static int test_read_dh_x942_params(void)
63*e0c4386eSCy Schubert {
64*e0c4386eSCy Schubert     int testresult = 0;
65*e0c4386eSCy Schubert     BIO *infile = getfile("x942params.pem");
66*e0c4386eSCy Schubert     DH *dh = NULL;
67*e0c4386eSCy Schubert 
68*e0c4386eSCy Schubert     if (!TEST_ptr(infile))
69*e0c4386eSCy Schubert         goto err;
70*e0c4386eSCy Schubert 
71*e0c4386eSCy Schubert     dh = PEM_read_bio_DHparams(infile, NULL, NULL, NULL);
72*e0c4386eSCy Schubert     if (!TEST_ptr(dh))
73*e0c4386eSCy Schubert         goto err;
74*e0c4386eSCy Schubert 
75*e0c4386eSCy Schubert     testresult = 1;
76*e0c4386eSCy Schubert 
77*e0c4386eSCy Schubert  err:
78*e0c4386eSCy Schubert     BIO_free(infile);
79*e0c4386eSCy Schubert     DH_free(dh);
80*e0c4386eSCy Schubert     return testresult;
81*e0c4386eSCy Schubert }
82*e0c4386eSCy Schubert #endif
83*e0c4386eSCy Schubert 
84*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DSA
test_read_dsa_params(void)85*e0c4386eSCy Schubert static int test_read_dsa_params(void)
86*e0c4386eSCy Schubert {
87*e0c4386eSCy Schubert     int testresult = 0;
88*e0c4386eSCy Schubert     BIO *infile = getfile("dsaparams.pem");
89*e0c4386eSCy Schubert     DSA *dsa = NULL;
90*e0c4386eSCy Schubert 
91*e0c4386eSCy Schubert     if (!TEST_ptr(infile))
92*e0c4386eSCy Schubert         goto err;
93*e0c4386eSCy Schubert 
94*e0c4386eSCy Schubert     dsa = PEM_read_bio_DSAparams(infile, NULL, NULL, NULL);
95*e0c4386eSCy Schubert     if (!TEST_ptr(dsa))
96*e0c4386eSCy Schubert         goto err;
97*e0c4386eSCy Schubert 
98*e0c4386eSCy Schubert     testresult = 1;
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert  err:
101*e0c4386eSCy Schubert     BIO_free(infile);
102*e0c4386eSCy Schubert     DSA_free(dsa);
103*e0c4386eSCy Schubert     return testresult;
104*e0c4386eSCy Schubert }
105*e0c4386eSCy Schubert 
test_read_dsa_private(void)106*e0c4386eSCy Schubert static int test_read_dsa_private(void)
107*e0c4386eSCy Schubert {
108*e0c4386eSCy Schubert     int testresult = 0;
109*e0c4386eSCy Schubert     BIO *infile = getfile("dsaprivatekey.pem");
110*e0c4386eSCy Schubert     DSA *dsa = NULL;
111*e0c4386eSCy Schubert 
112*e0c4386eSCy Schubert     if (!TEST_ptr(infile))
113*e0c4386eSCy Schubert         goto err;
114*e0c4386eSCy Schubert 
115*e0c4386eSCy Schubert     dsa = PEM_read_bio_DSAPrivateKey(infile, NULL, NULL, NULL);
116*e0c4386eSCy Schubert     if (!TEST_ptr(dsa))
117*e0c4386eSCy Schubert         goto err;
118*e0c4386eSCy Schubert 
119*e0c4386eSCy Schubert     testresult = 1;
120*e0c4386eSCy Schubert 
121*e0c4386eSCy Schubert  err:
122*e0c4386eSCy Schubert     BIO_free(infile);
123*e0c4386eSCy Schubert     DSA_free(dsa);
124*e0c4386eSCy Schubert     return testresult;
125*e0c4386eSCy Schubert }
126*e0c4386eSCy Schubert 
test_read_dsa_public(void)127*e0c4386eSCy Schubert static int test_read_dsa_public(void)
128*e0c4386eSCy Schubert {
129*e0c4386eSCy Schubert     int testresult = 0;
130*e0c4386eSCy Schubert     BIO *infile = getfile("dsapublickey.pem");
131*e0c4386eSCy Schubert     DSA *dsa = NULL;
132*e0c4386eSCy Schubert 
133*e0c4386eSCy Schubert     if (!TEST_ptr(infile))
134*e0c4386eSCy Schubert         goto err;
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert     dsa = PEM_read_bio_DSA_PUBKEY(infile, NULL, NULL, NULL);
137*e0c4386eSCy Schubert     if (!TEST_ptr(dsa))
138*e0c4386eSCy Schubert         goto err;
139*e0c4386eSCy Schubert 
140*e0c4386eSCy Schubert     testresult = 1;
141*e0c4386eSCy Schubert 
142*e0c4386eSCy Schubert  err:
143*e0c4386eSCy Schubert     BIO_free(infile);
144*e0c4386eSCy Schubert     DSA_free(dsa);
145*e0c4386eSCy Schubert     return testresult;
146*e0c4386eSCy Schubert }
147*e0c4386eSCy Schubert #endif
148*e0c4386eSCy Schubert 
test_read_rsa_private(void)149*e0c4386eSCy Schubert static int test_read_rsa_private(void)
150*e0c4386eSCy Schubert {
151*e0c4386eSCy Schubert     int testresult = 0;
152*e0c4386eSCy Schubert     BIO *infile = getfile("rsaprivatekey.pem");
153*e0c4386eSCy Schubert     RSA *rsa = NULL;
154*e0c4386eSCy Schubert 
155*e0c4386eSCy Schubert     if (!TEST_ptr(infile))
156*e0c4386eSCy Schubert         goto err;
157*e0c4386eSCy Schubert 
158*e0c4386eSCy Schubert     rsa = PEM_read_bio_RSAPrivateKey(infile, NULL, NULL, NULL);
159*e0c4386eSCy Schubert     if (!TEST_ptr(rsa))
160*e0c4386eSCy Schubert         goto err;
161*e0c4386eSCy Schubert 
162*e0c4386eSCy Schubert     testresult = 1;
163*e0c4386eSCy Schubert 
164*e0c4386eSCy Schubert  err:
165*e0c4386eSCy Schubert     BIO_free(infile);
166*e0c4386eSCy Schubert     RSA_free(rsa);
167*e0c4386eSCy Schubert     return testresult;
168*e0c4386eSCy Schubert }
169*e0c4386eSCy Schubert 
test_read_rsa_public(void)170*e0c4386eSCy Schubert static int test_read_rsa_public(void)
171*e0c4386eSCy Schubert {
172*e0c4386eSCy Schubert     int testresult = 0;
173*e0c4386eSCy Schubert     BIO *infile = getfile("rsapublickey.pem");
174*e0c4386eSCy Schubert     RSA *rsa = NULL;
175*e0c4386eSCy Schubert 
176*e0c4386eSCy Schubert     if (!TEST_ptr(infile))
177*e0c4386eSCy Schubert         goto err;
178*e0c4386eSCy Schubert 
179*e0c4386eSCy Schubert     rsa = PEM_read_bio_RSA_PUBKEY(infile, NULL, NULL, NULL);
180*e0c4386eSCy Schubert     if (!TEST_ptr(rsa))
181*e0c4386eSCy Schubert         goto err;
182*e0c4386eSCy Schubert 
183*e0c4386eSCy Schubert     testresult = 1;
184*e0c4386eSCy Schubert 
185*e0c4386eSCy Schubert  err:
186*e0c4386eSCy Schubert     BIO_free(infile);
187*e0c4386eSCy Schubert     RSA_free(rsa);
188*e0c4386eSCy Schubert     return testresult;
189*e0c4386eSCy Schubert }
190*e0c4386eSCy Schubert 
setup_tests(void)191*e0c4386eSCy Schubert int setup_tests(void)
192*e0c4386eSCy Schubert {
193*e0c4386eSCy Schubert     if (!test_skip_common_options()) {
194*e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
195*e0c4386eSCy Schubert         return 0;
196*e0c4386eSCy Schubert     }
197*e0c4386eSCy Schubert 
198*e0c4386eSCy Schubert     if (!TEST_ptr(datadir = test_get_argument(0))) {
199*e0c4386eSCy Schubert         TEST_error("Error getting data dir\n");
200*e0c4386eSCy Schubert         return 0;
201*e0c4386eSCy Schubert     }
202*e0c4386eSCy Schubert 
203*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
204*e0c4386eSCy Schubert     ADD_TEST(test_read_dh_params);
205*e0c4386eSCy Schubert     ADD_TEST(test_read_dh_x942_params);
206*e0c4386eSCy Schubert #endif
207*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DSA
208*e0c4386eSCy Schubert     ADD_TEST(test_read_dsa_params);
209*e0c4386eSCy Schubert     ADD_TEST(test_read_dsa_private);
210*e0c4386eSCy Schubert     ADD_TEST(test_read_dsa_public);
211*e0c4386eSCy Schubert #endif
212*e0c4386eSCy Schubert     ADD_TEST(test_read_rsa_private);
213*e0c4386eSCy Schubert     ADD_TEST(test_read_rsa_public);
214*e0c4386eSCy Schubert 
215*e0c4386eSCy Schubert     return 1;
216*e0c4386eSCy Schubert }
217