1 /* $OpenBSD: key.c,v 1.132 2017/12/18 02:25:15 djm Exp $ */
3 * placed in the public domain
14 #define SSH_KEY_NO_DEFINE
24 fatal_on_fatal_errors(int r, const char *func, int extra_fatal)
26 if (r == SSH_ERR_INTERNAL_ERROR ||
27 r == SSH_ERR_ALLOC_FAIL ||
28 (extra_fatal != 0 && r == extra_fatal))
29 fatal("%s: %s", func, ssh_err(r));
33 key_from_blob(const u_char *blob, u_int blen)
38 if ((r = sshkey_from_blob(blob, blen, &ret)) != 0) {
39 fatal_on_fatal_errors(r, __func__, 0);
40 error("%s: %s", __func__, ssh_err(r));
47 key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
57 if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
58 fatal_on_fatal_errors(r, __func__, 0);
59 error("%s: %s", __func__, ssh_err(r));
63 fatal("%s: giant len %zu", __func__, blen);
72 key_sign(const Key *key, u_char **sigp, u_int *lenp,
73 const u_char *data, u_int datalen, const char *alg)
83 if ((r = sshkey_sign(key, &sig, &siglen,
84 data, datalen, alg, datafellows)) != 0) {
85 fatal_on_fatal_errors(r, __func__, 0);
86 error("%s: %s", __func__, ssh_err(r));
90 fatal("%s: giant len %zu", __func__, siglen);
99 key_demote(const Key *k)
104 if ((r = sshkey_demote(k, &ret)) != 0)
105 fatal("%s: %s", __func__, ssh_err(r));
110 key_drop_cert(Key *k)
114 if ((r = sshkey_drop_cert(k)) != 0) {
115 fatal_on_fatal_errors(r, __func__, 0);
116 error("%s: %s", __func__, ssh_err(r));
123 key_cert_check_authority(const Key *k, int want_host, int require_principal,
124 const char *name, const char **reason)
128 if ((r = sshkey_cert_check_authority(k, want_host, require_principal,
129 name, reason)) != 0) {
130 fatal_on_fatal_errors(r, __func__, 0);
131 error("%s: %s", __func__, ssh_err(r));
140 key_load_cert(const char *filename)
145 if ((r = sshkey_load_cert(filename, &ret)) != 0) {
146 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
147 /* Old authfile.c ignored all file errors. */
148 if (r == SSH_ERR_SYSTEM_ERROR)
149 debug("%s: %s", __func__, ssh_err(r));
151 error("%s: %s", __func__, ssh_err(r));
159 key_load_public(const char *filename, char **commentp)
164 if ((r = sshkey_load_public(filename, &ret, commentp)) != 0) {
165 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
166 /* Old authfile.c ignored all file errors. */
167 if (r == SSH_ERR_SYSTEM_ERROR)
168 debug("%s: %s", __func__, ssh_err(r));
170 error("%s: %s", __func__, ssh_err(r));
177 key_load_private(const char *path, const char *passphrase,
183 if ((r = sshkey_load_private(path, passphrase, &ret, commentp)) != 0) {
184 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
185 /* Old authfile.c ignored all file errors. */
186 if (r == SSH_ERR_SYSTEM_ERROR ||
187 r == SSH_ERR_KEY_WRONG_PASSPHRASE)
188 debug("%s: %s", __func__, ssh_err(r));
190 error("%s: %s", __func__, ssh_err(r));
197 key_load_private_cert(int type, const char *filename, const char *passphrase,
203 if ((r = sshkey_load_private_cert(type, filename, passphrase,
204 &ret, perm_ok)) != 0) {
205 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
206 /* Old authfile.c ignored all file errors. */
207 if (r == SSH_ERR_SYSTEM_ERROR ||
208 r == SSH_ERR_KEY_WRONG_PASSPHRASE)
209 debug("%s: %s", __func__, ssh_err(r));
211 error("%s: %s", __func__, ssh_err(r));
218 key_load_private_type(int type, const char *filename, const char *passphrase,
219 char **commentp, int *perm_ok)
224 if ((r = sshkey_load_private_type(type, filename, passphrase,
225 &ret, commentp, perm_ok)) != 0) {
226 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
227 /* Old authfile.c ignored all file errors. */
228 if (r == SSH_ERR_SYSTEM_ERROR ||
229 (r == SSH_ERR_KEY_WRONG_PASSPHRASE))
230 debug("%s: %s", __func__, ssh_err(r));
232 error("%s: %s", __func__, ssh_err(r));