Skip to Content
go-dicom(3)

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

ConcernDIMSEDICOMweb
EncryptionSCUConfig.TLS, SCPConfig.TLS, SCPConfig.OutboundTLSyour *http.Client (WithHTTPClient) and http.Server.ServeTLS
Server proves who it isits certificate, checked against CAFile or the system rootsthe same, by the HTTP client
Client proves who it isa client certificate (CAFile on the server) and Admita token or Basic credentials, checked by your handler
Who may connectSCPConfig.Admit, or -allow on the commandsthe 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:

FieldOn a serverOn a client
CertFile, KeyFilethe certificate it presents (required)the certificate it presents, for mutual TLS
CAFilerequires a client certificate, verified against these CAsverifies the server against these CAs, instead of the system roots
ServerNamethe name to verify the server's certificate for, when it is not the host
InsecureSkipVerifyno verification (testing only)
MinVersiondefault TLS 1.2default TLS 1.2
Configa *tls.Config of your own; when set, the other fields are not usedthe 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:

CallerResult
CT_SCANNER with its certificateecho succeeded
MR_SCANNER with the same certificateassociation rejected: result=1, source=1, reason=3
no client certificatethe 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.

CheckMeans
not-tlsThe client sent no TLS record. It is most likely speaking plain DICOM to a TLS port.
no-client-certificateThe server requires a client certificate and got none.
unknown-authorityThe client's certificate is not from a trusted CA.
certificate-invalidTrusted CA, but expired, not yet valid, or not for client authentication.
peer-alertThe client ended the handshake, usually because it rejected the server's certificate.
protocolNo shared version or cipher suite, or a malformed handshake.
connectionThe 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:

FlagClient commandsServer commandsTLSConfig field
-tlsassociate over TLSaccept TLS only(non-nil)
-tls-cert, -tls-keycertificate for mutual TLScertificate to present (required)CertFile, KeyFile
-tls-caCAs to verify the serverCAs a client certificate must come fromCAFile
-tls-server-namename to verify the server forServerName
-tls-insecureskip verificationInsecureSkipVerify
$ 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 -tls

A -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:

EntryAdmits
CT_SCANNERthat calling AE title, from anywhere
10.0.0.5, 10.0.0.0/8any title from that address or range
PRISMA1@10.0.0.5that 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 -allow

The 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's Check is the stable form to act on.
  • TLSConfig.Config replaces every other field, ServerName and MinVersion included.
  • 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, MaxDataSetSize and MaxNonStoreDataSetSize on 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.

See also

On this page