summaryrefslogtreecommitdiff
path: root/sshkey.c
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2018-08-24 12:49:36 +0100
committerColin Watson <cjwatson@debian.org>2018-08-30 00:57:27 +0100
commit816386e17654ca36834bebbf351419e460fad8f6 (patch)
tree3dc79d831cb73bc25b92f5a4d18f8e328c0c570a /sshkey.c
parent3e6f76c7039d3df22b1d0a3a5f30150efb09b69d (diff)
parent16a47fc4b04977a14f44dd433c8da1499fa80671 (diff)
New upstream release (7.8p1)
Closes: #907534
Diffstat (limited to 'sshkey.c')
-rw-r--r--sshkey.c106
1 files changed, 84 insertions, 22 deletions
diff --git a/sshkey.c b/sshkey.c
index 088872860..91e99a262 100644
--- a/sshkey.c
+++ b/sshkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshkey.c,v 1.64 2018/03/22 07:05:48 markus Exp $ */ 1/* $OpenBSD: sshkey.c,v 1.66 2018/07/03 13:20:25 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Alexander von Gernler. All rights reserved. 4 * Copyright (c) 2008 Alexander von Gernler. All rights reserved.
@@ -83,47 +83,65 @@ static int sshkey_from_blob_internal(struct sshbuf *buf,
83struct keytype { 83struct keytype {
84 const char *name; 84 const char *name;
85 const char *shortname; 85 const char *shortname;
86 const char *sigalg;
86 int type; 87 int type;
87 int nid; 88 int nid;
88 int cert; 89 int cert;
89 int sigonly; 90 int sigonly;
90}; 91};
91static const struct keytype keytypes[] = { 92static const struct keytype keytypes[] = {
92 { "ssh-ed25519", "ED25519", KEY_ED25519, 0, 0, 0 }, 93 { "ssh-ed25519", "ED25519", NULL, KEY_ED25519, 0, 0, 0 },
93 { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT", 94 { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT", NULL,
94 KEY_ED25519_CERT, 0, 1, 0 }, 95 KEY_ED25519_CERT, 0, 1, 0 },
95#ifdef WITH_XMSS 96#ifdef WITH_XMSS
96 { "ssh-xmss@openssh.com", "XMSS", KEY_XMSS, 0, 0, 0 }, 97 { "ssh-xmss@openssh.com", "XMSS", NULL, KEY_XMSS, 0, 0, 0 },
97 { "ssh-xmss-cert-v01@openssh.com", "XMSS-CERT", 98 { "ssh-xmss-cert-v01@openssh.com", "XMSS-CERT", NULL,
98 KEY_XMSS_CERT, 0, 1, 0 }, 99 KEY_XMSS_CERT, 0, 1, 0 },
99#endif /* WITH_XMSS */ 100#endif /* WITH_XMSS */
100#ifdef WITH_OPENSSL 101#ifdef WITH_OPENSSL
101 { "ssh-rsa", "RSA", KEY_RSA, 0, 0, 0 }, 102 { "ssh-rsa", "RSA", NULL, KEY_RSA, 0, 0, 0 },
102 { "rsa-sha2-256", "RSA", KEY_RSA, 0, 0, 1 }, 103 { "rsa-sha2-256", "RSA", NULL, KEY_RSA, 0, 0, 1 },
103 { "rsa-sha2-512", "RSA", KEY_RSA, 0, 0, 1 }, 104 { "rsa-sha2-512", "RSA", NULL, KEY_RSA, 0, 0, 1 },
104 { "ssh-dss", "DSA", KEY_DSA, 0, 0, 0 }, 105 { "ssh-dss", "DSA", NULL, KEY_DSA, 0, 0, 0 },
105# ifdef OPENSSL_HAS_ECC 106# ifdef OPENSSL_HAS_ECC
106 { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA, NID_X9_62_prime256v1, 0, 0 }, 107 { "ecdsa-sha2-nistp256", "ECDSA", NULL,
107 { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA, NID_secp384r1, 0, 0 }, 108 KEY_ECDSA, NID_X9_62_prime256v1, 0, 0 },
109 { "ecdsa-sha2-nistp384", "ECDSA", NULL,
110 KEY_ECDSA, NID_secp384r1, 0, 0 },
108# ifdef OPENSSL_HAS_NISTP521 111# ifdef OPENSSL_HAS_NISTP521
109 { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0, 0 }, 112 { "ecdsa-sha2-nistp521", "ECDSA", NULL,
113 KEY_ECDSA, NID_secp521r1, 0, 0 },
110# endif /* OPENSSL_HAS_NISTP521 */ 114# endif /* OPENSSL_HAS_NISTP521 */
111# endif /* OPENSSL_HAS_ECC */ 115# endif /* OPENSSL_HAS_ECC */
112 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT, 0, 1, 0 }, 116 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", NULL,
113 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT, 0, 1, 0 }, 117 KEY_RSA_CERT, 0, 1, 0 },
118 { "rsa-sha2-256-cert-v01@openssh.com", "RSA-CERT",
119 "ssh-rsa-sha2-256", KEY_RSA_CERT, 0, 1, 1 },
120 { "rsa-sha2-512-cert-v01@openssh.com", "RSA-CERT",
121 "ssh-rsa-sha2-512", KEY_RSA_CERT, 0, 1, 1 },
122 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", NULL,
123 KEY_DSA_CERT, 0, 1, 0 },
124 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", NULL,
125 KEY_RSA_CERT, 0, 1, 0 },
126 { "rsa-sha2-256-cert-v01@openssh.com", "RSA-CERT",
127 "ssh-rsa-sha2-256", KEY_RSA_CERT, 0, 1, 1 },
128 { "rsa-sha2-512-cert-v01@openssh.com", "RSA-CERT",
129 "ssh-rsa-sha2-512", KEY_RSA_CERT, 0, 1, 1 },
130 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", NULL,
131 KEY_DSA_CERT, 0, 1, 0 },
114# ifdef OPENSSL_HAS_ECC 132# ifdef OPENSSL_HAS_ECC
115 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT", 133 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT", NULL,
116 KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1, 0 }, 134 KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1, 0 },
117 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT", 135 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT", NULL,
118 KEY_ECDSA_CERT, NID_secp384r1, 1, 0 }, 136 KEY_ECDSA_CERT, NID_secp384r1, 1, 0 },
119# ifdef OPENSSL_HAS_NISTP521 137# ifdef OPENSSL_HAS_NISTP521
120 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT", 138 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT", NULL,
121 KEY_ECDSA_CERT, NID_secp521r1, 1, 0 }, 139 KEY_ECDSA_CERT, NID_secp521r1, 1, 0 },
122# endif /* OPENSSL_HAS_NISTP521 */ 140# endif /* OPENSSL_HAS_NISTP521 */
123# endif /* OPENSSL_HAS_ECC */ 141# endif /* OPENSSL_HAS_ECC */
124#endif /* WITH_OPENSSL */ 142#endif /* WITH_OPENSSL */
125 { "null", "null", KEY_NULL, 0, 0, 0 }, 143 { "null", "null", NULL, KEY_NULL, 0, 0, 0 },
126 { NULL, NULL, -1, -1, 0, 0 } 144 { NULL, NULL, NULL, -1, -1, 0, 0 }
127}; 145};
128 146
129const char * 147const char *
@@ -2199,8 +2217,8 @@ sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
2199 return r; 2217 return r;
2200} 2218}
2201 2219
2202int 2220static int
2203sshkey_sigtype(const u_char *sig, size_t siglen, char **sigtypep) 2221get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
2204{ 2222{
2205 int r; 2223 int r;
2206 struct sshbuf *b = NULL; 2224 struct sshbuf *b = NULL;
@@ -2224,6 +2242,50 @@ sshkey_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
2224 return r; 2242 return r;
2225} 2243}
2226 2244
2245/*
2246 * Returns the expected signature algorithm for a given public key algorithm.
2247 */
2248const char *
2249sshkey_sigalg_by_name(const char *name)
2250{
2251 const struct keytype *kt;
2252
2253 for (kt = keytypes; kt->type != -1; kt++) {
2254 if (strcmp(kt->name, name) != 0)
2255 continue;
2256 if (kt->sigalg != NULL)
2257 return kt->sigalg;
2258 if (!kt->cert)
2259 return kt->name;
2260 return sshkey_ssh_name_from_type_nid(
2261 sshkey_type_plain(kt->type), kt->nid);
2262 }
2263 return NULL;
2264}
2265
2266/*
2267 * Verifies that the signature algorithm appearing inside the signature blob
2268 * matches that which was requested.
2269 */
2270int
2271sshkey_check_sigtype(const u_char *sig, size_t siglen,
2272 const char *requested_alg)
2273{
2274 const char *expected_alg;
2275 char *sigtype = NULL;
2276 int r;
2277
2278 if (requested_alg == NULL)
2279 return 0;
2280 if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
2281 return SSH_ERR_INVALID_ARGUMENT;
2282 if ((r = get_sigtype(sig, siglen, &sigtype)) != 0)
2283 return r;
2284 r = strcmp(expected_alg, sigtype) == 0;
2285 free(sigtype);
2286 return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED;
2287}
2288
2227int 2289int
2228sshkey_sign(const struct sshkey *key, 2290sshkey_sign(const struct sshkey *key,
2229 u_char **sigp, size_t *lenp, 2291 u_char **sigp, size_t *lenp,