version 1.108, 2014/12/04 02:24:32 |
version 1.109, 2015/01/08 10:14:08 |
|
|
|
|
/* Load a key from a fd into a buffer */ |
/* Load a key from a fd into a buffer */ |
int |
int |
sshkey_load_file(int fd, const char *filename, struct sshbuf *blob) |
sshkey_load_file(int fd, struct sshbuf *blob) |
{ |
{ |
u_char buf[1024]; |
u_char buf[1024]; |
size_t len; |
size_t len; |
|
|
* otherwise. |
* otherwise. |
*/ |
*/ |
static int |
static int |
sshkey_load_public_rsa1(int fd, const char *filename, |
sshkey_load_public_rsa1(int fd, struct sshkey **keyp, char **commentp) |
struct sshkey **keyp, char **commentp) |
|
{ |
{ |
struct sshbuf *b = NULL; |
struct sshbuf *b = NULL; |
int r; |
int r; |
|
|
|
|
if ((b = sshbuf_new()) == NULL) |
if ((b = sshbuf_new()) == NULL) |
return SSH_ERR_ALLOC_FAIL; |
return SSH_ERR_ALLOC_FAIL; |
if ((r = sshkey_load_file(fd, filename, b)) != 0) |
if ((r = sshkey_load_file(fd, b)) != 0) |
goto out; |
goto out; |
if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0) |
if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0) |
goto out; |
goto out; |
|
|
} |
} |
#endif /* WITH_SSH1 */ |
#endif /* WITH_SSH1 */ |
|
|
#ifdef WITH_OPENSSL |
|
/* XXX Deprecate? */ |
|
int |
|
sshkey_load_private_pem(int fd, int type, const char *passphrase, |
|
struct sshkey **keyp, char **commentp) |
|
{ |
|
struct sshbuf *buffer = NULL; |
|
int r; |
|
|
|
*keyp = NULL; |
|
if (commentp != NULL) |
|
*commentp = NULL; |
|
|
|
if ((buffer = sshbuf_new()) == NULL) |
|
return SSH_ERR_ALLOC_FAIL; |
|
if ((r = sshkey_load_file(fd, NULL, buffer)) != 0) |
|
goto out; |
|
if ((r = sshkey_parse_private_pem_fileblob(buffer, type, passphrase, |
|
keyp, commentp)) != 0) |
|
goto out; |
|
r = 0; |
|
out: |
|
sshbuf_free(buffer); |
|
return r; |
|
} |
|
#endif /* WITH_OPENSSL */ |
|
|
|
/* XXX remove error() calls from here? */ |
/* XXX remove error() calls from here? */ |
int |
int |
sshkey_perm_ok(int fd, const char *filename) |
sshkey_perm_ok(int fd, const char *filename) |
|
|
struct sshkey **keyp, char **commentp, int *perm_ok) |
struct sshkey **keyp, char **commentp, int *perm_ok) |
{ |
{ |
int fd, r; |
int fd, r; |
struct sshbuf *buffer = NULL; |
|
|
|
*keyp = NULL; |
*keyp = NULL; |
if (commentp != NULL) |
if (commentp != NULL) |
|
|
if (perm_ok != NULL) |
if (perm_ok != NULL) |
*perm_ok = 1; |
*perm_ok = 1; |
|
|
|
r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp); |
|
out: |
|
close(fd); |
|
return r; |
|
} |
|
|
|
int |
|
sshkey_load_private_type_fd(int fd, int type, const char *passphrase, |
|
struct sshkey **keyp, char **commentp) |
|
{ |
|
struct sshbuf *buffer = NULL; |
|
int r; |
|
|
if ((buffer = sshbuf_new()) == NULL) { |
if ((buffer = sshbuf_new()) == NULL) { |
r = SSH_ERR_ALLOC_FAIL; |
r = SSH_ERR_ALLOC_FAIL; |
goto out; |
goto out; |
} |
} |
if ((r = sshkey_load_file(fd, filename, buffer)) != 0) |
if ((r = sshkey_load_file(fd, buffer)) != 0 || |
|
(r = sshkey_parse_private_fileblob_type(buffer, type, |
|
passphrase, keyp, commentp)) != 0) |
goto out; |
goto out; |
if ((r = sshkey_parse_private_fileblob_type(buffer, type, passphrase, |
|
keyp, commentp)) != 0) |
/* success */ |
goto out; |
|
r = 0; |
r = 0; |
out: |
out: |
close(fd); |
|
if (buffer != NULL) |
if (buffer != NULL) |
sshbuf_free(buffer); |
sshbuf_free(buffer); |
return r; |
return r; |
|
|
r = SSH_ERR_ALLOC_FAIL; |
r = SSH_ERR_ALLOC_FAIL; |
goto out; |
goto out; |
} |
} |
if ((r = sshkey_load_file(fd, filename, buffer)) != 0 || |
if ((r = sshkey_load_file(fd, buffer)) != 0 || |
(r = sshkey_parse_private_fileblob(buffer, passphrase, filename, |
(r = sshkey_parse_private_fileblob(buffer, passphrase, filename, |
keyp, commentp)) != 0) |
keyp, commentp)) != 0) |
goto out; |
goto out; |
|
|
goto skip; |
goto skip; |
#ifdef WITH_SSH1 |
#ifdef WITH_SSH1 |
/* try rsa1 private key */ |
/* try rsa1 private key */ |
r = sshkey_load_public_rsa1(fd, filename, keyp, commentp); |
r = sshkey_load_public_rsa1(fd, keyp, commentp); |
close(fd); |
close(fd); |
switch (r) { |
switch (r) { |
case SSH_ERR_INTERNAL_ERROR: |
case SSH_ERR_INTERNAL_ERROR: |