PLCnext API Documentation  22.9.0.33
Certificate.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/AppDomainSingleton.hxx"
9 #include "Arp/System/Commons/Logging.h"
10 #include "Arp/System/Commons/Security/Asn1Time.hpp"
11 #include "Arp/System/Commons/Security/ItemInfo.hpp"
12 #include "Arp/System/Commons/Security/Internal/OpenSslBio.hpp"
13 #include <map>
14 #include <vector>
15 
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 
19 using namespace Arp::System::Commons::Security::Internal;
20 
21 namespace Arp { namespace System { namespace Commons { namespace Security
22 {
23 
25 class Certificate : private Loggable<Certificate>
26 {
27 public: // typedefs
28 
29 public: // construction/destruction
31  Certificate(void);
33  Certificate(const String& filename);
35  Certificate(const std::vector<byte>& pemOrDerEncoding);
37  Certificate(const Certificate& arg);
39  virtual Certificate& operator=(const Certificate& arg);
41  virtual ~Certificate(void);
42 
43 public: // operators
45  operator X509*() const;
46 
47 public: // static operations
49  static String Rfc2253Format(X509_NAME* name);
50 
61  static String GetAttributeValueByNid(X509_NAME *name, int nid);
62 
70  static String HexString(ASN1_INTEGER* integer);
71 
72 public: // setter/getter operations
77  const String& GetFilename(void) const;
78 
81 
83  String GetSubject(void) const;
84 
87 
90 
92  String GetIssuer(void) const;
93 
96 
98  const String& GetIdentifier(void) const;
99 
101  DateTime GetValidityNotBefore(void) const; // please care: for some values DateTime::ToIso8601String does not work on 32 bit systems
102 
105 
107  DateTime GetValidityNotAfter(void) const; // please care: for some values DateTime::ToIso8601String does not work on 32 bit systems
108 
111 
113  std::vector<byte> GetPemData(void) const;
114 
116  std::vector<byte> GetDerData(void) const;
117 
119  ItemInfo GetItemInfo(void) const;
120 
121 public: // operations
123  void LoadFromFile(const String& filename);
124 
126  int LoadFromMemory(const std::vector<byte>& data, int offset = 0);
127 
133  bool TryLoadFromMemory(const std::vector<byte>& data, int& offset);
134 
140  bool TryLoadFromBio(OpenSslBio& bio, int& offset);
141 
143  void SaveToFilePem(const String& filename) const;
144 
146  void AppendToFilePem(const String& filename) const;
147 
149  void SaveToFileDer(const String& filename) const;
150 
151 private: // static methods
152 
153 private: // methods
154  bool loadPemFile(OpenSslBio& pBio);
155  bool loadDerFile(OpenSslBio& pBio);
156  void generateIdentifier(void);
157 
158 protected: // fields
159  String identifier;
160  X509* internalCertificate;
161 
162 private: // fields
163  String filename;
164 
165 private: // static fields
166 };
167 
169 // inline methods of class Certificate
170 
171 inline Certificate::operator X509*() const
172 {
173  return this->internalCertificate;
174 }
175 
176 inline const String& Certificate::GetFilename() const
177 {
178  return this->filename;
179 }
180 
181 }}}} // end of namespace Arp::System::Commons::Security
The class contains date and time informations.
Definition: DateTime.hpp:45
Helper class to handle ASN1Time objects in x.509 Certificates
Definition: Asn1Time.hpp:23
Class to handle x.509 certificates
Definition: Certificate.hpp:26
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
std::vector< byte > GetDerData(void) const
returns the certificate encoded in DER format
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
virtual Certificate & operator=(const Certificate &arg)
Assignment operator.
void SaveToFileDer(const String &filename) const
saves the certificate in DER format to filename
ItemInfo GetItemInfo(void) const
returns ItemInfo for the Certificate
std::vector< byte > GetPemData(void) const
returns the certificate encoded in PEM format
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
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)
const String & GetIdentifier(void) const
returns an unique identifier for this certificate as string (sha256 hash of the x....
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.
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:176
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)
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