2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/crypto.hh"
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-07-23 23:18:00 +02:00
|
|
|
#include <absl/strings/escaping.h>
|
|
|
|
|
2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/globals.hh"
|
|
|
|
#include "libutil/util.hh"
|
2016-02-16 16:38:44 +01:00
|
|
|
|
|
|
|
#if HAVE_SODIUM
|
|
|
|
#include <sodium.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-08-05 12:03:06 +02:00
|
|
|
// TODO(riking): convert to string_view to reduce allocations
|
2020-05-24 23:29:21 +02:00
|
|
|
static std::pair<std::string, std::string> split(const std::string& s) {
|
2016-02-16 16:38:44 +01:00
|
|
|
size_t colon = s.find(':');
|
|
|
|
if (colon == std::string::npos || colon == 0) {
|
|
|
|
return {"", ""};
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
return {std::string(s, 0, colon), std::string(s, colon + 1)};
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
Key::Key(const std::string& s) {
|
2016-02-16 16:38:44 +01:00
|
|
|
auto ss = split(s);
|
|
|
|
|
|
|
|
name = ss.first;
|
2020-08-05 12:03:06 +02:00
|
|
|
std::string keyb64 = ss.second;
|
2016-02-16 16:38:44 +01:00
|
|
|
|
2020-08-05 12:03:06 +02:00
|
|
|
if (name.empty() || keyb64.empty()) {
|
2016-02-16 16:38:44 +01:00
|
|
|
throw Error("secret key is corrupt");
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
|
2020-08-05 12:03:06 +02:00
|
|
|
if (!absl::Base64Unescape(keyb64, &key)) {
|
2020-07-23 23:18:00 +02:00
|
|
|
// TODO(grfn): replace this with StatusOr
|
|
|
|
throw Error("Invalid Base64");
|
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
SecretKey::SecretKey(const std::string& s) : Key(s) {
|
2016-02-16 16:38:44 +01:00
|
|
|
#if HAVE_SODIUM
|
|
|
|
if (key.size() != crypto_sign_SECRETKEYBYTES) {
|
|
|
|
throw Error("secret key is not valid");
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-15 12:11:27 +01:00
|
|
|
#if !HAVE_SODIUM
|
2016-02-17 12:41:41 +01:00
|
|
|
[[noreturn]] static void noSodium() {
|
|
|
|
throw Error(
|
|
|
|
"Nix was not compiled with libsodium, required for signed binary cache "
|
|
|
|
"support");
|
|
|
|
}
|
2016-03-15 12:11:27 +01:00
|
|
|
#endif
|
2016-02-17 12:41:41 +01:00
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
std::string SecretKey::signDetached(const std::string& data) const {
|
|
|
|
#if HAVE_SODIUM
|
|
|
|
unsigned char sig[crypto_sign_BYTES];
|
2020-08-02 00:32:00 +02:00
|
|
|
unsigned long long sigLen;
|
2016-02-16 16:38:44 +01:00
|
|
|
crypto_sign_detached(sig, &sigLen, (unsigned char*)data.data(), data.size(),
|
|
|
|
(unsigned char*)key.data());
|
2020-08-02 02:17:44 +02:00
|
|
|
return name + ":" +
|
|
|
|
absl::Base64Escape(std::string(reinterpret_cast<char*>(sig), sigLen));
|
2016-02-16 16:38:44 +01:00
|
|
|
#else
|
2016-02-17 12:41:41 +01:00
|
|
|
noSodium();
|
2016-02-16 16:38:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-04 17:08:30 +01:00
|
|
|
PublicKey SecretKey::toPublicKey() const {
|
|
|
|
#if HAVE_SODIUM
|
|
|
|
unsigned char pk[crypto_sign_PUBLICKEYBYTES];
|
|
|
|
crypto_sign_ed25519_sk_to_pk(pk, (unsigned char*)key.data());
|
2020-08-02 02:17:44 +02:00
|
|
|
return PublicKey(name, std::string(reinterpret_cast<char*>(pk),
|
|
|
|
crypto_sign_PUBLICKEYBYTES));
|
2016-03-04 17:08:30 +01:00
|
|
|
#else
|
|
|
|
noSodium();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
PublicKey::PublicKey(const std::string& s) : Key(s) {
|
2016-02-16 16:38:44 +01:00
|
|
|
#if HAVE_SODIUM
|
|
|
|
if (key.size() != crypto_sign_PUBLICKEYBYTES) {
|
|
|
|
throw Error("public key is not valid");
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool verifyDetached(const std::string& data, const std::string& sig,
|
|
|
|
const PublicKeys& publicKeys) {
|
2016-02-17 12:41:41 +01:00
|
|
|
#if HAVE_SODIUM
|
2016-02-16 16:38:44 +01:00
|
|
|
auto ss = split(sig);
|
|
|
|
|
|
|
|
auto key = publicKeys.find(ss.first);
|
|
|
|
if (key == publicKeys.end()) {
|
|
|
|
return false;
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
|
2020-07-23 23:18:00 +02:00
|
|
|
std::string sig2;
|
|
|
|
if (!absl::Base64Unescape(ss.second, &sig2)) {
|
|
|
|
// TODO(grfn): replace this with StatusOr
|
|
|
|
throw Error("Invalid Base64");
|
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
if (sig2.size() != crypto_sign_BYTES) {
|
|
|
|
throw Error("signature is not valid");
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-02-16 16:38:44 +01:00
|
|
|
|
|
|
|
return crypto_sign_verify_detached(
|
2020-08-02 02:17:44 +02:00
|
|
|
reinterpret_cast<unsigned char*>(sig2.data()),
|
|
|
|
(unsigned char*)data.data(), data.size(),
|
|
|
|
(unsigned char*)key->second.key.data()) == 0;
|
2016-02-17 12:41:41 +01:00
|
|
|
#else
|
|
|
|
noSodium();
|
|
|
|
#endif
|
2016-02-16 16:38:44 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 14:29:50 +02:00
|
|
|
PublicKeys getDefaultPublicKeys() {
|
|
|
|
PublicKeys publicKeys;
|
2016-04-07 15:07:00 +02:00
|
|
|
|
|
|
|
// FIXME: filter duplicates
|
|
|
|
|
2020-05-20 23:58:43 +02:00
|
|
|
for (const auto& s : settings.trustedPublicKeys.get()) {
|
2016-03-29 14:29:50 +02:00
|
|
|
PublicKey key(s);
|
|
|
|
publicKeys.emplace(key.name, key);
|
|
|
|
}
|
2016-04-07 15:07:00 +02:00
|
|
|
|
2020-05-20 23:58:43 +02:00
|
|
|
for (const auto& secretKeyFile : settings.secretKeyFiles.get()) {
|
2016-04-07 15:07:00 +02:00
|
|
|
try {
|
|
|
|
SecretKey secretKey(readFile(secretKeyFile));
|
|
|
|
publicKeys.emplace(secretKey.name, secretKey.toPublicKey());
|
|
|
|
} catch (SysError& e) {
|
|
|
|
/* Ignore unreadable key files. That's normal in a
|
|
|
|
multi-user installation. */
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2016-04-07 15:07:00 +02:00
|
|
|
|
2016-03-29 14:29:50 +02:00
|
|
|
return publicKeys;
|
|
|
|
}
|
|
|
|
|
2016-02-16 16:38:44 +01:00
|
|
|
} // namespace nix
|