geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

179 lines
5.9 KiB

  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, "ECDSA MD5 hash not as expected, digest "
  67. "%s != %s\n", buf, EXPECTED_ECDSA_MD5_HASH_DIGEST);
  68. return 1;
  69. }
  70. sha1_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  71. if(sha1_hash == NULL) {
  72. print_last_session_error(
  73. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA1)");
  74. return 1;
  75. }
  76. calculate_digest(sha1_hash, SHA1_HASH_SIZE, buf, BUFSIZ);
  77. if(strcmp(buf, EXPECTED_ECDSA_SHA1_HASH_DIGEST) != 0) {
  78. fprintf(stderr, "ECDSA SHA1 hash not as expected, digest "
  79. "%s != %s\n", buf, EXPECTED_ECDSA_SHA1_HASH_DIGEST);
  80. return 1;
  81. }
  82. sha256_hash = libssh2_hostkey_hash(session,
  83. LIBSSH2_HOSTKEY_HASH_SHA256);
  84. if(sha256_hash == NULL) {
  85. print_last_session_error(
  86. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA256)");
  87. return 1;
  88. }
  89. calculate_digest(sha256_hash, SHA256_HASH_SIZE, buf, BUFSIZ);
  90. if(strcmp(buf, EXPECTED_ECDSA_SHA256_HASH_DIGEST) != 0) {
  91. fprintf(stderr, "ECDSA SHA256 hash not as expected, digest "
  92. "%s != %s\n", buf, EXPECTED_ECDSA_SHA256_HASH_DIGEST);
  93. return 1;
  94. }
  95. }
  96. else if(type == LIBSSH2_HOSTKEY_TYPE_RSA) {
  97. md5_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
  98. if(md5_hash == NULL) {
  99. print_last_session_error(
  100. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_MD5)");
  101. return 1;
  102. }
  103. calculate_digest(md5_hash, MD5_HASH_SIZE, buf, BUFSIZ);
  104. if(strcmp(buf, EXPECTED_RSA_MD5_HASH_DIGEST) != 0) {
  105. fprintf(stderr, "MD5 hash not as expected, digest "
  106. "%s != %s\n", buf, EXPECTED_RSA_MD5_HASH_DIGEST);
  107. return 1;
  108. }
  109. sha1_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  110. if(sha1_hash == NULL) {
  111. print_last_session_error(
  112. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA1)");
  113. return 1;
  114. }
  115. calculate_digest(sha1_hash, SHA1_HASH_SIZE, buf, BUFSIZ);
  116. if(strcmp(buf, EXPECTED_RSA_SHA1_HASH_DIGEST) != 0) {
  117. fprintf(stderr, "SHA1 hash not as expected, digest "
  118. "%s != %s\n", buf, EXPECTED_RSA_SHA1_HASH_DIGEST);
  119. return 1;
  120. }
  121. sha256_hash = libssh2_hostkey_hash(session,
  122. LIBSSH2_HOSTKEY_HASH_SHA256);
  123. if(sha256_hash == NULL) {
  124. print_last_session_error(
  125. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA256)");
  126. return 1;
  127. }
  128. calculate_digest(sha256_hash, SHA256_HASH_SIZE, buf, BUFSIZ);
  129. if(strcmp(buf, EXPECTED_RSA_SHA256_HASH_DIGEST) != 0) {
  130. fprintf(stderr, "SHA256 hash not as expected, digest "
  131. "%s != %s\n", buf, EXPECTED_RSA_SHA256_HASH_DIGEST);
  132. return 1;
  133. }
  134. }
  135. else {
  136. fprintf(stderr, "Unexpected type of hostkey: %i\n", type);
  137. return 1;
  138. }
  139. return 0;
  140. }