IBR-DTNSuite  0.10
SecurityKey.cpp
Go to the documentation of this file.
1 /*
2  * SecurityKey.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
23 #include <ibrcommon/Logger.h>
24 #include <fstream>
25 #include <sstream>
26 
27 #include <openssl/pem.h>
28 #include <openssl/err.h>
29 
30 namespace dtn
31 {
32  namespace security
33  {
35  : type(KEY_UNSPEC)
36  {}
37 
39  {}
40 
42  {
43  RSA_free(key);
44  }
45 
46  void SecurityKey::free(EVP_PKEY* key)
47  {
48  EVP_PKEY_free(key);
49  }
50 
51  const std::string SecurityKey::getData() const
52  {
53  std::ifstream stream(file.getPath().c_str(), ios::in);
54  std::stringstream ss;
55 
56  ss << stream.rdbuf();
57 
58  stream.close();
59 
60  return ss.str();
61  }
62 
64  {
65  switch (type)
66  {
67  case KEY_PRIVATE:
68  return getPrivateRSA();
69  case KEY_PUBLIC:
70  return getPublicRSA();
71  default:
72  return NULL;
73  }
74  }
75 
76  EVP_PKEY* SecurityKey::getEVP() const
77  {
78  EVP_PKEY* ret = EVP_PKEY_new();
79  FILE * pkey_file = fopen(file.getPath().c_str(), "r");
80 
81  switch (type)
82  {
83  case KEY_PRIVATE:
84  {
85  ret = PEM_read_PrivateKey(pkey_file, &ret, NULL, NULL);
86  break;
87  }
88 
89  case KEY_PUBLIC:
90  {
91  ret = PEM_read_PUBKEY(pkey_file, &ret, NULL, NULL);
92  break;
93  }
94 
95  default:
96  ret = NULL;
97  break;
98  }
99 
100  fclose(pkey_file);
101  return ret;
102  }
103 
104  RSA* SecurityKey::getPrivateRSA() const
105  {
106  RSA *rsa = RSA_new();
107 
108  FILE * rsa_pkey_file = fopen(file.getPath().c_str(), "r");
109  if (!rsa_pkey_file) {
110  IBRCOMMON_LOGGER_ex(critical) << "Failed to open " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
111  throw ibrcommon::Exception("Failed to open " + file.getPath());
112  }
113  if (!PEM_read_RSAPrivateKey(rsa_pkey_file, &rsa, NULL, NULL)) {
114  IBRCOMMON_LOGGER_ex(critical) << "Error loading RSA private key file: " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
115  ERR_print_errors_fp(stderr);
116  throw ibrcommon::Exception("Error loading RSA private key file: " + file.getPath());
117  }
118  fclose(rsa_pkey_file);
119  return rsa;
120  }
121 
122  RSA* SecurityKey::getPublicRSA() const
123  {
124  RSA *rsa = RSA_new();
125 
126  FILE * rsa_pkey_file = fopen(file.getPath().c_str(), "r");
127  if (!rsa_pkey_file) {
128  IBRCOMMON_LOGGER_ex(critical) << "Failed to open " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
129  throw ibrcommon::Exception("Failed to open " + file.getPath());
130  }
131  if (!PEM_read_RSA_PUBKEY(rsa_pkey_file, &rsa, NULL, NULL)) {
132  IBRCOMMON_LOGGER_ex(critical) << "Error loading RSA public key file: " << file.getPath() << IBRCOMMON_LOGGER_ENDL;
133  ERR_print_errors_fp(stderr);
134  throw ibrcommon::Exception("Error loading RSA public key file: " + file.getPath());
135  }
136  fclose(rsa_pkey_file);
137  return rsa;
138  }
139  }
140 }