Example #1
0
// decode s from base64, XMLencoded or read from the file name s
const char *WMSUtilDecode(CPLString &s, const char *encoding) {
    if (EQUAL(encoding, "base64")) {
        std::vector<char> buffer(s.begin(), s.end());
        buffer.push_back('\0');
        int nSize =
            CPLBase64DecodeInPlace(reinterpret_cast<GByte *>(&buffer[0]));
        s.assign(&buffer[0], nSize);
    }
    else if (EQUAL(encoding, "XMLencoded")) {
        int len = static_cast<int>(s.size());
        char *result = CPLUnescapeString(s.c_str(), &len, CPLES_XML);
        s.assign(result, static_cast<size_t>(len));
        CPLFree(result);
    }
    else if (EQUAL(encoding, "file")) { // Not an encoding but an external file
        VSILFILE *f = VSIFOpenL(s.c_str(), "rb");
        s.clear(); // Return an empty string if file can't be opened or read
        if (f) {
            VSIFSeekL(f, 0, SEEK_END);
            size_t size = static_cast<size_t>(VSIFTellL(f));
            VSIFSeekL(f, 0, SEEK_SET);
            std::vector<char> buffer(size);
            if (VSIFReadL(reinterpret_cast<void *>(&buffer[0]), size, 1, f))
                s.assign(&buffer[0], buffer.size());
            VSIFCloseL(f);
        }
    }
    return s.c_str();
}
Example #2
0
bool Account::checkSupported()
{
    Settings &settings = Settings::instance();
    // Read user id, start/end dates, account type
    std::string userId, startDate, endDate, accountType("true"), sign;
    bool supported = settings.getBool("account/supported", false);
    if(!supported) {
        warningMessage(_("Account is not supported"));
        return false;
    }

    userId = settings.getString("account/user_id", "");
    startDate = settings.getString("account/start_date", "");
    endDate = settings.getString("account/end_date", "");
    sign = settings.getString("account/sign", "");

    std::string baMessage = userId + startDate + endDate + accountType;

    GByte *key = reinterpret_cast<GByte*>(CPLStrdup(sign.c_str()));
    int nLength = CPLBase64DecodeInPlace(key);
    std::string baSignature;
    baSignature.assign(reinterpret_cast<const char*>(key),
                       static_cast<size_t>(nLength));
    memset(key, 0, baSignature.size());
    CPLFree(key);

    bool verify = verifyRSASignature(
                reinterpret_cast<const unsigned char*>(baMessage.c_str()),
                static_cast<unsigned int>(baMessage.size()),
                reinterpret_cast<const unsigned char*>(baSignature.c_str()),
                static_cast<unsigned int>(baSignature.size()));
    if(!verify) {
        return false;
    }

    time_t current = time(nullptr);
    time_t start = timeFromString(startDate.c_str());
    time_t end = timeFromString(endDate.c_str());
    bool out = current >= start && current <= end;
    if(!out) {
        warningMessage(_("Account is supported. Verify success. Period expired."));
    }
    return out;
}