issue-75.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2020 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <jose/jose.h>
  18. #include <jose/openssl.h>
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <openssl/opensslv.h>
  22. #include <openssl/ssl.h>
  23. /*
  24. * In this test we load a (RSA, 512-bit) PEM file asa n EVP_PKEY*, then
  25. * convert it to JWK with jose_openssl_jwk_from_EVP_PKEY().
  26. *
  27. * Afterwards, we convert this JWK to EVP_PKEY* again, with
  28. * jose_openssl_jwk_to_EVP_PKEY(), and once more convert the
  29. * resulting EVP_PKEY* back to JWK with jose_openssl_jwk_from_EVP_PKEY().
  30. *
  31. * We then compare the two JWKs, and they should be equal.
  32. */
  33. int
  34. main(int argc, char *argv[])
  35. {
  36. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  37. SSL_library_init();
  38. #else
  39. OPENSSL_init_ssl(0, NULL);
  40. #endif
  41. BIO* pfile = BIO_new_file("rsa512.pem", "r");
  42. assert(pfile);
  43. EVP_PKEY* pkey = PEM_read_bio_PrivateKey(pfile, NULL, 0, NULL);
  44. assert(pkey);
  45. BIO_free(pfile);
  46. json_auto_t* jwk = jose_openssl_jwk_from_EVP_PKEY(NULL, pkey);
  47. assert(jwk);
  48. EVP_PKEY* from_jwk = jose_openssl_jwk_to_EVP_PKEY(NULL, jwk);
  49. assert(from_jwk);
  50. json_auto_t* converted_jwk = jose_openssl_jwk_from_EVP_PKEY(NULL, from_jwk);
  51. assert(converted_jwk);
  52. EVP_PKEY_free(pkey);
  53. EVP_PKEY_free(from_jwk);
  54. assert(json_equal(jwk, converted_jwk));
  55. return EXIT_SUCCESS;
  56. }