| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Package dnssec implements a plugin that signs responses on-the-fly using
 | 
					
						
							| 
									
										
										
										
											2016-09-25 08:39:20 +01:00
										 |  |  | // NSEC black lies.
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | package dnssec
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/cache"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/response"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/singleflight"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // Dnssec signs the reply on-the-fly.
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | type Dnssec struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Next plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	zones    []string
 | 
					
						
							|  |  |  | 	keys     []*DNSKEY
 | 
					
						
							|  |  |  | 	inflight *singleflight.Group
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	cache    *cache.Cache
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // New returns a new Dnssec.
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | func New(zones []string, keys []*DNSKEY, next plugin.Handler, c *cache.Cache) Dnssec {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	return Dnssec{Next: next,
 | 
					
						
							|  |  |  | 		zones:    zones,
 | 
					
						
							|  |  |  | 		keys:     keys,
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 		cache:    c,
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		inflight: new(singleflight.Group),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // Sign signs the message in state. it takes care of negative or nodata responses. It
 | 
					
						
							| 
									
										
										
										
											2017-10-20 09:22:02 +01:00
										 |  |  | // uses NSEC black lies for authenticated denial of existence. For delegations it
 | 
					
						
							|  |  |  | // will insert DS records and sign those.
 | 
					
						
							|  |  |  | // Signatures will be cached for a short while. By default we sign for 8 days,
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | // starting 3 hours ago.
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | func (d Dnssec) Sign(state request.Request, now time.Time) *dns.Msg {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	req := state.Req
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 09:22:02 +01:00
										 |  |  | 	incep, expir := incepExpir(now)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-29 15:06:42 +01:00
										 |  |  | 	mt, _ := response.Typify(req, time.Now().UTC()) // TODO(miek): need opt record here?
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	if mt == response.Delegation {
 | 
					
						
							| 
									
										
										
										
											2017-12-01 11:14:39 +00:00
										 |  |  | 		// This reverts 11203e44. Reverting with git revert leads to conflicts in dnskey.go, and I'm
 | 
					
						
							|  |  |  | 		// not sure yet if we just should fiddle with inserting DSs or not.
 | 
					
						
							|  |  |  | 		// Easy way to, see #1211 for discussion.
 | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 			ttl := req.Ns[0].Header().Ttl
 | 
					
						
							| 
									
										
										
										
											2017-10-20 09:22:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 11:14:39 +00:00
										 |  |  | 			ds := []dns.RR{}
 | 
					
						
							|  |  |  | 			for i := range d.keys {
 | 
					
						
							|  |  |  | 				ds = append(ds, d.keys[i].D)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if sigs, err := d.sign(ds, zone, ttl, incep, expir); err == nil {
 | 
					
						
							|  |  |  | 				req.Ns = append(req.Ns, ds...)
 | 
					
						
							|  |  |  | 				req.Ns = append(req.Ns, sigs...)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		*/
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		return req
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-08 13:28:35 +02:00
										 |  |  | 	if mt == response.NameError || mt == response.NoData {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		if req.Ns[0].Header().Rrtype != dns.TypeSOA || len(req.Ns) > 1 {
 | 
					
						
							|  |  |  | 			return req
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ttl := req.Ns[0].Header().Ttl
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | 		if sigs, err := d.sign(req.Ns, state.Zone, ttl, incep, expir); err == nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 			req.Ns = append(req.Ns, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | 		if sigs, err := d.nsec(state, mt, ttl, incep, expir); err == nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 			req.Ns = append(req.Ns, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if len(req.Ns) > 1 { // actually added nsec and sigs, reset the rcode
 | 
					
						
							|  |  |  | 			req.Rcode = dns.RcodeSuccess
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return req
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, r := range rrSets(req.Answer) {
 | 
					
						
							|  |  |  | 		ttl := r[0].Header().Ttl
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | 		if sigs, err := d.sign(r, state.Zone, ttl, incep, expir); err == nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 			req.Answer = append(req.Answer, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, r := range rrSets(req.Ns) {
 | 
					
						
							|  |  |  | 		ttl := r[0].Header().Ttl
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | 		if sigs, err := d.sign(r, state.Zone, ttl, incep, expir); err == nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 			req.Ns = append(req.Ns, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, r := range rrSets(req.Extra) {
 | 
					
						
							|  |  |  | 		ttl := r[0].Header().Ttl
 | 
					
						
							| 
									
										
										
										
											2018-01-03 11:11:56 +00:00
										 |  |  | 		if sigs, err := d.sign(r, state.Zone, ttl, incep, expir); err == nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-27 10:48:22 +00:00
										 |  |  | 			req.Extra = append(sigs, req.Extra...) // prepend to leave OPT alone
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return req
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (d Dnssec) sign(rrs []dns.RR, signerName string, ttl, incep, expir uint32) ([]dns.RR, error) {
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	k := hash(rrs)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	sgs, ok := d.get(k)
 | 
					
						
							|  |  |  | 	if ok {
 | 
					
						
							|  |  |  | 		return sgs, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sigs, err := d.inflight.Do(k, func() (interface{}, error) {
 | 
					
						
							|  |  |  | 		sigs := make([]dns.RR, len(d.keys))
 | 
					
						
							|  |  |  | 		var e error
 | 
					
						
							|  |  |  | 		for i, k := range d.keys {
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 			sig := k.newRRSIG(signerName, ttl, incep, expir)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 			e = sig.Sign(k.s, rrs)
 | 
					
						
							|  |  |  | 			sigs[i] = sig
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		d.set(k, sigs)
 | 
					
						
							|  |  |  | 		return sigs, e
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	return sigs.([]dns.RR), err
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | func (d Dnssec) set(key uint32, sigs []dns.RR) {
 | 
					
						
							| 
									
										
										
										
											2016-10-17 05:04:36 -07:00
										 |  |  | 	d.cache.Add(key, sigs)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | func (d Dnssec) get(key uint32) ([]dns.RR, bool) {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	if s, ok := d.cache.Get(key); ok {
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 		cacheHits.Inc()
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		return s.([]dns.RR), true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 	cacheMisses.Inc()
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	return nil, false
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func incepExpir(now time.Time) (uint32, uint32) {
 | 
					
						
							|  |  |  | 	incep := uint32(now.Add(-3 * time.Hour).Unix()) // -(2+1) hours, be sure to catch daylight saving time and such
 | 
					
						
							|  |  |  | 	expir := uint32(now.Add(eightDays).Unix())      // sign for 8 days
 | 
					
						
							|  |  |  | 	return incep, expir
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							| 
									
										
										
										
											2016-10-17 05:04:36 -07:00
										 |  |  | 	eightDays  = 8 * 24 * time.Hour
 | 
					
						
							|  |  |  | 	defaultCap = 10000 // default capacity of the cache.
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | )
 |