Line data Source code
1 : /* 2 : * Distributed under the OpenDDS License. 3 : * See: http://www.OpenDDS.org/license.html 4 : */ 5 : 6 : #ifndef OPENDDS_DCPS_SECURITY_SSL_CERTIFICATE_H 7 : #define OPENDDS_DCPS_SECURITY_SSL_CERTIFICATE_H 8 : 9 : #include <dds/DCPS/security/OpenDDS_Security_Export.h> 10 : #include <dds/DCPS/unique_ptr.h> 11 : 12 : #include <dds/DdsDcpsCoreC.h> 13 : #include <dds/DdsSecurityCoreC.h> 14 : 15 : #include <openssl/x509.h> 16 : 17 : #include <string> 18 : #include <vector> 19 : #include <iostream> 20 : 21 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 22 : 23 : namespace OpenDDS { 24 : namespace Security { 25 : namespace SSL { 26 : 27 : class verify_signature_impl; 28 : 29 : class OpenDDS_Security_Export Certificate { 30 : public: 31 : friend class verify_signature_impl; 32 : 33 : typedef DCPS::unique_ptr<Certificate> unique_ptr; 34 : 35 : friend OpenDDS_Security_Export std::ostream& operator<<(std::ostream&, 36 : const Certificate&); 37 : 38 : friend OpenDDS_Security_Export bool operator==(const Certificate& lhs, 39 : const Certificate& rhs); 40 : 41 : explicit Certificate(const std::string& uri, const std::string& password = ""); 42 : 43 : explicit Certificate(const DDS::OctetSeq& src); 44 : 45 : Certificate(const Certificate& other); 46 : 47 : Certificate(); 48 : 49 : virtual ~Certificate(); 50 : 51 : Certificate& operator=(const Certificate& rhs); 52 : 53 : bool load(DDS::Security::SecurityException& ex, 54 : const std::string& uri, 55 : const std::string& password = ""); 56 : 57 : /** 58 : * @return int 0 on success; 1 on failure. 59 : */ 60 : int validate(const Certificate& ca, unsigned long int flags = 0u) const; 61 : 62 : /** 63 : * @return int 0 on success; 1 on failure. 64 : */ 65 : int verify_signature( 66 : const DDS::OctetSeq& src, 67 : const std::vector<const DDS::OctetSeq*>& expected_contents) const; 68 : 69 : /** 70 : * @return int 0 on success; 1 on failure. 71 : */ 72 : int subject_name_to_str(std::string& dst, 73 : unsigned long flags = XN_FLAG_ONELINE) const; 74 : 75 : /** 76 : * @return int 0 on success; 1 on failure. 77 : */ 78 : int subject_name_digest(std::vector<CORBA::Octet>& dst) const; 79 : 80 : /** 81 : * @return int 0 on success; 1 on failure. 82 : */ 83 : int serialize(DDS::OctetSeq& dst) const; 84 : 85 : /** 86 : * @return int 0 on success; 1 on failure. 87 : */ 88 : int deserialize(const DDS::OctetSeq& src); 89 : 90 42 : const DDS::OctetSeq& original_bytes() const { return original_bytes_; } 91 : 92 42 : const char* dsign_algo() const { return dsign_algo_.c_str(); } 93 : 94 : const char* keypair_algo() const; 95 : 96 52 : X509* x509() const { return x_; } 97 : 98 : private: 99 : 100 220 : bool loaded() 101 : { 102 439 : return (x_ != NULL) && 103 439 : (0 < original_bytes_.length()); 104 : } 105 : 106 : /** 107 : * @return int 0 on success; 1 on failure. 108 : */ 109 : int cache_dsign_algo(); 110 : 111 : 112 : void load_cert_bytes(const std::string& path); 113 : 114 : void load_cert_data_bytes(const std::string& data); 115 : 116 : static X509* x509_from_pem(const std::string& path, 117 : const std::string& password = ""); 118 : static X509* x509_from_pem(const DDS::OctetSeq& bytes, 119 : const std::string& password = ""); 120 : 121 : X509* x_; 122 : DDS::OctetSeq original_bytes_; 123 : std::string dsign_algo_; 124 : }; 125 : 126 : OpenDDS_Security_Export std::ostream& operator<<(std::ostream&, const Certificate&); 127 : 128 : OpenDDS_Security_Export bool operator==(const Certificate& lhs, 129 : const Certificate& rhs); 130 : } // namespace SSL 131 : } // namespace Security 132 : } // namespace OpenDDS 133 : 134 : OPENDDS_END_VERSIONED_NAMESPACE_DECL 135 : 136 : #endif