27 #include <ibrcommon/Logger.h>
28 #include <ibrcommon/ssl/TLSStream.h>
34 const std::string SecurityCertificateManager::TAG =
"SecurityCertificateManager";
37 : _initialized(false), _cert(NULL), _privateKey(NULL)
61 return _trustedCAPath;
66 ibrcommon::File certificate = conf.getSecurity().getCertificate();
67 ibrcommon::File privateKey = conf.getSecurity().getKey();
68 ibrcommon::File trustedCAPath = conf.getSecurity().getTrustedCAPath();
76 fp = fopen(certificate.getPath().c_str(),
"r");
77 if(!fp || !PEM_read_X509(fp, &cert, NULL, NULL)){
78 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, error) <<
"Could not read the certificate File: " << certificate.getPath() <<
"." << IBRCOMMON_LOGGER_ENDL;
82 throw ibrcommon::IOException(
"Could not read the certificate.");
87 fp = fopen(privateKey.getPath().c_str(),
"r");
88 if(!fp || !PEM_read_PrivateKey(fp, &key, NULL, NULL)){
89 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, error) <<
"Could not read the PrivateKey File: " << privateKey.getPath() <<
"." << IBRCOMMON_LOGGER_ENDL;
93 throw ibrcommon::IOException(
"Could not read the PrivateKey.");
98 if(!trustedCAPath.isDirectory()){
99 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, error) <<
"the trustedCAPath is not valid: " << trustedCAPath.getPath() <<
"." << IBRCOMMON_LOGGER_ENDL;
100 throw ibrcommon::IOException(
"Invalid trustedCAPath.");
105 _trustedCAPath = trustedCAPath;
107 }
catch (
const ibrcommon::IOException &ex) {
114 ibrcommon::MutexLock l(_initialization_lock);
117 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, info) <<
"already initialized." << IBRCOMMON_LOGGER_ENDL;
126 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, info) <<
"Initialization succeeded." << IBRCOMMON_LOGGER_ENDL;
138 return SecurityCertificateManager::TAG;
144 if(!certificate || cn.empty()){
148 X509_NAME *cert_name;
149 X509_NAME_ENTRY *name_entry;
150 ASN1_STRING *eid_string;
152 unsigned char *utf8_eid;
156 if(!(cert_name = X509_get_subject_name(certificate))){
161 eid_string = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING);
167 ASN1_STRING_set(eid_string, cn.c_str(), -1);
169 utf8_eid_len = ASN1_STRING_to_UTF8(&utf8_eid, eid_string);
170 if(utf8_eid_len <= 0){
171 std::stringstream ss; ss <<
"ASN1_STRING_to_UTF8() returned " << utf8_eid_len <<
".";
177 lastpos = X509_NAME_get_index_by_NID(cert_name, NID_commonName, lastpos);
183 name_entry = X509_NAME_get_entry(cert_name, lastpos);
185 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, error) <<
"X509_NAME_get_entry returned NULL unexpectedly." << IBRCOMMON_LOGGER_ENDL;
190 ASN1_STRING *asn1 = X509_NAME_ENTRY_get_data(name_entry);
192 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, error) <<
"X509_NAME_ENTRY_get_data returned NULL unexpectedly." << IBRCOMMON_LOGGER_ENDL;
196 unsigned char *utf8_cert_name;
198 utf8_cert_len = ASN1_STRING_to_UTF8(&utf8_cert_name, asn1);
199 if(utf8_cert_len <= 0){
200 IBRCOMMON_LOGGER_TAG(SecurityCertificateManager::TAG, error) <<
"ASN1_STRING_to_UTF8() returned " << utf8_cert_len <<
"." << IBRCOMMON_LOGGER_ENDL;
205 if(utf8_cert_len != utf8_eid_len){
208 if(memcmp(utf8_eid, utf8_cert_name, utf8_eid_len) == 0){
209 OPENSSL_free(utf8_cert_name);
210 OPENSSL_free(utf8_eid);
213 OPENSSL_free(utf8_cert_name);
216 OPENSSL_free(utf8_eid);
218 char *subject_line = X509_NAME_oneline(cert_name, NULL, 0);
219 std::stringstream ss;
222 ss <<
"Certificate does not fit. Expected: " << cn <<
", Certificate Subject: " << subject_line <<
".";
static Configuration & getInstance(bool reset=false)
virtual void componentUp()
virtual ~SecurityCertificateManager()
const X509 * getCert() const
retrieve the saved certificate
virtual void componentDown()
virtual void onConfigurationChanged(const dtn::daemon::Configuration &conf)
const EVP_PKEY * getPrivateKey() const
retrieve the saved private key
SecurityCertificateManager()
int init(int argc, char **argv)
const ibrcommon::File & getTrustedCAPath() const
retrieve the saved directory holding trusted certificates
bool isInitialized()
checks if this class has already been initialized with a certificate and private key ...
static void validateSubject(X509 *certificate, const std::string &cn)
Validates if the CommonName in the given X509 certificate corresponds to the given EID...
virtual const std::string getName() const