summaryrefslogtreecommitdiff
path: root/authfile.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2020-04-08 00:09:24 +0000
committerDamien Miller <djm@mindrot.org>2020-04-08 10:14:21 +1000
commitd01f39304eaab0352793b490a25e1ab5f59a5366 (patch)
tree48f5406f0ffd2d0c1434d87ea38a9730ddf37cf3 /authfile.c
parentf290ab0833e44355fc006e4e67b92446c14673ef (diff)
downloadopenssh-git-d01f39304eaab0352793b490a25e1ab5f59a5366.tar.gz
upstream: simplify sshkey_try_load_public()
ok markus@ OpenBSD-Commit-ID: 05a5d46562aafcd70736c792208b1856064f40ad
Diffstat (limited to 'authfile.c')
-rw-r--r--authfile.c58
1 files changed, 19 insertions, 39 deletions
diff --git a/authfile.c b/authfile.c
index 20b66d9b..953812f4 100644
--- a/authfile.c
+++ b/authfile.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: authfile.c,v 1.137 2020/01/25 23:02:13 djm Exp $ */
+/* $OpenBSD: authfile.c,v 1.138 2020/04/08 00:09:24 djm Exp $ */
/*
* Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
*
@@ -195,17 +195,24 @@ sshkey_load_private(const char *filename, const char *passphrase,
}
static int
-sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
+sshkey_try_load_public(struct sshkey **kp, const char *filename,
+ char **commentp)
{
FILE *f;
char *line = NULL, *cp;
size_t linesize = 0;
int r;
+ struct sshkey *k = NULL;
+ *kp = NULL;
if (commentp != NULL)
*commentp = NULL;
if ((f = fopen(filename, "r")) == NULL)
return SSH_ERR_SYSTEM_ERROR;
+ if ((k = sshkey_new(KEY_UNSPEC)) == NULL) {
+ fclose(f);
+ return SSH_ERR_ALLOC_FAIL;
+ }
while (getline(&line, &linesize, f) != -1) {
cp = line;
switch (*cp) {
@@ -230,12 +237,15 @@ sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
if (*commentp == NULL)
r = SSH_ERR_ALLOC_FAIL;
}
+ /* success */
+ *kp = k;
free(line);
fclose(f);
return r;
}
}
}
+ free(k);
free(line);
fclose(f);
return SSH_ERR_INVALID_FORMAT;
@@ -245,8 +255,7 @@ sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
int
sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
{
- struct sshkey *pub = NULL;
- char *file = NULL;
+ char *pubfile = NULL;
int r;
if (keyp != NULL)
@@ -254,35 +263,17 @@ sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
if (commentp != NULL)
*commentp = NULL;
- if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
- if (keyp != NULL) {
- *keyp = pub;
- pub = NULL;
- }
- r = 0;
+ if ((r = sshkey_try_load_public(keyp, filename, commentp)) == 0)
goto out;
- }
- sshkey_free(pub);
/* try .pub suffix */
- if (asprintf(&file, "%s.pub", filename) == -1)
+ if (asprintf(&pubfile, "%s.pub", filename) == -1)
return SSH_ERR_ALLOC_FAIL;
- if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
+ if ((r = sshkey_try_load_public(keyp, pubfile, commentp)) == 0)
goto out;
- }
- if ((r = sshkey_try_load_public(pub, file, commentp)) == 0) {
- if (keyp != NULL) {
- *keyp = pub;
- pub = NULL;
- }
- r = 0;
- }
+
out:
- free(file);
- sshkey_free(pub);
+ free(pubfile);
return r;
}
@@ -300,18 +291,7 @@ sshkey_load_cert(const char *filename, struct sshkey **keyp)
if (asprintf(&file, "%s-cert.pub", filename) == -1)
return SSH_ERR_ALLOC_FAIL;
- if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
- goto out;
- }
- if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
- goto out;
- /* success */
- if (keyp != NULL) {
- *keyp = pub;
- pub = NULL;
- }
- r = 0;
- out:
+ r = sshkey_try_load_public(keyp, file, NULL);
free(file);
sshkey_free(pub);
return r;