Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "ibrdtn/security/SecurityKey.h"
00009 #include <ibrcommon/Logger.h>
00010 #include <fstream>
00011 #include <sstream>
00012
00013 #include <openssl/pem.h>
00014 #include <openssl/err.h>
00015
00016 namespace dtn
00017 {
00018 namespace security
00019 {
00020 SecurityKey::SecurityKey()
00021 {};
00022
00023 SecurityKey::~SecurityKey()
00024 {};
00025
00026 void SecurityKey::free(RSA* key)
00027 {
00028 RSA_free(key);
00029 }
00030
00031 void SecurityKey::free(EVP_PKEY* key)
00032 {
00033 EVP_PKEY_free(key);
00034 }
00035
00036 const std::string SecurityKey::getData() const
00037 {
00038 std::ifstream stream(file.getPath().c_str(), ios::in);
00039 std::stringstream ss;
00040
00041 ss << stream.rdbuf();
00042
00043 stream.close();
00044
00045 return ss.str();
00046 }
00047
00048 RSA* SecurityKey::getRSA() const
00049 {
00050 switch (type)
00051 {
00052 case KEY_PRIVATE:
00053 return getPrivateRSA();
00054 case KEY_PUBLIC:
00055 return getPublicRSA();
00056 default:
00057 return NULL;
00058 }
00059 }
00060
00061 EVP_PKEY* SecurityKey::getEVP() const
00062 {
00063 EVP_PKEY* ret = EVP_PKEY_new();
00064 FILE * pkey_file = fopen(file.getPath().c_str(), "r");
00065
00066 switch (type)
00067 {
00068 case KEY_PRIVATE:
00069 {
00070 ret = PEM_read_PrivateKey(pkey_file, &ret, NULL, NULL);
00071 break;
00072 }
00073
00074 case KEY_PUBLIC:
00075 {
00076 ret = PEM_read_PUBKEY(pkey_file, &ret, NULL, NULL);
00077 break;
00078 }
00079
00080 default:
00081 ret = NULL;
00082 break;
00083 }
00084
00085 fclose(pkey_file);
00086 return ret;
00087 }
00088
00089 RSA* SecurityKey::getPrivateRSA() const
00090 {
00091 RSA *rsa = RSA_new();
00092
00093 FILE * rsa_pkey_file = fopen(file.getPath().c_str(), "r");
00094 if (!rsa_pkey_file) {
00095 IBRCOMMON_LOGGER_ex(critical) << "Failed to open " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
00096 throw ibrcommon::Exception("Failed to open " + file.getPath());
00097 }
00098 if (!PEM_read_RSAPrivateKey(rsa_pkey_file, &rsa, NULL, NULL)) {
00099 IBRCOMMON_LOGGER_ex(critical) << "Error loading RSA private key file: " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
00100 ERR_print_errors_fp(stderr);
00101 throw ibrcommon::Exception("Error loading RSA private key file: " + file.getPath());
00102 }
00103 fclose(rsa_pkey_file);
00104 return rsa;
00105 }
00106
00107 RSA* SecurityKey::getPublicRSA() const
00108 {
00109 RSA *rsa = RSA_new();
00110
00111 FILE * rsa_pkey_file = fopen(file.getPath().c_str(), "r");
00112 if (!rsa_pkey_file) {
00113 IBRCOMMON_LOGGER_ex(critical) << "Failed to open " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
00114 throw ibrcommon::Exception("Failed to open " + file.getPath());
00115 }
00116 if (!PEM_read_RSA_PUBKEY(rsa_pkey_file, &rsa, NULL, NULL)) {
00117 IBRCOMMON_LOGGER_ex(critical) << "Error loading RSA public key file: " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
00118 ERR_print_errors_fp(stderr);
00119 throw ibrcommon::Exception("Error loading RSA public key file: " + file.getPath());
00120 }
00121 fclose(rsa_pkey_file);
00122 return rsa;
00123 }
00124 }
00125 }