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