Skip to content

Commit ae3ee38

Browse files
faemittensbrocaar
authored andcommitted
Verify CN of client certificates with Basic Station backend (#129)
1 parent 1cd24a0 commit ae3ee38

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

internal/backend/basicstation/backend.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ func (b *Backend) handleRouterInfo(r *http.Request, c *websocket.Conn) {
251251
URI: fmt.Sprintf("%s://%s/gateway/%s", b.scheme, r.Host, lorawan.EUI64(req.Router)),
252252
}
253253

254+
if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
255+
var cn lorawan.EUI64
256+
257+
if err := cn.UnmarshalText([]byte(r.TLS.PeerCertificates[0].Subject.CommonName)); err != nil || cn != lorawan.EUI64(req.Router) {
258+
resp.URI = ""
259+
resp.Error = fmt.Sprintf("certificate CommonName %s does not match router %s",
260+
r.TLS.PeerCertificates[0].Subject.CommonName, lorawan.EUI64(req.Router))
261+
}
262+
}
263+
254264
c.SetWriteDeadline(time.Now().Add(b.writeTimeout))
255265
if err := c.WriteJSON(resp); err != nil {
256266
log.WithError(err).Error("backend/basicstation: websocket send message error")
@@ -278,6 +288,18 @@ func (b *Backend) handleGateway(r *http.Request, c *websocket.Conn) {
278288
return
279289
}
280290

291+
if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
292+
var cn lorawan.EUI64
293+
if err := cn.UnmarshalText([]byte(r.TLS.PeerCertificates[0].Subject.CommonName)); err != nil || cn != gatewayID {
294+
log.WithFields(log.Fields{
295+
"gateway_id": gatewayID,
296+
"common_name": r.TLS.PeerCertificates[0].Subject.CommonName,
297+
}).Error("backend/basicstation: CommonName verification failed")
298+
return
299+
}
300+
}
301+
302+
281303
// make sure we're not overwriting an existing connection
282304
_, err := b.gateways.get(gatewayID)
283305
if err == nil {

0 commit comments

Comments
 (0)