Skip to content

Commit ef6de8d

Browse files
authored
Merge pull request libgit2#5704 from lhchavez/ssh-raw-certificate
Also add the raw hostkey to `git_cert_hostkey`
2 parents 079a40c + 29fe5f6 commit ef6de8d

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

include/git2/cert.h

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,37 +80,65 @@ typedef enum {
8080
GIT_CERT_SSH_SHA1 = (1 << 1),
8181
/** SHA-256 is available */
8282
GIT_CERT_SSH_SHA256 = (1 << 2),
83+
/** Raw hostkey is available */
84+
GIT_CERT_SSH_RAW = (1 << 3),
8385
} git_cert_ssh_t;
8486

87+
typedef enum {
88+
/** The raw key is of an unknown type. */
89+
GIT_CERT_SSH_RAW_TYPE_UNKNOWN = 0,
90+
/** The raw key is an RSA key. */
91+
GIT_CERT_SSH_RAW_TYPE_RSA = 1,
92+
/** The raw key is a DSS key. */
93+
GIT_CERT_SSH_RAW_TYPE_DSS = 2,
94+
} git_cert_ssh_raw_type_t;
95+
8596
/**
8697
* Hostkey information taken from libssh2
8798
*/
8899
typedef struct {
89100
git_cert parent; /**< The parent cert */
90101

91102
/**
92-
* A hostkey type from libssh2, either
93-
* `GIT_CERT_SSH_MD5` or `GIT_CERT_SSH_SHA1`
103+
* A bitmask containing the available fields.
94104
*/
95105
git_cert_ssh_t type;
96106

97107
/**
98-
* Hostkey hash. If type has `GIT_CERT_SSH_MD5` set, this will
108+
* Hostkey hash. If `type` has `GIT_CERT_SSH_MD5` set, this will
99109
* have the MD5 hash of the hostkey.
100110
*/
101111
unsigned char hash_md5[16];
102112

103113
/**
104-
* Hostkey hash. If type has `GIT_CERT_SSH_SHA1` set, this will
114+
* Hostkey hash. If `type` has `GIT_CERT_SSH_SHA1` set, this will
105115
* have the SHA-1 hash of the hostkey.
106116
*/
107117
unsigned char hash_sha1[20];
108118

109119
/**
110-
* Hostkey hash. If type has `GIT_CERT_SSH_SHA256` set, this will
120+
* Hostkey hash. If `type` has `GIT_CERT_SSH_SHA256` set, this will
111121
* have the SHA-256 hash of the hostkey.
112122
*/
113123
unsigned char hash_sha256[32];
124+
125+
/**
126+
* Raw hostkey type. If `type` has `GIT_CERT_SSH_RAW` set, this will
127+
* have the type of the raw hostkey.
128+
*/
129+
git_cert_ssh_raw_type_t raw_type;
130+
131+
/**
132+
* Pointer to the raw hostkey. If `type` has `GIT_CERT_SSH_RAW` set,
133+
* this will have the raw contents of the hostkey.
134+
*/
135+
const char *hostkey;
136+
137+
/**
138+
* Raw hostkey length. If `type` has `GIT_CERT_SSH_RAW` set, this will
139+
* have the length of the raw contents of the hostkey.
140+
*/
141+
size_t hostkey_len;
114142
} git_cert_hostkey;
115143

116144
/**

src/transports/ssh.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,28 @@ static int _git_ssh_setup_conn(
563563
if (t->owner->certificate_check_cb != NULL) {
564564
git_cert_hostkey cert = {{ 0 }}, *cert_ptr;
565565
const char *key;
566+
size_t cert_len;
567+
int cert_type;
566568

567569
cert.parent.cert_type = GIT_CERT_HOSTKEY_LIBSSH2;
568570

571+
key = libssh2_session_hostkey(session, &cert_len, &cert_type);
572+
if (key != NULL) {
573+
cert.type |= GIT_CERT_SSH_RAW;
574+
cert.hostkey = key;
575+
cert.hostkey_len = cert_len;
576+
switch (cert_type) {
577+
case LIBSSH2_HOSTKEY_TYPE_RSA:
578+
cert.raw_type = GIT_CERT_SSH_RAW_TYPE_RSA;
579+
break;
580+
case LIBSSH2_HOSTKEY_TYPE_DSS:
581+
cert.raw_type = GIT_CERT_SSH_RAW_TYPE_DSS;
582+
break;
583+
default:
584+
cert.raw_type = GIT_CERT_SSH_RAW_TYPE_UNKNOWN;
585+
}
586+
}
587+
569588
#ifdef LIBSSH2_HOSTKEY_HASH_SHA256
570589
key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA256);
571590
if (key != NULL) {

0 commit comments

Comments
 (0)