Commit c36565c0 by Brad Morgan

Added SSH public key authentication

parent 7621519f
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include "net.h" #include "net.h"
#include "types.h" #include "types.h"
#include <libssh2.h>
/** /**
* @file git2/transport.h * @file git2/transport.h
* @brief Git transport interfaces and functions * @brief Git transport interfaces and functions
...@@ -28,6 +30,7 @@ typedef enum { ...@@ -28,6 +30,7 @@ typedef enum {
/* git_cred_userpass_plaintext */ /* git_cred_userpass_plaintext */
GIT_CREDTYPE_USERPASS_PLAINTEXT = 1, GIT_CREDTYPE_USERPASS_PLAINTEXT = 1,
GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE = 2, GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE = 2,
GIT_CREDTYPE_SSH_PUBLICKEY = 3,
} git_credtype_t; } git_credtype_t;
/* The base structure for all credential types */ /* The base structure for all credential types */
...@@ -44,7 +47,7 @@ typedef struct git_cred_userpass_plaintext { ...@@ -44,7 +47,7 @@ typedef struct git_cred_userpass_plaintext {
char *password; char *password;
} git_cred_userpass_plaintext; } git_cred_userpass_plaintext;
/* A plaintext username and password */ /* A ssh key file and passphrase */
typedef struct git_cred_ssh_keyfile_passphrase { typedef struct git_cred_ssh_keyfile_passphrase {
git_cred parent; git_cred parent;
char *publickey; char *publickey;
...@@ -52,6 +55,15 @@ typedef struct git_cred_ssh_keyfile_passphrase { ...@@ -52,6 +55,15 @@ typedef struct git_cred_ssh_keyfile_passphrase {
char *passphrase; char *passphrase;
} git_cred_ssh_keyfile_passphrase; } git_cred_ssh_keyfile_passphrase;
/* A ssh public key and authentication callback */
typedef struct git_cred_ssh_publickey {
git_cred parent;
char *publickey;
size_t publickey_len;
void *sign_callback;
void *sign_data;
} git_cred_ssh_publickey;
/** /**
* Creates a new plain-text username and password credential object. * Creates a new plain-text username and password credential object.
* The supplied credential parameter will be internally duplicated. * The supplied credential parameter will be internally duplicated.
...@@ -83,6 +95,24 @@ GIT_EXTERN(int) git_cred_ssh_keyfile_passphrase_new( ...@@ -83,6 +95,24 @@ GIT_EXTERN(int) git_cred_ssh_keyfile_passphrase_new(
const char *passphrase); const char *passphrase);
/** /**
* Creates a new ssh public key credential object.
* The supplied credential parameter will be internally duplicated.
*
* @param out The newly created credential object.
* @param publickey The bytes of the public key.
* @param publickey_len The length of the public key in bytes.
* @param sign_callback The callback method for authenticating.
* @param sign_data The abstract data sent to the sign_callback method.
* @return 0 for success or an error code for failure
*/
GIT_EXTERN(int) git_cred_ssh_publickey_new(
git_cred **out,
const char *publickey,
size_t publickey_len,
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
void *sign_data);
/**
* Signature of a function which acquires a credential object. * Signature of a function which acquires a credential object.
* *
* @param cred The newly created credential object. * @param cred The newly created credential object.
......
...@@ -135,3 +135,52 @@ int git_cred_ssh_keyfile_passphrase_new( ...@@ -135,3 +135,52 @@ int git_cred_ssh_keyfile_passphrase_new(
*cred = &c->parent; *cred = &c->parent;
return 0; return 0;
} }
static void ssh_publickey_free(struct git_cred *cred)
{
git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred;
git__free(c->publickey);
c->sign_callback = NULL;
c->sign_data = NULL;
memset(c, 0, sizeof(*c));
git__free(c);
}
int git_cred_ssh_publickey_new(
git_cred **cred,
const char *publickey,
size_t publickey_len,
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
void *sign_data)
{
git_cred_ssh_publickey *c;
if (!cred)
return -1;
c = git__malloc(sizeof(git_cred_ssh_publickey));
GITERR_CHECK_ALLOC(c);
c->parent.credtype = GIT_CREDTYPE_SSH_PUBLICKEY;
c->parent.free = ssh_publickey_free;
c->publickey = git__malloc(publickey_len);
memcpy(c->publickey, publickey, publickey_len);
if (!c->publickey) {
git__free(c);
return -1;
}
c->publickey_len = publickey_len;
c->sign_callback = sign_callback;
c->sign_data = sign_data;
*cred = &c->parent;
return 0;
}
...@@ -253,6 +253,18 @@ static int _git_ssh_authenticate_session( ...@@ -253,6 +253,18 @@ static int _git_ssh_authenticate_session(
); );
break; break;
} }
case GIT_CREDTYPE_SSH_PUBLICKEY: {
git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred;
rc = libssh2_userauth_publickey(
session,
user,
(const unsigned char *)c->publickey,
c->publickey_len,
c->sign_callback,
&c->sign_data
);
break;
}
default: default:
rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED; rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment