PLCnext API Documentation 25.0.2.69
Certificate.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
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: // construction/destruction
27 Certificate(void);
28 Certificate(const String& filename);
29 Certificate(const std::vector<byte>& pemOrDerEncoding);
30 explicit Certificate(X509* cert);
31
32
33 Certificate(const Certificate& arg);
34 Certificate(Certificate&& arg) noexcept;
37 Certificate& operator=(Certificate&& arg) noexcept;
38 virtual ~Certificate(void);
39
40public: // operators
41 operator X509*() const;
42
43public: // static operations
44 static String Rfc2253Format(X509_NAME* name);
45 static String GetAttributeValueByNid(X509_NAME *name, int nid);
46 static String HexString(ASN1_INTEGER* integer);
47
48public: // setter/getter operations
49 const String& GetFilename(void) const;
50 String GetSerialNumberHex(void) const;
51 String GetSubject(void) const;
52 String GetSubjectCommonName(void) const;
54 String GetIssuer(void) const;
55 String GetIssuerCommonName(void) const;
56 const String& GetIdentifier(void) const;
59 DateTime GetValidityNotAfter(void) const;
61 std::vector<byte> GetPemData(void) const;
62 std::vector<byte> GetDerData(void) const;
63 ItemInfo GetItemInfo(void) const;
64
65public: // operations
66 void LoadFromFile(const String& filename);
67 int LoadFromMemory(const std::vector<byte>& data, int offset = 0);
68 bool TryLoadFromMemory(const std::vector<byte>& data, int& offset);
69 bool TryLoadFromBio(OpenSslBio& bio, int& offset);
70 void SaveToFilePem(const String& filename) const;
71 void AppendToFilePem(const String& filename) const;
72 void SaveToFileDer(const String& filename) const;
73
74private: // static methods
75
76private: // methods
77 bool loadPemFile(OpenSslBio& pBio);
78 bool loadDerFile(OpenSslBio& pBio);
79 void generateIdentifier(void);
80
81protected: // fields
82 String identifier;
83 X509* internalCertificate;
84
85private: // fields
86 String filename;
87
88private: // static fields
89};
90
91}}}} // end of namespace Arp::System::Commons::Security
This class contains date and time informations.
Definition: DateTime.hpp:27
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Derive from this class to inherit logging functionality.
Definition: Loggable.hxx:28
Helper class to handle ASN1Time objects in x.509 Certificates
Definition: Asn1Time.hpp:21
Class to handle x.509 certificates
Definition: Certificate.hpp:25
static String Rfc2253Format(X509_NAME *name)
static function to convert an openssl X509_NAME to an rfc2253 string
Definition: Certificate.cpp:377
static String HexString(ASN1_INTEGER *integer)
static function convert an openssl ASN1_INTEGER to hexadecimal String representation
Definition: Certificate.cpp:355
DateTime GetValidityNotAfter(void) const
returns the notAfter field of the x.509 as DateTime object
Definition: Certificate.cpp:464
Asn1Time GetValidityNotBeforeAsn1(void) const
returns the notBefore field of the x.509 as Asn1Time object
Definition: Certificate.cpp:457
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
Definition: Certificate.cpp:427
bool TryLoadFromMemory(const std::vector< byte > &data, int &offset)
tries to load the certificate from memory. byte vector can be PEM or DER encoded
Definition: Certificate.cpp:161
void AppendToFilePem(const String &filename) const
appends the certificate in PEM format to filename
Definition: Certificate.cpp:254
String GetSerialNumberHex(void) const
returns the filename were this x.509 certificate was loaded from
Definition: Certificate.cpp:342
String GetSubjectCommonName(void) const
returns the common name of the subject
Definition: Certificate.cpp:390
void SaveToFilePem(const String &filename) const
saves the certificate in PEM format to filename
Definition: Certificate.cpp:239
void SaveToFileDer(const String &filename) const
saves the certificate in DER format to filename
Definition: Certificate.cpp:269
ItemInfo GetItemInfo(void) const
returns ItemInfo for the Certificate
Definition: Certificate.cpp:543
bool TryLoadFromBio(OpenSslBio &bio, int &offset)
tries to load the certificate from OpenSslBio.
Definition: Certificate.cpp:216
String GetSubjectSerialNumber(void) const
return the serialnumber of the subject as string
Definition: Certificate.cpp:397
Certificate & operator=(const Certificate &arg)
Assignment operator.
Definition: Certificate.cpp:90
std::vector< byte > GetPemData(void) const
returns the certificate encoded in PEM format
Definition: Certificate.cpp:499
int LoadFromMemory(const std::vector< byte > &data, int offset=0)
loads the certificate from memory. byte vector can be PEM or DER encoded
Definition: Certificate.cpp:197
String GetSubject(void) const
returns the subject of the x.509 certificate as string (rfc2253 formatted)
Definition: Certificate.cpp:370
DateTime GetValidityNotBefore(void) const
returns the notBefore field of the x.509 as DateTime object
Definition: Certificate.cpp:451
virtual ~Certificate(void)
Destructs this instance and frees all resources.
Definition: Certificate.cpp:67
Certificate(void)
Constructs an empty Certificate instance to be loaded from file or memory.
Definition: Certificate.cpp:26
std::vector< byte > GetDerData(void) const
returns the certificate encoded in DER format
Definition: Certificate.cpp:521
void LoadFromFile(const String &filename)
loads the certificate from a file. File can be PEM or DER encoded
Definition: Certificate.cpp:124
Asn1Time GetValidityNotAfterAsn1(void) const
returns the notAfter field of the x.509 as Asn1Time object
Definition: Certificate.cpp:470
String GetIssuerCommonName(void) const
returns the common name of the issuer
Definition: Certificate.cpp:411
const String & GetFilename(void) const
returns the filename were this x.509 certificate was loaded from
Definition: Certificate.cpp:336
String GetIssuer(void) const
returns the issuer of the x.509 certificate as string (rfc2253 formatted)
Definition: Certificate.cpp:404
const String & GetIdentifier(void) const
returns an unique identifier for this certificate as string (sha256 hash of the x....
Definition: Certificate.cpp:476
Definition: ItemInfo.hpp:12
Root namespace for the PLCnext API