182498Sroberto/*
282498Sroberto * ntp_crypto.h - definitions for cryptographic operations
382498Sroberto */
4280849Scy#ifndef NTP_CRYPTO_H
5280849Scy#define NTP_CRYPTO_H
6280849Scy
7280849Scy/*
8280849Scy * Configuration codes (also needed for parser without AUTOKEY)
9280849Scy */
10280849Scy#define CRYPTO_CONF_NONE  0	/* nothing doing */
11280849Scy#define CRYPTO_CONF_PRIV  1	/* host name */
12280849Scy#define CRYPTO_CONF_IDENT 2	/* group name */
13280849Scy#define CRYPTO_CONF_CERT  3	/* certificate file name */
14280849Scy#define CRYPTO_CONF_RAND  4	/* random seed file name */
15280849Scy#define CRYPTO_CONF_IFFPAR 5	/* IFF parameters file name */
16280849Scy#define CRYPTO_CONF_GQPAR 6	/* GQ parameters file name */
17280849Scy#define	CRYPTO_CONF_MVPAR 7	/* MV parameters file name */
18280849Scy#define CRYPTO_CONF_PW	  8	/* private key password */
19280849Scy#define	CRYPTO_CONF_NID   9	/* specify digest name */
20280849Scy
21280849Scy#ifdef AUTOKEY
22280849Scy#ifndef OPENSSL
23280849Scy#error AUTOKEY should be defined only if OPENSSL is.
24280849Scyinvalidsyntax: AUTOKEY should be defined only if OPENSSL is.
25280849Scy#endif
26280849Scy
27316068Sdelphij#include "openssl/bn.h"
28132451Sroberto#include "openssl/evp.h"
29280849Scy#include "ntp_calendar.h"	/* for fields in the cert_info structure */
30280849Scy
31280849Scy
32132451Sroberto/*
33132451Sroberto * The following bits are set by the CRYPTO_ASSOC message from
34132451Sroberto * the server and are not modified by the client.
35132451Sroberto */
36132451Sroberto#define CRYPTO_FLAG_ENAB  0x0001 /* crypto enable */
37132451Sroberto#define CRYPTO_FLAG_TAI   0x0002 /* leapseconds table */
3882498Sroberto
39132451Sroberto#define CRYPTO_FLAG_PRIV  0x0010 /* PC identity scheme */
40132451Sroberto#define CRYPTO_FLAG_IFF   0x0020 /* IFF identity scheme */
41132451Sroberto#define CRYPTO_FLAG_GQ	  0x0040 /* GQ identity scheme */
42132451Sroberto#define	CRYPTO_FLAG_MV	  0x0080 /* MV identity scheme */
43132451Sroberto#define CRYPTO_FLAG_MASK  0x00f0 /* identity scheme mask */
44132451Sroberto
45132451Sroberto/*
46132451Sroberto * The following bits are used by the client during the protocol
47132451Sroberto * exchange.
48132451Sroberto */
49280849Scy#define CRYPTO_FLAG_CERT  0x0100 /* public key verified */
50132451Sroberto#define CRYPTO_FLAG_VRFY  0x0200 /* identity verified */
51132451Sroberto#define CRYPTO_FLAG_PROV  0x0400 /* signature verified */
52280849Scy#define CRYPTO_FLAG_COOK  0x0800 /* cookie verifed */
53132451Sroberto#define CRYPTO_FLAG_AUTO  0x1000 /* autokey verified */
54132451Sroberto#define CRYPTO_FLAG_SIGN  0x2000 /* certificate signed */
55280849Scy#define CRYPTO_FLAG_LEAP  0x4000 /* leapsecond values verified */
56280849Scy#define	CRYPTO_FLAG_ALL   0x7f00 /* all mask */
5782498Sroberto
5882498Sroberto/*
59132451Sroberto * Flags used for certificate management
6082498Sroberto */
61182007Sroberto#define	CERT_TRUST	0x01	/* certificate is trusted */
62182007Sroberto#define CERT_SIGN	0x02	/* certificate is signed */
63182007Sroberto#define CERT_VALID	0x04	/* certificate is valid */
64182007Sroberto#define CERT_PRIV	0x08	/* certificate is private */
65132451Sroberto#define CERT_ERROR	0x80	/* certificate has errors */
6682498Sroberto
6782498Sroberto/*
6882498Sroberto * Extension field definitions
6982498Sroberto */
70132451Sroberto#define	CRYPTO_MAXLEN	1024	/* max extension field length */
71132451Sroberto#define CRYPTO_VN	2	/* current protocol version number */
72132451Sroberto#define CRYPTO_CMD(x)	(((CRYPTO_VN << 8) | (x)) << 16)
73132451Sroberto#define CRYPTO_NULL	CRYPTO_CMD(0) /* no operation */
74132451Sroberto#define CRYPTO_ASSOC	CRYPTO_CMD(1) /* association */
75132451Sroberto#define CRYPTO_CERT	CRYPTO_CMD(2) /* certificate */
76132451Sroberto#define CRYPTO_COOK	CRYPTO_CMD(3) /* cookie value */
77132451Sroberto#define CRYPTO_AUTO	CRYPTO_CMD(4) /* autokey values */
78280849Scy#define CRYPTO_LEAP	CRYPTO_CMD(5) /* leapsecond values */
79132451Sroberto#define	CRYPTO_SIGN	CRYPTO_CMD(6) /* certificate sign */
80132451Sroberto#define CRYPTO_IFF	CRYPTO_CMD(7) /* IFF identity scheme */
81132451Sroberto#define CRYPTO_GQ	CRYPTO_CMD(8) /* GQ identity scheme */
82132451Sroberto#define	CRYPTO_MV	CRYPTO_CMD(9) /* MV identity scheme */
83132451Sroberto#define CRYPTO_RESP	0x80000000 /* response */
84132451Sroberto#define CRYPTO_ERROR	0x40000000 /* error */
8582498Sroberto
86132451Sroberto/*
87132451Sroberto * Autokey event codes
88132451Sroberto */
89132451Sroberto#define XEVNT_CMD(x)	(CRPT_EVENT | (x))
90132451Sroberto#define XEVNT_OK	XEVNT_CMD(0) /* success */
91132451Sroberto#define XEVNT_LEN	XEVNT_CMD(1) /* bad field format or length */
92132451Sroberto#define XEVNT_TSP	XEVNT_CMD(2) /* bad timestamp */
93132451Sroberto#define XEVNT_FSP	XEVNT_CMD(3) /* bad filestamp */
94182007Sroberto#define XEVNT_PUB	XEVNT_CMD(4) /* bad or missing public key */
95132451Sroberto#define XEVNT_MD	XEVNT_CMD(5) /* unsupported digest type */
96132451Sroberto#define XEVNT_KEY	XEVNT_CMD(6) /* unsupported identity type */
97132451Sroberto#define XEVNT_SGL	XEVNT_CMD(7) /* bad signature length */
98132451Sroberto#define XEVNT_SIG	XEVNT_CMD(8) /* signature not verified */
99132451Sroberto#define XEVNT_VFY	XEVNT_CMD(9) /* certificate not verified */
100182007Sroberto#define XEVNT_PER	XEVNT_CMD(10) /* host certificate expired */
101132451Sroberto#define XEVNT_CKY	XEVNT_CMD(11) /* bad or missing cookie */
102280849Scy#define XEVNT_DAT	XEVNT_CMD(12) /* bad or missing leapseconds */
103132451Sroberto#define XEVNT_CRT	XEVNT_CMD(13) /* bad or missing certificate */
104182007Sroberto#define XEVNT_ID	XEVNT_CMD(14) /* bad or missing group key */
105182007Sroberto#define	XEVNT_ERR	XEVNT_CMD(15) /* protocol error */
10682498Sroberto
10782498Sroberto/*
108132451Sroberto * Miscellaneous crypto stuff
10982498Sroberto */
110132451Sroberto#define NTP_MAXSESSION	100	/* maximum session key list entries */
111280849Scy#define	NTP_MAXEXTEN	2048	/* maximum extension field size */
112280849Scy#define	NTP_AUTOMAX	12	/* default key list timeout (log2 s) */
113280849Scy#define	KEY_REVOKE	17	/* default key revoke timeout (log2 s) */
114280849Scy#define	NTP_REFRESH	19	/* default restart timeout (log2 s) */
115280849Scy#define	NTP_MAXKEY	65535	/* maximum symmetric key ID */
11682498Sroberto
11782498Sroberto/*
118132451Sroberto * The autokey structure holds the values used to authenticate key IDs.
119132451Sroberto */
120132451Srobertostruct autokey {		/* network byte order */
121132451Sroberto	keyid_t	key;		/* key ID */
122132451Sroberto	int32	seq;		/* key number */
123132451Sroberto};
124132451Sroberto
125132451Sroberto/*
126132451Sroberto * The value structure holds variable length data such as public
127132451Sroberto * key, agreement parameters, public valule and leapsecond table.
128132451Sroberto * They are in network byte order.
129132451Sroberto */
130132451Srobertostruct value {			/* network byte order */
131132451Sroberto	tstamp_t tstamp;	/* timestamp */
132132451Sroberto	tstamp_t fstamp;	/* filestamp */
133132451Sroberto	u_int32	vallen;		/* value length */
134280849Scy	void	*ptr;		/* data pointer (various) */
135132451Sroberto	u_int32	siglen;		/* signature length */
136132451Sroberto	u_char	*sig;		/* signature */
137132451Sroberto};
138132451Sroberto
139132451Sroberto/*
140132451Sroberto * The packet extension field structures are used to hold values
141132451Sroberto * and signatures in network byte order.
142132451Sroberto */
143132451Srobertostruct exten {
144132451Sroberto	u_int32	opcode;		/* opcode */
145132451Sroberto	u_int32	associd;	/* association ID */
146132451Sroberto	u_int32	tstamp;		/* timestamp */
147132451Sroberto	u_int32	fstamp;		/* filestamp */
148132451Sroberto	u_int32	vallen;		/* value length */
149132451Sroberto	u_int32	pkt[1];		/* start of value field */
150132451Sroberto};
151132451Sroberto
152280849Scy
153132451Sroberto/*
154132451Sroberto * The certificate info/value structure
155132451Sroberto */
156132451Srobertostruct cert_info {
157132451Sroberto	struct cert_info *link;	/* forward link */
158132451Sroberto	u_int	flags;		/* flags that wave */
159132451Sroberto	EVP_PKEY *pkey;		/* generic key */
160132451Sroberto	long	version;	/* X509 version */
161132451Sroberto	int	nid;		/* signature/digest ID */
162132451Sroberto	const EVP_MD *digest;	/* message digest algorithm */
163132451Sroberto	u_long	serial;		/* serial number */
164280849Scy	struct calendar first;	/* not valid before */
165280849Scy	struct calendar last;	/* not valid after */
166132451Sroberto	char	*subject;	/* subject common name */
167132451Sroberto	char	*issuer;	/* issuer common name */
168280849Scy	BIGNUM	*grpkey;	/* GQ group key */
169132451Sroberto	struct value cert;	/* certificate/value */
170132451Sroberto};
171132451Sroberto
172132451Sroberto/*
173280849Scy * The keys info/value structure
174280849Scy */
175280849Scystruct pkey_info {
176280849Scy	struct pkey_info *link; /* forward link */
177280849Scy	EVP_PKEY *pkey;		/* generic key */
178280849Scy	char	*name;		/* file name */
179280849Scy	tstamp_t fstamp;	/* filestamp */
180280849Scy};
181280849Scy
182280849Scy/*
18382498Sroberto * Cryptographic values
18482498Sroberto */
18582498Srobertoextern	u_int	crypto_flags;	/* status word */
186280849Scyextern	int	crypto_nid;	/* digest nid */
187132451Srobertoextern	struct value hostval;	/* host name/value */
188132451Srobertoextern	struct cert_info *cinfo; /* host certificate information */
18982498Srobertoextern	struct value tai_leap;	/* leapseconds table */
190280849Scy#endif /* AUTOKEY */
191280849Scy#endif /* NTP_CRYPTO_H */
192