mirror of
https://github.com/libssh2/libssh2.git
synced 2026-10-28 17:01:54 +03:00
Host Key RSA 256/512 support #536
This commit is contained in:
161
src/hostkey.c
161
src/hostkey.c
@@ -227,6 +227,139 @@ hostkey_method_ssh_rsa_signv(LIBSSH2_SESSION * session,
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* hostkey_method_ssh_rsa_sha2_256_sig_verify
|
||||
*
|
||||
* Verify signature created by remote
|
||||
*/
|
||||
|
||||
static int
|
||||
hostkey_method_ssh_rsa_sha2_256_sig_verify(LIBSSH2_SESSION * session,
|
||||
const unsigned char *sig,
|
||||
size_t sig_len,
|
||||
const unsigned char *m,
|
||||
size_t m_len, void **abstract)
|
||||
{
|
||||
libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *) (*abstract);
|
||||
(void) session;
|
||||
|
||||
/* Skip past keyname_len(4) + keyname(12){"rsa-sha2-256"} + signature_len(4) */
|
||||
if(sig_len < 20)
|
||||
return -1;
|
||||
|
||||
sig += 20;
|
||||
sig_len -= 20;
|
||||
return _libssh2_rsa_sha2_verify(rsactx, SHA256_DIGEST_LENGTH, sig, sig_len, m, m_len);
|
||||
}
|
||||
|
||||
/*
|
||||
* hostkey_method_ssh_rsa_sha2_256_signv
|
||||
*
|
||||
* Construct a signature from an array of vectors
|
||||
*/
|
||||
|
||||
static int
|
||||
hostkey_method_ssh_rsa_sha2_256_signv(LIBSSH2_SESSION * session,
|
||||
unsigned char **signature,
|
||||
size_t *signature_len,
|
||||
int veccount,
|
||||
const struct iovec datavec[],
|
||||
void **abstract)
|
||||
{
|
||||
libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *) (*abstract);
|
||||
|
||||
#ifdef _libssh2_rsa_sha2_256_signv
|
||||
return _libssh2_rsa_sha2_256_signv(session, signature, signature_len,
|
||||
veccount, datavec, rsactx);
|
||||
#else
|
||||
int ret;
|
||||
int i;
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
libssh2_sha256_ctx ctx;
|
||||
|
||||
libssh2_sha256_init(&ctx);
|
||||
for(i = 0; i < veccount; i++) {
|
||||
libssh2_sha256_update(ctx, datavec[i].iov_base, datavec[i].iov_len);
|
||||
}
|
||||
libssh2_sha256_final(ctx, hash);
|
||||
|
||||
ret = _libssh2_rsa_sha2_sign(session, rsactx, hash, SHA256_DIGEST_LENGTH,
|
||||
signature, signature_len);
|
||||
if(ret) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* hostkey_method_ssh_rsa_sha2_512_sig_verify
|
||||
*
|
||||
* Verify signature created by remote
|
||||
*/
|
||||
|
||||
static int
|
||||
hostkey_method_ssh_rsa_sha2_512_sig_verify(LIBSSH2_SESSION * session,
|
||||
const unsigned char *sig,
|
||||
size_t sig_len,
|
||||
const unsigned char *m,
|
||||
size_t m_len, void **abstract)
|
||||
{
|
||||
libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *) (*abstract);
|
||||
(void) session;
|
||||
|
||||
/* Skip past keyname_len(4) + keyname(12){"rsa-sha2-512"} + signature_len(4) */
|
||||
if(sig_len < 20)
|
||||
return -1;
|
||||
|
||||
sig += 20;
|
||||
sig_len -= 20;
|
||||
return _libssh2_rsa_sha2_verify(rsactx, SHA512_DIGEST_LENGTH, sig, sig_len, m, m_len);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* hostkey_method_ssh_rsa_sha2_512_signv
|
||||
*
|
||||
* Construct a signature from an array of vectors
|
||||
*/
|
||||
static int
|
||||
hostkey_method_ssh_rsa_sha2_512_signv(LIBSSH2_SESSION * session,
|
||||
unsigned char **signature,
|
||||
size_t *signature_len,
|
||||
int veccount,
|
||||
const struct iovec datavec[],
|
||||
void **abstract)
|
||||
{
|
||||
libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *) (*abstract);
|
||||
|
||||
#ifdef _libssh2_rsa_sha2_512_signv
|
||||
return _libssh2_rsa_sha2_512_signv(session, signature, signature_len,
|
||||
veccount, datavec, rsactx);
|
||||
#else
|
||||
int ret;
|
||||
int i;
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH];
|
||||
libssh2_sha512_ctx ctx;
|
||||
|
||||
libssh2_sha512_init(&ctx);
|
||||
for(i = 0; i < veccount; i++) {
|
||||
libssh2_sha512_update(ctx, datavec[i].iov_base, datavec[i].iov_len);
|
||||
}
|
||||
libssh2_sha512_final(ctx, hash);
|
||||
|
||||
ret = _libssh2_rsa_sha2_sign(session, rsactx, hash, SHA512_DIGEST_LENGTH,
|
||||
signature, signature_len);
|
||||
if(ret) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* hostkey_method_ssh_rsa_dtor
|
||||
*
|
||||
@@ -260,6 +393,32 @@ static const LIBSSH2_HOSTKEY_METHOD hostkey_method_ssh_rsa = {
|
||||
NULL, /* encrypt */
|
||||
hostkey_method_ssh_rsa_dtor,
|
||||
};
|
||||
|
||||
|
||||
static const LIBSSH2_HOSTKEY_METHOD hostkey_method_ssh_rsa_sha2_256 = {
|
||||
"rsa-sha2-256",
|
||||
SHA256_DIGEST_LENGTH,
|
||||
hostkey_method_ssh_rsa_init,
|
||||
hostkey_method_ssh_rsa_initPEM,
|
||||
hostkey_method_ssh_rsa_initPEMFromMemory,
|
||||
hostkey_method_ssh_rsa_sha2_256_sig_verify,
|
||||
hostkey_method_ssh_rsa_sha2_256_signv,
|
||||
NULL, /* encrypt */
|
||||
hostkey_method_ssh_rsa_dtor,
|
||||
};
|
||||
|
||||
static const LIBSSH2_HOSTKEY_METHOD hostkey_method_ssh_rsa_sha2_512 = {
|
||||
"rsa-sha2-512",
|
||||
SHA512_DIGEST_LENGTH,
|
||||
hostkey_method_ssh_rsa_init,
|
||||
hostkey_method_ssh_rsa_initPEM,
|
||||
hostkey_method_ssh_rsa_initPEMFromMemory,
|
||||
hostkey_method_ssh_rsa_sha2_512_sig_verify,
|
||||
hostkey_method_ssh_rsa_sha2_512_signv,
|
||||
NULL, /* encrypt */
|
||||
hostkey_method_ssh_rsa_dtor,
|
||||
};
|
||||
|
||||
#endif /* LIBSSH2_RSA */
|
||||
|
||||
#if LIBSSH2_DSA
|
||||
@@ -1043,6 +1202,8 @@ static const LIBSSH2_HOSTKEY_METHOD *hostkey_methods[] = {
|
||||
&hostkey_method_ssh_ed25519,
|
||||
#endif
|
||||
#if LIBSSH2_RSA
|
||||
&hostkey_method_ssh_rsa_sha2_512,
|
||||
&hostkey_method_ssh_rsa_sha2_256,
|
||||
&hostkey_method_ssh_rsa,
|
||||
#endif /* LIBSSH2_RSA */
|
||||
#if LIBSSH2_DSA
|
||||
|
||||
Reference in New Issue
Block a user