2019-11-07 09:24:37 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "ProofKey.hpp"
|
|
|
|
#include "LOOLWSD.hpp"
|
|
|
|
|
2020-01-21 09:35:01 -06:00
|
|
|
#include <algorithm>
|
2019-11-07 09:24:37 -06:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
2020-01-21 09:35:01 -06:00
|
|
|
#include <vector>
|
2019-11-07 09:24:37 -06:00
|
|
|
|
|
|
|
#include <Poco/Base64Decoder.h>
|
|
|
|
#include <Poco/Base64Encoder.h>
|
|
|
|
#include <Poco/BinaryWriter.h>
|
|
|
|
#include <Poco/Crypto/RSADigestEngine.h>
|
|
|
|
#include <Poco/Crypto/RSAKey.h>
|
|
|
|
#include <Poco/Dynamic/Var.h>
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
|
|
|
#include <Poco/LineEndingConverter.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/Timestamp.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
2020-02-18 14:46:46 -06:00
|
|
|
#include "Exceptions.hpp"
|
2019-11-07 09:24:37 -06:00
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Util.hpp>
|
|
|
|
|
|
|
|
namespace{
|
|
|
|
|
2020-02-06 03:21:58 -06:00
|
|
|
std::vector<unsigned char> getBytesLE(const unsigned char* bytesInHostOrder, const size_t n)
|
2020-01-21 09:35:01 -06:00
|
|
|
{
|
|
|
|
std::vector<unsigned char> ret(n);
|
|
|
|
#if !defined __BYTE_ORDER__
|
|
|
|
static_assert(false, "Byte order is not detected on this platform!");
|
|
|
|
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2020-02-06 03:21:58 -06:00
|
|
|
std::copy_n(bytesInHostOrder, n, ret.begin());
|
2020-01-21 09:35:01 -06:00
|
|
|
#else
|
2020-02-06 03:21:58 -06:00
|
|
|
std::copy_n(bytesInHostOrder, n, ret.rbegin());
|
2020-01-21 09:35:01 -06:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:21:58 -06:00
|
|
|
std::vector<unsigned char> getBytesBE(const unsigned char* bytesInHostOrder, const size_t n)
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> ret(n);
|
|
|
|
#if !defined __BYTE_ORDER__
|
|
|
|
static_assert(false, "Byte order is not detected on this platform!");
|
|
|
|
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
std::copy_n(bytesInHostOrder, n, ret.rbegin());
|
|
|
|
#else
|
|
|
|
std::copy_n(bytesInHostOrder, n, ret.begin());
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns passed number as vector of bytes (little-endian)
|
2020-01-21 09:35:01 -06:00
|
|
|
template <typename T>
|
2020-02-06 03:21:58 -06:00
|
|
|
std::vector<unsigned char> ToLEBytes(const T& x)
|
2020-01-21 09:35:01 -06:00
|
|
|
{
|
|
|
|
return getBytesLE(reinterpret_cast<const unsigned char*>(&x), sizeof(x));
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:21:58 -06:00
|
|
|
// Returns passed number as vector of bytes (network order = big-endian)
|
|
|
|
template <typename T>
|
|
|
|
std::vector<unsigned char> ToNetworkOrderBytes(const T& x)
|
2020-01-21 09:35:01 -06:00
|
|
|
{
|
2020-02-06 03:21:58 -06:00
|
|
|
return getBytesBE(reinterpret_cast<const unsigned char*>(&x), sizeof(x));
|
2020-01-21 09:35:01 -06:00
|
|
|
}
|
|
|
|
|
2020-04-08 12:25:44 -05:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::string Proof::BytesToBase64(const std::vector<unsigned char>& bytes)
|
2020-01-21 09:35:01 -06:00
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
// The signature generated contains CRLF line endings.
|
|
|
|
// Use a line ending converter to remove these CRLF
|
|
|
|
Poco::OutputLineEndingConverter lineEndingConv(oss, "");
|
|
|
|
Poco::Base64Encoder encoder(lineEndingConv);
|
|
|
|
encoder << std::string(bytes.begin(), bytes.end());
|
|
|
|
encoder.close();
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2020-04-08 12:25:44 -05:00
|
|
|
std::vector<unsigned char> Proof::Base64ToBytes(const std::string &str)
|
|
|
|
{
|
|
|
|
std::istringstream oss(str);
|
|
|
|
Poco::Base64Decoder decoder(oss);
|
|
|
|
|
|
|
|
char c = 0;
|
|
|
|
std::vector<unsigned char> vec;
|
|
|
|
while (decoder.get(c))
|
|
|
|
vec.push_back(c);
|
|
|
|
|
|
|
|
return vec;
|
2020-04-07 15:42:30 -05:00
|
|
|
}
|
2019-11-07 09:24:37 -06:00
|
|
|
|
2020-04-08 15:39:54 -05:00
|
|
|
void Proof::initialize()
|
|
|
|
{
|
|
|
|
if (m_pKey)
|
|
|
|
{
|
|
|
|
const auto m = m_pKey->modulus();
|
|
|
|
const auto e = m_pKey->encryptionExponent();
|
|
|
|
const auto capiBlob = RSA2CapiBlob(m, e);
|
|
|
|
|
2020-07-28 03:12:14 -05:00
|
|
|
const auto sv = BytesToBase64(capiBlob);
|
|
|
|
const auto sm = BytesToBase64(m);
|
|
|
|
const auto se = BytesToBase64(e);
|
|
|
|
|
|
|
|
m_aAttribs.emplace_back("value", sv);
|
|
|
|
m_aAttribs.emplace_back("modulus", sm);
|
|
|
|
m_aAttribs.emplace_back("exponent", se);
|
|
|
|
|
|
|
|
// TODO: implement proper rotation; for now, just duplicate * to old*
|
|
|
|
|
|
|
|
m_aAttribs.emplace_back("oldvalue", sv);
|
|
|
|
m_aAttribs.emplace_back("oldmodulus", sm);
|
|
|
|
m_aAttribs.emplace_back("oldexponent", se);
|
2020-04-08 15:39:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Proof::Proof(Type)
|
|
|
|
: m_pKey(new Poco::Crypto::RSAKey(
|
|
|
|
Poco::Crypto::RSAKey::KeyLength::KL_2048,
|
|
|
|
Poco::Crypto::RSAKey::Exponent::EXP_LARGE))
|
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:24:37 -06:00
|
|
|
Proof::Proof()
|
|
|
|
: m_pKey([]() -> Poco::Crypto::RSAKey* {
|
2020-02-06 03:44:05 -06:00
|
|
|
const auto keyPath = ProofKeyPath();
|
2019-11-07 09:24:37 -06:00
|
|
|
try
|
|
|
|
{
|
2020-02-06 03:44:05 -06:00
|
|
|
return new Poco::Crypto::RSAKey("", keyPath);
|
|
|
|
}
|
|
|
|
catch (const Poco::FileNotFoundException& e)
|
|
|
|
{
|
|
|
|
std::string msg = e.displayText() +
|
|
|
|
"\nNo proof-key will be present in discovery."
|
2020-04-23 08:55:32 -05:00
|
|
|
"\nIf you need to use WOPI security, generate an RSA key using this command:"
|
|
|
|
"\n loolwsd-generate-proof-key"
|
|
|
|
"\nor if your config dir is not /etc, you can run ssh-keygen manually:"
|
|
|
|
"\n ssh-keygen -t rsa -N \"\" -m PEM -f \"" + keyPath + "\""
|
|
|
|
"\nNote: the proof_key file must be readable by the loolwsd process.";
|
2020-02-06 03:44:05 -06:00
|
|
|
LOG_WRN(msg);
|
2019-11-07 09:24:37 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& e)
|
|
|
|
{
|
2020-04-20 02:07:25 -05:00
|
|
|
LOG_ERR("Could not open proof RSA key, Poco exception: " << e.displayText());
|
2019-11-07 09:24:37 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2020-04-20 02:07:25 -05:00
|
|
|
LOG_ERR("Could not open proof RSA key, standard exception: " << e.what());
|
2019-11-07 09:24:37 -06:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2020-02-06 03:44:05 -06:00
|
|
|
LOG_ERR("Could not open proof RSA key: unknown exception");
|
2019-11-07 09:24:37 -06:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}())
|
|
|
|
{
|
2020-04-08 15:39:54 -05:00
|
|
|
initialize();
|
2019-11-07 09:24:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Proof::ProofKeyPath()
|
|
|
|
{
|
2020-02-14 03:00:42 -06:00
|
|
|
static const std::string keyPath =
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
DEBUG_ABSSRCDIR
|
|
|
|
#else
|
|
|
|
LOOLWSD_CONFIGDIR
|
|
|
|
#endif
|
|
|
|
"/proof_key";
|
2019-11-07 09:24:37 -06:00
|
|
|
return keyPath;
|
|
|
|
}
|
|
|
|
|
2020-02-18 14:46:46 -06:00
|
|
|
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-mqqb/ade9efde-3ec8-4e47-9ae9-34b64d8081bb
|
2020-02-06 03:21:58 -06:00
|
|
|
std::vector<unsigned char> Proof::RSA2CapiBlob(const std::vector<unsigned char>& modulus,
|
|
|
|
const std::vector<unsigned char>& exponent)
|
|
|
|
{
|
2020-02-18 14:46:46 -06:00
|
|
|
// Exponent might have arbitrary length in OpenSSL; we need exactly 4
|
|
|
|
if (exponent.size() > 4)
|
|
|
|
throw ParseError("Proof key public exponent is longer than 4 bytes.");
|
|
|
|
// make sure exponent length is correct; assume we are passed big-endian vectors
|
|
|
|
std::vector<unsigned char> exponent32LE(4);
|
|
|
|
std::copy(exponent.rbegin(), exponent.rend(), exponent32LE.begin());
|
|
|
|
|
2020-02-06 03:21:58 -06:00
|
|
|
std::vector<unsigned char> capiBlob = {
|
|
|
|
0x06, 0x02, 0x00, 0x00,
|
|
|
|
0x00, 0xA4, 0x00, 0x00,
|
|
|
|
0x52, 0x53, 0x41, 0x31,
|
|
|
|
};
|
|
|
|
// modulus size in bits - 4 bytes (little-endian)
|
|
|
|
const auto bitLen = ToLEBytes<std::uint32_t>(modulus.size() * 8);
|
2020-02-18 14:46:46 -06:00
|
|
|
capiBlob.reserve(capiBlob.size() + bitLen.size() + exponent32LE.size() + modulus.size());
|
2020-02-06 03:21:58 -06:00
|
|
|
std::copy(bitLen.begin(), bitLen.end(), std::back_inserter(capiBlob));
|
|
|
|
// exponent - 4 bytes (little-endian)
|
2020-02-18 14:46:46 -06:00
|
|
|
std::copy(exponent32LE.begin(), exponent32LE.end(), std::back_inserter(capiBlob));
|
|
|
|
// modulus (passed big-endian, stored little-endian)
|
2020-02-06 03:21:58 -06:00
|
|
|
std::copy(modulus.rbegin(), modulus.rend(), std::back_inserter(capiBlob));
|
|
|
|
return capiBlob;
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:24:37 -06:00
|
|
|
int64_t Proof::DotNetTicks(const std::chrono::system_clock::time_point& utc)
|
|
|
|
{
|
|
|
|
// Get time point for Unix epoch; unfortunately from_time_t isn't constexpr
|
|
|
|
const auto aUnxEpoch(std::chrono::system_clock::from_time_t(0));
|
|
|
|
const auto duration_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(utc - aUnxEpoch);
|
|
|
|
return duration_ns.count() / 100 + 621355968000000000;
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:21:58 -06:00
|
|
|
std::vector<unsigned char> Proof::GetProof(const std::string& access_token, const std::string& uri,
|
|
|
|
int64_t ticks)
|
2019-11-07 09:24:37 -06:00
|
|
|
{
|
2020-02-14 12:01:06 -06:00
|
|
|
assert(access_token.size() <= static_cast<size_t>(std::numeric_limits<int32_t>::max()));
|
|
|
|
std::string uri_upper = uri;
|
|
|
|
for (auto& c : uri_upper)
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
c -= 'a' - 'A';
|
|
|
|
assert(uri_upper.size() <= static_cast<size_t>(std::numeric_limits<int32_t>::max()));
|
|
|
|
const auto access_token_size = ToNetworkOrderBytes<int32_t>(access_token.size());
|
|
|
|
const auto uri_size = ToNetworkOrderBytes<int32_t>(uri_upper.size());
|
2020-02-06 03:21:58 -06:00
|
|
|
const auto ticks_bytes = ToNetworkOrderBytes(ticks);
|
|
|
|
const auto ticks_size = ToNetworkOrderBytes<int32_t>(ticks_bytes.size());
|
2020-02-14 12:01:06 -06:00
|
|
|
const size_t size = access_token_size.size() + access_token.size()
|
|
|
|
+ uri_size.size() + uri_upper.size() + ticks_size.size()
|
2020-02-06 03:21:58 -06:00
|
|
|
+ ticks_bytes.size();
|
|
|
|
std::vector<unsigned char> buf(size);
|
|
|
|
auto pos = std::copy(access_token_size.begin(), access_token_size.end(), buf.begin());
|
2020-02-14 12:01:06 -06:00
|
|
|
pos = std::copy(access_token.begin(), access_token.end(), pos);
|
2020-02-06 03:21:58 -06:00
|
|
|
pos = std::copy(uri_size.begin(), uri_size.end(), pos);
|
2020-02-14 12:01:06 -06:00
|
|
|
pos = std::copy(uri_upper.begin(), uri_upper.end(), pos);
|
2020-02-06 03:21:58 -06:00
|
|
|
pos = std::copy(ticks_size.begin(), ticks_size.end(), pos);
|
|
|
|
std::copy(ticks_bytes.begin(), ticks_bytes.end(), pos);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Proof::SignProof(const std::vector<unsigned char>& proof) const
|
2019-11-07 09:24:37 -06:00
|
|
|
{
|
|
|
|
assert(m_pKey);
|
2020-08-20 10:46:05 -05:00
|
|
|
// One per DocumentBroker that uses this via WopiStorage
|
|
|
|
static thread_local Poco::Crypto::RSADigestEngine digestEngine(*m_pKey, "SHA256");
|
2020-06-22 13:06:34 -05:00
|
|
|
digestEngine.reset();
|
2020-02-06 03:21:58 -06:00
|
|
|
digestEngine.update(proof.data(), proof.size());
|
|
|
|
return BytesToBase64(digestEngine.signature());
|
2019-11-07 09:24:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
VecOfStringPairs Proof::GetProofHeaders(const std::string& access_token, const std::string& uri) const
|
|
|
|
{
|
|
|
|
VecOfStringPairs vec;
|
|
|
|
if (m_pKey)
|
|
|
|
{
|
|
|
|
int64_t ticks = DotNetTicks(std::chrono::system_clock::now());
|
|
|
|
vec.emplace_back("X-WOPI-TimeStamp", std::to_string(ticks));
|
2020-07-28 03:12:14 -05:00
|
|
|
const auto sProof = SignProof(GetProof(access_token, uri, ticks));
|
|
|
|
vec.emplace_back("X-WOPI-Proof", sProof);
|
|
|
|
// TODO: implement proper rotation; for now, just duplicate X-WOPI-Proof to X-WOPI-ProofOld
|
|
|
|
vec.emplace_back("X-WOPI-ProofOld", sProof);
|
2019-11-07 09:24:37 -06:00
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Proof& GetProof()
|
|
|
|
{
|
|
|
|
static const Proof proof;
|
|
|
|
return proof;
|
|
|
|
}
|
|
|
|
|
|
|
|
VecOfStringPairs GetProofHeaders(const std::string& access_token, const std::string& uri)
|
|
|
|
{
|
|
|
|
return GetProof().GetProofHeaders(access_token, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
const VecOfStringPairs& GetProofKeyAttributes()
|
|
|
|
{
|
|
|
|
return GetProof().GetProofKeyAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|