PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
registry_query.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11
12#include "common/http_client.h"
14#include "common/wss_signer.h"
17
19
20#include <memory>
21#include <string>
22#include <utility>
23#include <vector>
24
25namespace kcenon::pacs::ihe::xds {
26
27namespace {
28
29// ITI-18 RegistryStoredQuery SOAP action URI. The default
30// http_options::soap_action is the ITI-41 action, which would make the
31// registry reject the POST on the first line of WS-Addressing routing.
32// Registry Query overwrites it on a copy of the caller's options so the
33// caller's struct is preserved across calls.
34constexpr const char* kIti18SoapAction =
35 "urn:ihe:iti:2007:RegistryStoredQuery";
36
37// Content-Type of a plain SOAP 1.2 request. ITI-18 carries no attachments
38// in either direction, so the wire is a single application/soap+xml part.
39// Composed at compile time from kIti18SoapAction so the action URI is not
40// duplicated (review m5).
41constexpr const char* kSoapContentType =
42 "application/soap+xml; charset=UTF-8; "
43 "action=\"urn:ihe:iti:2007:RegistryStoredQuery\"";
44
45// Public-API input-validation predicate: rejects ebRIM StoredQuery
46// slot-literal metacharacters. Kept at the public boundary so callers
47// see the invalid-input error without a transport or envelope call,
48// and so the rejection is visible to auditors tracing the public
49// surface (review M1). The envelope builder keeps its own copy of this
50// check as defense in depth.
51bool contains_slot_literal_metachar(std::string_view v) {
52 for (char c : v) {
53 if (c == '\'' || c == '(' || c == ')' || c == ',') return true;
54 if (static_cast<unsigned char>(c) < 0x20) return true;
55 }
56 return false;
57}
58
59// Map the generic envelope signer/verifier failure codes to the
60// query-specific namespace so callers can branch on
61// registry_query_signature_* without pulling the Consumer vocabulary.
62int map_signature_error(int raw) {
63 if (raw ==
65 return static_cast<int>(
67 }
68 if (raw == static_cast<int>(error_code::consumer_signature_missing)) {
69 return static_cast<int>(error_code::registry_query_signature_missing);
70 }
71 return raw;
72}
73
74} // namespace
75
77public:
79 : opts_(std::move(opts)), signing_(std::move(signing)) {}
80
82 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> sink) {
83 audit_sink_ = std::move(sink);
84 }
85
87 const std::string& patient_id,
88 const registry_query_options& options) {
89 auto do_audit =
91 audit_find(patient_id, r);
92 return r;
93 };
94
95 if (patient_id.empty()) {
96 return do_audit(kcenon::common::make_error<registry_query_result>(
97 static_cast<int>(
99 "find_documents: patient_id is empty",
100 std::string(error_source)));
101 }
102 if (contains_slot_literal_metachar(patient_id)) {
103 return do_audit(kcenon::common::make_error<registry_query_result>(
104 static_cast<int>(
106 "find_documents: patient_id contains a character that is "
107 "not legal inside an ebRIM StoredQuery slot literal "
108 "('(),\\' or control character)",
109 std::string(error_source)));
110 }
111 for (const auto& s : options.status_values) {
112 if (contains_slot_literal_metachar(s)) {
113 return do_audit(
114 kcenon::common::make_error<registry_query_result>(
115 static_cast<int>(
117 "find_documents: options.status_values contains a "
118 "slot-literal metacharacter",
119 std::string(error_source)));
120 }
121 }
122 if (contains_slot_literal_metachar(options.creation_time_from) ||
123 contains_slot_literal_metachar(options.creation_time_to)) {
124 return do_audit(kcenon::common::make_error<registry_query_result>(
125 static_cast<int>(
127 "find_documents: options.creation_time_* contains a "
128 "slot-literal metacharacter",
129 std::string(error_source)));
130 }
131
133 req.patient_id = patient_id;
134 req.options = options;
135
136 return do_audit(
138 }
139
141 const std::vector<std::string>& uuids) {
142 auto do_audit =
144 audit_get(uuids, r);
145 return r;
146 };
147
148 if (uuids.empty()) {
149 return do_audit(kcenon::common::make_error<registry_query_result>(
150 static_cast<int>(
152 "get_documents: uuids is empty",
153 std::string(error_source)));
154 }
155 for (const auto& u : uuids) {
156 if (u.empty()) {
157 return do_audit(
158 kcenon::common::make_error<registry_query_result>(
159 static_cast<int>(
161 "get_documents: uuids contains an empty element",
162 std::string(error_source)));
163 }
164 if (contains_slot_literal_metachar(u)) {
165 return do_audit(
166 kcenon::common::make_error<registry_query_result>(
167 static_cast<int>(
169 "get_documents: uuids contains an element with a "
170 "slot-literal metacharacter",
171 std::string(error_source)));
172 }
173 }
174
176 req.document_uuids = uuids;
177
178 return do_audit(
180 }
181
182private:
184 const registry_query_request& req,
186 auto env_result = detail::build_iti18_envelope(req, kind);
187 if (env_result.is_err()) {
188 return kcenon::common::make_error<registry_query_result>(
189 env_result.error());
190 }
191 auto env = env_result.value();
192
193 auto sign_result = detail::sign_envelope(
196 if (sign_result.is_err()) {
197 return kcenon::common::make_error<registry_query_result>(
198 sign_result.error());
199 }
200
201 // Per-call copy: the ITI-18 action is specific to this transaction
202 // and must not mutate the shared opts_ member, so other transaction
203 // types sharing the same http_options template do not pick up a
204 // stale action from a previous call.
205 http_options call_opts = opts_;
206 call_opts.soap_action = kIti18SoapAction;
207
208 auto http_result =
209 detail::http_post(call_opts, kSoapContentType, env.xml);
210 if (http_result.is_err()) {
211 return kcenon::common::make_error<registry_query_result>(
212 http_result.error());
213 }
214 auto response = http_result.value();
215
216 // Verify the signature on the response envelope before trusting
217 // its status attribute or any entry it carries. An unsigned
218 // "Failure" response could otherwise be used to deny service or
219 // mask real successful queries; this check short-circuits before
220 // parse_query_response sees the body.
221 auto verify = detail::verify_envelope_integrity(response.body);
222 if (verify.is_err()) {
223 auto err = verify.error();
224 err.code = map_signature_error(err.code);
225 return kcenon::common::make_error<registry_query_result>(err);
226 }
227
228 return detail::parse_query_response(response.body);
229 }
230
231 void audit_find(const std::string& patient_id,
233 if (!audit_sink_) return;
234 kcenon::pacs::security::xds_iti18_event e;
235 e.registry_endpoint = opts_.endpoint;
236 e.query_kind =
237 kcenon::pacs::security::xds_iti18_event::kind::find_documents;
238 e.patient_id = patient_id;
239 if (r.is_ok()) {
241 } else {
242 e.outcome =
244 e.failure_description = r.error().message;
245 }
246 audit_sink_->on_iti18_query(e);
247 }
248
249 void audit_get(const std::vector<std::string>& uuids,
251 if (!audit_sink_) return;
252 kcenon::pacs::security::xds_iti18_event e;
253 e.registry_endpoint = opts_.endpoint;
254 e.query_kind =
255 kcenon::pacs::security::xds_iti18_event::kind::get_documents;
256 e.document_uuids = uuids;
257 if (r.is_ok()) {
259 } else {
260 e.outcome =
262 e.failure_description = r.error().message;
263 }
264 audit_sink_->on_iti18_query(e);
265 }
266
269 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> audit_sink_;
270};
271
273 : impl_(std::make_unique<impl>(std::move(opts), std::move(signing))) {}
274
276registry_query& registry_query::operator=(registry_query&&) noexcept = default;
277
278registry_query::~registry_query() = default;
279
281 const std::string& patient_id, const registry_query_options& options) {
282 return impl_->find_documents(patient_id, options);
283}
284
286 const std::vector<std::string>& uuids) {
287 return impl_->get_documents(uuids);
288}
289
291 std::shared_ptr<kcenon::pacs::security::xds_audit_sink> sink) {
292 impl_->set_audit_sink(std::move(sink));
293}
294
295} // namespace kcenon::pacs::ihe::xds
kcenon::common::Result< registry_query_result > find_documents(const std::string &patient_id, const registry_query_options &options)
void audit_find(const std::string &patient_id, const kcenon::common::Result< registry_query_result > &r)
void audit_get(const std::vector< std::string > &uuids, const kcenon::common::Result< registry_query_result > &r)
std::shared_ptr< kcenon::pacs::security::xds_audit_sink > audit_sink_
impl(http_options opts, signing_material signing)
void set_audit_sink(std::shared_ptr< kcenon::pacs::security::xds_audit_sink > sink)
kcenon::common::Result< registry_query_result > get_documents(const std::vector< std::string > &uuids)
kcenon::common::Result< registry_query_result > execute(const registry_query_request &req, detail::stored_query_kind kind)
ITI-18 Registry Query actor.
kcenon::common::Result< registry_query_result > get_documents(const std::vector< std::string > &uuids)
Execute the GetDocuments stored query (urn:uuid:5737b14c-8a1a-4539-b659-e03a34a5e1e4).
registry_query(http_options opts, signing_material signing)
Construct an ITI-18 Registry Query actor bound to a registry endpoint and a signing credential.
void set_audit_sink(std::shared_ptr< kcenon::pacs::security::xds_audit_sink > sink)
Install an ATNA audit sink.
kcenon::common::Result< registry_query_result > find_documents(const std::string &patient_id, const registry_query_options &options={})
Execute the FindDocuments stored query (urn:uuid:14d4debf-8f97-4251-9a1e-a3a9d68b2a6f).
libcurl-backed HTTPS POST client for ITI-41 transport.
kcenon::common::Result< built_envelope > build_iti18_envelope(const registry_query_request &req, stored_query_kind kind)
Build a SOAP envelope for ITI-18 from a registry_query_request.
kcenon::common::Result< http_response > http_post(const http_options &opts, const std::string &content_type, const std::string &body)
POST body to opts.endpoint with the given Content-Type.
stored_query_kind
Discriminator for the two stored queries this actor supports.
kcenon::common::Result< bool > sign_envelope(built_envelope &env, std::string_view cert_pem, std::string_view key_pem, std::string_view key_password)
Sign the body + timestamp of env in place.
kcenon::common::Result< registry_query_result > parse_query_response(const std::string &root_xml)
Parse an AdhocQueryResponse envelope into a registry_query_result.
kcenon::common::Result< bool > verify_envelope_integrity(std::string_view signed_xml)
Verify the structural integrity of a previously signed envelope against the certificate embedded in i...
constexpr std::string_view error_source
Short identifier used as the "source" field of make_error.
Definition errors.h:95
SOAP 1.2 envelope builder for IHE XDS.b ITI-18 RegistryStoredQuery.
Parser for IHE XDS.b ITI-18 AdhocQueryResponse.
IHE XDS.b Registry Query Actor (ITI-18 RegistryStoredQuery).
SOAP 1.2 envelope builder for IHE XDS.b ITI-41 requests.
HTTPS transport options for the Document Source actor.
Optional creation-time window for ITI-18 FindDocuments queries.
ITI-18 RegistryStoredQuery request target.
ITI-18 RegistryStoredQueryResponse payload.
Signing material for WS-Security XML-DSig.
WS-Security XML-DSig signer backed by OpenSSL EVP_DigestSign.
ATNA audit event helpers for the IHE XDS.b actors (ITI-18/41/43).