Example #1
0
Status
loginServerGetLoginPackage(const Lobby &lobby,
                           DataSlice LP1, DataSlice LRA1, LoginPackage &result, JsonPtr &rootKeyBox)
{
    const auto url = ABC_SERVER_ROOT "/account/loginpackage/get";
    ServerRequestJson json;
    ABC_CHECK(json.setup(lobby));
    if (LP1.size())
        ABC_CHECK(json.authKeySet(base64Encode(LP1)));
    if (LRA1.size())
        ABC_CHECK(json.recoveryAuthKeySet(base64Encode(LRA1)));

    HttpReply reply;
    ABC_CHECK(AirbitzRequest().post(reply, url, json.encode()));
    ServerReplyJson replyJson;
    ABC_CHECK(replyJson.decode(reply.body));
    ABC_CHECK(replyJson.ok());

    struct ResultJson:
        public JsonObject
    {
        ABC_JSON_CONSTRUCTORS(ResultJson, JsonObject)
        ABC_JSON_STRING(package, "login_package", nullptr)
        ABC_JSON_VALUE(rootKeyBox, "rootKeyBox", JsonPtr)
    } resultJson(replyJson.results());

    ABC_CHECK(resultJson.packageOk());
    ABC_CHECK(result.decode(resultJson.package()));
    if (json_is_object(resultJson.rootKeyBox().get()))
        rootKeyBox = resultJson.rootKeyBox();
    return Status();
}
Example #2
0
Status
loginServerChangePassword(const Login &login,
                          DataSlice newLP1, DataSlice newLRA1,
                          const CarePackage &carePackage, const LoginPackage &loginPackage)
{
    const auto url = ABC_SERVER_ROOT "/account/password/update";
    JsonPtr json(json_pack("{ss, ss, ss, ss, ss}",
                           ABC_SERVER_JSON_L1_FIELD,      base64Encode(login.lobby.authId()).c_str(),
                           ABC_SERVER_JSON_LP1_FIELD,     base64Encode(login.authKey()).c_str(),
                           ABC_SERVER_JSON_NEW_LP1_FIELD, base64Encode(newLP1).c_str(),
                           ABC_SERVER_JSON_CARE_PACKAGE_FIELD,  carePackage.encode().c_str(),
                           ABC_SERVER_JSON_LOGIN_PACKAGE_FIELD, loginPackage.encode().c_str()));
    if (newLRA1.size())
    {
        json_object_set_new(json.get(), ABC_SERVER_JSON_NEW_LRA1_FIELD,
                            json_string(base64Encode(newLRA1).c_str()));
    }

    HttpReply reply;
    ABC_CHECK(AirbitzRequest().post(reply, url, json.encode()));
    ServerReplyJson replyJson;
    ABC_CHECK(replyJson.decode(reply.body));
    ABC_CHECK(replyJson.ok());

    return Status();
}
Example #3
0
/**
 * Encodes data in an arbitrary power-of-2 base.
 * @param Bytes number of bytes per chunk of characters.
 * @param Chars number of characters per chunk.
 */
template<unsigned Bytes, unsigned Chars> std::string
chunkEncode(DataSlice data, const char *alphabet)
{
    std::string out;
    auto chunks = (data.size() + Bytes - 1) / Bytes; // Rounding up
    out.reserve(Chars * chunks);

    constexpr unsigned shift = 8 * Bytes / Chars; // Bits per character
    uint16_t buffer = 0; // Bits waiting to be written out, MSB first
    int bits = 0; // Number of bits currently in the buffer
    auto i = data.begin();
    while (i != data.end() || 0 < bits)
    {
        // Reload the buffer if we need more bits:
        if (i != data.end() && bits < shift)
        {
            buffer |= *i++ << (8 - bits);
            bits += 8;
        }

        // Write out the most-significant bits in the buffer:
        out += alphabet[buffer >> (16 - shift)];
        buffer <<= shift;
        bits -= shift;
    }

    // Pad the final string to a multiple of the chunk size:
    out.append(-out.size() % Chars, '=');
    return out;
}
Example #4
0
std::string
toString(DataSlice slice)
{
    // Due to a bug, lots of AirBitz encrypted blobs end with a null byte.
    // Get rid of those:
    auto size = slice.size();
    if (0 < size && !slice.data()[size - 1])
        size--;

    return std::string(reinterpret_cast<const char *>(slice.data()), size);
}
Example #5
0
Status
ScryptSnrp::hash(DataChunk &result, DataSlice data, size_t size) const
{
    DataChunk out(size);

    int rc = crypto_scrypt(data.data(), data.size(),
                           salt.data(), salt.size(), n, r, p, out.data(), size);
    if (rc)
        return ABC_ERROR(ABC_CC_ScryptError, "Error calculating Scrypt hash");

    result = std::move(out);
    return Status();
}
Example #6
0
Status
loginServerChangePassword(const Login &login,
                          DataSlice newLP1, DataSlice newLRA1,
                          const CarePackage &carePackage,
                          const LoginPackage &loginPackage)
{
    const auto url = ABC_SERVER_ROOT "/v1/account/password/update";
    ServerRequestJson json;
    ABC_CHECK(json.setup(login));
    ABC_CHECK(json.set(ABC_SERVER_JSON_NEW_LP1_FIELD, base64Encode(newLP1)));
    ABC_CHECK(json.set(ABC_SERVER_JSON_CARE_PACKAGE_FIELD, carePackage.encode()));
    ABC_CHECK(json.set(ABC_SERVER_JSON_LOGIN_PACKAGE_FIELD, loginPackage.encode()));
    if (newLRA1.size())
    {
        ABC_CHECK(json.set(ABC_SERVER_JSON_NEW_LRA1_FIELD, base64Encode(newLRA1)));
    }

    HttpReply reply;
    ABC_CHECK(AirbitzRequest().post(reply, url, json.encode()));
    ServerReplyJson replyJson;
    ABC_CHECK(replyJson.decode(reply));

    return Status();
}
Example #7
0
/**
 * Casts new-style data to an old-style U08Buf type.
 * DANGER! THIS IS NOT CONST-CORRECT!
 */
inline U08Buf
toU08Buf(DataSlice slice)
{
    return U08Buf(const_cast<uint8_t *>(slice.data()), slice.size());
}