Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
PKI/PEMFile.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Cryptography_PEMFile_h_
5#define _Stroika_Foundation_Cryptography_PEMFile_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <filesystem>
10
12#include "Stroika/Foundation/Containers/Sequence.h"
13#include "Stroika/Foundation/Cryptography/PKI/Certificate.h"
14#include "Stroika/Foundation/Cryptography/PKI/PrivateKey.h"
16
17/**
18 * \file
19 *
20 * \brief a PEM File is a data structure commonly used to store private keys, and or certificates (as well as several other things not currently supported here).
21 *
22 * At this level of abstraction - its just a bunch of bytes, but used in overloading, in other APIs, so they correctly
23 * know how to interpret the bytes.
24 *
25 * https://datatracker.ietf.org/doc/html/rfc7468
26 *
27 */
28
29namespace Stroika::Foundation::Cryptography::PKI::PEMFile {
30
31 using Containers::Sequence;
32 using Memory::BLOB;
33 using Traversal::Iterable;
34
35 /**
36 * @todo - can also be CRL, certificate request, etc... - message, many things can be inside
37 *
38 * \note since this is being developed to support webserver, main target support is PrivateKey/Cert --LGP 2025-01-03
39 */
40 using EntryType = variant<Certificate::Ptr, PrivateKey::Ptr>;
41
42 /**
43 */
44 class IRep {
45 public:
46 virtual ~IRep () = default;
47
48 public:
49 virtual BLOB GetData () const = 0;
50 virtual Sequence<EntryType> GetEntries () const = 0;
51 };
52
53 /**
54 */
55 struct Ptr : shared_ptr<IRep> {
56 using inherited = shared_ptr<IRep>;
57 /**
58 */
59 using inherited::inherited;
60
61 Characters::String ToString () const;
62
63 BLOB GetData () const;
64 Sequence<EntryType> GetEntries () const;
65 template <Common::IAnyOf<Certificate::Ptr, PrivateKey::Ptr> T>
66 Iterable<T> GetByType () const;
67 };
68
69 /**
70 * \par Example Usage:
71 * \code
72 * PEMFile::Ptr myCertPem{New (IO::FileSystem::FileInputStream::New ("my-cert.pem").ReadAll ()))};
73 * Certificate::Ptr cert = pem.GetByType<Certificate::Ptr> ().FirstValue (nullptr);
74 * PrivateKey::Ptr pkey = pem.GetByType<PrivateKey::Ptr> ().FirstValue (nullptr);
75 * \endcode
76 *
77 * \par Example Usage:
78 * \code
79 * PEMFile::Ptr myCertPem{"my-cert.pem"}; // same as above example, uses FileInputStream::New as above
80 * \endcode
81 *
82 * \par Example Usage:
83 * \code
84 * auto [pk, cert] = Certificate::New (Certificate::SelfSignedCertParams{
85 * .fValidDates = validDates, .fSubject = {.fCountry = "US"sv, .fOrganization = company, .fCommonName = commonName}});
86 * // construct from existing collection of certs, and private keys
87 * PEMFile::Ptr pem = PEMFile::New ({pk, cert});
88 * \endcode
89 */
90 Ptr New (const filesystem::path& pemFile);
91 Ptr New (const BLOB& pemData);
92 Ptr New (const Sequence<EntryType>& entries);
93
94}
95
96/*
97 ********************************************************************************
98 ***************************** Implementation Details ***************************
99 ********************************************************************************
100 */
101#include "PEMFile.inl"
102
103#endif /*_Stroika_Foundation_Cryptography_PEMFile_h_*/
variant< Certificate::Ptr, PrivateKey::Ptr > EntryType
Definition PKI/PEMFile.h:40
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187