xref: /openbsd/usr.sbin/rpki-client/extern.h (revision 3e8d4b7d)
1 /*	$OpenBSD: extern.h,v 1.224 2024/06/08 13:30:35 tb Exp $ */
2 /*
3  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef EXTERN_H
18 #define EXTERN_H
19 
20 #include <sys/queue.h>
21 #include <sys/tree.h>
22 #include <sys/time.h>
23 
24 #include <openssl/x509.h>
25 #include <openssl/x509v3.h>
26 
27 #define CTASSERT(x)	extern char  _ctassert[(x) ? 1 : -1 ] \
28 			    __attribute__((__unused__))
29 
30 enum cert_as_type {
31 	CERT_AS_ID, /* single identifier */
32 	CERT_AS_INHERIT, /* inherit from issuer */
33 	CERT_AS_RANGE, /* range of identifiers */
34 };
35 
36 /*
37  * An AS identifier range.
38  * The maximum AS identifier is an unsigned 32 bit integer (RFC 6793).
39  */
40 struct cert_as_range {
41 	uint32_t	 min; /* minimum non-zero */
42 	uint32_t	 max; /* maximum */
43 };
44 
45 /*
46  * An autonomous system (AS) object.
47  * AS identifiers are unsigned 32 bit integers (RFC 6793).
48  */
49 struct cert_as {
50 	enum cert_as_type type; /* type of AS specification */
51 	union {
52 		uint32_t id; /* singular identifier */
53 		struct cert_as_range range; /* range */
54 	};
55 };
56 
57 /*
58  * AFI values are assigned by IANA.
59  * In rpki-client, we only accept the IPV4 and IPV6 AFI values.
60  */
61 enum afi {
62 	AFI_IPV4 = 1,
63 	AFI_IPV6 = 2
64 };
65 
66 /*
67  * An IP address as parsed from RFC 3779, section 2.2.3.8.
68  * This is either in a certificate or an ROA.
69  * It may either be IPv4 or IPv6.
70  */
71 struct ip_addr {
72 	unsigned char	 addr[16]; /* binary address prefix */
73 	unsigned char	 prefixlen; /* number of valid bits in address */
74 };
75 
76 /*
77  * An IP address (IPv4 or IPv6) range starting at the minimum and making
78  * its way to the maximum.
79  */
80 struct ip_addr_range {
81 	struct ip_addr min; /* minimum ip */
82 	struct ip_addr max; /* maximum ip */
83 };
84 
85 enum cert_ip_type {
86 	CERT_IP_ADDR, /* IP address range w/shared prefix */
87 	CERT_IP_INHERIT, /* inherited IP address */
88 	CERT_IP_RANGE /* range of IP addresses */
89 };
90 
91 /*
92  * A single IP address family (AFI, address or range) as defined in RFC
93  * 3779, 2.2.3.2.
94  * The RFC specifies multiple address or ranges per AFI; this structure
95  * encodes both the AFI and a single address or range.
96  */
97 struct cert_ip {
98 	enum afi		afi; /* AFI value */
99 	enum cert_ip_type	type; /* type of IP entry */
100 	unsigned char		min[16]; /* full range minimum */
101 	unsigned char		max[16]; /* full range maximum */
102 	union {
103 		struct ip_addr ip; /* singular address */
104 		struct ip_addr_range range; /* range */
105 	};
106 };
107 
108 enum cert_purpose {
109 	CERT_PURPOSE_INVALID,
110 	CERT_PURPOSE_TA,
111 	CERT_PURPOSE_CA,
112 	CERT_PURPOSE_EE,
113 	CERT_PURPOSE_BGPSEC_ROUTER,
114 };
115 
116 /*
117  * Parsed components of a validated X509 certificate stipulated by RFC
118  * 6847 and further (within) by RFC 3779.
119  * All AS numbers are guaranteed to be non-overlapping and properly
120  * inheriting.
121  */
122 struct cert {
123 	struct cert_ip	*ips; /* list of IP address ranges */
124 	size_t		 ipsz; /* length of "ips" */
125 	struct cert_as	*as; /* list of AS numbers and ranges */
126 	size_t		 asz; /* length of "asz" */
127 	int		 talid; /* cert is covered by which TAL */
128 	int		 certid;
129 	unsigned int	 repoid; /* repository of this cert file */
130 	char		*repo; /* CA repository (rsync:// uri) */
131 	char		*mft; /* manifest (rsync:// uri) */
132 	char		*notify; /* RRDP notify (https:// uri) */
133 	char		*crl; /* CRL location (rsync:// or NULL) */
134 	char		*aia; /* AIA (or NULL, for trust anchor) */
135 	char		*aki; /* AKI (or NULL, for trust anchor) */
136 	char		*ski; /* SKI */
137 	enum cert_purpose	 purpose; /* BGPSec or CA */
138 	char		*pubkey; /* Subject Public Key Info */
139 	X509		*x509; /* the cert */
140 	time_t		 notbefore; /* cert's Not Before */
141 	time_t		 notafter; /* cert's Not After */
142 	time_t		 expires; /* when the signature path expires */
143 };
144 
145 /*
146  * The TAL file conforms to RFC 7730.
147  * It is the top-level structure of RPKI and defines where we can find
148  * certificates for TAs (trust anchors).
149  * It also includes the public key for verifying those trust anchor
150  * certificates.
151  */
152 struct tal {
153 	char		**uri; /* well-formed rsync URIs */
154 	size_t		 urisz; /* number of URIs */
155 	unsigned char	*pkey; /* DER-encoded public key */
156 	size_t		 pkeysz; /* length of pkey */
157 	char		*descr; /* basename of tal file */
158 	int		 id; /* ID of this TAL */
159 };
160 
161 /*
162  * Resource types specified by the RPKI profiles.
163  * There might be others we don't consider.
164  */
165 enum rtype {
166 	RTYPE_INVALID,
167 	RTYPE_TAL,
168 	RTYPE_MFT,
169 	RTYPE_ROA,
170 	RTYPE_CER,
171 	RTYPE_CRL,
172 	RTYPE_GBR,
173 	RTYPE_REPO,
174 	RTYPE_FILE,
175 	RTYPE_RSC,
176 	RTYPE_ASPA,
177 	RTYPE_TAK,
178 	RTYPE_GEOFEED,
179 	RTYPE_SPL,
180 };
181 
182 enum location {
183 	DIR_UNKNOWN,
184 	DIR_TEMP,
185 	DIR_VALID,
186 };
187 
188 /*
189  * Files specified in an MFT have their bodies hashed with SHA256.
190  */
191 struct mftfile {
192 	char		*file; /* filename (CER/ROA/CRL, no path) */
193 	enum rtype	 type; /* file type as determined by extension */
194 	enum location	 location;	/* temporary or valid directory */
195 	unsigned char	 hash[SHA256_DIGEST_LENGTH]; /* sha256 of body */
196 };
197 
198 /*
199  * A manifest, RFC 6486.
200  * This consists of a bunch of files found in the same directory as the
201  * manifest file.
202  */
203 struct mft {
204 	char		*path; /* relative path to directory of the MFT */
205 	struct mftfile	*files; /* file and hash */
206 	char		*seqnum; /* manifestNumber */
207 	char		*aia; /* AIA */
208 	char		*aki; /* AKI */
209 	char		*sia; /* SIA signedObject */
210 	char		*ski; /* SKI */
211 	char		*crl; /* CRL file name */
212 	unsigned char	 mfthash[SHA256_DIGEST_LENGTH];
213 	unsigned char	 crlhash[SHA256_DIGEST_LENGTH];
214 	time_t		 signtime; /* CMS signing-time attribute */
215 	time_t		 thisupdate; /* from the eContent */
216 	time_t		 nextupdate; /* from the eContent */
217 	time_t		 expires; /* when the signature path expires */
218 	size_t		 filesz; /* number of filenames */
219 	unsigned int	 repoid;
220 	int		 talid;
221 	int		 certid;
222 };
223 
224 /*
225  * An IP address prefix for a given ROA.
226  * This encodes the maximum length, AFI (v6/v4), and address.
227  * FIXME: are the min/max necessary or just used in one place?
228  */
229 struct roa_ip {
230 	enum afi	 afi; /* AFI value */
231 	struct ip_addr	 addr; /* the address prefix itself */
232 	unsigned char	 min[16]; /* full range minimum */
233 	unsigned char	 max[16]; /* full range maximum */
234 	unsigned char	 maxlength; /* max length or zero */
235 };
236 
237 /*
238  * An ROA, RFC 6482.
239  * This consists of the concerned ASID and its IP prefixes.
240  */
241 struct roa {
242 	uint32_t	 asid; /* asID of ROA (if 0, RFC 6483 sec 4) */
243 	struct roa_ip	*ips; /* IP prefixes */
244 	size_t		 ipsz; /* number of IP prefixes */
245 	int		 talid; /* ROAs are covered by which TAL */
246 	int		 valid; /* validated resources */
247 	char		*aia; /* AIA */
248 	char		*aki; /* AKI */
249 	char		*sia; /* SIA signedObject */
250 	char		*ski; /* SKI */
251 	time_t		 signtime; /* CMS signing-time attribute */
252 	time_t		 notbefore; /* EE cert's Not Before */
253 	time_t		 notafter; /* EE cert's Not After */
254 	time_t		 expires; /* when the signature path expires */
255 };
256 
257 struct rscfile {
258 	char		*filename; /* an optional filename on the checklist */
259 	unsigned char	 hash[SHA256_DIGEST_LENGTH]; /* the digest */
260 };
261 
262 /*
263  * A Signed Checklist (RSC)
264  */
265 struct rsc {
266 	int		 talid; /* RSC covered by what TAL */
267 	int		 valid; /* eContent resources covered by EE's 3779? */
268 	struct cert_ip	*ips; /* IP prefixes */
269 	size_t		 ipsz; /* number of IP prefixes */
270 	struct cert_as	*as; /* AS resources */
271 	size_t		 asz; /* number of AS resources */
272 	struct rscfile	*files; /* FileAndHashes in the RSC */
273 	size_t		 filesz; /* number of FileAndHashes */
274 	char		*aia; /* AIA */
275 	char		*aki; /* AKI */
276 	char		*ski; /* SKI */
277 	time_t		 signtime; /* CMS signing-time attribute */
278 	time_t		 notbefore; /* EE cert's Not Before */
279 	time_t		 notafter; /* Not After of the RSC EE */
280 	time_t		 expires; /* when the signature path expires */
281 };
282 
283 /*
284  * An IP address prefix in a given SignedPrefixList.
285  */
286 struct spl_pfx {
287 	enum afi	 afi;
288 	struct ip_addr	 prefix;
289 };
290 
291 /*
292  * An SPL, draft-ietf-sidrops-rpki-prefixlist
293  * This consists of an ASID and its IP prefixes.
294  */
295 struct spl {
296 	uint32_t	 asid;
297 	struct spl_pfx	*pfxs;
298 	size_t		 pfxsz;
299 	int		 talid;
300 	char		*aia;
301 	char		*aki;
302 	char		*sia;
303 	char		*ski;
304 	time_t		 signtime; /* CMS signing-time attribute */
305 	time_t		 notbefore; /* EE cert's Not Before */
306 	time_t		 notafter; /* EE cert's Not After */
307 	time_t		 expires; /* when the certification path expires */
308 	int		 valid;
309 };
310 
311 /*
312  * Datastructure representing the TAKey sequence inside TAKs.
313  */
314 struct takey {
315 	char		**comments; /* Comments */
316 	size_t		 commentsz; /* number of Comments */
317 	char		**uris; /* CertificateURI */
318 	size_t		 urisz; /* number of CertificateURIs */
319 	unsigned char	*pubkey; /* DER encoded SubjectPublicKeyInfo */
320 	size_t		 pubkeysz;
321 	char		*ski; /* hex encoded SubjectKeyIdentifier of pubkey */
322 };
323 
324 /*
325  * A Signed TAL (TAK) draft-ietf-sidrops-signed-tal-12
326  */
327 struct tak {
328 	int		 talid; /* TAK covered by what TAL */
329 	struct takey	*current;
330 	struct takey	*predecessor;
331 	struct takey	*successor;
332 	char		*aia; /* AIA */
333 	char		*aki; /* AKI */
334 	char		*sia; /* SIA signed Object */
335 	char		*ski; /* SKI */
336 	time_t		 signtime; /* CMS signing-time attribute */
337 	time_t		 notbefore; /* EE cert's Not Before */
338 	time_t		 notafter; /* Not After of the TAK EE */
339 	time_t		 expires; /* when the signature path expires */
340 };
341 
342 /*
343  * A single geofeed record
344  */
345 struct geoip {
346 	struct cert_ip	*ip;
347 	char		*loc;
348 };
349 
350 /*
351  * A geofeed file
352  */
353 struct geofeed {
354 	struct geoip	*geoips; /* Prefix + location entry in the CSV */
355 	size_t		 geoipsz; /* number of IPs */
356 	char		*aia; /* AIA */
357 	char		*aki; /* AKI */
358 	char		*ski; /* SKI */
359 	time_t		 signtime; /* CMS signing-time attribute */
360 	time_t		 notbefore; /* EE cert's Not Before */
361 	time_t		 notafter; /* Not After of the Geofeed EE */
362 	time_t		 expires; /* when the signature path expires */
363 	int		 valid; /* all resources covered */
364 };
365 
366 /*
367  * A single Ghostbuster record
368  */
369 struct gbr {
370 	char		*vcard;
371 	char		*aia; /* AIA */
372 	char		*aki; /* AKI */
373 	char		*sia; /* SIA signedObject */
374 	char		*ski; /* SKI */
375 	time_t		 signtime; /* CMS signing-time attribute */
376 	time_t		 notbefore; /* EE cert's Not Before */
377 	time_t		 notafter; /* Not After of the GBR EE */
378 	time_t		 expires; /* when the signature path expires */
379 	int		 talid; /* TAL the GBR is chained up to */
380 };
381 
382 /*
383  * A single ASPA record
384  */
385 struct aspa {
386 	int			 valid; /* contained in issuer auth */
387 	int			 talid; /* TAL the ASPA is chained up to */
388 	char			*aia; /* AIA */
389 	char			*aki; /* AKI */
390 	char			*sia; /* SIA signedObject */
391 	char			*ski; /* SKI */
392 	uint32_t		 custasid; /* the customerASID */
393 	uint32_t		*providers; /* the providers */
394 	size_t			 providersz; /* number of providers */
395 	time_t			 signtime; /* CMS signing-time attribute */
396 	time_t			 notbefore; /* EE cert's Not Before */
397 	time_t			 notafter; /* notAfter of the ASPA EE cert */
398 	time_t			 expires; /* when the signature path expires */
399 };
400 
401 /*
402  * A Validated ASPA Payload (VAP) tree element.
403  * To ease transformation, this struct mimics ASPA RTR PDU structure.
404  */
405 struct vap {
406 	RB_ENTRY(vap)		 entry;
407 	uint32_t		 custasid;
408 	uint32_t		*providers;
409 	size_t			 providersz;
410 	time_t			 expires;
411 	int			 talid;
412 	unsigned int		 repoid;
413 	int			 overflowed;
414 };
415 
416 /*
417  * Tree of VAPs sorted by afi, custasid, and provideras.
418  */
419 RB_HEAD(vap_tree, vap);
420 RB_PROTOTYPE(vap_tree, vap, entry, vapcmp);
421 
422 /*
423  * A single VRP element (including ASID)
424  */
425 struct vrp {
426 	RB_ENTRY(vrp)	entry;
427 	struct ip_addr	addr;
428 	uint32_t	asid;
429 	enum afi	afi;
430 	unsigned char	maxlength;
431 	time_t		expires; /* transitive expiry moment */
432 	int		talid; /* covered by which TAL */
433 	unsigned int	repoid;
434 };
435 /*
436  * Tree of VRP sorted by afi, addr, maxlength and asid
437  */
438 RB_HEAD(vrp_tree, vrp);
439 RB_PROTOTYPE(vrp_tree, vrp, entry, vrpcmp);
440 
441 /*
442  * Validated SignedPrefixList Payload
443  * A single VSP element (including ASID)
444  * draft-ietf-sidrops-rpki-prefixlist
445  */
446 struct vsp {
447 	RB_ENTRY(vsp)	 entry;
448 	uint32_t	 asid;
449 	struct spl_pfx	*prefixes;
450 	size_t		 prefixesz;
451 	time_t		 expires;
452 	int		 talid;
453 	unsigned int	 repoid;
454 };
455 /*
456  * Tree of VSP sorted by asid
457  */
458 RB_HEAD(vsp_tree, vsp);
459 RB_PROTOTYPE(vsp_tree, vsp, entry, vspcmp);
460 
461 /*
462  * A single BGPsec Router Key (including ASID)
463  */
464 struct brk {
465 	RB_ENTRY(brk)	 entry;
466 	uint32_t	 asid;
467 	int		 talid; /* covered by which TAL */
468 	char		*ski; /* Subject Key Identifier */
469 	char		*pubkey; /* Subject Public Key Info */
470 	time_t		 expires; /* transitive expiry moment */
471 };
472 /*
473  * Tree of BRK sorted by asid
474  */
475 RB_HEAD(brk_tree, brk);
476 RB_PROTOTYPE(brk_tree, brk, entry, brkcmp);
477 
478 /*
479  * A single CRL
480  */
481 struct crl {
482 	RB_ENTRY(crl)	 entry;
483 	char		*aki;
484 	char		*mftpath;
485 	X509_CRL	*x509_crl;
486 	time_t		 thisupdate;	/* do not use before */
487 	time_t		 nextupdate;	/* do not use after */
488 };
489 /*
490  * Tree of CRLs sorted by uri
491  */
492 RB_HEAD(crl_tree, crl);
493 
494 /*
495  * An authentication tuple.
496  * This specifies a public key and a subject key identifier used to
497  * verify children nodes in the tree of entities.
498  */
499 struct auth {
500 	RB_ENTRY(auth)	 entry;
501 	struct cert	*cert; /* owner information */
502 	struct auth	*issuer; /* pointer to issuer or NULL for TA cert */
503 	int		 any_inherits;
504 	int		 depth;
505 };
506 /*
507  * Tree of auth sorted by ski
508  */
509 RB_HEAD(auth_tree, auth);
510 
511 struct auth	*auth_find(struct auth_tree *, int);
512 struct auth	*auth_insert(const char *, struct auth_tree *, struct cert *,
513 		    struct auth *);
514 
515 enum http_result {
516 	HTTP_FAILED,	/* anything else */
517 	HTTP_OK,	/* 200 OK */
518 	HTTP_NOT_MOD,	/* 304 Not Modified */
519 };
520 
521 /*
522  * Message types for communication with RRDP process.
523  */
524 enum rrdp_msg {
525 	RRDP_START,
526 	RRDP_SESSION,
527 	RRDP_FILE,
528 	RRDP_CLEAR,
529 	RRDP_END,
530 	RRDP_HTTP_REQ,
531 	RRDP_HTTP_INI,
532 	RRDP_HTTP_FIN,
533 	RRDP_ABORT,
534 };
535 
536 /* Maximum number of delta files per RRDP notification file. */
537 #define MAX_RRDP_DELTAS		300
538 
539 /*
540  * RRDP session state, needed to pickup at the right spot on next run.
541  */
542 struct rrdp_session {
543 	char			*last_mod;
544 	char			*session_id;
545 	long long		 serial;
546 	char			*deltas[MAX_RRDP_DELTAS];
547 };
548 
549 /*
550  * File types used in RRDP_FILE messages.
551  */
552 enum publish_type {
553 	PUB_ADD,
554 	PUB_UPD,
555 	PUB_DEL,
556 };
557 
558 /*
559  * An entity (MFT, ROA, certificate, etc.) that needs to be downloaded
560  * and parsed.
561  */
562 struct entity {
563 	TAILQ_ENTRY(entity) entries;
564 	char		*path;		/* path relative to repository */
565 	char		*file;		/* filename or valid repo path */
566 	char		*mftaki;	/* expected AKI (taken from Manifest) */
567 	unsigned char	*data;		/* optional data blob */
568 	size_t		 datasz;	/* length of optional data blob */
569 	unsigned int	 repoid;	/* repository identifier */
570 	int		 talid;		/* tal identifier */
571 	int		 certid;
572 	enum rtype	 type;		/* type of entity (not RTYPE_EOF) */
573 	enum location	 location;	/* which directory the file lives in */
574 };
575 TAILQ_HEAD(entityq, entity);
576 
577 enum stype {
578 	STYPE_OK,
579 	STYPE_FAIL,
580 	STYPE_INVALID,
581 	STYPE_BGPSEC,
582 	STYPE_TOTAL,
583 	STYPE_UNIQUE,
584 	STYPE_DEC_UNIQUE,
585 	STYPE_PROVIDERS,
586 	STYPE_OVERFLOW,
587 };
588 
589 struct repo;
590 struct filepath;
591 RB_HEAD(filepath_tree, filepath);
592 
593 
594 /*
595  * Statistics collected during run-time.
596  */
597 struct repotalstats {
598 	uint32_t	 certs; /* certificates */
599 	uint32_t	 certs_fail; /* invalid certificate */
600 	uint32_t	 mfts; /* total number of manifests */
601 	uint32_t	 mfts_fail; /* failing syntactic parse */
602 	uint32_t	 roas; /* route origin authorizations */
603 	uint32_t	 roas_fail; /* failing syntactic parse */
604 	uint32_t	 roas_invalid; /* invalid resources */
605 	uint32_t	 aspas; /* ASPA objects */
606 	uint32_t	 aspas_fail; /* ASPA objects failing syntactic parse */
607 	uint32_t	 aspas_invalid; /* ASPAs with invalid customerASID */
608 	uint32_t	 brks; /* number of BGPsec Router Key (BRK) certs */
609 	uint32_t	 crls; /* revocation lists */
610 	uint32_t	 gbrs; /* ghostbuster records */
611 	uint32_t	 taks; /* signed TAL objects */
612 	uint32_t	 vaps; /* total number of Validated ASPA Payloads */
613 	uint32_t	 vaps_uniqs; /* total number of unique VAPs */
614 	uint32_t	 vaps_pas; /* total number of providers */
615 	uint32_t	 vaps_overflowed; /* VAPs with too many providers */
616 	uint32_t	 vrps; /* total number of Validated ROA Payloads */
617 	uint32_t	 vrps_uniqs; /* number of unique vrps */
618 	uint32_t	 spls; /* signed prefix list */
619 	uint32_t	 spls_fail; /* failing syntactic parse */
620 	uint32_t	 spls_invalid; /* invalid spls */
621 	uint32_t	 vsps; /* total number of Validated SPL Payloads */
622 	uint32_t	 vsps_uniqs; /* number of unique vsps */
623 };
624 
625 struct repostats {
626 	uint32_t	 del_files;	/* number of files removed in cleanup */
627 	uint32_t	 extra_files;	/* number of superfluous files */
628 	uint32_t	 del_extra_files;/* number of removed extra files */
629 	uint32_t	 del_dirs;	/* number of dirs removed in cleanup */
630 	uint32_t	 new_files;	/* moved from DIR_TEMP to DIR_VALID */
631 	struct timespec	 sync_time;	/* time to sync repo */
632 };
633 
634 struct stats {
635 	uint32_t	 tals; /* total number of locators */
636 	uint32_t	 repos; /* repositories */
637 	uint32_t	 rsync_repos; /* synced rsync repositories */
638 	uint32_t	 rsync_fails; /* failed rsync repositories */
639 	uint32_t	 http_repos; /* synced http repositories */
640 	uint32_t	 http_fails; /* failed http repositories */
641 	uint32_t	 rrdp_repos; /* synced rrdp repositories */
642 	uint32_t	 rrdp_fails; /* failed rrdp repositories */
643 	uint32_t	 skiplistentries; /* number of skiplist entries */
644 
645 	struct repotalstats	repo_tal_stats;
646 	struct repostats	repo_stats;
647 	struct timespec		elapsed_time;
648 	struct timespec		user_time;
649 	struct timespec		system_time;
650 };
651 
652 struct ibuf;
653 struct msgbuf;
654 
655 /* global variables */
656 extern int verbose;
657 extern int noop;
658 extern int filemode;
659 extern int excludeaspa;
660 extern int experimental;
661 extern const char *tals[];
662 extern const char *taldescs[];
663 extern unsigned int talrepocnt[];
664 extern struct repotalstats talstats[];
665 extern int talsz;
666 
667 /* Routines for RPKI entities. */
668 
669 void		 tal_buffer(struct ibuf *, const struct tal *);
670 void		 tal_free(struct tal *);
671 struct tal	*tal_parse(const char *, char *, size_t);
672 struct tal	*tal_read(struct ibuf *);
673 
674 void		 cert_buffer(struct ibuf *, const struct cert *);
675 void		 cert_free(struct cert *);
676 void		 auth_tree_free(struct auth_tree *);
677 struct cert	*cert_parse_ee_cert(const char *, int, X509 *);
678 struct cert	*cert_parse_pre(const char *, const unsigned char *, size_t);
679 struct cert	*cert_parse(const char *, struct cert *);
680 struct cert	*ta_parse(const char *, struct cert *, const unsigned char *,
681 		    size_t);
682 struct cert	*cert_read(struct ibuf *);
683 void		 cert_insert_brks(struct brk_tree *, struct cert *);
684 
685 enum rtype	 rtype_from_file_extension(const char *);
686 void		 mft_buffer(struct ibuf *, const struct mft *);
687 void		 mft_free(struct mft *);
688 struct mft	*mft_parse(X509 **, const char *, int, const unsigned char *,
689 		    size_t);
690 struct mft	*mft_read(struct ibuf *);
691 int		 mft_compare_issued(const struct mft *, const struct mft *);
692 int		 mft_compare_seqnum(const struct mft *, const struct mft *);
693 
694 void		 roa_buffer(struct ibuf *, const struct roa *);
695 void		 roa_free(struct roa *);
696 struct roa	*roa_parse(X509 **, const char *, int, const unsigned char *,
697 		    size_t);
698 struct roa	*roa_read(struct ibuf *);
699 void		 roa_insert_vrps(struct vrp_tree *, struct roa *,
700 		    struct repo *);
701 
702 void		 spl_buffer(struct ibuf *, const struct spl *);
703 void		 spl_free(struct spl *);
704 struct spl	*spl_parse(X509 **, const char *, int, const unsigned char *,
705 		    size_t);
706 struct spl	*spl_read(struct ibuf *);
707 void		 spl_insert_vsps(struct vsp_tree *, struct spl *,
708 		    struct repo *);
709 
710 void		 gbr_free(struct gbr *);
711 struct gbr	*gbr_parse(X509 **, const char *, int, const unsigned char *,
712 		    size_t);
713 
714 void		 geofeed_free(struct geofeed *);
715 struct geofeed	*geofeed_parse(X509 **, const char *, int, char *, size_t);
716 
717 void		 rsc_free(struct rsc *);
718 struct rsc	*rsc_parse(X509 **, const char *, int, const unsigned char *,
719 		    size_t);
720 
721 void		 takey_free(struct takey *);
722 void		 tak_free(struct tak *);
723 struct tak	*tak_parse(X509 **, const char *, int, const unsigned char *,
724 		    size_t);
725 
726 void		 aspa_buffer(struct ibuf *, const struct aspa *);
727 void		 aspa_free(struct aspa *);
728 void		 aspa_insert_vaps(char *, struct vap_tree *, struct aspa *,
729 		    struct repo *);
730 struct aspa	*aspa_parse(X509 **, const char *, int, const unsigned char *,
731 		    size_t);
732 struct aspa	*aspa_read(struct ibuf *);
733 
734 /* crl.c */
735 struct crl	*crl_parse(const char *, const unsigned char *, size_t);
736 struct crl	*crl_get(struct crl_tree *, const struct auth *);
737 int		 crl_insert(struct crl_tree *, struct crl *);
738 void		 crl_free(struct crl *);
739 void		 crl_tree_free(struct crl_tree *);
740 
741 /* Validation of our objects. */
742 
743 int		 valid_cert(const char *, struct auth *, const struct cert *);
744 int		 valid_roa(const char *, struct cert *, struct roa *);
745 int		 valid_filehash(int, const char *, size_t);
746 int		 valid_hash(unsigned char *, size_t, const char *, size_t);
747 int		 valid_filename(const char *, size_t);
748 int		 valid_uri(const char *, size_t, const char *);
749 int		 valid_origin(const char *, const char *);
750 int		 valid_x509(char *, X509_STORE_CTX *, X509 *, struct auth *,
751 		    struct crl *, const char **);
752 int		 valid_rsc(const char *, struct cert *, struct rsc *);
753 int		 valid_econtent_version(const char *, const ASN1_INTEGER *,
754 		    uint64_t);
755 int		 valid_aspa(const char *, struct cert *, struct aspa *);
756 int		 valid_geofeed(const char *, struct cert *, struct geofeed *);
757 int		 valid_uuid(const char *);
758 int		 valid_ca_pkey(const char *, EVP_PKEY *);
759 int		 valid_spl(const char *, struct cert *, struct spl *);
760 
761 /* Working with CMS. */
762 unsigned char	*cms_parse_validate(X509 **, const char *,
763 		    const unsigned char *, size_t,
764 		    const ASN1_OBJECT *, size_t *, time_t *);
765 int		 cms_parse_validate_detached(X509 **, const char *,
766 		    const unsigned char *, size_t,
767 		    const ASN1_OBJECT *, BIO *, time_t *);
768 
769 /* Work with RFC 3779 IP addresses, prefixes, ranges. */
770 
771 int		 ip_addr_afi_parse(const char *, const ASN1_OCTET_STRING *,
772 		    enum afi *);
773 int		 ip_addr_parse(const ASN1_BIT_STRING *,
774 		    enum afi, const char *, struct ip_addr *);
775 void		 ip_addr_print(const struct ip_addr *, enum afi, char *,
776 		    size_t);
777 int		 ip_addr_check_overlap(const struct cert_ip *,
778 		    const char *, const struct cert_ip *, size_t, int);
779 int		 ip_addr_check_covered(enum afi, const unsigned char *,
780 		    const unsigned char *, const struct cert_ip *, size_t);
781 int		 ip_cert_compose_ranges(struct cert_ip *);
782 void		 ip_roa_compose_ranges(struct roa_ip *);
783 void		 ip_warn(const char *, const char *, const struct cert_ip *);
784 
785 int		 sbgp_addr(const char *, struct cert_ip *, size_t *,
786 		    enum afi, const ASN1_BIT_STRING *);
787 int		 sbgp_addr_range(const char *, struct cert_ip *, size_t *,
788 		    enum afi, const IPAddressRange *);
789 
790 int		 sbgp_parse_ipaddrblk(const char *, const IPAddrBlocks *,
791 		    struct cert_ip **, size_t *);
792 
793 /* Work with RFC 3779 AS numbers, ranges. */
794 
795 int		 as_id_parse(const ASN1_INTEGER *, uint32_t *);
796 int		 as_check_overlap(const struct cert_as *, const char *,
797 		    const struct cert_as *, size_t, int);
798 int		 as_check_covered(uint32_t, uint32_t,
799 		    const struct cert_as *, size_t);
800 void		 as_warn(const char *, const char *, const struct cert_as *);
801 
802 int		 sbgp_as_id(const char *, struct cert_as *, size_t *,
803 		    const ASN1_INTEGER *);
804 int		 sbgp_as_range(const char *, struct cert_as *, size_t *,
805 		    const ASRange *);
806 
807 int		 sbgp_parse_assysnum(const char *, const ASIdentifiers *,
808 		    struct cert_as **, size_t *);
809 
810 /* Constraints-specific */
811 void		 constraints_load(void);
812 void		 constraints_unload(void);
813 void		 constraints_parse(void);
814 int		 constraints_validate(const char *, const struct cert *);
815 
816 /* Parser-specific */
817 void		 entity_free(struct entity *);
818 void		 entity_read_req(struct ibuf *, struct entity *);
819 void		 entityq_flush(struct entityq *, struct repo *);
820 void		 proc_parser(int) __attribute__((noreturn));
821 void		 proc_filemode(int) __attribute__((noreturn));
822 
823 /* Rsync-specific. */
824 
825 char		*rsync_base_uri(const char *);
826 void		 proc_rsync(char *, char *, int) __attribute__((noreturn));
827 
828 /* HTTP and RRDP processes. */
829 
830 void		 proc_http(char *, int) __attribute__((noreturn));
831 void		 proc_rrdp(int) __attribute__((noreturn));
832 
833 /* Repository handling */
834 int		 filepath_add(struct filepath_tree *, char *, int, time_t);
835 void		 rrdp_clear(unsigned int);
836 void		 rrdp_session_save(unsigned int, struct rrdp_session *);
837 void		 rrdp_session_free(struct rrdp_session *);
838 void		 rrdp_session_buffer(struct ibuf *,
839 		    const struct rrdp_session *);
840 struct rrdp_session	*rrdp_session_read(struct ibuf *);
841 int		 rrdp_handle_file(unsigned int, enum publish_type, char *,
842 		    char *, size_t, char *, size_t);
843 char		*repo_basedir(const struct repo *, int);
844 unsigned int	 repo_id(const struct repo *);
845 const char	*repo_uri(const struct repo *);
846 void		 repo_fetch_uris(const struct repo *, const char **,
847 		    const char **);
848 int		 repo_synced(const struct repo *);
849 const char	*repo_proto(const struct repo *);
850 int		 repo_talid(const struct repo *);
851 struct repo	*ta_lookup(int, struct tal *);
852 struct repo	*repo_lookup(int, const char *, const char *);
853 struct repo	*repo_byid(unsigned int);
854 int		 repo_queued(struct repo *, struct entity *);
855 void		 repo_cleanup(struct filepath_tree *, int);
856 int		 repo_check_timeout(int);
857 void		 repostats_new_files_inc(struct repo *, const char *);
858 void		 repo_stat_inc(struct repo *, int, enum rtype, enum stype);
859 void		 repo_tal_stats_collect(void (*)(const struct repo *,
860 		    const struct repotalstats *, void *), int, void *);
861 void		 repo_stats_collect(void (*)(const struct repo *,
862 		    const struct repostats *, void *), void *);
863 void		 repo_free(void);
864 
865 void		 rsync_finish(unsigned int, int);
866 void		 http_finish(unsigned int, enum http_result, const char *);
867 void		 rrdp_finish(unsigned int, int);
868 
869 void		 rsync_fetch(unsigned int, const char *, const char *,
870 		    const char *);
871 void		 rsync_abort(unsigned int);
872 void		 http_fetch(unsigned int, const char *, const char *, int);
873 void		 rrdp_fetch(unsigned int, const char *, const char *,
874 		    struct rrdp_session *);
875 void		 rrdp_abort(unsigned int);
876 void		 rrdp_http_done(unsigned int, enum http_result, const char *);
877 
878 /* Encoding functions for hex and base64. */
879 
880 unsigned char	*load_file(const char *, size_t *);
881 int		 base64_decode_len(size_t, size_t *);
882 int		 base64_decode(const unsigned char *, size_t,
883 		    unsigned char **, size_t *);
884 int		 base64_encode_len(size_t, size_t *);
885 int		 base64_encode(const unsigned char *, size_t, char **);
886 char		*hex_encode(const unsigned char *, size_t);
887 int		 hex_decode(const char *, char *, size_t);
888 
889 
890 /* Functions for moving data between processes. */
891 
892 struct ibuf	*io_new_buffer(void);
893 void		 io_simple_buffer(struct ibuf *, const void *, size_t);
894 void		 io_buf_buffer(struct ibuf *, const void *, size_t);
895 void		 io_str_buffer(struct ibuf *, const char *);
896 void		 io_close_buffer(struct msgbuf *, struct ibuf *);
897 void		 io_read_buf(struct ibuf *, void *, size_t);
898 void		 io_read_str(struct ibuf *, char **);
899 void		 io_read_buf_alloc(struct ibuf *, void **, size_t *);
900 struct ibuf	*io_buf_read(int, struct ibuf **);
901 struct ibuf	*io_buf_recvfd(int, struct ibuf **);
902 
903 /* X509 helpers. */
904 
905 void		 x509_init_oid(void);
906 int		 x509_cache_extensions(X509 *, const char *);
907 int		 x509_get_aia(X509 *, const char *, char **);
908 int		 x509_get_aki(X509 *, const char *, char **);
909 int		 x509_get_sia(X509 *, const char *, char **);
910 int		 x509_get_ski(X509 *, const char *, char **);
911 int		 x509_get_notbefore(X509 *, const char *, time_t *);
912 int		 x509_get_notafter(X509 *, const char *, time_t *);
913 int		 x509_get_crl(X509 *, const char *, char **);
914 char		*x509_get_pubkey(X509 *, const char *);
915 char		*x509_pubkey_get_ski(X509_PUBKEY *, const char *);
916 enum cert_purpose	 x509_get_purpose(X509 *, const char *);
917 int		 x509_get_time(const ASN1_TIME *, time_t *);
918 char		*x509_convert_seqnum(const char *, const ASN1_INTEGER *);
919 int		 x509_location(const char *, const char *, GENERAL_NAME *,
920 		    char **);
921 int		 x509_inherits(X509 *);
922 int		 x509_any_inherits(X509 *);
923 int		 x509_valid_name(const char *, const char *, const X509_NAME *);
924 time_t		 x509_find_expires(time_t, struct auth *, struct crl_tree *);
925 
926 /* printers */
927 char		*nid2str(int);
928 const char	*purpose2str(enum cert_purpose);
929 char		*time2str(time_t);
930 void		 x509_print(const X509 *);
931 void		 tal_print(const struct tal *);
932 void		 cert_print(const struct cert *);
933 void		 crl_print(const struct crl *);
934 void		 mft_print(const X509 *, const struct mft *);
935 void		 roa_print(const X509 *, const struct roa *);
936 void		 gbr_print(const X509 *, const struct gbr *);
937 void		 rsc_print(const X509 *, const struct rsc *);
938 void		 aspa_print(const X509 *, const struct aspa *);
939 void		 tak_print(const X509 *, const struct tak *);
940 void		 geofeed_print(const X509 *, const struct geofeed *);
941 void		 spl_print(const X509 *, const struct spl *);
942 
943 /* Missing RFC 3779 API */
944 IPAddrBlocks *IPAddrBlocks_new(void);
945 void IPAddrBlocks_free(IPAddrBlocks *);
946 
947 /* Output! */
948 
949 extern int	 outformats;
950 #define FORMAT_OPENBGPD	0x01
951 #define FORMAT_BIRD	0x02
952 #define FORMAT_CSV	0x04
953 #define FORMAT_JSON	0x08
954 #define FORMAT_OMETRIC	0x10
955 
956 int		 outputfiles(struct vrp_tree *v, struct brk_tree *b,
957 		    struct vap_tree *, struct vsp_tree *, struct stats *);
958 int		 outputheader(FILE *, struct stats *);
959 int		 output_bgpd(FILE *, struct vrp_tree *, struct brk_tree *,
960 		    struct vap_tree *, struct vsp_tree *, struct stats *);
961 int		 output_bird1v4(FILE *, struct vrp_tree *, struct brk_tree *,
962 		    struct vap_tree *, struct vsp_tree *, struct stats *);
963 int		 output_bird1v6(FILE *, struct vrp_tree *, struct brk_tree *,
964 		    struct vap_tree *, struct vsp_tree *, struct stats *);
965 int		 output_bird2(FILE *, struct vrp_tree *, struct brk_tree *,
966 		    struct vap_tree *, struct vsp_tree *, struct stats *);
967 int		 output_csv(FILE *, struct vrp_tree *, struct brk_tree *,
968 		    struct vap_tree *, struct vsp_tree *, struct stats *);
969 int		 output_json(FILE *, struct vrp_tree *, struct brk_tree *,
970 		    struct vap_tree *, struct vsp_tree *, struct stats *);
971 int		 output_ometric(FILE *, struct vrp_tree *, struct brk_tree *,
972 		    struct vap_tree *, struct vsp_tree *, struct stats *);
973 
974 void		 logx(const char *fmt, ...)
975 		    __attribute__((format(printf, 1, 2)));
976 time_t		 getmonotime(void);
977 time_t		 get_current_time(void);
978 
979 int	mkpath(const char *);
980 int	mkpathat(int, const char *);
981 
982 #define RPKI_PATH_OUT_DIR	"/var/db/rpki-client"
983 #define RPKI_PATH_BASE_DIR	"/var/cache/rpki-client"
984 
985 #define DEFAULT_SKIPLIST_FILE	"/etc/rpki/skiplist"
986 
987 /* Maximum number of TAL files we'll load. */
988 #define	TALSZ_MAX		8
989 #define	CERTID_MAX		1000000
990 
991 /*
992  * Maximum number of elements in the sbgp-ipAddrBlock (IP) and
993  * sbgp-autonomousSysNum (AS) X.509v3 extension of CA/EE certificates.
994  */
995 #define MAX_IP_SIZE		200000
996 #define MAX_AS_SIZE		200000
997 
998 /* Maximum acceptable URI length */
999 #define MAX_URI_LENGTH		2048
1000 
1001 /* Min/Max acceptable file size */
1002 #define MIN_FILE_SIZE		100
1003 #define MAX_FILE_SIZE		4000000
1004 
1005 /* Maximum number of FileNameAndHash entries per RSC checklist. */
1006 #define MAX_CHECKLIST_ENTRIES	100000
1007 
1008 /* Maximum number of FileAndHash entries per manifest. */
1009 #define MAX_MANIFEST_ENTRIES	100000
1010 
1011 /* Maximum number of Providers per ASPA object. */
1012 #define MAX_ASPA_PROVIDERS	10000
1013 
1014 /* Maximum depth of the RPKI tree. */
1015 #define MAX_CERT_DEPTH		12
1016 
1017 /* Maximum number of concurrent http and rsync requests. */
1018 #define MAX_HTTP_REQUESTS	64
1019 #define MAX_RSYNC_REQUESTS	16
1020 
1021 /* How many seconds to wait for a connection to succeed. */
1022 #define MAX_CONN_TIMEOUT	15
1023 
1024 /* How many seconds to wait for IO from a remote server. */
1025 #define MAX_IO_TIMEOUT		30
1026 
1027 /* Maximum number of delegated hosting locations (repositories) for each TAL. */
1028 #define MAX_REPO_PER_TAL	1000
1029 
1030 #define HTTP_PROTO		"http://"
1031 #define HTTP_PROTO_LEN		(sizeof(HTTP_PROTO) - 1)
1032 #define HTTPS_PROTO		"https://"
1033 #define HTTPS_PROTO_LEN		(sizeof(HTTPS_PROTO) - 1)
1034 #define RSYNC_PROTO		"rsync://"
1035 #define RSYNC_PROTO_LEN		(sizeof(RSYNC_PROTO) - 1)
1036 
1037 #endif /* ! EXTERN_H */
1038