1 #include "session_fixture.h"
2 
3 #include <libssh2.h>
4 
5 #include <stdio.h>
6 
7 static const char *EXPECTED_RSA_HOSTKEY =
8     "AAAAB3NzaC1yc2EAAAABIwAAAQEArrr/JuJmaZligyfS8vcNur+mWR2ddDQtVdhHzdKU"
9     "UoR6/Om6cvxpe61H1YZO1xCpLUBXmkki4HoNtYOpPB2W4V+8U4BDeVBD5crypEOE1+7B"
10     "Am99fnEDxYIOZq2/jTP0yQmzCpWYS3COyFmkOL7sfX1wQMeW5zQT2WKcxC6FSWbhDqrB"
11     "eNEGi687hJJoJ7YXgY/IdiYW5NcOuqRSWljjGS3dAJsHHWk4nJbhjEDXbPaeduMAwQU9"
12     "i6ELfP3r+q6wdu0P4jWaoo3De1aYxnToV/ldXykpipON4NPamsb6Ph2qlJQKypq7J4iQ"
13     "gkIIbCU1A31+4ExvcIVoxLQw/aTSbw==";
14 
15 static const char *EXPECTED_ECDSA_HOSTKEY =
16     "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC+/syyeKJD9dC2ZH"
17     "9Q7iJGReR4YM3rUCMsSynkyXojdfSClGCMY7JvWlt30ESjYvxoTfSRGx6WvaqYK/vPoYQ4=";
18 
test(LIBSSH2_SESSION * session)19 int test(LIBSSH2_SESSION *session)
20 {
21     int rc;
22     size_t len;
23     int type;
24     unsigned int expected_len = 0;
25     char *expected_hostkey = NULL;
26 
27     const char *hostkey = libssh2_session_hostkey(session, &len, &type);
28     if(hostkey == NULL) {
29         print_last_session_error("libssh2_session_hostkey");
30         return 1;
31     }
32 
33     if(type == LIBSSH2_HOSTKEY_TYPE_ECDSA_256) {
34         rc = libssh2_base64_decode(session, &expected_hostkey, &expected_len,
35                                    EXPECTED_ECDSA_HOSTKEY, strlen(EXPECTED_ECDSA_HOSTKEY));
36     }
37     else if(type == LIBSSH2_HOSTKEY_TYPE_RSA) {
38         rc = libssh2_base64_decode(session, &expected_hostkey, &expected_len,
39                                    EXPECTED_RSA_HOSTKEY, strlen(EXPECTED_RSA_HOSTKEY));
40     }
41     else {
42         fprintf(stderr, "Unexpected type of hostkey: %i\n", type);
43         return 1;
44     }
45 
46     if(rc != 0) {
47         print_last_session_error("libssh2_base64_decode");
48         return 1;
49     }
50 
51     if(len != expected_len) {
52         fprintf(stderr, "Hostkey does not have the expected length %ld != %d\n",
53                 (unsigned long)len, expected_len);
54         return 1;
55     }
56 
57     if(memcmp(hostkey, expected_hostkey, len) != 0) {
58         fprintf(stderr, "Hostkeys do not match\n");
59         return 1;
60     }
61 
62     return 0;
63 }
64