pem_read_depr_test.c 4.0 KB

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