test_hostkey_hash.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "session_fixture.h"
  2. #include "libssh2_config.h"
  3. #include <libssh2.h>
  4. #include <stdio.h>
  5. static const char *EXPECTED_RSA_HOSTKEY =
  6. "AAAAB3NzaC1yc2EAAAABIwAAAQEArrr/JuJmaZligyfS8vcNur+mWR2ddDQtVdhHzdKU"
  7. "UoR6/Om6cvxpe61H1YZO1xCpLUBXmkki4HoNtYOpPB2W4V+8U4BDeVBD5crypEOE1+7B"
  8. "Am99fnEDxYIOZq2/jTP0yQmzCpWYS3COyFmkOL7sfX1wQMeW5zQT2WKcxC6FSWbhDqrB"
  9. "eNEGi687hJJoJ7YXgY/IdiYW5NcOuqRSWljjGS3dAJsHHWk4nJbhjEDXbPaeduMAwQU9"
  10. "i6ELfP3r+q6wdu0P4jWaoo3De1aYxnToV/ldXykpipON4NPamsb6Ph2qlJQKypq7J4iQ"
  11. "gkIIbCU1A31+4ExvcIVoxLQw/aTSbw==";
  12. static const char *EXPECTED_ECDSA_HOSTKEY =
  13. "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC+/syyeKJD9dC2ZH"
  14. "9Q7iJGReR4YM3rUCMsSynkyXojdfSClGCMY7JvWlt30ESjYvxoTfSRGx6WvaqYK/vPoYQ4=";
  15. static const char *EXPECTED_RSA_MD5_HASH_DIGEST =
  16. "0C0ED1A5BB10275F76924CE187CE5C5E";
  17. static const char *EXPECTED_RSA_SHA1_HASH_DIGEST =
  18. "F3CD59E2913F4422B80F7B0A82B2B89EAE449387";
  19. static const char *EXPECTED_RSA_SHA256_HASH_DIGEST =
  20. "92E3DA49DF3C7F99A828F505ED8239397A5D1F62914459760F878F7510F563A3";
  21. static const char *EXPECTED_ECDSA_MD5_HASH_DIGEST =
  22. "0402E4D897580BBC911379CBD88BCD3D";
  23. static const char *EXPECTED_ECDSA_SHA1_HASH_DIGEST =
  24. "12FDAD1E3B31B10BABB00F2A8D1B9A62C326BD2F";
  25. static const char *EXPECTED_ECDSA_SHA256_HASH_DIGEST =
  26. "56FCD975B166C3F0342D0036E44C311A86C0EAE40713B53FC776369BAE7F5264";
  27. static const int MD5_HASH_SIZE = 16;
  28. static const int SHA1_HASH_SIZE = 20;
  29. static const int SHA256_HASH_SIZE = 32;
  30. static void calculate_digest(const char *hash, size_t hash_len, char *buffer,
  31. size_t buffer_len)
  32. {
  33. size_t i;
  34. char *p = buffer;
  35. char *end = buffer + buffer_len;
  36. for(i = 0; i < hash_len && p < end; ++i) {
  37. p += snprintf(p, end - p, "%02X", (unsigned char)hash[i]);
  38. }
  39. }
  40. int test(LIBSSH2_SESSION *session)
  41. {
  42. char buf[BUFSIZ];
  43. const char *hostkey;
  44. const char *md5_hash;
  45. const char *sha1_hash;
  46. const char *sha256_hash;
  47. int type;
  48. size_t len;
  49. /* these are the host keys under test, they are currently unused */
  50. (void)EXPECTED_RSA_HOSTKEY;
  51. (void)EXPECTED_ECDSA_HOSTKEY;
  52. hostkey = libssh2_session_hostkey(session, &len, &type);
  53. if(hostkey == NULL) {
  54. print_last_session_error("libssh2_session_hostkey");
  55. return 1;
  56. }
  57. if(type == LIBSSH2_HOSTKEY_TYPE_ECDSA_256) {
  58. md5_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
  59. if(md5_hash == NULL) {
  60. print_last_session_error(
  61. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_MD5)");
  62. return 1;
  63. }
  64. calculate_digest(md5_hash, MD5_HASH_SIZE, buf, BUFSIZ);
  65. if(strcmp(buf, EXPECTED_ECDSA_MD5_HASH_DIGEST) != 0) {
  66. fprintf(stderr,
  67. "ECDSA MD5 hash not as expected - digest %s != %s\n",
  68. buf, EXPECTED_ECDSA_MD5_HASH_DIGEST);
  69. return 1;
  70. }
  71. sha1_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  72. if(sha1_hash == NULL) {
  73. print_last_session_error(
  74. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA1)");
  75. return 1;
  76. }
  77. calculate_digest(sha1_hash, SHA1_HASH_SIZE, buf, BUFSIZ);
  78. if(strcmp(buf, EXPECTED_ECDSA_SHA1_HASH_DIGEST) != 0) {
  79. fprintf(stderr,
  80. "ECDSA SHA1 hash not as expected - digest %s != %s\n",
  81. buf, EXPECTED_ECDSA_SHA1_HASH_DIGEST);
  82. return 1;
  83. }
  84. sha256_hash = libssh2_hostkey_hash(session,
  85. LIBSSH2_HOSTKEY_HASH_SHA256);
  86. if(sha256_hash == NULL) {
  87. print_last_session_error(
  88. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA256)");
  89. return 1;
  90. }
  91. calculate_digest(sha256_hash, SHA256_HASH_SIZE, buf, BUFSIZ);
  92. if(strcmp(buf, EXPECTED_ECDSA_SHA256_HASH_DIGEST) != 0) {
  93. fprintf(stderr,
  94. "ECDSA SHA256 hash not as expected - digest %s != %s\n",
  95. buf, EXPECTED_ECDSA_SHA256_HASH_DIGEST);
  96. return 1;
  97. }
  98. }
  99. else if(type == LIBSSH2_HOSTKEY_TYPE_RSA) {
  100. md5_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
  101. if(md5_hash == NULL) {
  102. print_last_session_error(
  103. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_MD5)");
  104. return 1;
  105. }
  106. calculate_digest(md5_hash, MD5_HASH_SIZE, buf, BUFSIZ);
  107. if(strcmp(buf, EXPECTED_RSA_MD5_HASH_DIGEST) != 0) {
  108. fprintf(stderr,
  109. "MD5 hash not as expected - digest %s != %s\n",
  110. buf, EXPECTED_RSA_MD5_HASH_DIGEST);
  111. return 1;
  112. }
  113. sha1_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  114. if(sha1_hash == NULL) {
  115. print_last_session_error(
  116. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA1)");
  117. return 1;
  118. }
  119. calculate_digest(sha1_hash, SHA1_HASH_SIZE, buf, BUFSIZ);
  120. if(strcmp(buf, EXPECTED_RSA_SHA1_HASH_DIGEST) != 0) {
  121. fprintf(stderr,
  122. "SHA1 hash not as expected - digest %s != %s\n",
  123. buf, EXPECTED_RSA_SHA1_HASH_DIGEST);
  124. return 1;
  125. }
  126. sha256_hash = libssh2_hostkey_hash(session,
  127. LIBSSH2_HOSTKEY_HASH_SHA256);
  128. if(sha256_hash == NULL) {
  129. print_last_session_error(
  130. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA256)");
  131. return 1;
  132. }
  133. calculate_digest(sha256_hash, SHA256_HASH_SIZE, buf, BUFSIZ);
  134. if(strcmp(buf, EXPECTED_RSA_SHA256_HASH_DIGEST) != 0) {
  135. fprintf(stderr,
  136. "SHA256 hash not as expected - digest %s != %s\n",
  137. buf, EXPECTED_RSA_SHA256_HASH_DIGEST);
  138. return 1;
  139. }
  140. }
  141. else {
  142. fprintf(stderr, "Unexpected type of hostkey: %i\n", type);
  143. return 1;
  144. }
  145. return 0;
  146. }