Ejemplo n.º 1
0
bool checkDigest(const String& source, uint8_t hashAlgorithmsUsed, const CSPDirectiveListVector& policies)
{
    // Any additions or subtractions from this struct should also modify the
    // respective entries in the kSupportedPrefixes array in
    // CSPSourceList::parseHash().
    static const struct {
        ContentSecurityPolicyHashAlgorithm cspHashAlgorithm;
        HashAlgorithm algorithm;
    } kAlgorithmMap[] = {
        { ContentSecurityPolicyHashAlgorithmSha1, HashAlgorithmSha1 },
        { ContentSecurityPolicyHashAlgorithmSha256, HashAlgorithmSha256 },
        { ContentSecurityPolicyHashAlgorithmSha384, HashAlgorithmSha384 },
        { ContentSecurityPolicyHashAlgorithmSha512, HashAlgorithmSha512 }
    };

    // Only bother normalizing the source/computing digests if there are any checks to be done.
    if (hashAlgorithmsUsed == ContentSecurityPolicyHashAlgorithmNone)
        return false;

    StringUTF8Adaptor utf8Source(source);

    for (const auto& algorithmMap : kAlgorithmMap) {
        DigestValue digest;
        if (algorithmMap.cspHashAlgorithm & hashAlgorithmsUsed) {
            bool digestSuccess = computeDigest(algorithmMap.algorithm, utf8Source.data(), utf8Source.length(), digest);
            if (digestSuccess && isAllowedByAllWithHash<allowed>(policies, CSPHashValue(algorithmMap.cspHashAlgorithm, digest)))
                return true;
        }
    }

    return false;
}
Ejemplo n.º 2
0
int UtlCryptoKeyRsa::sign(const unsigned char* pSrc,
                          int srcLen,
                          unsigned char* pDest,
                          int* pDestLen) const
{
   if (!pSrc || !srcLen || !pDest || !pDestLen ||
      *pDestLen < getMaxSignatureSize(srcLen))
   {
      if (pDestLen)
         *pDestLen = 0;
      return 0;
   }

   *pDestLen = 0;

   // First, compute the Message Digest (MD) of the source data
   int mdLen = EVP_MAX_MD_SIZE;
   unsigned char md[EVP_MAX_MD_SIZE];
   if (computeDigest(pSrc, srcLen, &md[0], &mdLen) == 0)
      return 0;

   // Next, sign the Message Digest & return that
   if (!RSA_sign(getDigestAlgType(), &md[0], mdLen,
      pDest, (unsigned*)pDestLen, mpRsa))
   {
      osPrintf("*****RSA_sign failed");
      return 0;
   }

   setLastError(0);
   return *pDestLen;
}
bool SubresourceIntegrity::CheckSubresourceIntegrity(const IntegrityMetadataSet& metadataSet, const char* content, size_t size, const KURL& resourceUrl, Document& document, String& errorMessage)
{
    if (!metadataSet.size())
        return true;

    HashAlgorithm strongestAlgorithm = HashAlgorithmSha256;
    for (const IntegrityMetadata& metadata : metadataSet)
        strongestAlgorithm = getPrioritizedHashFunction(metadata.algorithm(), strongestAlgorithm);

    DigestValue digest;
    for (const IntegrityMetadata& metadata : metadataSet) {
        if (metadata.algorithm() != strongestAlgorithm)
            continue;

        digest.clear();
        bool digestSuccess = computeDigest(metadata.algorithm(), content, size, digest);

        if (digestSuccess) {
            Vector<char> hashVector;
            base64Decode(metadata.digest(), hashVector);
            DigestValue convertedHashVector;
            convertedHashVector.append(reinterpret_cast<uint8_t*>(hashVector.data()), hashVector.size());

            if (DigestsEqual(digest, convertedHashVector)) {
                UseCounter::count(document, UseCounter::SRIElementWithMatchingIntegrityAttribute);
                return true;
            }
        }
    }

    digest.clear();
    if (computeDigest(HashAlgorithmSha256, content, size, digest)) {
        // This message exposes the digest of the resource to the console.
        // Because this is only to the console, that's okay for now, but we
        // need to be very careful not to expose this in exceptions or
        // JavaScript, otherwise it risks exposing information about the
        // resource cross-origin.
        errorMessage = "Failed to find a valid digest in the 'integrity' attribute for resource '" + resourceUrl.elidedString() + "' with computed SHA-256 integrity '" + digestToString(digest) + "'. The resource has been blocked.";
    } else {
        errorMessage = "There was an error computing an integrity value for resource '" + resourceUrl.elidedString() + "'. The resource has been blocked.";
    }
    UseCounter::count(document, UseCounter::SRIElementWithNonMatchingIntegrityAttribute);
    return false;
}
Ejemplo n.º 4
0
String WebSocketHandshake::getExpectedWebSocketAccept(const String& secWebSocketKey)
{
    static const char webSocketKeyGUID[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    CString keyData = secWebSocketKey.ascii();

    StringBuilder digestable;
    digestable.append(secWebSocketKey);
    digestable.append(webSocketKeyGUID, strlen(webSocketKeyGUID));
    CString digestableCString = digestable.toString().utf8();
    DigestValue digest;
    bool digestSuccess = computeDigest(HashAlgorithmSha1, digestableCString.data(), digestableCString.length(), digest);
    RELEASE_ASSERT(digestSuccess);

    return base64Encode(reinterpret_cast<const char*>(digest.data()), sha1HashSize);
}
Ejemplo n.º 5
0
int UtlCryptoKeyRsa::verify(const unsigned char* pSrc,
                            int srcLen,
                            const unsigned char* pSig,
                            int sigLen) const
{
   // First, compute the Message Digest (MD) of the source data
   int mdLen = EVP_MAX_MD_SIZE;
   unsigned char md[EVP_MAX_MD_SIZE];
   if (computeDigest(pSrc, srcLen, &md[0], &mdLen) == 0)
      return -1;

   // Next, see if the signature matches the MD
   if (!RSA_verify(getDigestAlgType(), &md[0], mdLen,
      const_cast<unsigned char*>(pSig), sigLen, mpRsa))
   {
      return -1;
   }

   return 0;
}
Ejemplo n.º 6
0
//Copied from aegis-crypto0 bin/accli.c
bool PackageUtils::createSignature(int ih, int oh, const char* resource_id)
{
    int mdlen;
    unsigned char digest[DIGESTLEN];
    struct aegis_signature_t signature;
    aegis_crypto_result res;

    mdlen = computeDigest(ih, digest, sizeof(digest));
    if (0 == mdlen) {
        qDebug()<<"Could not calculate digest in "<<Q_FUNC_INFO;
        return false;
    }

    res = aegis_crypto_sign(digest, mdlen, resource_id, &signature);
    if (aegis_crypto_ok != res) {
        qDebug()<<"Failed to sign the signature in "<<Q_FUNC_INFO<<aegis_crypto_last_error_str();
        return false;
    }

    char* str_sig = NULL;
    if (0 < aegis_crypto_signature_to_string(&signature,
                                             aegis_as_hexstring,
                                            resource_id,
                                             &str_sig)){
        //signature creation successfull. Write it to file
        ssize_t len = strlen(str_sig);
        ssize_t written = write(oh, str_sig, len);
        if (written < len) {
            qDebug()<<"Can not write outputfile"<<Q_FUNC_INFO;
        }
    }
    //write terminating charactersignature
    write(oh,"\n",1);
    if (NULL != str_sig)
        aegis_crypto_free(str_sig);
    return true;
}
Ejemplo n.º 7
0
User::User(const UserName& name) : _name(name), _digest(computeDigest(_name)) {}
Ejemplo n.º 8
0
User::User(const UserName& name)
    : _name(name), _digest(computeDigest(_name)), _refCount(0), _isValid(1) {}