| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | package tls
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2019-05-31 09:30:15 -07:00
										 |  |  | 	ctls "crypto/tls"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/tls"
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 10:40:43 +01:00
										 |  |  | func init() { plugin.Register("tls", setup) }
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2019-05-31 09:30:15 -07:00
										 |  |  | 	err := parseTLS(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return plugin.Error("tls", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-28 14:03:34 +03:00
										 |  |  | func setTLSDefaults(tls *ctls.Config) {
 | 
					
						
							|  |  |  | 	tls.MinVersion = ctls.VersionTLS12
 | 
					
						
							|  |  |  | 	tls.MaxVersion = ctls.VersionTLS13
 | 
					
						
							|  |  |  | 	tls.CipherSuites = []uint16{
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		ctls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	tls.PreferServerCipherSuites = true
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-31 09:30:15 -07:00
										 |  |  | func parseTLS(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	config := dnsserver.GetConfig(c)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if config.TLSConfig != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("tls", c.Errf("TLS already configured for this server instance"))
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		args := c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2018-05-15 19:53:46 +03:00
										 |  |  | 		if len(args) < 2 || len(args) > 3 {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			return plugin.Error("tls", c.ArgErr())
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-05-31 09:30:15 -07:00
										 |  |  | 		clientAuth := ctls.NoClientCert
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "client_auth":
 | 
					
						
							|  |  |  | 				authTypeArgs := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(authTypeArgs) != 1 {
 | 
					
						
							|  |  |  | 					return c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				switch authTypeArgs[0] {
 | 
					
						
							|  |  |  | 				case "nocert":
 | 
					
						
							|  |  |  | 					clientAuth = ctls.NoClientCert
 | 
					
						
							|  |  |  | 				case "request":
 | 
					
						
							|  |  |  | 					clientAuth = ctls.RequestClientCert
 | 
					
						
							|  |  |  | 				case "require":
 | 
					
						
							|  |  |  | 					clientAuth = ctls.RequireAnyClientCert
 | 
					
						
							|  |  |  | 				case "verify_if_given":
 | 
					
						
							|  |  |  | 					clientAuth = ctls.VerifyClientCertIfGiven
 | 
					
						
							|  |  |  | 				case "require_and_verify":
 | 
					
						
							|  |  |  | 					clientAuth = ctls.RequireAndVerifyClientCert
 | 
					
						
							|  |  |  | 				default:
 | 
					
						
							|  |  |  | 					return c.Errf("unknown authentication type '%s'", authTypeArgs[0])
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			default:
 | 
					
						
							|  |  |  | 				return c.Errf("unknown option '%s'", c.Val())
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-05-15 19:53:46 +03:00
										 |  |  | 		tls, err := tls.NewTLSConfigFromArgs(args...)
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-05-31 09:30:15 -07:00
										 |  |  | 			return err
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-05-31 09:30:15 -07:00
										 |  |  | 		tls.ClientAuth = clientAuth
 | 
					
						
							|  |  |  | 		// NewTLSConfigFromArgs only sets RootCAs, so we need to let ClientCAs refer to it.
 | 
					
						
							|  |  |  | 		tls.ClientCAs = tls.RootCAs
 | 
					
						
							| 
									
										
										
										
											2019-06-28 14:03:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		setTLSDefaults(tls)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 		config.TLSConfig = tls
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |