xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/mdnsd/dnssec.h (revision 5ffb0c9b03b5149ff4f5821a62be4a52408ada2a)
1*5ffb0c9bSToomas Soome /* -*- Mode: C; tab-width: 4 -*-
2*5ffb0c9bSToomas Soome  *
3*5ffb0c9bSToomas Soome  * Copyright (c) 2011 Apple Computer, Inc. All rights reserved.
4*5ffb0c9bSToomas Soome  *
5*5ffb0c9bSToomas Soome  * Licensed under the Apache License, Version 2.0 (the "License");
6*5ffb0c9bSToomas Soome  * you may not use this file except in compliance with the License.
7*5ffb0c9bSToomas Soome  * You may obtain a copy of the License at
8*5ffb0c9bSToomas Soome  *
9*5ffb0c9bSToomas Soome  *     http://www.apache.org/licenses/LICENSE-2.0
10*5ffb0c9bSToomas Soome  *
11*5ffb0c9bSToomas Soome  * Unless required by applicable law or agreed to in writing, software
12*5ffb0c9bSToomas Soome  * distributed under the License is distributed on an "AS IS" BASIS,
13*5ffb0c9bSToomas Soome  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*5ffb0c9bSToomas Soome  * See the License for the specific language governing permissions and
15*5ffb0c9bSToomas Soome  * limitations under the License.
16*5ffb0c9bSToomas Soome  */
17*5ffb0c9bSToomas Soome #ifndef __DNSSEC_H
18*5ffb0c9bSToomas Soome #define __DNSSEC_H
19*5ffb0c9bSToomas Soome 
20*5ffb0c9bSToomas Soome #include "CryptoAlg.h"
21*5ffb0c9bSToomas Soome #include "mDNSDebug.h"
22*5ffb0c9bSToomas Soome 
23*5ffb0c9bSToomas Soome typedef enum
24*5ffb0c9bSToomas Soome {
25*5ffb0c9bSToomas Soome     RRVS_rr, RRVS_rrsig, RRVS_key, RRVS_rrsig_key, RRVS_ds, RRVS_done,
26*5ffb0c9bSToomas Soome } RRVerifierSet;
27*5ffb0c9bSToomas Soome 
28*5ffb0c9bSToomas Soome typedef struct RRVerifier_struct RRVerifier;
29*5ffb0c9bSToomas Soome typedef struct DNSSECVerifier_struct DNSSECVerifier;
30*5ffb0c9bSToomas Soome typedef struct AuthChain_struct AuthChain;
31*5ffb0c9bSToomas Soome typedef struct InsecureContext_struct InsecureContext;
32*5ffb0c9bSToomas Soome 
33*5ffb0c9bSToomas Soome struct RRVerifier_struct
34*5ffb0c9bSToomas Soome {
35*5ffb0c9bSToomas Soome     RRVerifier *next;
36*5ffb0c9bSToomas Soome     mDNSu16 rrtype;
37*5ffb0c9bSToomas Soome     mDNSu16 rrclass;
38*5ffb0c9bSToomas Soome     mDNSu32 rroriginalttl;
39*5ffb0c9bSToomas Soome     mDNSu16 rdlength;
40*5ffb0c9bSToomas Soome     mDNSu16 found;
41*5ffb0c9bSToomas Soome     mDNSu32 namehash;
42*5ffb0c9bSToomas Soome     mDNSu32 rdatahash;
43*5ffb0c9bSToomas Soome     domainname name;
44*5ffb0c9bSToomas Soome     mDNSu8  *rdata;
45*5ffb0c9bSToomas Soome };
46*5ffb0c9bSToomas Soome 
47*5ffb0c9bSToomas Soome // Each AuthChain element has one rrset (with multiple resource records of same type), rrsig and key
48*5ffb0c9bSToomas Soome // that validates the rrset.
49*5ffb0c9bSToomas Soome struct AuthChain_struct
50*5ffb0c9bSToomas Soome {
51*5ffb0c9bSToomas Soome 	AuthChain  *next;		// Next element in the chain
52*5ffb0c9bSToomas Soome 	RRVerifier *rrset;		// RRSET that is authenticated
53*5ffb0c9bSToomas Soome 	RRVerifier *rrsig;		// Signature for that RRSET
54*5ffb0c9bSToomas Soome 	RRVerifier *key;		// Public key for that RRSET
55*5ffb0c9bSToomas Soome };
56*5ffb0c9bSToomas Soome 
57*5ffb0c9bSToomas Soome #define ResetAuthChain(dv) {    \
58*5ffb0c9bSToomas Soome     (dv)->ac = mDNSNULL;        \
59*5ffb0c9bSToomas Soome     (dv)->actail = &((dv)->ac); \
60*5ffb0c9bSToomas Soome }
61*5ffb0c9bSToomas Soome 
62*5ffb0c9bSToomas Soome typedef void DNSSECVerifierCallback (mDNS *const m, DNSSECVerifier *dv, DNSSECStatus status);
63*5ffb0c9bSToomas Soome //
64*5ffb0c9bSToomas Soome // When we do a validation for a question, there might be additional validations that needs to be done e.g.,
65*5ffb0c9bSToomas Soome // wildcard expanded answer. It is also possible that in the case of nsec we need to prove both that a wildcard
66*5ffb0c9bSToomas Soome // does not apply and the closest encloser proves that name does not exist. We identify these with the following
67*5ffb0c9bSToomas Soome // flags.
68*5ffb0c9bSToomas Soome //
69*5ffb0c9bSToomas Soome // Note: In the following, by "marking the validation", we mean that as part of validation we need to prove
70*5ffb0c9bSToomas Soome // the ones that are marked with.
71*5ffb0c9bSToomas Soome //
72*5ffb0c9bSToomas Soome // A wildcard may be used to answer a question. In that case, we need to verify that the right wildcard was
73*5ffb0c9bSToomas Soome // used in answering the question. This is done by marking the validation with WILDCARD_PROVES_ANSWER_EXPANDED.
74*5ffb0c9bSToomas Soome //
75*5ffb0c9bSToomas Soome // Sometimes we get a NXDOMAIN response. In this case, we may have a wildcard where we need to prove
76*5ffb0c9bSToomas Soome // that the wildcard proves that the name does not exist. This is done by marking the validation with
77*5ffb0c9bSToomas Soome // WILDCARD_PROVES_NONAME_EXISTS.
78*5ffb0c9bSToomas Soome //
79*5ffb0c9bSToomas Soome // In the case of NODATA error, sometimes the name may exist but the query type does not exist. This is done by
80*5ffb0c9bSToomas Soome // marking the validation with NSEC_PROVES_NOTYPE_EXISTS.
81*5ffb0c9bSToomas Soome //
82*5ffb0c9bSToomas Soome // In both NXDOMAIN and NODATA proofs, we may have to prove that the NAME does not exist. This is done by marking
83*5ffb0c9bSToomas Soome // the validation with NSEC_PROVES_NONAME_EXISTS.
84*5ffb0c9bSToomas Soome //
85*5ffb0c9bSToomas Soome #define WILDCARD_PROVES_ANSWER_EXPANDED 0x00000001
86*5ffb0c9bSToomas Soome #define WILDCARD_PROVES_NONAME_EXISTS   0x00000002
87*5ffb0c9bSToomas Soome #define NSEC_PROVES_NOTYPE_EXISTS       0x00000004
88*5ffb0c9bSToomas Soome #define NSEC_PROVES_NONAME_EXISTS       0x00000008
89*5ffb0c9bSToomas Soome #define NSEC3_OPT_OUT					0x00000010 // OptOut was set in NSEC3
90*5ffb0c9bSToomas Soome 
91*5ffb0c9bSToomas Soome struct DNSSECVerifier_struct
92*5ffb0c9bSToomas Soome {
93*5ffb0c9bSToomas Soome     domainname origName;            // Original question name that needs verification
94*5ffb0c9bSToomas Soome     mDNSu16 origType;               // Original question type corresponding to origName
95*5ffb0c9bSToomas Soome     mDNSu16 currQtype;              // Current question type that is being verified
96*5ffb0c9bSToomas Soome     mDNSInterfaceID InterfaceID;    // InterfaceID of the question
97*5ffb0c9bSToomas Soome     DNSQuestion q;
98*5ffb0c9bSToomas Soome     mDNSu8 recursed;                // Number of times recursed during validation
99*5ffb0c9bSToomas Soome     mDNSu8 ValidationRequired;      // Copy of the question's ValidationRequired status
100*5ffb0c9bSToomas Soome     mDNSu8 InsecureProofDone;
101*5ffb0c9bSToomas Soome     mDNSu8 NumPackets;              // Number of packets that we send on the wire for DNSSEC verification.
102*5ffb0c9bSToomas Soome     mDNSs32 StartTime;              // Time the DNSSEC verification starts
103*5ffb0c9bSToomas Soome     mDNSu32 flags;
104*5ffb0c9bSToomas Soome     RRVerifierSet next;
105*5ffb0c9bSToomas Soome     domainname *wildcardName;       // set if the answer is wildcard expanded
106*5ffb0c9bSToomas Soome     RRVerifier *pendingNSEC;
107*5ffb0c9bSToomas Soome     DNSSECVerifierCallback *DVCallback;
108*5ffb0c9bSToomas Soome     DNSSECVerifier *parent;
109*5ffb0c9bSToomas Soome     RRVerifier *rrset;              // rrset for which we have to verify
110*5ffb0c9bSToomas Soome     RRVerifier *rrsig;              // RRSIG for rrset
111*5ffb0c9bSToomas Soome     RRVerifier *key;                // DNSKEY for rrset
112*5ffb0c9bSToomas Soome     RRVerifier *rrsigKey;           // RRSIG for DNSKEY
113*5ffb0c9bSToomas Soome     RRVerifier *ds;                 // DS for DNSKEY set in parent zone
114*5ffb0c9bSToomas Soome     AuthChain *saveac;
115*5ffb0c9bSToomas Soome     AuthChain *ac;
116*5ffb0c9bSToomas Soome     AuthChain **actail;
117*5ffb0c9bSToomas Soome     AlgContext *ctx;
118*5ffb0c9bSToomas Soome };
119*5ffb0c9bSToomas Soome 
120*5ffb0c9bSToomas Soome 
121*5ffb0c9bSToomas Soome struct InsecureContext_struct
122*5ffb0c9bSToomas Soome {
123*5ffb0c9bSToomas Soome     DNSSECVerifier *dv;             // dv for which we are doing the insecure proof
124*5ffb0c9bSToomas Soome     mDNSu8 skip;                    // labels to skip for forming the name from origName
125*5ffb0c9bSToomas Soome     DNSSECStatus status;            // status to deliver when done
126*5ffb0c9bSToomas Soome     mDNSu8 triggerLabelCount;       // Label count of the name that triggered the insecure proof
127*5ffb0c9bSToomas Soome     DNSQuestion q;
128*5ffb0c9bSToomas Soome };
129*5ffb0c9bSToomas Soome 
130*5ffb0c9bSToomas Soome #define LogDNSSEC LogOperation
131*5ffb0c9bSToomas Soome 
132*5ffb0c9bSToomas Soome #define DNS_SERIAL_GT(a, b) ((int)((a) - (b)) > 0)
133*5ffb0c9bSToomas Soome #define DNS_SERIAL_LT(a, b) ((int)((a) - (b)) < 0)
134*5ffb0c9bSToomas Soome 
135*5ffb0c9bSToomas Soome extern void StartDNSSECVerification(mDNS *const m, void *context);
136*5ffb0c9bSToomas Soome extern RRVerifier* AllocateRRVerifier(const ResourceRecord *const rr, mStatus *status);
137*5ffb0c9bSToomas Soome extern mStatus AddRRSetToVerifier(DNSSECVerifier *dv, const ResourceRecord *const rr, RRVerifier *rv, RRVerifierSet set);
138*5ffb0c9bSToomas Soome extern void VerifySignature(mDNS *const m, DNSSECVerifier *dv, DNSQuestion *q);
139*5ffb0c9bSToomas Soome extern void FreeDNSSECVerifier(mDNS *const m, DNSSECVerifier *dv);
140*5ffb0c9bSToomas Soome extern DNSSECVerifier *AllocateDNSSECVerifier(mDNS *const m, const domainname *name, mDNSu16 rrtype, mDNSInterfaceID InterfaceID,
141*5ffb0c9bSToomas Soome                                               mDNSu8 ValidationRequired, DNSSECVerifierCallback dvcallback, mDNSQuestionCallback qcallback);
142*5ffb0c9bSToomas Soome extern void InitializeQuestion(mDNS *const m, DNSQuestion *question, mDNSInterfaceID InterfaceID, const domainname *qname,
143*5ffb0c9bSToomas Soome                                mDNSu16 qtype, mDNSQuestionCallback *callback, void *context);
144*5ffb0c9bSToomas Soome extern void ValidateRRSIG(DNSSECVerifier *dv, RRVerifierSet type, const ResourceRecord *const rr);
145*5ffb0c9bSToomas Soome extern void AuthChainLink(DNSSECVerifier *dv, AuthChain *ae);
146*5ffb0c9bSToomas Soome extern mStatus DNSNameToLowerCase(domainname *d, domainname *result);
147*5ffb0c9bSToomas Soome extern int DNSMemCmp(const mDNSu8 *const m1, const mDNSu8 *const m2, int len);
148*5ffb0c9bSToomas Soome extern int DNSSECCanonicalOrder(const domainname *const d1, const domainname *const d2, int *subdomain);
149*5ffb0c9bSToomas Soome extern void ProveInsecure(mDNS *const m, DNSSECVerifier *dv, InsecureContext *ic, domainname *trigger);
150*5ffb0c9bSToomas Soome extern void BumpDNSSECStats(mDNS *const m, DNSSECStatsAction action, DNSSECStatsType type, mDNSu32 value);
151*5ffb0c9bSToomas Soome extern char *DNSSECStatusName(DNSSECStatus status);
152*5ffb0c9bSToomas Soome 
153*5ffb0c9bSToomas Soome // DNSSECProbe belongs in DNSSECSupport.h but then we don't want to expose yet another plaform specific dnssec file
154*5ffb0c9bSToomas Soome // to other platforms where dnssec is not supported.
155*5ffb0c9bSToomas Soome extern void DNSSECProbe(mDNS *const m);
156*5ffb0c9bSToomas Soome 
157*5ffb0c9bSToomas Soome #endif // __DNSSEC_H
158