TLS and security
How to encrypt DICOM associations and DICOMweb requests with TLS, and how to decide who may connect. Plain DICOM over TCP has neither encryption nor authentication.
Overview
| Concern | DIMSE | DICOMweb |
|---|---|---|
| Encryption | SCUConfig.TLS, SCPConfig.TLS, SCPConfig.OutboundTLS | your *http.Client (WithHTTPClient) and http.Server.ServeTLS |
| Server proves who it is | its certificate, checked against CAFile or the system roots | the same, by the HTTP client |
| Client proves who it is | a client certificate (CAFile on the server) and Admit | a token or Basic credentials, checked by your handler |
| Who may connect | SCPConfig.Admit, or -allow on the commands | the handler you wrap the server in |
Caution
An AE title is a name, not an identity. Anything that can reach the port can claim any calling AE title. Check a calling AE title against a verified certificate, or limit callers by address, before you trust it.
TLS for associations
TLSConfig
configures both sides:
| Field | On a server | On a client |
|---|---|---|
CertFile, KeyFile | the certificate it presents (required) | the certificate it presents, for mutual TLS |
CAFile | requires a client certificate, verified against these CAs | verifies the server against these CAs, instead of the system roots |
ServerName | the name to verify the server's certificate for, when it is not the host | |
InsecureSkipVerify | no verification (testing only) | |
MinVersion | default TLS 1.2 | default TLS 1.2 |
Config | a *tls.Config of your own; when set, the other fields are not used | the same |
A server that admits callers by their certificate:
events := network.NewEventManager()
events.On(network.EVTTLSHandshakeFailed, func(e *network.Event) {
var hs *network.TLSHandshakeError
if errors.As(e.Error, &hs) {
log.Printf("TLS refused %s: %s", e.RemoteAddr, hs.Check)
}
})
server, err := network.StartServer(ctx, network.SCPConfig{
AETitle: "ARCHIVE",
BindAddress: "127.0.0.1",
TLS: &network.TLSConfig{
CertFile: "pki/node.crt",
KeyFile: "pki/node.key",
CAFile: "pki/ca.crt", // a client must present a certificate from this CA
},
// Admit by the verified certificate, not by the calling AE title alone.
Admit: func(ctx context.Context, req *network.AssociationRequest) *network.AssociationRejection {
if len(req.VerifiedChains) == 0 || req.VerifiedChains[0][0].Subject.CommonName != req.CallingAE {
return network.RejectCallingAE()
}
return nil
},
Events: events,
}, &network.EchoHandler{})
if err != nil {
return err
}
defer server.Stop()A client that presents its certificate:
scu := network.NewSCU(network.SCUConfig{
CallingAE: "CT_SCANNER",
CalledAE: "ARCHIVE",
Address: server.Addr(),
TLS: &network.TLSConfig{
CertFile: "pki/client.crt",
KeyFile: "pki/client.key",
CAFile: "pki/ca.crt", // verify the server against this CA, not the system roots
ServerName: "localhost",
},
})
if err := scu.Associate(ctx, network.DefaultVerificationContexts()); err != nil {
return err
}
if err := scu.Echo(ctx); err != nil {
return err
}
scu.Release(ctx)In this run, the client certificate's common name was CT_SCANNER:
| Caller | Result |
|---|---|
CT_SCANNER with its certificate | echo succeeded |
MR_SCANNER with the same certificate | association rejected: result=1, source=1, reason=3 |
| no client certificate | the handshake failed; the event handler logged TLS refused 127.0.0.1:59143: no-client-certificate |
VerifiedChains holds only chains the handshake verified, leaf first. It is
nil over plain TCP, over TLS without CAFile, and when a custom
tls.Config asks for a certificate without verifying it.
Note
SCUConfigTLS and SCPConfigTLS are deprecated. Nothing reads them, so a
setting placed there has no effect. Use the TLS field of SCUConfig and
SCPConfig.
Associations the SCP opens
An SCP opens associations of its own for C-MOVE sub-operations, storage
commitment results and UPS event reports. SCPConfig.TLS does not cover
them. Set SCPConfig.OutboundTLS for them, since a peer the SCP calls need
not be reached the way the SCP is. SCPConfig.ResolveOutbound can give one peer
its own TLS in the OutboundTarget it returns.
qrscp -tls-outbound uses -tls-cert and -tls-ca for this, so the
certificate must allow client authentication.
Handshake failures
A failed handshake on an SCP raises EVTTLSHandshakeFailed. Its Error is
a *TLSHandshakeError. That error names the failed check and never carries
any of the certificate's contents, so it is safe to log.
Check | Means |
|---|---|
not-tls | The client sent no TLS record. It is most likely speaking plain DICOM to a TLS port. |
no-client-certificate | The server requires a client certificate and got none. |
unknown-authority | The client's certificate is not from a trusted CA. |
certificate-invalid | Trusted CA, but expired, not yet valid, or not for client authentication. |
peer-alert | The client ended the handshake, usually because it rejected the server's certificate. |
protocol | No shared version or cipher suite, or a malformed handshake. |
connection | The connection ended or timed out before the handshake finished. |
With -events, the commands write these as JSON lines:
{"event":"EVT_TLS_HANDSHAKE_FAILED","remoteAddr":"127.0.0.1:51257","error":"tls handshake failed (no-client-certificate): the client presented no certificate"}
{"event":"EVT_TLS_HANDSHAKE_FAILED",...,"error":"tls handshake failed (not-tls): the client did not send a TLS record"}See The -events log for the rest of the event stream.
The command line
Every network command takes the same TLS flags:
| Flag | Client commands | Server commands | TLSConfig field |
|---|---|---|---|
-tls | associate over TLS | accept TLS only | (non-nil) |
-tls-cert, -tls-key | certificate for mutual TLS | certificate to present (required) | CertFile, KeyFile |
-tls-ca | CAs to verify the server | CAs a client certificate must come from | CAFile |
-tls-server-name | name to verify the server for | ServerName | |
-tls-insecure | skip verification | InsecureSkipVerify |
$ go-dicom echoscp -port 41020 -tls -tls-cert pki/node.crt -tls-key pki/node.key -tls-ca pki/ca.crt -events tls-events.jsonl
$ go-dicom echoscu -aec ECHOSCP -tls -tls-ca pki/ca.crt -tls-cert pki/client.crt -tls-key pki/client.key 127.0.0.1:41020
Requesting Association with 127.0.0.1:41020 (AE: ECHOSCP)
Association accepted
Sending C-ECHO...
C-ECHO response: Success (0x0000)
Releasing Association
$ go-dicom echoscu -aec ECHOSCP -tls -tls-ca pki/ca.crt 127.0.0.1:41020
Requesting Association with 127.0.0.1:41020 (AE: ECHOSCP)
Error: association failed: failed to read association response: communication error [READ_PDU]: failed to read PDU header: remote error: tls: certificate required
$ go-dicom echoscu -aec ECHOSCP 127.0.0.1:41020
Requesting Association with 127.0.0.1:41020 (AE: ECHOSCP)
Error: association failed: failed to read association response: communication error [READ_PDU]: failed to read PDU header: EOF
$ go-dicom echoscu -aec ECHOSCP -tls-ca pki/ca.crt 127.0.0.1:41020
Error: a -tls-* option was given without -tlsA -tls-* option without -tls is an error, not ignored, so a command never
quietly connects in the clear.
Admitting callers
SCPConfig.Admit runs before anything is negotiated. It gets the calling
and called AE titles, both addresses, VerifiedChains and UserIdentity.
It returns nil to admit, or an *AssociationRejection such as
RejectCallingAE(). A refused caller learns nothing about what the SCP
supports. See Building an SCP.
The server commands take -allow (and -allow-file) for the same thing:
| Entry | Admits |
|---|---|
CT_SCANNER | that calling AE title, from anywhere |
10.0.0.5, 10.0.0.0/8 | any title from that address or range |
PRISMA1@10.0.0.5 | that title, from that address only |
With both titles and addresses listed, a caller needs a listed title and a listed address. A host name is read as an AE title, not resolved.
$ go-dicom storescp -port 41005 -output received2 -allow CT_SCANNER
...
Refused "STORESCU" from 127.0.0.1:61234: the calling AE title is not on -allowThe refused storescu saw association rejected: result=1, source=1, reason=3.
Warning
UserIdentity is not checked by the SCP. A user name, password, Kerberos
ticket, SAML assertion or JSON Web Token is for Admit to verify.
SecondaryField holds a password in clear, so do not log either field.
DICOMweb over HTTPS
The DICOMweb client uses whatever TLS its *http.Client has:
pem, err := os.ReadFile("pki/ca.crt")
if err != nil {
return err
}
roots := x509.NewCertPool()
if !roots.AppendCertsFromPEM(pem) {
return errors.New("no certificate in pki/ca.crt")
}
cert, err := tls.LoadX509KeyPair("pki/client.crt", "pki/client.key")
if err != nil {
return err
}
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: roots,
Certificates: []tls.Certificate{cert}, // for a server that asks for one
MinVersion: tls.VersionTLS12,
},
},
Timeout: 10 * time.Minute,
}
client, err := dicomweb.NewClient("https://localhost:41091/dicom-web",
dicomweb.WithHTTPClient(httpClient),
dicomweb.WithBearerToken(os.Getenv("DICOMWEB_TOKEN")))On the server side, dicomweb.Server is an http.Handler, so
srv.ServeTLS(l, "pki/node.crt", "pki/node.key") serves it over HTTPS.
qrscp -tls -web serves its DICOMweb service over HTTPS with the same
certificate. With -tls-ca, it also asks for a client certificate:
$ go-dicom qrscp -port 41021 -output archive-tls -tls -tls-cert pki/node.crt -tls-key pki/node.key -web 41082
...
DICOMweb: https://127.0.0.1:41082/dicom-web
$ go-dicom stow -tls-ca pki/ca.crt https://127.0.0.1:41082/dicom-web study/ct.dcm
Stored 1 instance(s)Caution
Server does no authentication. qrscp -web refuses an address other
machines can reach unless it has -web-token. In your own program, wrap the
handler as shown in DICOMweb server.
Limits and gotchas
- The SCP logs a refused handshake at ERROR level:
failed to read association request: ... tls: client didn't provide a certificate. The event'sCheckis the stable form to act on. TLSConfig.Configreplaces every other field,ServerNameandMinVersionincluded.- There is no authentication beyond TLS client certificates,
Admit, and whatever wraps a DICOMweb handler. The SCP neither checks user identity nor treats AE titles as identity. - Set
MaxAssociations,MaxDataSetSizeandMaxNonStoreDataSetSizeon an exposed SCP. See Building an SCP. - CONFORMANCE.md §7 lists the limits enforced on what a peer sends: PDU lengths, the size of the command set, timeouts, and decompression bounds.
- Vulnerability reporting is in SECURITY.md.