PLCnext API Documentation  21.0.0.35466
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 
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 
80  String GetSerialNumberHex(void) const;
81 
83  String GetSubject(void) const;
84 
86  String GetSubjectCommonName(void) const;
87 
89  String GetSubjectSerialNumber(void) const;
90 
92  String GetIssuer(void) const;
93 
95  String GetIssuerCommonName(void) const;
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 
104  Asn1Time GetValidityNotBeforeAsn1(void) const;
105 
107  DateTime GetValidityNotAfter(void) const; // please care: for some values DateTime::ToIso8601String does not work on 32 bit systems
108 
110  Asn1Time GetValidityNotAfterAsn1(void) const;
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
Definition: OpenSslBio.hpp:10
Class to handle x.509 certificates
Definition: Certificate.hpp:25
The class contains date and time informations.
Definition: DateTime.hpp:44
Root namespace for the PLCnext API
Definition: ItemInfo.hpp:11
System components used by the System, Device, Plc or Io domains.
const String & GetFilename(void) const
returns the filename were this x.509 certificate was loaded from
Definition: Certificate.hpp:176
Helper class to handle ASN1Time objects in x.509 Certificates
Definition: Asn1Time.hpp:22