1255767Sdes/* $OpenBSD: key.h,v 1.37 2013/05/19 02:42:42 djm Exp $ */
276259Sgreen
365668Skris/*
492555Sdes * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
565668Skris *
665668Skris * Redistribution and use in source and binary forms, with or without
765668Skris * modification, are permitted provided that the following conditions
865668Skris * are met:
965668Skris * 1. Redistributions of source code must retain the above copyright
1065668Skris *    notice, this list of conditions and the following disclaimer.
1165668Skris * 2. Redistributions in binary form must reproduce the above copyright
1265668Skris *    notice, this list of conditions and the following disclaimer in the
1365668Skris *    documentation and/or other materials provided with the distribution.
1465668Skris *
1565668Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1665668Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1765668Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1865668Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1965668Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2065668Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2165668Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2265668Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2365668Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2465668Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2565668Skris */
2658582Skris#ifndef KEY_H
2758582Skris#define KEY_H
2858582Skris
29204917Sdes#include "buffer.h"
3076259Sgreen#include <openssl/rsa.h>
3176259Sgreen#include <openssl/dsa.h>
32221420Sdes#ifdef OPENSSL_HAS_ECC
33221420Sdes#include <openssl/ec.h>
34221420Sdes#endif
3576259Sgreen
3658582Skristypedef struct Key Key;
3758582Skrisenum types {
3876259Sgreen	KEY_RSA1,
3958582Skris	KEY_RSA,
4058582Skris	KEY_DSA,
41221420Sdes	KEY_ECDSA,
42204917Sdes	KEY_RSA_CERT,
43204917Sdes	KEY_DSA_CERT,
44221420Sdes	KEY_ECDSA_CERT,
45215116Sdes	KEY_RSA_CERT_V00,
46215116Sdes	KEY_DSA_CERT_V00,
4776259Sgreen	KEY_UNSPEC
4858582Skris};
4976259Sgreenenum fp_type {
5076259Sgreen	SSH_FP_SHA1,
51240075Sdes	SSH_FP_MD5,
52240075Sdes	SSH_FP_SHA256
5376259Sgreen};
5476259Sgreenenum fp_rep {
5576259Sgreen	SSH_FP_HEX,
56181111Sdes	SSH_FP_BUBBLEBABBLE,
57181111Sdes	SSH_FP_RANDOMART
5876259Sgreen};
5992555Sdes
6092555Sdes/* key is stored in external hardware */
6192555Sdes#define KEY_FLAG_EXT		0x0001
6292555Sdes
63204917Sdes#define CERT_MAX_PRINCIPALS	256
64204917Sdesstruct KeyCert {
65204917Sdes	Buffer		 certblob; /* Kept around for use on wire */
66204917Sdes	u_int		 type; /* SSH2_CERT_TYPE_USER or SSH2_CERT_TYPE_HOST */
67215116Sdes	u_int64_t	 serial;
68204917Sdes	char		*key_id;
69204917Sdes	u_int		 nprincipals;
70204917Sdes	char		**principals;
71204917Sdes	u_int64_t	 valid_after, valid_before;
72215116Sdes	Buffer		 critical;
73215116Sdes	Buffer		 extensions;
74204917Sdes	Key		*signature_key;
75204917Sdes};
76204917Sdes
7758582Skrisstruct Key {
7892555Sdes	int	 type;
7992555Sdes	int	 flags;
8058582Skris	RSA	*rsa;
8158582Skris	DSA	*dsa;
82221420Sdes	int	 ecdsa_nid;	/* NID of curve */
83221420Sdes#ifdef OPENSSL_HAS_ECC
84221420Sdes	EC_KEY	*ecdsa;
85221420Sdes#else
86221420Sdes	void	*ecdsa;
87221420Sdes#endif
88204917Sdes	struct KeyCert *cert;
8958582Skris};
9058582Skris
91126274SdesKey		*key_new(int);
92204917Sdesvoid		 key_add_private(Key *);
93126274SdesKey		*key_new_private(int);
94126274Sdesvoid		 key_free(Key *);
95126274SdesKey		*key_demote(const Key *);
96204917Sdesint		 key_equal_public(const Key *, const Key *);
97126274Sdesint		 key_equal(const Key *, const Key *);
98255767Sdeschar		*key_fingerprint(const Key *, enum fp_type, enum fp_rep);
99248619Sdesu_char		*key_fingerprint_raw(const Key *, enum fp_type, u_int *);
100126274Sdesconst char	*key_type(const Key *);
101207319Sdesconst char	*key_cert_type(const Key *);
102126274Sdesint		 key_write(const Key *, FILE *);
103126274Sdesint		 key_read(Key *, char **);
104126274Sdesu_int		 key_size(const Key *);
10558582Skris
10692555SdesKey	*key_generate(int, u_int);
107126274SdesKey	*key_from_private(const Key *);
10892555Sdesint	 key_type_from_name(char *);
109204917Sdesint	 key_is_cert(const Key *);
110204917Sdesint	 key_type_plain(int);
111215116Sdesint	 key_to_certified(Key *, int);
112204917Sdesint	 key_drop_cert(Key *);
113204917Sdesint	 key_certify(Key *, Key *);
114204917Sdesvoid	 key_cert_copy(const Key *, struct Key *);
115204917Sdesint	 key_cert_check_authority(const Key *, int, int, const char *,
116204917Sdes	    const char **);
117248619Sdesint	 key_cert_is_legacy(const Key *);
11876259Sgreen
119221420Sdesint		 key_ecdsa_nid_from_name(const char *);
120221420Sdesint		 key_curve_name_to_nid(const char *);
121255767Sdesconst char	*key_curve_nid_to_name(int);
122221420Sdesu_int		 key_curve_nid_to_bits(int);
123221420Sdesint		 key_ecdsa_bits_to_nid(int);
124221420Sdes#ifdef OPENSSL_HAS_ECC
125221420Sdesint		 key_ecdsa_key_to_nid(EC_KEY *);
126255767Sdesconst EVP_MD	*key_ec_nid_to_evpmd(int nid);
127221420Sdesint		 key_ec_validate_public(const EC_GROUP *, const EC_POINT *);
128221420Sdesint		 key_ec_validate_private(const EC_KEY *);
129221420Sdes#endif
130255767Sdeschar		*key_alg_list(void);
131221420Sdes
132126274SdesKey		*key_from_blob(const u_char *, u_int);
133126274Sdesint		 key_to_blob(const Key *, u_char **, u_int *);
134126274Sdesconst char	*key_ssh_name(const Key *);
135221420Sdesconst char	*key_ssh_name_plain(const Key *);
136126274Sdesint		 key_names_valid2(const char *);
13776259Sgreen
138126274Sdesint	 key_sign(const Key *, u_char **, u_int *, const u_char *, u_int);
139126274Sdesint	 key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
14076259Sgreen
141126274Sdesint	 ssh_dss_sign(const Key *, u_char **, u_int *, const u_char *, u_int);
142126274Sdesint	 ssh_dss_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
143221420Sdesint	 ssh_ecdsa_sign(const Key *, u_char **, u_int *, const u_char *, u_int);
144221420Sdesint	 ssh_ecdsa_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
145126274Sdesint	 ssh_rsa_sign(const Key *, u_char **, u_int *, const u_char *, u_int);
146126274Sdesint	 ssh_rsa_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
147113908Sdes
148221420Sdes#if defined(OPENSSL_HAS_ECC) && (defined(DEBUG_KEXECDH) || defined(DEBUG_PK))
149221420Sdesvoid	key_dump_ec_point(const EC_GROUP *, const EC_POINT *);
150221420Sdesvoid	key_dump_ec_key(const EC_KEY *);
15158582Skris#endif
152221420Sdes
153221420Sdes#endif
154