blob: 63cc1ad1a2036b924aea66738b42256111d2c2c7 [file] [log] [blame]
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00001/*
2 * Copyright (c) 2019 Google LLC
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef SSHSIG_H
18#define SSHSIG_H
19
20struct sshbuf;
21struct sshkey;
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +000022struct sshsigopt;
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +000023struct sshkey_sig_details;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000024
25typedef int sshsig_signer(struct sshkey *, u_char **, size_t *,
djm@openbsd.org9a14c642019-10-31 21:23:19 +000026 const u_char *, size_t, const char *, const char *, u_int, void *);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000027
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +000028/* Buffer-oriented API */
29
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000030/*
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +000031 * Creates a detached SSH signature for a given buffer.
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000032 * Returns 0 on success or a negative SSH_ERR_* error code on failure.
33 * out is populated with the detached signature, or NULL on failure.
34 */
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +000035int sshsig_signb(struct sshkey *key, const char *hashalg,
djm@openbsd.org9a14c642019-10-31 21:23:19 +000036 const char *sk_provider, const struct sshbuf *message,
37 const char *sig_namespace, struct sshbuf **out,
38 sshsig_signer *signer, void *signer_ctx);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000039
40/*
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +000041 * Verifies that a detached signature is valid and optionally returns key
42 * used to sign via argument.
43 * Returns 0 on success or a negative SSH_ERR_* error code on failure.
44 */
45int sshsig_verifyb(struct sshbuf *signature,
46 const struct sshbuf *message, const char *sig_namespace,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +000047 struct sshkey **sign_keyp, struct sshkey_sig_details **sig_details);
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +000048
49/* File/FD-oriented API */
50
51/*
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000052 * Creates a detached SSH signature for a given file.
53 * Returns 0 on success or a negative SSH_ERR_* error code on failure.
54 * out is populated with the detached signature, or NULL on failure.
55 */
56int sshsig_sign_fd(struct sshkey *key, const char *hashalg,
djm@openbsd.org9a14c642019-10-31 21:23:19 +000057 const char *sk_provider, int fd, const char *sig_namespace,
58 struct sshbuf **out, sshsig_signer *signer, void *signer_ctx);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000059
60/*
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000061 * Verifies that a detached signature over a file is valid and optionally
62 * returns key used to sign via argument.
63 * Returns 0 on success or a negative SSH_ERR_* error code on failure.
64 */
65int sshsig_verify_fd(struct sshbuf *signature, int fd,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +000066 const char *sig_namespace, struct sshkey **sign_keyp,
67 struct sshkey_sig_details **sig_details);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000068
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +000069/* Utility functions */
70
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000071/*
72 * Return a base64 encoded "ASCII armoured" version of a raw signature.
73 */
74int sshsig_armor(const struct sshbuf *blob, struct sshbuf **out);
75
76/*
77 * Decode a base64 encoded armoured signature to a raw signature.
78 */
79int sshsig_dearmor(struct sshbuf *sig, struct sshbuf **out);
80
81/*
82 * Checks whether a particular key/principal/namespace is permitted by
83 * an allowed_keys file. Returns 0 on success.
84 */
85int sshsig_check_allowed_keys(const char *path, const struct sshkey *sign_key,
86 const char *principal, const char *ns);
87
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +000088/* Parse zero or more allowed_keys signature options */
89struct sshsigopt *sshsigopt_parse(const char *opts,
90 const char *path, u_long linenum, const char **errstrp);
91
92/* Free signature options */
93void sshsigopt_free(struct sshsigopt *opts);
94
djm@openbsd.orge027c042020-01-23 04:54:34 +000095/* Get public key from signature */
djm@openbsd.org72a8bea2020-01-23 23:31:52 +000096int sshsig_get_pubkey(struct sshbuf *signature, struct sshkey **pubkey);
djm@openbsd.orge027c042020-01-23 04:54:34 +000097
98/* Find principal in allowed_keys file, given a sshkey. Returns
99 * 0 on success.
100 */
djm@openbsd.org72a8bea2020-01-23 23:31:52 +0000101int sshsig_find_principals(const char *path, const struct sshkey *sign_key,
djm@openbsd.orge027c042020-01-23 04:54:34 +0000102 char **principal);
103
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000104#endif /* SSHSIG_H */