| Index: content/renderer/webcrypto/shared_crypto.cc
|
| diff --git a/content/renderer/webcrypto/shared_crypto.cc b/content/renderer/webcrypto/shared_crypto.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a42e4d1c93535df4f20e056418546fe60163a1db
|
| --- /dev/null
|
| +++ b/content/renderer/webcrypto/shared_crypto.cc
|
| @@ -0,0 +1,465 @@
|
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/renderer/webcrypto/shared_crypto.h"
|
| +
|
| +#include "base/logging.h"
|
| +#include "content/renderer/webcrypto/crypto_data.h"
|
| +#include "content/renderer/webcrypto/platform_crypto.h"
|
| +#include "content/renderer/webcrypto/webcrypto_util.h"
|
| +#include "crypto/secure_util.h"
|
| +#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
|
| +#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
|
| +#include "third_party/WebKit/public/platform/WebCryptoKey.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace webcrypto {
|
| +
|
| +namespace {
|
| +
|
| +// TODO(eroman): Move this helper to WebCryptoKey.
|
| +bool KeyUsageAllows(const blink::WebCryptoKey& key,
|
| + const blink::WebCryptoKeyUsage usage) {
|
| + return ((key.usages() & usage) != 0);
|
| +}
|
| +
|
| +const size_t kAesBlockSizeBytes = 16;
|
| +
|
| +Status EncryptAesCbc(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::SymKey* sym_key = platform::ToSymKey(key);
|
| + if (!sym_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + const blink::WebCryptoAesCbcParams* params = algorithm.aesCbcParams();
|
| + if (!params)
|
| + return Status::ErrorUnexpected();
|
| +
|
| + CryptoData iv(params->iv().data(), params->iv().size());
|
| + if (iv.byte_length() != kAesBlockSizeBytes)
|
| + return Status::ErrorIncorrectSizeAesCbcIv();
|
| +
|
| + return platform::EncryptAesCbc(sym_key, iv, data, buffer);
|
| +}
|
| +
|
| +Status DecryptAesCbc(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::SymKey* sym_key = platform::ToSymKey(key);
|
| + if (!sym_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + const blink::WebCryptoAesCbcParams* params = algorithm.aesCbcParams();
|
| + if (!params)
|
| + return Status::ErrorUnexpected();
|
| +
|
| + CryptoData iv(params->iv().data(), params->iv().size());
|
| + if (iv.byte_length() != kAesBlockSizeBytes)
|
| + return Status::ErrorIncorrectSizeAesCbcIv();
|
| +
|
| + return platform::DecryptAesCbc(sym_key, iv, data, buffer);
|
| +}
|
| +
|
| +Status EncryptAesGcm(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::SymKey* sym_key = platform::ToSymKey(key);
|
| + if (!sym_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams();
|
| + if (!params)
|
| + return Status::ErrorUnexpected();
|
| +
|
| + return platform::EncryptAesGcm(sym_key, params, data, buffer);
|
| +}
|
| +
|
| +Status DecryptAesGcm(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::SymKey* sym_key = platform::ToSymKey(key);
|
| + if (!sym_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams();
|
| + if (!params)
|
| + return Status::ErrorUnexpected();
|
| +
|
| + return platform::DecryptAesGcm(sym_key, params, data, buffer);
|
| +}
|
| +
|
| +Status EncryptRsaEsPkcs1v1_5(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::PublicKey* public_key = platform::ToPublicKey(key);
|
| + if (!public_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + // RSAES encryption does not support empty input
|
| + if (!data.byte_length())
|
| + return Status::Error();
|
| +
|
| + return platform::EncryptRsaEsPkcs1v1_5(public_key, data, buffer);
|
| +}
|
| +
|
| +Status DecryptRsaEsPkcs1v1_5(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::PrivateKey* private_key = platform::ToPrivateKey(key);
|
| + if (!private_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + // RSAES decryption does not support empty input
|
| + if (!data.byte_length())
|
| + return Status::Error();
|
| +
|
| + return platform::DecryptRsaEsPkcs1v1_5(private_key, data, buffer);
|
| +}
|
| +
|
| +Status SignHmac(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::SymKey* sym_key = platform::ToSymKey(key);
|
| + if (!sym_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + const blink::WebCryptoHmacParams* params = algorithm.hmacParams();
|
| + if (!params)
|
| + return Status::ErrorUnexpected();
|
| +
|
| + if (!IsHashAlgorithm(params->hash().id()))
|
| + return Status::ErrorUnexpected();
|
| +
|
| + if (params->hash().id() != GetInnerHashAlgorithm(key.algorithm()).id())
|
| + return Status::ErrorUnexpected();
|
| +
|
| + return platform::SignHmac(sym_key, params->hash(), data, buffer);
|
| +}
|
| +
|
| +Status VerifyHmac(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& signature,
|
| + const CryptoData& data,
|
| + bool* signature_match) {
|
| + blink::WebArrayBuffer result;
|
| + Status status = SignHmac(algorithm, key, data, &result);
|
| + if (status.IsError())
|
| + return status;
|
| +
|
| + // Do not allow verification of truncated signatures.
|
| + *signature_match =
|
| + result.byteLength() == signature.byte_length() &&
|
| + crypto::SecureMemEqual(
|
| + result.data(), signature.bytes(), signature.byte_length());
|
| +
|
| + return Status::Success();
|
| +}
|
| +
|
| +Status SignRsaSsaPkcs1v1_5(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + platform::PrivateKey* private_key = platform::ToPrivateKey(key);
|
| + if (!private_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + const blink::WebCryptoRsaSsaParams* params = algorithm.rsaSsaParams();
|
| + if (!params)
|
| + return Status::ErrorUnexpected();
|
| +
|
| + if (!IsHashAlgorithm(params->hash().id()))
|
| + return Status::ErrorUnexpected();
|
| +
|
| + // TODO(eroman): Verify the key has not been used with any other hash.
|
| +
|
| + return platform::SignRsaSsaPkcs1v1_5(
|
| + private_key, params->hash(), data, buffer);
|
| +}
|
| +
|
| +Status VerifyRsaSsaPkcs1v1_5(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& signature,
|
| + const CryptoData& data,
|
| + bool* signature_match) {
|
| + platform::PublicKey* public_key = platform::ToPublicKey(key);
|
| + if (!public_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| +
|
| + const blink::WebCryptoRsaSsaParams* params = algorithm.rsaSsaParams();
|
| + if (!params)
|
| + return Status::ErrorUnexpected();
|
| +
|
| + if (!IsHashAlgorithm(params->hash().id()))
|
| + return Status::ErrorUnexpected();
|
| +
|
| + // TODO(eroman): Verify the key has not been used with any other hash.
|
| +
|
| + return platform::VerifyRsaSsaPkcs1v1_5(
|
| + public_key, params->hash(), signature, data, signature_match);
|
| +}
|
| +
|
| +Status ImportKeyRaw(const CryptoData& key_data,
|
| + const blink::WebCryptoAlgorithm& algorithm_or_null,
|
| + bool extractable,
|
| + blink::WebCryptoKeyUsageMask usage_mask,
|
| + blink::WebCryptoKey* key) {
|
| + if (algorithm_or_null.isNull())
|
| + return Status::ErrorMissingAlgorithmImportRawKey();
|
| +
|
| + switch (algorithm_or_null.id()) {
|
| + case blink::WebCryptoAlgorithmIdAesCbc:
|
| + if (key_data.byte_length() != 16 && key_data.byte_length() != 24 &&
|
| + key_data.byte_length() != 32) {
|
| + return Status::Error();
|
| + }
|
| + // Fallthrough intentional!
|
| + case blink::WebCryptoAlgorithmIdHmac:
|
| + case blink::WebCryptoAlgorithmIdAesKw:
|
| + case blink::WebCryptoAlgorithmIdAesGcm:
|
| + return platform::ImportKeyRaw(
|
| + key_data, algorithm_or_null, extractable, usage_mask, key);
|
| +
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +void Init() {
|
| + platform::Init();
|
| +}
|
| +
|
| +Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageEncrypt))
|
| + return Status::ErrorUnexpected();
|
| + if (algorithm.id() != key.algorithm().id())
|
| + return Status::ErrorUnexpected();
|
| +
|
| + switch (algorithm.id()) {
|
| + case blink::WebCryptoAlgorithmIdAesCbc:
|
| + return EncryptAesCbc(algorithm, key, data, buffer);
|
| + case blink::WebCryptoAlgorithmIdAesGcm:
|
| + return EncryptAesGcm(algorithm, key, data, buffer);
|
| + case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5:
|
| + return EncryptRsaEsPkcs1v1_5(algorithm, key, data, buffer);
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageDecrypt))
|
| + return Status::ErrorUnexpected();
|
| + if (algorithm.id() != key.algorithm().id())
|
| + return Status::ErrorUnexpected();
|
| +
|
| + switch (algorithm.id()) {
|
| + case blink::WebCryptoAlgorithmIdAesCbc:
|
| + return DecryptAesCbc(algorithm, key, data, buffer);
|
| + case blink::WebCryptoAlgorithmIdAesGcm:
|
| + return DecryptAesGcm(algorithm, key, data, buffer);
|
| + case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5:
|
| + return DecryptRsaEsPkcs1v1_5(algorithm, key, data, buffer);
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +Status Digest(const blink::WebCryptoAlgorithm& algorithm,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + switch (algorithm.id()) {
|
| + case blink::WebCryptoAlgorithmIdSha1:
|
| + case blink::WebCryptoAlgorithmIdSha224:
|
| + case blink::WebCryptoAlgorithmIdSha256:
|
| + case blink::WebCryptoAlgorithmIdSha384:
|
| + case blink::WebCryptoAlgorithmIdSha512:
|
| + return platform::DigestSha(algorithm.id(), data, buffer);
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
|
| + bool extractable,
|
| + blink::WebCryptoKeyUsageMask usage_mask,
|
| + blink::WebCryptoKey* key) {
|
| + unsigned int keylen_bytes = 0;
|
| +
|
| + // Get the secret key length in bytes from generation parameters.
|
| + // This resolves any defaults.
|
| + switch (algorithm.id()) {
|
| + case blink::WebCryptoAlgorithmIdAesCbc:
|
| + case blink::WebCryptoAlgorithmIdAesGcm:
|
| + case blink::WebCryptoAlgorithmIdAesKw: {
|
| + if ((algorithm.aesKeyGenParams()->lengthBits() % 8) != 0)
|
| + return Status::ErrorGenerateKeyLength();
|
| + keylen_bytes = algorithm.aesKeyGenParams()->lengthBits() / 8;
|
| + break;
|
| + }
|
| + case blink::WebCryptoAlgorithmIdHmac: {
|
| + const blink::WebCryptoHmacKeyParams* params = algorithm.hmacKeyParams();
|
| + DCHECK(params);
|
| + if (params->hasLengthBytes()) {
|
| + keylen_bytes = params->optionalLengthBytes();
|
| + } else {
|
| + keylen_bytes = ShaBlockSizeBytes(params->hash().id());
|
| + if (keylen_bytes == 0)
|
| + return Status::ErrorUnsupported();
|
| + }
|
| + break;
|
| + }
|
| +
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +
|
| + // TODO(eroman): Is this correct? HMAC can import zero-length keys, so should
|
| + // probably be able to allowed to generate them too.
|
| + if (keylen_bytes == 0)
|
| + return Status::ErrorGenerateKeyLength();
|
| +
|
| + return platform::GenerateSecretKey(
|
| + algorithm, extractable, usage_mask, keylen_bytes, key);
|
| +}
|
| +
|
| +Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm,
|
| + bool extractable,
|
| + blink::WebCryptoKeyUsageMask usage_mask,
|
| + blink::WebCryptoKey* public_key,
|
| + blink::WebCryptoKey* private_key) {
|
| + // TODO(padolph): Handle other asymmetric algorithm key generation.
|
| + switch (algorithm.id()) {
|
| + case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5:
|
| + case blink::WebCryptoAlgorithmIdRsaOaep:
|
| + case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
|
| + if (!algorithm.rsaKeyGenParams())
|
| + return Status::ErrorUnexpected();
|
| + return platform::GenerateRsaKeyPair(
|
| + algorithm, extractable, usage_mask, public_key, private_key);
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +Status ImportKey(blink::WebCryptoKeyFormat format,
|
| + const CryptoData& key_data,
|
| + const blink::WebCryptoAlgorithm& algorithm_or_null,
|
| + bool extractable,
|
| + blink::WebCryptoKeyUsageMask usage_mask,
|
| + blink::WebCryptoKey* key) {
|
| + switch (format) {
|
| + case blink::WebCryptoKeyFormatRaw:
|
| + return ImportKeyRaw(
|
| + key_data, algorithm_or_null, extractable, usage_mask, key);
|
| + return platform::ImportKeyRaw(
|
| + key_data, algorithm_or_null, extractable, usage_mask, key);
|
| + case blink::WebCryptoKeyFormatSpki:
|
| + return platform::ImportKeySpki(
|
| + key_data, algorithm_or_null, extractable, usage_mask, key);
|
| + case blink::WebCryptoKeyFormatPkcs8:
|
| + return platform::ImportKeyPkcs8(
|
| + key_data, algorithm_or_null, extractable, usage_mask, key);
|
| + case blink::WebCryptoKeyFormatJwk:
|
| + return ImportKeyJwk(
|
| + key_data, algorithm_or_null, extractable, usage_mask, key);
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +Status ExportKey(blink::WebCryptoKeyFormat format,
|
| + const blink::WebCryptoKey& key,
|
| + blink::WebArrayBuffer* buffer) {
|
| + if (!key.extractable())
|
| + return Status::ErrorKeyNotExtractable();
|
| +
|
| + switch (format) {
|
| + case blink::WebCryptoKeyFormatRaw: {
|
| + platform::SymKey* sym_key = platform::ToSymKey(key);
|
| + if (!sym_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| + return platform::ExportKeyRaw(sym_key, buffer);
|
| + }
|
| + case blink::WebCryptoKeyFormatSpki: {
|
| + platform::PublicKey* public_key = platform::ToPublicKey(key);
|
| + if (!public_key)
|
| + return Status::ErrorUnexpectedKeyType();
|
| + return platform::ExportKeySpki(public_key, buffer);
|
| + }
|
| + case blink::WebCryptoKeyFormatPkcs8:
|
| + case blink::WebCryptoKeyFormatJwk:
|
| + // TODO(eroman):
|
| + return Status::ErrorUnsupported();
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +Status Sign(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& data,
|
| + blink::WebArrayBuffer* buffer) {
|
| + if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageSign))
|
| + return Status::ErrorUnexpected();
|
| + if (algorithm.id() != key.algorithm().id())
|
| + return Status::ErrorUnexpected();
|
| +
|
| + switch (algorithm.id()) {
|
| + case blink::WebCryptoAlgorithmIdHmac:
|
| + return SignHmac(algorithm, key, data, buffer);
|
| + case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
|
| + return SignRsaSsaPkcs1v1_5(algorithm, key, data, buffer);
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +Status VerifySignature(const blink::WebCryptoAlgorithm& algorithm,
|
| + const blink::WebCryptoKey& key,
|
| + const CryptoData& signature,
|
| + const CryptoData& data,
|
| + bool* signature_match) {
|
| + if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageVerify))
|
| + return Status::ErrorUnexpected();
|
| + if (algorithm.id() != key.algorithm().id())
|
| + return Status::ErrorUnexpected();
|
| +
|
| + if (!signature.byte_length()) {
|
| + // None of the algorithms generate valid zero-length signatures so this
|
| + // will necessarily fail verification. Early return to protect
|
| + // implementations from dealing with a NULL signature pointer.
|
| + *signature_match = false;
|
| + return Status::Success();
|
| + }
|
| +
|
| + switch (algorithm.id()) {
|
| + case blink::WebCryptoAlgorithmIdHmac:
|
| + return VerifyHmac(algorithm, key, signature, data, signature_match);
|
| + case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
|
| + return VerifyRsaSsaPkcs1v1_5(
|
| + algorithm, key, signature, data, signature_match);
|
| + default:
|
| + return Status::ErrorUnsupported();
|
| + }
|
| +}
|
| +
|
| +} // namespace webcrypto
|
| +} // namespace content
|
|
|