PLCnext API Documentation 23.6.0.37
Certificate.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/Security/Asn1Time.hpp"
10#include "Arp/System/Commons/Security/ItemInfo.hpp"
11#include "Arp/System/Commons/Security/Internal/OpenSslBio.hpp"
12#include <map>
13#include <vector>
14
15#include <openssl/x509.h>
16#include <openssl/x509v3.h>
17
18using namespace Arp::System::Commons::Security::Internal;
19
20namespace Arp { namespace System { namespace Commons { namespace Security
21{
22
24class Certificate : private Loggable<Certificate>
25{
26public: // typedefs
27
28public: // construction/destruction
32 Certificate(const String& filename);
34 Certificate(const std::vector<byte>& pemOrDerEncoding);
35 explicit Certificate(X509* cert);
36
37
40 Certificate(Certificate&& arg) noexcept;
42 virtual Certificate& operator=(const Certificate& arg);
43 Certificate& operator=(Certificate&& arg) noexcept;
45 virtual ~Certificate(void);
46
47public: // operators
49 operator X509*() const;
50
51public: // static operations
53 static String Rfc2253Format(X509_NAME* name);
54
65 static String GetAttributeValueByNid(X509_NAME *name, int nid);
66
74 static String HexString(ASN1_INTEGER* integer);
75
76public: // setter/getter operations
81 const String& GetFilename(void) const;
82
85
87 String GetSubject(void) const;
88
91
94
96 String GetIssuer(void) const;
97
100
102 const String& GetIdentifier(void) const;
103
105 DateTime GetValidityNotBefore(void) const; // please care: for some values DateTime::ToIso8601String does not work on 32 bit systems
106
109
111 DateTime GetValidityNotAfter(void) const; // please care: for some values DateTime::ToIso8601String does not work on 32 bit systems
112
115
117 std::vector<byte> GetPemData(void) const;
118
120 std::vector<byte> GetDerData(void) const;
121
124
125public: // operations
127 void LoadFromFile(const String& filename);
128
130 int LoadFromMemory(const std::vector<byte>& data, int offset = 0);
131
137 bool TryLoadFromMemory(const std::vector<byte>& data, int& offset);
138
144 bool TryLoadFromBio(OpenSslBio& bio, int& offset);
145
147 void SaveToFilePem(const String& filename) const;
148
150 void AppendToFilePem(const String& filename) const;
151
153 void SaveToFileDer(const String& filename) const;
154
155private: // static methods
156
157private: // methods
158 bool loadPemFile(OpenSslBio& pBio);
159 bool loadDerFile(OpenSslBio& pBio);
160 void generateIdentifier(void);
161
162protected: // fields
163 String identifier;
164 X509* internalCertificate;
165
166private: // fields
167 String filename;
168
169private: // static fields
170};
171
173// inline methods of class Certificate
174
175inline Certificate::operator X509*() const
176{
177 return this->internalCertificate;
178}
179
180inline const String& Certificate::GetFilename() const
181{
182 return this->filename;
183}
184
185}}}} // end of namespace Arp::System::Commons::Security
The class contains date and time informations.
Definition: DateTime.hpp:46
Helper class to handle ASN1Time objects in x.509 Certificates
Definition: Asn1Time.hpp:22
Class to handle x.509 certificates
Definition: Certificate.hpp:25
virtual ~Certificate(void)
Destructs this instance and frees all resources.
DateTime GetValidityNotAfter(void) const
returns the notAfter field of the x.509 as DateTime object
Asn1Time GetValidityNotBeforeAsn1(void) const
returns the notBefore field of the x.509 as Asn1Time object
Certificate(const std::vector< byte > &pemOrDerEncoding)
Constructs an Certificate instance load from memory
bool TryLoadFromMemory(const std::vector< byte > &data, int &offset)
tries to load the certificate from memory. byte vector can be PEM or DER encoded
void AppendToFilePem(const String &filename) const
appends the certificate in PEM format to filename
static String Rfc2253Format(X509_NAME *name)
static function to convert an openssl X509_NAME to an rfc2253 string
Certificate(const String &filename)
Constructs an Certificate instance load from a file
String GetSerialNumberHex(void) const
returns the filename were this x.509 certificate was loaded from
String GetSubjectCommonName(void) const
returns the common name of the subject
void SaveToFilePem(const String &filename) const
saves the certificate in PEM format to filename
void SaveToFileDer(const String &filename) const
saves the certificate in DER format to filename
virtual Certificate & operator=(const Certificate &arg)
Assignment operator.
ItemInfo GetItemInfo(void) const
returns ItemInfo for the Certificate
bool TryLoadFromBio(OpenSslBio &bio, int &offset)
tries to load the certificate from OpenSslBio.
static String GetAttributeValueByNid(X509_NAME *name, int nid)
static function to extract an attribute value out of an openssl X509_NAME object identified by NID
String GetSubjectSerialNumber(void) const
return the serialnumber of the subject as string
std::vector< byte > GetPemData(void) const
returns the certificate encoded in PEM format
int LoadFromMemory(const std::vector< byte > &data, int offset=0)
loads the certificate from memory. byte vector can be PEM or DER encoded
String GetSubject(void) const
returns the subject of the x.509 certificate as string (rfc2253 formatted)
DateTime GetValidityNotBefore(void) const
returns the notBefore field of the x.509 as DateTime object
Certificate(void)
Constructs an empty Certificate instance to be loaded from file or memory.
std::vector< byte > GetDerData(void) const
returns the certificate encoded in DER format
void LoadFromFile(const String &filename)
loads the certificate from a file. File can be PEM or DER encoded
Asn1Time GetValidityNotAfterAsn1(void) const
returns the notAfter field of the x.509 as Asn1Time object
String GetIssuerCommonName(void) const
returns the common name of the issuer
const String & GetFilename(void) const
returns the filename were this x.509 certificate was loaded from
Definition: Certificate.hpp:180
static String HexString(ASN1_INTEGER *integer)
static function convert an openssl ASN1_INTEGER to hexadecimal String representation
String GetIssuer(void) const
returns the issuer of the x.509 certificate as string (rfc2253 formatted)
const String & GetIdentifier(void) const
returns an unique identifier for this certificate as string (sha256 hash of the x....
Certificate(const Certificate &arg)
Copy constructor.
Definition: ItemInfo.hpp:12
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API