X509Certificate.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // X509Certificate.h
  3. //
  4. // Library: Crypto
  5. // Package: Certificate
  6. // Module: X509Certificate
  7. //
  8. // Definition of the X509Certificate class.
  9. //
  10. // Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Crypto_X509Certificate_INCLUDED
  16. #define Crypto_X509Certificate_INCLUDED
  17. #include "Poco/Crypto/Crypto.h"
  18. #include "Poco/Crypto/OpenSSLInitializer.h"
  19. #include "Poco/DateTime.h"
  20. #include "Poco/SharedPtr.h"
  21. #include <vector>
  22. #include <set>
  23. #include <istream>
  24. #include <openssl/ssl.h>
  25. namespace Poco {
  26. namespace Crypto {
  27. class Crypto_API X509Certificate
  28. /// This class represents a X509 Certificate.
  29. {
  30. public:
  31. typedef std::vector<X509Certificate> List;
  32. enum NID
  33. /// Name identifier for extracting information from
  34. /// a certificate subject's or issuer's distinguished name.
  35. {
  36. NID_COMMON_NAME = 13,
  37. NID_COUNTRY = 14,
  38. NID_LOCALITY_NAME = 15,
  39. NID_STATE_OR_PROVINCE = 16,
  40. NID_ORGANIZATION_NAME = 17,
  41. NID_ORGANIZATION_UNIT_NAME = 18,
  42. NID_PKCS9_EMAIL_ADDRESS = 48,
  43. NID_SERIAL_NUMBER = 105
  44. };
  45. explicit X509Certificate(std::istream& istr);
  46. /// Creates the X509Certificate object by reading
  47. /// a certificate in PEM format from a stream.
  48. explicit X509Certificate(const std::string& path);
  49. /// Creates the X509Certificate object by reading
  50. /// a certificate in PEM format from a file.
  51. explicit X509Certificate(X509* pCert);
  52. /// Creates the X509Certificate from an existing
  53. /// OpenSSL certificate. Ownership is taken of
  54. /// the certificate.
  55. X509Certificate(X509* pCert, bool shared);
  56. /// Creates the X509Certificate from an existing
  57. /// OpenSSL certificate. Ownership is taken of
  58. /// the certificate. If shared is true, the
  59. /// certificate's reference count is incremented.
  60. X509Certificate(const X509Certificate& cert);
  61. /// Creates the certificate by copying another one.
  62. X509Certificate& operator = (const X509Certificate& cert);
  63. /// Assigns a certificate.
  64. void swap(X509Certificate& cert);
  65. /// Exchanges the certificate with another one.
  66. ~X509Certificate();
  67. /// Destroys the X509Certificate.
  68. long version() const;
  69. /// Returns the version of the certificate.
  70. const std::string& serialNumber() const;
  71. /// Returns the certificate serial number as a
  72. /// string in decimal encoding.
  73. const std::string& issuerName() const;
  74. /// Returns the certificate issuer's distinguished name.
  75. std::string issuerName(NID nid) const;
  76. /// Extracts the information specified by the given
  77. /// NID (name identifier) from the certificate issuer's
  78. /// distinguished name.
  79. const std::string& subjectName() const;
  80. /// Returns the certificate subject's distinguished name.
  81. std::string subjectName(NID nid) const;
  82. /// Extracts the information specified by the given
  83. /// NID (name identifier) from the certificate subject's
  84. /// distinguished name.
  85. std::string commonName() const;
  86. /// Returns the common name stored in the certificate
  87. /// subject's distinguished name.
  88. void extractNames(std::string& commonName, std::set<std::string>& domainNames) const;
  89. /// Extracts the common name and the alias domain names from the
  90. /// certificate.
  91. Poco::DateTime validFrom() const;
  92. /// Returns the date and time the certificate is valid from.
  93. Poco::DateTime expiresOn() const;
  94. /// Returns the date and time the certificate expires.
  95. void save(std::ostream& stream) const;
  96. /// Writes the certificate to the given stream.
  97. /// The certificate is written in PEM format.
  98. void save(const std::string& path) const;
  99. /// Writes the certificate to the file given by path.
  100. /// The certificate is written in PEM format.
  101. bool issuedBy(const X509Certificate& issuerCertificate) const;
  102. /// Checks whether the certificate has been issued by
  103. /// the issuer given by issuerCertificate. This can be
  104. /// used to validate a certificate chain.
  105. ///
  106. /// Verifies if the certificate has been signed with the
  107. /// issuer's private key, using the public key from the issuer
  108. /// certificate.
  109. ///
  110. /// Returns true if verification against the issuer certificate
  111. /// was successful, false otherwise.
  112. bool equals(const X509Certificate& otherCertificate) const;
  113. /// Checks whether the certificate is equal to
  114. /// the other certificate, by comparing the hashes
  115. /// of both certificates.
  116. ///
  117. /// Returns true if both certificates are identical,
  118. /// otherwise false.
  119. const X509* certificate() const;
  120. /// Returns the underlying OpenSSL certificate.
  121. X509* dup() const;
  122. /// Duplicates and returns the underlying OpenSSL certificate. Note that
  123. /// the caller assumes responsibility for the lifecycle of the created
  124. /// certificate.
  125. std::string signatureAlgorithm() const;
  126. /// Returns the certificate signature algorithm long name.
  127. void print(std::ostream& out) const;
  128. /// Prints the certificate information to ostream.
  129. static List readPEM(const std::string& pemFileName);
  130. /// Reads and returns a list of certificates from
  131. /// the specified PEM file.
  132. static void writePEM(const std::string& pemFileName, const List& list);
  133. /// Writes the list of certificates to the specified PEM file.
  134. protected:
  135. void load(std::istream& stream);
  136. /// Loads the certificate from the given stream. The
  137. /// certificate must be in PEM format.
  138. void load(const std::string& path);
  139. /// Loads the certificate from the given file. The
  140. /// certificate must be in PEM format.
  141. void init();
  142. /// Extracts issuer and subject name from the certificate.
  143. private:
  144. enum
  145. {
  146. NAME_BUFFER_SIZE = 256
  147. };
  148. std::string _issuerName;
  149. std::string _subjectName;
  150. std::string _serialNumber;
  151. X509* _pCert;
  152. OpenSSLInitializer _openSSLInitializer;
  153. };
  154. //
  155. // inlines
  156. //
  157. inline long X509Certificate::version() const
  158. {
  159. // This is defined by standards (X.509 et al) to be
  160. // one less than the certificate version.
  161. // So, eg. a version 3 certificate will return 2.
  162. return X509_get_version(_pCert) + 1;
  163. }
  164. inline const std::string& X509Certificate::serialNumber() const
  165. {
  166. return _serialNumber;
  167. }
  168. inline const std::string& X509Certificate::issuerName() const
  169. {
  170. return _issuerName;
  171. }
  172. inline const std::string& X509Certificate::subjectName() const
  173. {
  174. return _subjectName;
  175. }
  176. inline const X509* X509Certificate::certificate() const
  177. {
  178. return _pCert;
  179. }
  180. inline X509* X509Certificate::dup() const
  181. {
  182. return X509_dup(_pCert);
  183. }
  184. } } // namespace Poco::Crypto
  185. #endif // Crypto_X509Certificate_INCLUDED