PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
certificate.h
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
18#pragma once
19
20#include <kcenon/common/patterns/result.h>
21
22#include <chrono>
23#include <cstdint>
24#include <memory>
25#include <span>
26#include <string>
27#include <string_view>
28#include <vector>
29
30namespace kcenon::pacs::security {
31
32// Forward declarations for PIMPL
33class certificate_impl;
34class private_key_impl;
35
57public:
62
67
72
76 auto operator=(const certificate& other) -> certificate&;
77
81 auto operator=(certificate&& other) noexcept -> certificate&;
82
87
88 // ========================================================================
89 // Factory Methods
90 // ========================================================================
91
97 [[nodiscard]] static auto load_from_pem(std::string_view path)
99
105 [[nodiscard]] static auto load_from_pem_string(std::string_view pem_data)
107
113 [[nodiscard]] static auto load_from_der(std::span<const std::uint8_t> der_data)
115
116 // ========================================================================
117 // Certificate Information
118 // ========================================================================
119
124 [[nodiscard]] auto subject_name() const -> std::string;
125
130 [[nodiscard]] auto subject_common_name() const -> std::string;
131
136 [[nodiscard]] auto subject_organization() const -> std::string;
137
142 [[nodiscard]] auto issuer_name() const -> std::string;
143
148 [[nodiscard]] auto serial_number() const -> std::string;
149
154 [[nodiscard]] auto thumbprint() const -> std::string;
155
156 // ========================================================================
157 // Validity
158 // ========================================================================
159
164 [[nodiscard]] auto not_before() const -> std::chrono::system_clock::time_point;
165
170 [[nodiscard]] auto not_after() const -> std::chrono::system_clock::time_point;
171
176 [[nodiscard]] auto is_valid() const -> bool;
177
182 [[nodiscard]] auto is_expired() const -> bool;
183
184 // ========================================================================
185 // Export
186 // ========================================================================
187
192 [[nodiscard]] auto to_pem() const -> std::string;
193
198 [[nodiscard]] auto to_der() const -> std::vector<std::uint8_t>;
199
200 // ========================================================================
201 // Internal Access
202 // ========================================================================
203
208 [[nodiscard]] auto is_loaded() const noexcept -> bool;
209
213 [[nodiscard]] auto impl() const noexcept -> const certificate_impl*;
214 [[nodiscard]] auto impl() noexcept -> certificate_impl*;
215
216private:
217 friend class certificate_chain; // Allow certificate_chain to access impl_
218 std::unique_ptr<certificate_impl> impl_;
219};
220
240public:
244 private_key();
245
249 private_key(const private_key&) = delete;
250
255
259 auto operator=(const private_key&) -> private_key& = delete;
260
265
270
271 // ========================================================================
272 // Factory Methods
273 // ========================================================================
274
281 [[nodiscard]] static auto load_from_pem(
282 std::string_view path,
283 std::string_view password = ""
285
292 [[nodiscard]] static auto load_from_pem_string(
293 std::string_view pem_data,
294 std::string_view password = ""
296
297 // ========================================================================
298 // Key Information
299 // ========================================================================
300
305 [[nodiscard]] auto algorithm_name() const -> std::string;
306
311 [[nodiscard]] auto key_size() const -> int;
312
317 [[nodiscard]] auto is_loaded() const noexcept -> bool;
318
319 // ========================================================================
320 // Internal Access
321 // ========================================================================
322
326 [[nodiscard]] auto impl() const noexcept -> const private_key_impl*;
327 [[nodiscard]] auto impl() noexcept -> private_key_impl*;
328
329private:
330 std::unique_ptr<private_key_impl> impl_;
331};
332
340public:
344 certificate_chain() = default;
345
350 void add(certificate cert);
351
356 [[nodiscard]] auto end_entity() const -> const certificate*;
357
362 [[nodiscard]] auto certificates() const -> const std::vector<certificate>&;
363
368 [[nodiscard]] auto empty() const noexcept -> bool;
369
374 [[nodiscard]] auto size() const noexcept -> size_t;
375
385 [[nodiscard]] static auto load_from_pem(std::string_view path)
387
388private:
389 std::vector<certificate> certs_;
390};
391
392} // namespace kcenon::pacs::security
Represents a certificate chain for validation.
certificate_chain()=default
Default constructor - creates an empty chain.
auto issuer_name() const -> std::string
Get the issuer distinguished name.
auto serial_number() const -> std::string
Get the certificate serial number.
std::unique_ptr< certificate_impl > impl_
auto to_der() const -> std::vector< std::uint8_t >
Export certificate as DER bytes.
auto is_expired() const -> bool
Check if the certificate has expired.
static auto load_from_der(std::span< const std::uint8_t > der_data) -> kcenon::common::Result< certificate >
Load certificate from DER-encoded bytes.
static auto load_from_pem(std::string_view path) -> kcenon::common::Result< certificate >
Load certificate from PEM file.
auto impl() const noexcept -> const certificate_impl *
Get internal implementation (for internal use only)
auto subject_name() const -> std::string
Get the subject distinguished name.
auto is_valid() const -> bool
Check if the certificate is currently valid.
auto not_before() const -> std::chrono::system_clock::time_point
Get the not-before date.
auto operator=(certificate &&other) noexcept -> certificate &
Move assignment.
auto to_pem() const -> std::string
Export certificate as PEM string.
auto subject_common_name() const -> std::string
Get the common name from the subject.
auto operator=(const certificate &other) -> certificate &
Copy assignment.
auto thumbprint() const -> std::string
Get the certificate thumbprint (SHA-256)
auto is_loaded() const noexcept -> bool
Check if certificate is loaded.
static auto load_from_pem_string(std::string_view pem_data) -> kcenon::common::Result< certificate >
Load certificate from PEM string.
auto not_after() const -> std::chrono::system_clock::time_point
Get the not-after date.
certificate()
Default constructor - creates an empty certificate.
auto subject_organization() const -> std::string
Get the organization from the subject.
certificate(certificate &&other) noexcept
Move constructor.
auto operator=(const private_key &) -> private_key &=delete
Copy assignment (deleted)
auto operator=(private_key &&other) noexcept -> private_key &
Move assignment.
~private_key()
Destructor - securely erases key material.
private_key(const private_key &)=delete
Copy constructor (deleted - private keys should not be copied)
private_key(private_key &&other) noexcept
Move constructor.
@ empty
Z - Replace with zero-length value.