1/*	$OpenBSD: ssherr.c,v 1.10 2020/01/25 23:13:09 djm Exp $	*/
2/*
3 * Copyright (c) 2011 Damien Miller
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#include "includes.h"
18__RCSID("$NetBSD: ssherr.c,v 1.10 2022/10/19 11:54:02 christos Exp $");
19
20#include <errno.h>
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include "ssherr.h"
26
27const char *
28ssh_err(int n)
29{
30	switch (n) {
31	case SSH_ERR_SUCCESS:
32		return "success";
33	case SSH_ERR_INTERNAL_ERROR:
34		return "unexpected internal error";
35	case SSH_ERR_ALLOC_FAIL:
36		return "memory allocation failed";
37	case SSH_ERR_MESSAGE_INCOMPLETE:
38		return "incomplete message";
39	case SSH_ERR_INVALID_FORMAT:
40		return "invalid format";
41	case SSH_ERR_BIGNUM_IS_NEGATIVE:
42		return "bignum is negative";
43	case SSH_ERR_STRING_TOO_LARGE:
44		return "string is too large";
45	case SSH_ERR_BIGNUM_TOO_LARGE:
46		return "bignum is too large";
47	case SSH_ERR_ECPOINT_TOO_LARGE:
48		return "elliptic curve point is too large";
49	case SSH_ERR_NO_BUFFER_SPACE:
50		return "insufficient buffer space";
51	case SSH_ERR_INVALID_ARGUMENT:
52		return "invalid argument";
53	case SSH_ERR_KEY_BITS_MISMATCH:
54		return "key bits do not match";
55	case SSH_ERR_EC_CURVE_INVALID:
56		return "invalid elliptic curve";
57	case SSH_ERR_KEY_TYPE_MISMATCH:
58		return "key type does not match";
59	case SSH_ERR_KEY_TYPE_UNKNOWN:
60		return "unknown or unsupported key type";
61	case SSH_ERR_EC_CURVE_MISMATCH:
62		return "elliptic curve does not match";
63	case SSH_ERR_EXPECTED_CERT:
64		return "plain key provided where certificate required";
65	case SSH_ERR_KEY_LACKS_CERTBLOB:
66		return "key lacks certificate data";
67	case SSH_ERR_KEY_CERT_UNKNOWN_TYPE:
68		return "unknown/unsupported certificate type";
69	case SSH_ERR_KEY_CERT_INVALID_SIGN_KEY:
70		return "invalid certificate signing key";
71	case SSH_ERR_KEY_INVALID_EC_VALUE:
72		return "invalid elliptic curve value";
73	case SSH_ERR_SIGNATURE_INVALID:
74		return "incorrect signature";
75	case SSH_ERR_LIBCRYPTO_ERROR:
76		return "error in libcrypto";  /* XXX fetch and return */
77	case SSH_ERR_UNEXPECTED_TRAILING_DATA:
78		return "unexpected bytes remain after decoding";
79	case SSH_ERR_SYSTEM_ERROR:
80		return strerror(errno);
81	case SSH_ERR_KEY_CERT_INVALID:
82		return "invalid certificate";
83	case SSH_ERR_AGENT_COMMUNICATION:
84		return "communication with agent failed";
85	case SSH_ERR_AGENT_FAILURE:
86		return "agent refused operation";
87	case SSH_ERR_DH_GEX_OUT_OF_RANGE:
88		return "DH GEX group out of range";
89	case SSH_ERR_DISCONNECTED:
90		return "disconnected";
91	case SSH_ERR_MAC_INVALID:
92		return "message authentication code incorrect";
93	case SSH_ERR_NO_CIPHER_ALG_MATCH:
94		return "no matching cipher found";
95	case SSH_ERR_NO_MAC_ALG_MATCH:
96		return "no matching MAC found";
97	case SSH_ERR_NO_COMPRESS_ALG_MATCH:
98		return "no matching compression method found";
99	case SSH_ERR_NO_KEX_ALG_MATCH:
100		return "no matching key exchange method found";
101	case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
102		return "no matching host key type found";
103	case SSH_ERR_PROTOCOL_MISMATCH:
104		return "protocol version mismatch";
105	case SSH_ERR_NO_PROTOCOL_VERSION:
106		return "could not read protocol version";
107	case SSH_ERR_NO_HOSTKEY_LOADED:
108		return "could not load host key";
109	case SSH_ERR_NEED_REKEY:
110		return "rekeying not supported by peer";
111	case SSH_ERR_PASSPHRASE_TOO_SHORT:
112		return "passphrase is too short (minimum five characters)";
113	case SSH_ERR_FILE_CHANGED:
114		return "file changed while reading";
115	case SSH_ERR_KEY_UNKNOWN_CIPHER:
116		return "key encrypted using unsupported cipher";
117	case SSH_ERR_KEY_WRONG_PASSPHRASE:
118		return "incorrect passphrase supplied to decrypt private key";
119	case SSH_ERR_KEY_BAD_PERMISSIONS:
120		return "bad permissions";
121	case SSH_ERR_KEY_CERT_MISMATCH:
122		return "certificate does not match key";
123	case SSH_ERR_KEY_NOT_FOUND:
124		return "key not found";
125	case SSH_ERR_AGENT_NOT_PRESENT:
126		return "agent not present";
127	case SSH_ERR_AGENT_NO_IDENTITIES:
128		return "agent contains no identities";
129	case SSH_ERR_BUFFER_READ_ONLY:
130		return "internal error: buffer is read-only";
131	case SSH_ERR_KRL_BAD_MAGIC:
132		return "KRL file has invalid magic number";
133	case SSH_ERR_KEY_REVOKED:
134		return "Key is revoked";
135	case SSH_ERR_CONN_CLOSED:
136		return "Connection closed";
137	case SSH_ERR_CONN_TIMEOUT:
138		return "Connection timed out";
139	case SSH_ERR_CONN_CORRUPT:
140		return "Connection corrupted";
141	case SSH_ERR_PROTOCOL_ERROR:
142		return "Protocol error";
143	case SSH_ERR_KEY_LENGTH:
144		return "Invalid key length";
145	case SSH_ERR_NUMBER_TOO_LARGE:
146		return "number is too large";
147	case SSH_ERR_SIGN_ALG_UNSUPPORTED:
148		return "signature algorithm not supported";
149	case SSH_ERR_FEATURE_UNSUPPORTED:
150		return "requested feature not supported";
151	case SSH_ERR_DEVICE_NOT_FOUND:
152		return "device not found";
153	default:
154		{
155			static char buf[1024];
156			snprintf(buf, sizeof(buf), "unknown error %d", n);
157			return buf;
158		}
159	}
160}
161