PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
kcenon::pacs::security::private_key Class Reference

Represents a private key for signing operations. More...

#include <certificate.h>

Collaboration diagram for kcenon::pacs::security::private_key:
Collaboration graph

Public Member Functions

 private_key ()
 Default constructor - creates an empty key.
 
 private_key (const private_key &)=delete
 Copy constructor (deleted - private keys should not be copied)
 
 private_key (private_key &&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.
 
auto algorithm_name () const -> std::string
 Get the algorithm name.
 
auto key_size () const -> int
 Get the key size in bits.
 
auto is_loaded () const noexcept -> bool
 Check if key is loaded.
 
auto impl () const noexcept -> const private_key_impl *
 Get internal implementation (for internal use only)
 
auto impl () noexcept -> private_key_impl *
 

Static Public Member Functions

static auto load_from_pem (std::string_view path, std::string_view password="") -> kcenon::common::Result< private_key >
 Load private key from PEM file.
 
static auto load_from_pem_string (std::string_view pem_data, std::string_view password="") -> kcenon::common::Result< private_key >
 Load private key from PEM string.
 

Private Attributes

std::unique_ptr< private_key_impl > impl_
 

Detailed Description

Represents a private key for signing operations.

This class wraps a private key (RSA or EC) and provides methods to load and use the key for digital signature creation.

Thread Safety: This class is thread-safe for read operations.

Example:
auto key_result = private_key::load_from_pem("/path/to/key.pem", "password");
if (key_result.is_ok()) {
auto& key = key_result.value();
std::cout << "Key type: " << key.algorithm_name() << "\n";
std::cout << "Key size: " << key.key_size() << " bits\n";
}
static auto load_from_pem(std::string_view path, std::string_view password="") -> kcenon::common::Result< private_key >
Load private key from PEM file.

Definition at line 239 of file certificate.h.

Constructor & Destructor Documentation

◆ private_key() [1/3]

kcenon::pacs::security::private_key::private_key ( )

Default constructor - creates an empty key.

Definition at line 562 of file certificate.cpp.

562: impl_(std::make_unique<private_key_impl>()) {}
std::unique_ptr< private_key_impl > impl_

◆ private_key() [2/3]

kcenon::pacs::security::private_key::private_key ( const private_key & )
delete

Copy constructor (deleted - private keys should not be copied)

◆ private_key() [3/3]

kcenon::pacs::security::private_key::private_key ( private_key && other)
defaultnoexcept

Move constructor.

◆ ~private_key()

kcenon::pacs::security::private_key::~private_key ( )
default

Destructor - securely erases key material.

Member Function Documentation

◆ algorithm_name()

auto kcenon::pacs::security::private_key::algorithm_name ( ) const -> std::string
nodiscard

Get the algorithm name.

Returns
Algorithm name (e.g., "RSA", "EC")

Definition at line 610 of file certificate.cpp.

610 {
611 if (!impl_->is_loaded()) {
612 return "";
613 }
614
615 int type = EVP_PKEY_base_id(impl_->pkey());
616 switch (type) {
617 case EVP_PKEY_RSA:
618 case EVP_PKEY_RSA2:
619 return "RSA";
620 case EVP_PKEY_EC:
621 return "EC";
622 case EVP_PKEY_DSA:
623 return "DSA";
624 case EVP_PKEY_ED25519:
625 return "ED25519";
626 case EVP_PKEY_ED448:
627 return "ED448";
628 default:
629 return "Unknown";
630 }
631}

References impl_.

◆ impl() [1/2]

auto kcenon::pacs::security::private_key::impl ( ) const -> const private_key_impl*
nodiscardnoexcept

Get internal implementation (for internal use only)

Definition at line 645 of file certificate.cpp.

645 {
646 return impl_.get();
647}

References impl_.

◆ impl() [2/2]

auto kcenon::pacs::security::private_key::impl ( ) -> private_key_impl*
nodiscardnoexcept

Definition at line 649 of file certificate.cpp.

649 {
650 return impl_.get();
651}

References impl_.

◆ is_loaded()

auto kcenon::pacs::security::private_key::is_loaded ( ) const -> bool
nodiscardnoexcept

Check if key is loaded.

Returns
true if key data is loaded

Definition at line 641 of file certificate.cpp.

641 {
642 return impl_ && impl_->is_loaded();
643}

References impl_.

◆ key_size()

auto kcenon::pacs::security::private_key::key_size ( ) const -> int
nodiscard

Get the key size in bits.

Returns
Key size (e.g., 2048, 4096 for RSA; 256, 384 for EC)

Definition at line 633 of file certificate.cpp.

633 {
634 if (!impl_->is_loaded()) {
635 return 0;
636 }
637
638 return EVP_PKEY_bits(impl_->pkey());
639}

References impl_.

◆ load_from_pem()

auto kcenon::pacs::security::private_key::load_from_pem ( std::string_view path,
std::string_view password = "" ) -> kcenon::common::Result<private_key>
staticnodiscard

Load private key from PEM file.

Parameters
pathPath to PEM-encoded private key file
passwordOptional password for encrypted keys (empty for unencrypted)
Returns
Result containing private key or error

Definition at line 570 of file certificate.cpp.

571 {
572 auto content_result = read_file(path);
573 if (content_result.is_err()) {
574 return kcenon::common::make_error<private_key>(
575 content_result.error().code,
576 content_result.error().message,
577 "private_key");
578 }
579
580 return load_from_pem_string(content_result.value(), password);
581}
static auto load_from_pem_string(std::string_view pem_data, std::string_view password="") -> kcenon::common::Result< private_key >
Load private key from PEM string.

◆ load_from_pem_string()

auto kcenon::pacs::security::private_key::load_from_pem_string ( std::string_view pem_data,
std::string_view password = "" ) -> kcenon::common::Result<private_key>
staticnodiscard

Load private key from PEM string.

Parameters
pem_dataPEM-encoded private key data
passwordOptional password for encrypted keys
Returns
Result containing private key or error

Definition at line 583 of file certificate.cpp.

584 {
585 bio_ptr bio(BIO_new_mem_buf(pem_data.data(), static_cast<int>(pem_data.size())));
586 if (!bio) {
587 return kcenon::common::make_error<private_key>(
588 2, "Failed to create BIO: " + get_openssl_error(), "private_key");
589 }
590
591 EVP_PKEY* pkey = nullptr;
592 if (password.empty()) {
593 pkey = PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr);
594 } else {
595 pkey = PEM_read_bio_PrivateKey(
596 bio.get(), nullptr, nullptr,
597 const_cast<char*>(std::string(password).c_str()));
598 }
599
600 if (!pkey) {
601 return kcenon::common::make_error<private_key>(
602 3, "Failed to parse PEM private key: " + get_openssl_error(), "private_key");
603 }
604
605 private_key key;
606 key.impl_ = std::make_unique<private_key_impl>(pkey);
607 return key;
608}
private_key()
Default constructor - creates an empty key.

References impl_.

◆ operator=() [1/2]

auto kcenon::pacs::security::private_key::operator= ( const private_key & ) -> private_key &=delete
delete

Copy assignment (deleted)

◆ operator=() [2/2]

auto kcenon::pacs::security::private_key::operator= ( private_key && other) -> private_key &
defaultnoexcept

Move assignment.

Member Data Documentation

◆ impl_

std::unique_ptr<private_key_impl> kcenon::pacs::security::private_key::impl_
private

Definition at line 330 of file certificate.h.

Referenced by algorithm_name(), impl(), impl(), is_loaded(), key_size(), and load_from_pem_string().


The documentation for this class was generated from the following files: