2
0

pem_read_depr_test.c 4.1 KB

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