Skip to content

Commit 84f9a99

Browse files
committed
feat: add support for mDTLS
Signed-off-by: Felix Gateru <felix.gateru@gmail.com>
1 parent c33495e commit 84f9a99

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

.env

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,14 @@ MGATE_COAP_WITH_DTLS_TARGET_PORT=5683
103103
MGATE_COAP_WITH_DTLS_CERT_FILE=ssl/certs/server.crt
104104
MGATE_COAP_WITH_DTLS_KEY_FILE=ssl/certs/server.key
105105
MGATE_COAP_WITH_DTLS_SERVER_CA_FILE=ssl/certs/ca.crt
106-
MGATE_COAP_WITH_DTLS_CLIENT_CA_FILE=ssl/certs/ca.crt
106+
107+
MGATE_COAP_WITH_MDTLS_HOST=
108+
MGATE_COAP_WITH_MDTLS_PORT=5685
109+
MGATE_COAP_WITH_MDTLS_TARGET_HOST=localhost
110+
MGATE_COAP_WITH_MDTLS_TARGET_PORT=5683
111+
MGATE_COAP_WITH_MDTLS_CERT_FILE=ssl/certs/server.crt
112+
MGATE_COAP_WITH_MDTLS_KEY_FILE=ssl/certs/server.key
113+
MGATE_COAP_WITH_MDTLS_SERVER_CA_FILE=ssl/certs/ca.crt
114+
MGATE_COAP_WITH_MDTLS_CLIENT_CA_FILE=ssl/certs/ca.crt
115+
MGATE_COAP_WITH_MDTLS_CERT_VERIFICATION_METHODS=ocsp
116+
MGATE_COAP_WITH_MDTLS_OCSP_RESPONDER_URL=http://localhost:8880/ocsp

cmd/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838

3939
coapWithoutDTLS = "MGATE_COAP_WITHOUT_DTLS_"
4040
coapWithDTLS = "MGATE_COAP_WITH_DTLS_"
41+
coapWithmDTLS = "MGATE_COAP_WITH_MDTLS_"
4142
)
4243

4344
func main() {
@@ -200,6 +201,18 @@ func main() {
200201
return coapDTLSProxy.Listen(ctx)
201202
})
202203

204+
// mGate server Configuration for CoAP with mDTLS
205+
coapmDTLSConfig, err := mgate.NewConfig(env.Options{Prefix: coapWithmDTLS})
206+
if err != nil {
207+
panic(err)
208+
}
209+
210+
// mGate server for CoAP with mDTLS
211+
coapmDTLSProxy := coap.NewProxy(coapmDTLSConfig, handler, logger)
212+
g.Go(func() error {
213+
return coapmDTLSProxy.Listen(ctx)
214+
})
215+
203216
g.Go(func() error {
204217
return StopSignalHandler(ctx, cancel, logger)
205218
})

pkg/coap/coap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (p *Proxy) Listen(ctx context.Context) error {
120120
}
121121

122122
status := mptls.SecurityStatus(p.config.DTLSConfig)
123-
p.logger.Info(fmt.Sprintf("COAP proxy server started at %s with %s", net.JoinHostPort(p.config.Host, p.config.Port), status))
123+
p.logger.Info(fmt.Sprintf("COAP proxy server started at %s with %s", net.JoinHostPort(p.config.Host, p.config.Port), status))
124124

125125
if err := g.Wait(); err != nil {
126126
p.logger.Info(fmt.Sprintf("COAP proxy server at %s exiting with errors", net.JoinHostPort(p.config.Host, p.config.Port)), slog.String("error", err.Error()))

pkg/tls/tls.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ func SecurityStatus[sc TLSConfig](s sc) string {
143143
}
144144
return ret
145145
case *dtls.Config:
146-
return "DTLS"
146+
ret := "DTLS"
147+
148+
if c.ClientCAs != nil {
149+
ret += " and " + toClientAuthString(c.ClientAuth)
150+
}
151+
return ret
147152
default:
148153
return "no TLS"
149154
}
@@ -155,3 +160,20 @@ func loadCertFile(certFile string) ([]byte, error) {
155160
}
156161
return []byte{}, nil
157162
}
163+
164+
func toClientAuthString(cat dtls.ClientAuthType) string {
165+
switch cat {
166+
case dtls.NoClientCert:
167+
return "NoClientCert"
168+
case dtls.RequestClientCert:
169+
return "RequestClientCert"
170+
case dtls.RequireAnyClientCert:
171+
return "RequestAnyClientCert"
172+
case dtls.VerifyClientCertIfGiven:
173+
return "VerifyClientCertIfGiven"
174+
case dtls.RequireAndVerifyClientCert:
175+
return "RequireAndVerifyClientCert"
176+
default:
177+
return ""
178+
}
179+
}

0 commit comments

Comments
 (0)