blob: 6e338c495bbbb00aaf9ad2d51428ce0d3650fb0a [file] [log] [blame]
markus@openbsd.org7da5df12017-05-30 14:16:41 +00001/* $OpenBSD: key.c,v 1.131 2017/05/30 14:16:41 markus Exp $ */
Damien Miller450a7a12000-03-26 13:04:51 +10002/*
Damien Miller86687062014-07-02 15:28:02 +10003 * placed in the public domain
Damien Miller450a7a12000-03-26 13:04:51 +10004 */
Damien Millerd7834352006-08-05 12:39:39 +10005
Damien Miller450a7a12000-03-26 13:04:51 +10006#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +00007
Damien Millerd7834352006-08-05 12:39:39 +10008#include <sys/types.h>
Damien Miller86687062014-07-02 15:28:02 +10009#include <errno.h>
Damien Millerded319c2006-09-01 15:38:36 +100010#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100011#include <stdio.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000012#include <limits.h>
Damien Millere3476ed2006-07-24 14:13:33 +100013
Damien Miller86687062014-07-02 15:28:02 +100014#define SSH_KEY_NO_DEFINE
Damien Miller450a7a12000-03-26 13:04:51 +100015#include "key.h"
Damien Miller86687062014-07-02 15:28:02 +100016
17#include "compat.h"
18#include "sshkey.h"
19#include "ssherr.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "log.h"
Damien Miller86687062014-07-02 15:28:02 +100021#include "authfile.h"
Ben Lindstrom836f0e92002-06-23 21:21:30 +000022
Damien Miller86687062014-07-02 15:28:02 +100023static void
24fatal_on_fatal_errors(int r, const char *func, int extra_fatal)
Damien Miller0bc1bd82000-11-13 22:57:25 +110025{
Damien Miller86687062014-07-02 15:28:02 +100026 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));
Damien Miller0bc1bd82000-11-13 22:57:25 +110030}
31
Damien Miller4a3a9d42013-10-30 22:19:47 +110032Key *
33key_from_blob(const u_char *blob, u_int blen)
34{
Damien Miller86687062014-07-02 15:28:02 +100035 int r;
36 Key *ret = NULL;
Damien Miller4a3a9d42013-10-30 22:19:47 +110037
Damien Miller86687062014-07-02 15:28:02 +100038 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));
41 return NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +110042 }
Damien Miller86687062014-07-02 15:28:02 +100043 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +110044}
45
46int
Damien Millerf3747bf2013-01-18 11:44:04 +110047key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
48{
Damien Miller86687062014-07-02 15:28:02 +100049 u_char *blob;
50 size_t blen;
51 int r;
52
53 if (blobp != NULL)
54 *blobp = NULL;
55 if (lenp != NULL)
56 *lenp = 0;
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));
60 return 0;
61 }
62 if (blen > INT_MAX)
63 fatal("%s: giant len %zu", __func__, blen);
64 if (blobp != NULL)
65 *blobp = blob;
66 if (lenp != NULL)
67 *lenp = blen;
68 return blen;
Damien Millerf3747bf2013-01-18 11:44:04 +110069}
70
71int
Damien Miller86687062014-07-02 15:28:02 +100072key_sign(const Key *key, u_char **sigp, u_int *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000073 const u_char *data, u_int datalen, const char *alg)
Damien Miller0bc1bd82000-11-13 22:57:25 +110074{
Damien Miller86687062014-07-02 15:28:02 +100075 int r;
76 u_char *sig;
77 size_t siglen;
78
79 if (sigp != NULL)
80 *sigp = NULL;
81 if (lenp != NULL)
82 *lenp = 0;
83 if ((r = sshkey_sign(key, &sig, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000084 data, datalen, alg, datafellows)) != 0) {
Damien Miller86687062014-07-02 15:28:02 +100085 fatal_on_fatal_errors(r, __func__, 0);
86 error("%s: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +110087 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110088 }
Damien Miller86687062014-07-02 15:28:02 +100089 if (siglen > INT_MAX)
90 fatal("%s: giant len %zu", __func__, siglen);
91 if (sigp != NULL)
92 *sigp = sig;
93 if (lenp != NULL)
94 *lenp = siglen;
Damien Miller5be9d9e2013-12-07 11:24:01 +110095 return 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110096}
97
Damien Miller86687062014-07-02 15:28:02 +100098int
99key_verify(const Key *key, const u_char *signature, u_int signaturelen,
100 const u_char *data, u_int datalen)
101{
102 int r;
103
104 if ((r = sshkey_verify(key, signature, signaturelen,
105 data, datalen, datafellows)) != 0) {
106 fatal_on_fatal_errors(r, __func__, 0);
107 error("%s: %s", __func__, ssh_err(r));
108 return r == SSH_ERR_SIGNATURE_INVALID ? 0 : -1;
109 }
110 return 1;
111}
112
113Key *
114key_demote(const Key *k)
115{
116 int r;
117 Key *ret = NULL;
118
119 if ((r = sshkey_demote(k, &ret)) != 0)
120 fatal("%s: %s", __func__, ssh_err(r));
121 return ret;
122}
123
124int
Damien Miller86687062014-07-02 15:28:02 +1000125key_drop_cert(Key *k)
126{
127 int r;
128
129 if ((r = sshkey_drop_cert(k)) != 0) {
130 fatal_on_fatal_errors(r, __func__, 0);
131 error("%s: %s", __func__, ssh_err(r));
132 return -1;
133 }
134 return 0;
135}
136
Damien Miller0a80ca12010-02-27 07:55:05 +1100137int
Damien Miller0a80ca12010-02-27 07:55:05 +1100138key_cert_check_authority(const Key *k, int want_host, int require_principal,
139 const char *name, const char **reason)
140{
Damien Miller86687062014-07-02 15:28:02 +1000141 int r;
Damien Miller0a80ca12010-02-27 07:55:05 +1100142
Damien Miller86687062014-07-02 15:28:02 +1000143 if ((r = sshkey_cert_check_authority(k, want_host, require_principal,
144 name, reason)) != 0) {
145 fatal_on_fatal_errors(r, __func__, 0);
146 error("%s: %s", __func__, ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +1100147 return -1;
148 }
Damien Miller86687062014-07-02 15:28:02 +1000149 return 0;
150}
151
Damien Miller86687062014-07-02 15:28:02 +1000152/* authfile.c */
153
Damien Miller86687062014-07-02 15:28:02 +1000154Key *
155key_load_cert(const char *filename)
156{
157 int r;
158 Key *ret = NULL;
159
160 if ((r = sshkey_load_cert(filename, &ret)) != 0) {
161 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
Damien Millerd0bb9502014-07-09 13:07:28 +1000162 /* Old authfile.c ignored all file errors. */
163 if (r == SSH_ERR_SYSTEM_ERROR)
Damien Miller86687062014-07-02 15:28:02 +1000164 debug("%s: %s", __func__, ssh_err(r));
165 else
166 error("%s: %s", __func__, ssh_err(r));
167 return NULL;
168 }
169 return ret;
170
171}
172
173Key *
174key_load_public(const char *filename, char **commentp)
175{
176 int r;
177 Key *ret = NULL;
178
179 if ((r = sshkey_load_public(filename, &ret, commentp)) != 0) {
180 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
Damien Millerd0bb9502014-07-09 13:07:28 +1000181 /* Old authfile.c ignored all file errors. */
182 if (r == SSH_ERR_SYSTEM_ERROR)
Damien Miller86687062014-07-02 15:28:02 +1000183 debug("%s: %s", __func__, ssh_err(r));
184 else
185 error("%s: %s", __func__, ssh_err(r));
186 return NULL;
187 }
188 return ret;
189}
190
191Key *
192key_load_private(const char *path, const char *passphrase,
193 char **commentp)
194{
195 int r;
196 Key *ret = NULL;
197
198 if ((r = sshkey_load_private(path, passphrase, &ret, commentp)) != 0) {
199 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
Damien Millerd0bb9502014-07-09 13:07:28 +1000200 /* Old authfile.c ignored all file errors. */
Damien Millerdad9a4a2014-07-18 15:03:49 +1000201 if (r == SSH_ERR_SYSTEM_ERROR ||
202 r == SSH_ERR_KEY_WRONG_PASSPHRASE)
Damien Miller86687062014-07-02 15:28:02 +1000203 debug("%s: %s", __func__, ssh_err(r));
204 else
205 error("%s: %s", __func__, ssh_err(r));
206 return NULL;
207 }
208 return ret;
209}
210
211Key *
212key_load_private_cert(int type, const char *filename, const char *passphrase,
213 int *perm_ok)
214{
215 int r;
216 Key *ret = NULL;
217
218 if ((r = sshkey_load_private_cert(type, filename, passphrase,
219 &ret, perm_ok)) != 0) {
220 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
Damien Millerd0bb9502014-07-09 13:07:28 +1000221 /* Old authfile.c ignored all file errors. */
Damien Millerdad9a4a2014-07-18 15:03:49 +1000222 if (r == SSH_ERR_SYSTEM_ERROR ||
223 r == SSH_ERR_KEY_WRONG_PASSPHRASE)
Damien Miller86687062014-07-02 15:28:02 +1000224 debug("%s: %s", __func__, ssh_err(r));
225 else
226 error("%s: %s", __func__, ssh_err(r));
227 return NULL;
228 }
229 return ret;
230}
231
232Key *
233key_load_private_type(int type, const char *filename, const char *passphrase,
234 char **commentp, int *perm_ok)
235{
236 int r;
237 Key *ret = NULL;
238
239 if ((r = sshkey_load_private_type(type, filename, passphrase,
240 &ret, commentp, perm_ok)) != 0) {
241 fatal_on_fatal_errors(r, __func__, SSH_ERR_LIBCRYPTO_ERROR);
Damien Millerd0bb9502014-07-09 13:07:28 +1000242 /* Old authfile.c ignored all file errors. */
243 if (r == SSH_ERR_SYSTEM_ERROR ||
Damien Miller86687062014-07-02 15:28:02 +1000244 (r == SSH_ERR_KEY_WRONG_PASSPHRASE))
245 debug("%s: %s", __func__, ssh_err(r));
246 else
247 error("%s: %s", __func__, ssh_err(r));
248 return NULL;
249 }
250 return ret;
251}