PLCnext API Documentation 23.9.0.25
IdentityStore.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Commons/Logging.h"
9#include "Arp/System/Commons/Io/Path.hpp"
10#include "Arp/System/Commons/Security/KeyPair.hpp"
11#include "Arp/System/Commons/Security/Certificate.hpp"
12#include "Arp/System/Commons/Security/SecurityListType.hpp"
13#include "Arp/System/Commons/Security/ItemInfo.hpp"
14#include "Arp/System/Commons/Security/KeyPairType.hpp"
15#include "Arp/System/Commons/Security/SecurityConfigurationError.hpp"
16#include <map>
17#include <vector>
18#include <openssl/evp.h>
19
20namespace Arp { namespace System { namespace Commons { namespace Security
21{
22
25class IdentityStore : private Loggable<IdentityStore>
26{
27public: // type definitions
28 enum KeyMustExist : bool
29 {
30 IgnoreMissingKey = false,
31 RequireKeyExists = true
32 };
33
34public: // construction/destruction
36 IdentityStore(const String& basePath, const String& name, KeyMustExist requireKeyExists = RequireKeyExists);
38 IdentityStore(const IdentityStore& arg) = delete;
40 IdentityStore& operator=(const IdentityStore& arg) = delete;
42 ~IdentityStore(void) = default;
43
44public: // operators
45
46public: // static operations
47
48public: // setter/getter operations
50 String GetName(void) const;
54 String GetFullKeyFileName(void) const;
56 String GetFullTpmKeyFileName(void) const;
58 bool HasCertificate(void);
60 const std::shared_ptr<KeyPair>& GetKeyPair(void);
62 KeyPairType GetKeyType(void);
63
64public: // operations
67 void InitSslContext(SSL_CTX* pSslCtx)const;
68
71 std::vector<byte> GetPublicKey(void);
72
76 SecurityConfigurationError SetKeyPair(const std::vector<byte>& pemData);
77
81 SecurityConfigurationError SetCertificate(const std::vector<byte>& pemData);
82
85 std::vector<byte> GetPemCertificate(void);
86
89 std::vector<byte> GetDerCertificate(void);
90
93 std::vector<byte> GetPemCertificateWithChain(void);
94
97 std::vector<byte> GetDerCertificateWithChain(void);
98
100 std::vector<std::vector<byte>> GetIssuerPemCertificates(void);
101
104
106
108 SecurityConfigurationError ListContent(SecurityListType type, std::vector<ItemInfo>& result);
109
111
113 SecurityConfigurationError AddElement(SecurityListType type, const std::vector<byte>& pemData);
114
116
118 SecurityConfigurationError DeleteElement(SecurityListType type, const String& identifier);
119
121
123 SecurityConfigurationError GenerateKeyPair(KeyPairType type);
124
127 std::vector<byte> GenerateCSR();
128
131 bool VerifyCertMatchesWithPrivateKey(void);
132
133protected: // operations
134
135private: // static methods
136
137private: // methods
138 void loadKeyPair(KeyMustExist requireKeyExists);
139 bool LoadSoftwareKeyPair();
140 bool LoadHardwareKeyPair();
141 void loadCertWithChain(const String& file);
142 void listIssuerList(std::vector<ItemInfo>& result);
143 void listIdentityCert(std::vector<ItemInfo>& result);
144
145 void save(void);
146
147private: // fields
148 String storePath;
149
150 std::shared_ptr<KeyPair> keyPair;
151 Certificate cert;
152 std::vector<Certificate> issuers;
153
154private: // static fields
155
156 static const String CertificateFileName;
157 static const String KeyFileName;
158 static const String TpmKeyFileName;
159 static const String DirectorySeparator;
160};
161
163// inline methods of class CertificateStore
164
166{
167 return Io::Path::GetFileName(this->storePath);
168}
169
171{
172 return this->storePath + DirectorySeparator + CertificateFileName;
173}
174
176{
177 return this->storePath + DirectorySeparator + KeyFileName;
178}
179
181{
182 return this->storePath + DirectorySeparator + TpmKeyFileName;
183}
184
185inline KeyPairType IdentityStore::GetKeyType(void)
186{
187 return this->keyPair->GetKeyType();
188}
189
190}}}} // end of namespace Arp::System::Commons::Security
static String GetFileName(const String &path)
Get name of a file.
Class to handle x.509 certificates
Definition: Certificate.hpp:25
Class with represents a Identity (Certificate with Chain and private Key) and is able to initialze an...
Definition: IdentityStore.hpp:26
void CreateAllDirectories(void)
summary>list all elements in the list referenced by ListType
const std::shared_ptr< KeyPair > & GetKeyPair(void)
summary>Returns the type of the key pair
String GetName(void) const
Returns the name of the IdentityStore
Definition: IdentityStore.hpp:165
SecurityConfigurationError GenerateKeyPair(KeyPairType type)
SecurityConfigurationError DeleteElement(SecurityListType type, const String &identifier)
summary>Generate a new KeyPair for this IdentityStore
SecurityConfigurationError SetKeyPair(const std::vector< byte > &pemData)
IdentityStore(const IdentityStore &arg)=delete
Copy constructor.
SecurityConfigurationError AddElement(SecurityListType type, const std::vector< byte > &pemData)
summary>delete an elements from the list referenced by ListType, identified by identifier
std::vector< byte > GetPemCertificateWithChain(void)
std::vector< byte > GetDerCertificateWithChain(void)
summary>get the issuer certificates in PEM format
void InitSslContext(SSL_CTX *pSslCtx) const
Initializes a OpenSSL SSL_:CTX Structure with the private key and certificate
String GetFullTpmKeyFileName(void) const
summary>checks if a certificate is available for this IdentityStore
Definition: IdentityStore.hpp:180
std::vector< byte > GetPemCertificate(void)
SecurityConfigurationError SetCertificate(const std::vector< byte > &pemData)
String GetFullCertificateFileName(void) const
summary>Returns the absolute path to the key file
Definition: IdentityStore.hpp:170
IdentityStore & operator=(const IdentityStore &arg)=delete
Assignment operator.
std::vector< std::vector< byte > > GetIssuerPemCertificates(void)
summary>creates all needed directories inside the folder of this IdentityStore
SecurityConfigurationError ListContent(SecurityListType type, std::vector< ItemInfo > &result)
summary>add an element into the list referenced by ListType
std::vector< byte > GetDerCertificate(void)
~IdentityStore(void)=default
Destructs this instance and frees all resources.
IdentityStore(const String &basePath, const String &name, KeyMustExist requireKeyExists=RequireKeyExists)
Constructs an IdentityStore instance.
String GetFullKeyFileName(void) const
summary>Returns the absolute path to the tpm key file
Definition: IdentityStore.hpp:175
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API