| 
									
										
										
										
											2016-09-25 08:39:20 +01:00
										 |  |  | // Package dnssec implements a middleware that signs responses on-the-fly using
 | 
					
						
							|  |  |  | // NSEC black lies.
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | package dnssec
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/response"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/pkg/singleflight"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 05:04:36 -07:00
										 |  |  | 	"github.com/hashicorp/golang-lru"
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	Next middleware.Handler
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	zones    []string
 | 
					
						
							|  |  |  | 	keys     []*DNSKEY
 | 
					
						
							|  |  |  | 	inflight *singleflight.Group
 | 
					
						
							| 
									
										
										
										
											2016-10-17 05:04:36 -07:00
										 |  |  | 	cache    *lru.Cache
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // New returns a new Dnssec.
 | 
					
						
							| 
									
										
										
										
											2016-10-17 05:04:36 -07:00
										 |  |  | func New(zones []string, keys []*DNSKEY, next middleware.Handler, cache *lru.Cache) Dnssec {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	return Dnssec{Next: next,
 | 
					
						
							|  |  |  | 		zones:    zones,
 | 
					
						
							|  |  |  | 		keys:     keys,
 | 
					
						
							| 
									
										
										
										
											2016-10-17 05:04:36 -07:00
										 |  |  | 		cache:    cache,
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | // uses NSEC black lies for authenticated denial of existence. Signatures
 | 
					
						
							|  |  |  | // creates will be cached for a short while. By default we sign for 8 days,
 | 
					
						
							|  |  |  | // starting 3 hours ago.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (d Dnssec) Sign(state request.Request, zone string, now time.Time) *dns.Msg {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	req := state.Req
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	mt, _ := response.Typify(req) // TODO(miek): need opt record here?
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	if mt == response.Delegation {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		return req
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	incep, expir := incepExpir(now)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	if mt == response.NameError {
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if sigs, err := d.sign(req.Ns, zone, ttl, incep, expir); err == nil {
 | 
					
						
							|  |  |  | 			req.Ns = append(req.Ns, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if sigs, err := d.nsec(state.Name(), zone, ttl, incep, expir); err == nil {
 | 
					
						
							|  |  |  | 			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
 | 
					
						
							|  |  |  | 		if sigs, err := d.sign(r, zone, ttl, incep, expir); err == nil {
 | 
					
						
							|  |  |  | 			req.Answer = append(req.Answer, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, r := range rrSets(req.Ns) {
 | 
					
						
							|  |  |  | 		ttl := r[0].Header().Ttl
 | 
					
						
							|  |  |  | 		if sigs, err := d.sign(r, zone, ttl, incep, expir); err == nil {
 | 
					
						
							|  |  |  | 			req.Ns = append(req.Ns, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, r := range rrSets(req.Extra) {
 | 
					
						
							|  |  |  | 		ttl := r[0].Header().Ttl
 | 
					
						
							|  |  |  | 		if sigs, err := d.sign(r, 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) {
 | 
					
						
							|  |  |  | 	k := key(rrs)
 | 
					
						
							|  |  |  | 	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
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (d Dnssec) set(key string, sigs []dns.RR) {
 | 
					
						
							| 
									
										
										
										
											2016-10-17 05:04:36 -07:00
										 |  |  | 	d.cache.Add(key, sigs)
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (d Dnssec) get(key string) ([]dns.RR, bool) {
 | 
					
						
							|  |  |  | 	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
										 |  |  | )
 |