test_OpenSSL.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. // this is just a super basic include and compile and link test
  9. // it doesn't exercise the library much, but if this compiles and links
  10. // its likely that further testing needs to be done in a real full project
  11. // rather than artificially
  12. // test whether a header is found
  13. #include <openssl/ssl.h>
  14. #include <openssl/sha.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/rand.h>
  19. #include <openssl/err.h>
  20. int main(int argc, char* argv[])
  21. {
  22. if (argc<3)
  23. {
  24. printf("Not enough arguments: %s [SSL Version Text] [SSL Version Text SHA256 Hash]", argv[0]);
  25. return 1;
  26. }
  27. const char* inputOpenSSLVersionText = argv[1];
  28. printf("Validating version text '%s': ", inputOpenSSLVersionText);
  29. if (strcmp(OPENSSL_VERSION_TEXT, inputOpenSSLVersionText) != 0)
  30. {
  31. printf("FAILURE!\n OpenSSL OPENSSL_VERSION_TEXT returned invalid text '%s'. Expecting '%s'.\n", OPENSSL_VERSION_TEXT, inputOpenSSLVersionText);
  32. return 1;
  33. }
  34. else
  35. {
  36. printf("OK\n");
  37. }
  38. if (OPENSSL_init_crypto(0, NULL) == 0)
  39. {
  40. printf("FAILURE! OPENSSL failed call to OPENSSL_init_ssl!\n");
  41. return 1;
  42. }
  43. // Compute a sha-1 hash
  44. unsigned char hash[SHA_DIGEST_LENGTH];
  45. SHA1(inputOpenSSLVersionText, strlen(inputOpenSSLVersionText), hash);
  46. // Generate a sha1sum string from the hash
  47. char sha1_hex[SHA_DIGEST_LENGTH*2+1] = {'\0'};
  48. char* p = sha1_hex;
  49. for (int i=0; i<SHA_DIGEST_LENGTH;i++, p+=2)
  50. {
  51. sprintf(p,"%.2x",hash[i]);
  52. }
  53. // Compare against the expected sha1sum (lower)
  54. const char* inputOpenSSLVersionTextHash = argv[2];
  55. printf("Validating version text sha1sum '%s': ", inputOpenSSLVersionTextHash);
  56. if (strcmp(sha1_hex, inputOpenSSLVersionTextHash) != 0)
  57. {
  58. printf("FAILURE!\n OpenSSL failed sha1 sum comparison (%s != %s)\n", sha1_hex, inputOpenSSLVersionTextHash);
  59. return 1;
  60. }
  61. else
  62. {
  63. printf("OK\n");
  64. }
  65. int rsa_key_length = 1024u;
  66. // Test generating random numbers
  67. printf("Generating random %d bit number : ", rsa_key_length);
  68. BIGNUM* random_bn = BN_new();
  69. if (BN_rand(random_bn, rsa_key_length, 1, 0)!=1)
  70. {
  71. printf("FAILURE!\n Failed to generate random number (%ld)\n", ERR_get_error());
  72. return 1;
  73. }
  74. printf("OK\n");
  75. char* rand_number_text = BN_bn2hex(random_bn);
  76. printf("Random Number: %s\n", rand_number_text);
  77. printf("Generating RSA Key pair (%d bits) : ", rsa_key_length);
  78. RSA* rsa_key = RSA_new();
  79. if (rsa_key == NULL)
  80. {
  81. printf("FAILURE!\n Failed to create RSA key instance (%ld)\n", ERR_get_error());
  82. return 1;
  83. }
  84. // Set the RSA exponent to RSA_F4 (0x10001l) for key generation
  85. BIGNUM* rsa_exponent = BN_new();
  86. if (BN_set_word(rsa_exponent, RSA_F4)!=1)
  87. {
  88. printf("FAILURE!\n Failed to generate BN exponent (%ld)\n", ERR_get_error());
  89. return 1;
  90. }
  91. if (RSA_generate_key_ex(rsa_key, rsa_key_length, rsa_exponent, NULL) == 0)
  92. {
  93. printf("FAILURE!\n Failed to generate %d bit RSA key (%ld)\n", rsa_key_length, ERR_get_error());
  94. return 1;
  95. }
  96. printf("OK\n");
  97. printf("Private key values:\n");
  98. char* rsa_p = BN_bn2hex(RSA_get0_p(rsa_key));
  99. char* rsa_q = BN_bn2hex(RSA_get0_q(rsa_key));
  100. char* rsa_e = BN_bn2hex(RSA_get0_e(rsa_key));
  101. printf(" exponent : %s\n", rsa_e);
  102. printf(" prime (p) : %s\n", rsa_p);
  103. printf(" prime (q) : %s\n", rsa_q);
  104. OPENSSL_free(rsa_p);
  105. OPENSSL_free(rsa_q);
  106. OPENSSL_free(rsa_e);
  107. BN_free(rsa_exponent);
  108. RSA_free(rsa_key);
  109. printf("Success: All is ok!\n");
  110. OPENSSL_cleanup();
  111. return 0;
  112. }