Merge pull request #3157 from mgorny/ssh_memory_auth · nodegit/libgit2@2c8550f · GitHub
Skip to content

Commit 2c8550f

Browse files
committed
Merge pull request libgit2#3157 from mgorny/ssh_memory_auth
Support getting SSH keys from memory, pt. 2
2 parents 64be170 + 8085adf commit 2c8550f

5 files changed

Lines changed: 168 additions & 1 deletion

File tree

‎CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions

‎include/git2/transport.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ typedef enum {
108108
* it will ask via this credential type.
109109
*/
110110
GIT_CREDTYPE_USERNAME = (1u << 5),
111+
112+
/**
113+
* Credentials read from memory.
114+
*
115+
* Only available for libssh2+OpenSSL for now.
116+
*/
117+
GIT_CREDTYPE_SSH_MEMORY = (1u << 6),
111118
} git_credtype_t;
112119

113120
/* The base structure for all credential types */
@@ -290,6 +297,23 @@ GIT_EXTERN(int) git_cred_default_new(git_cred **out);
290297
*/
291298
GIT_EXTERN(int) git_cred_username_new(git_cred **cred, const char *username);
292299

300+
/**
301+
* Create a new ssh key credential object reading the keys from memory.
302+
*
303+
* @param out The newly created credential object.
304+
* @param username username to use to authenticate.
305+
* @param publickey The public key of the credential.
306+
* @param privatekey The private key of the credential.
307+
* @param passphrase The passphrase of the credential.
308+
* @return 0 for success or an error code for failure
309+
*/
310+
GIT_EXTERN(int) git_cred_ssh_key_memory_new(
311+
git_cred **out,
312+
const char *username,
313+
const char *publickey,
314+
const char *privatekey,
315+
const char *passphrase);
316+
293317
/**
294318
* Signature of a function which acquires a credential object.
295319
*

‎src/transports/cred.c‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
#include "smart.h"
1010
#include "git2/cred_helpers.h"
1111

12+
static int git_cred_ssh_key_type_new(
13+
git_cred **cred,
14+
const char *username,
15+
const char *publickey,
16+
const char *privatekey,
17+
const char *passphrase,
18+
git_credtype_t credtype);
19+
1220
int git_cred_has_username(git_cred *cred)
1321
{
1422
if (cred->credtype == GIT_CREDTYPE_DEFAULT)
@@ -31,6 +39,7 @@ const char *git_cred__username(git_cred *cred)
3139
return c->username;
3240
}
3341
case GIT_CREDTYPE_SSH_KEY:
42+
case GIT_CREDTYPE_SSH_MEMORY:
3443
{
3544
git_cred_ssh_key *c = (git_cred_ssh_key *) cred;
3645
return c->username;
@@ -174,6 +183,45 @@ int git_cred_ssh_key_new(
174183
const char *publickey,
175184
const char *privatekey,
176185
const char *passphrase)
186+
{
187+
return git_cred_ssh_key_type_new(
188+
cred,
189+
username,
190+
publickey,
191+
privatekey,
192+
passphrase,
193+
GIT_CREDTYPE_SSH_KEY);
194+
}
195+
196+
int git_cred_ssh_key_memory_new(
197+
git_cred **cred,
198+
const char *username,
199+
const char *publickey,
200+
const char *privatekey,
201+
const char *passphrase)
202+
{
203+
#ifdef GIT_SSH_MEMORY_CREDENTIALS
204+
return git_cred_ssh_key_type_new(
205+
cred,
206+
username,
207+
publickey,
208+
privatekey,
209+
passphrase,
210+
GIT_CREDTYPE_SSH_MEMORY);
211+
#else
212+
giterr_set(GITERR_INVALID,
213+
"This version of libgit2 was not built with ssh memory credentials.");
214+
return -1;
215+
#endif
216+
}
217+
218+
static int git_cred_ssh_key_type_new(
219+
git_cred **cred,
220+
const char *username,
221+
const char *publickey,
222+
const char *privatekey,
223+
const char *passphrase,
224+
git_credtype_t credtype)
177225
{
178226
git_cred_ssh_key *c;
179227

@@ -182,7 +230,7 @@ int git_cred_ssh_key_new(
182230
c = git__calloc(1, sizeof(git_cred_ssh_key));
183231
GITERR_CHECK_ALLOC(c);
184232

185-
c->parent.credtype = GIT_CREDTYPE_SSH_KEY;
233+
c->parent.credtype = credtype;
186234
c->parent.free = ssh_key_free;
187235

188236
c->username = git__strdup(username);

‎src/transports/ssh.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,25 @@ static int _git_ssh_authenticate_session(
370370
session, c->username, c->prompt_callback);
371371
break;
372372
}
373+
#ifdef GIT_SSH_MEMORY_CREDENTIALS
374+
case GIT_CREDTYPE_SSH_MEMORY: {
375+
git_cred_ssh_key *c = (git_cred_ssh_key *)cred;
376+
377+
assert(c->username);
378+
assert(c->privatekey);
379+
380+
rc = libssh2_userauth_publickey_frommemory(
381+
session,
382+
c->username,
383+
strlen(c->username),
384+
c->publickey,
385+
c->publickey ? strlen(c->publickey) : 0,
386+
c->privatekey,
387+
strlen(c->privatekey),
388+
c->passphrase);
389+
break;
390+
}
391+
#endif
373392
default:
374393
rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
375394
}
@@ -740,6 +759,9 @@ static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *use
740759
if (!git__prefixcmp(ptr, SSH_AUTH_PUBLICKEY)) {
741760
*out |= GIT_CREDTYPE_SSH_KEY;
742761
*out |= GIT_CREDTYPE_SSH_CUSTOM;
762+
#ifdef GIT_SSH_MEMORY_CREDENTIALS
763+
*out |= GIT_CREDTYPE_SSH_MEMORY;
764+
#endif
743765
ptr += strlen(SSH_AUTH_PUBLICKEY);
744766
continue;
745767
}

‎tests/online/clone.c‎

Lines changed: 68 additions & 0 deletions

0 commit comments

Comments
 (0)