Skip to content

Commit 9f842c0

Browse files
authored
[secrets] store fix (#50)
1 parent 9f33a9f commit 9f842c0

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

components/ssh/include/ssh/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace SSH
1717
{
1818
None = 0,
1919
MalformedKey,
20+
UnexpectedKey,
2021
Unsupported,
2122
InvalidState,
2223
Internal,

components/ssh/include/ssh/keys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace SSH
2424
Error Generate(KeyType keyType, uint32_t bits);
2525

2626
Error Wrap(ssh_key keyPtr);
27+
Error Own(ssh_key keyPtr);
2728
Error Load(const std::string& blob, const std::string& passphrase = std::string());
2829
std::expected<std::string, Error> Marshal(const std::string& passphrase = std::string()) const;
2930

components/ssh/src/keys.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ Error PrivateKey::Load(const std::string& blob, const std::string& passphrase)
9494
return Error::MalformedKey;
9595
}
9696

97+
rc = ssh_key_is_private(keyPtr);
98+
if (rc != 1) {
99+
ESP_LOGE(TAG, "tries to load not a priv key into priv key");
100+
return Error::UnexpectedKey;
101+
};
102+
97103
ssh_keytypes_e targetType = ssh_key_type(keyPtr);
98104
this->keyType = sshKeyType(targetType);
99105
if (this->keyType == KeyType::None) {
@@ -112,13 +118,23 @@ Error PrivateKey::Wrap(ssh_key keyPtr)
112118
return Error::InvalidState;
113119
}
114120

121+
return Own(keyPtr);
122+
}
123+
124+
Error PrivateKey::Own(ssh_key keyPtr)
125+
{
115126
ssh_keytypes_e targetType = ssh_key_type(keyPtr);
116127
this->keyType = sshKeyType(targetType);
117128
if (this->keyType == KeyType::None) {
118129
ESP_LOGE(TAG, "unsupported key type: %s", ssh_key_type_to_char(targetType));
119130
return Error::Unsupported;
120131
}
121132

133+
if (this->keyPtr != nullptr) {
134+
ssh_key_free(this->keyPtr);
135+
this->keyPtr = nullptr;
136+
}
137+
122138
this->keyPtr = keyPtr;
123139
return Error::None;
124140
}

main/secrets.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Error Secrets::Store()
104104
ESP_LOGE(TAG, "unable to store new secret key '%s': %d", SECRET_KEY_KEY, (int)err);
105105
return Error::InvalidSecretKey;
106106
}
107-
ESP_LOGI(TAG, "host key stored in: %s", SECRET_KEY_KEY);
107+
ESP_LOGI(TAG, "secret key stored in: %s", SECRET_KEY_KEY);
108108
} else {
109109
ESP_LOGW(TAG, "ignore empty secret key storing");
110110
}
@@ -144,12 +144,12 @@ Error Secrets::FromJson(const JsonObjectConst& obj) noexcept
144144

145145
Blob::Bytes newSecretKey = Blob::Base64Decode(obj["secret_key"].as<std::string_view>());
146146
if (newSecretKey.empty()) {
147-
ESP_LOGE(TAG, "unable to secret key");
147+
ESP_LOGE(TAG, "unable to parse secret key");
148148
return Error::ShitHappens;
149149
}
150150

151-
this->hostKey = std::move(newHostKey);
152-
this->secretKey = std::move(newSecretKey);
151+
this->hostKey.Own(newHostKey.Copy());
152+
this->secretKey = newSecretKey;
153153
return Error::None;
154154
}
155155

0 commit comments

Comments
 (0)