KeyCertPair.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * KeyCertPair.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "WString.h"
14 
15 namespace Ssl
16 {
21 {
22 public:
23  bool isValid() const
24  {
25  return key && certificate;
26  }
27 
38  bool assign(const uint8_t* newKey, unsigned newKeyLength, const uint8_t* newCertificate,
39  unsigned newCertificateLength, const char* newKeyPassword = nullptr);
40 
41  bool assign(String newKey, String newCertificate, const char* newKeyPassword = nullptr)
42  {
43  key = newKey;
44  certificate = newCertificate;
45  return key && certificate && setPassword(newKeyPassword);
46  }
54  bool assign(const KeyCertPair& keyCert)
55  {
56  *this = keyCert;
57  return (key == keyCert.key) && (keyPassword == keyCert.keyPassword) && (certificate == keyCert.certificate);
58  }
59 
60  void free()
61  {
62  key = nullptr;
63  keyPassword = nullptr;
64  certificate = nullptr;
65  }
66 
67  const uint8_t* getKey() const
68  {
69  return reinterpret_cast<const uint8_t*>(key.c_str());
70  }
71 
72  unsigned getKeyLength() const
73  {
74  return key.length();
75  }
76 
77  const char* getKeyPassword() const
78  {
79  return keyPassword.c_str();
80  }
81 
82  const uint8_t* getCertificate() const
83  {
84  return reinterpret_cast<const uint8_t*>(certificate.c_str());
85  }
86 
87  unsigned getCertificateLength() const
88  {
89  return certificate.length();
90  }
91 
92 private:
93  bool setPassword(const char* newKeyPassword);
94 
95 private:
96  String key;
97  String keyPassword;
98  String certificate;
99 };
100 
101 } // namespace Ssl
Class to manage an SSL key certificate with optional password.
Definition: KeyCertPair.h:21
unsigned getKeyLength() const
Definition: KeyCertPair.h:72
const char * getKeyPassword() const
Definition: KeyCertPair.h:77
bool assign(const uint8_t *newKey, unsigned newKeyLength, const uint8_t *newCertificate, unsigned newCertificateLength, const char *newKeyPassword=nullptr)
Create certificate using provided values.
void free()
Definition: KeyCertPair.h:60
const uint8_t * getCertificate() const
Definition: KeyCertPair.h:82
bool assign(const KeyCertPair &keyCert)
Assign another certificate to this structure.
Definition: KeyCertPair.h:54
unsigned getCertificateLength() const
Definition: KeyCertPair.h:87
const uint8_t * getKey() const
Definition: KeyCertPair.h:67
bool assign(String newKey, String newCertificate, const char *newKeyPassword=nullptr)
Definition: KeyCertPair.h:41
bool isValid() const
Definition: KeyCertPair.h:23
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:617
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:244
Definition: Alert.h:16