Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
SslKeyCertPair.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * SslKeyCertPair.h
8  *
9  ****/
10 
11 #ifndef _SMING_CORE_NETWORK_SSL_KEY_CERT_PAIR_H_
12 #define _SMING_CORE_NETWORK_SSL_KEY_CERT_PAIR_H_
13 
14 #include "ssl/ssl.h"
15 #include "WString.h"
16 
20 {
21 public:
22  bool isValid()
23  {
24  return key && certificate;
25  }
26 
36  bool assign(const uint8_t* newKey, unsigned newKeyLength, const uint8_t* newCertificate,
37  unsigned newCertificateLength, const char* newKeyPassword = nullptr)
38  {
39  free();
40 
41  if(newKeyLength != 0 && newKey != nullptr) {
42  if(!key.setLength(newKeyLength)) {
43  return false;
44  }
45  memcpy(key.begin(), newKey, newKeyLength);
46  }
47 
48  if(newCertificateLength != 0 && newCertificate != nullptr) {
49  if(!certificate.setLength(newCertificateLength)) {
50  return false;
51  }
52  memcpy(certificate.begin(), newCertificate, newCertificateLength);
53  }
54 
55  unsigned passwordLength = (newKeyPassword == nullptr) ? 0 : strlen(newKeyPassword);
56  if(passwordLength != 0) {
57  keyPassword.setString(newKeyPassword, passwordLength);
58  if(!keyPassword) {
59  return false;
60  }
61  }
62 
63  return true;
64  }
65 
71  bool assign(const SslKeyCertPair& keyCert)
72  {
73  *this = keyCert;
74  return (key == keyCert.key) && (keyPassword == keyCert.keyPassword) && (certificate == keyCert.certificate);
75  }
76 
77  void free()
78  {
79  key = nullptr;
80  keyPassword = nullptr;
81  certificate = nullptr;
82  }
83 
84  const uint8_t* getKey()
85  {
86  return reinterpret_cast<const uint8_t*>(key.c_str());
87  }
88 
89  unsigned getKeyLength()
90  {
91  return key.length();
92  }
93 
94  const char* getKeyPassword()
95  {
96  return keyPassword.c_str();
97  }
98 
99  const uint8_t* getCertificate()
100  {
101  return reinterpret_cast<const uint8_t*>(certificate.c_str());
102  }
103 
104  unsigned getCertificateLength()
105  {
106  return certificate.length();
107  }
108 
109 private:
110  String key;
111  String keyPassword;
112  String certificate;
113 };
114 
115 #endif /* _SMING_CORE_NETWORK_SSL_KEY_CERT_PAIR_H_ */
bool setLength(unsigned int length)
set the string length accordingly, expanding if necessary
bool assign(const SslKeyCertPair &keyCert)
Assign another certificate to this structure.
Definition: SslKeyCertPair.h:71
The string class.
Definition: WString.h:104
bool assign(const uint8_t *newKey, unsigned newKeyLength, const uint8_t *newCertificate, unsigned newCertificateLength, const char *newKeyPassword=nullptr)
Create certificate using provided values.
Definition: SslKeyCertPair.h:36
Class to manage an SSL key certificate with optional password.
Definition: SslKeyCertPair.h:19