rsa_sign.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include "base/numerics/safe_math.h"
  6. #include "components/webcrypto/algorithms/rsa_sign.h"
  7. #include "components/webcrypto/algorithms/util.h"
  8. #include "components/webcrypto/blink_key_handle.h"
  9. #include "components/webcrypto/status.h"
  10. #include "crypto/openssl_util.h"
  11. #include "third_party/blink/public/platform/web_crypto_key_algorithm.h"
  12. #include "third_party/boringssl/src/include/openssl/digest.h"
  13. #include "third_party/boringssl/src/include/openssl/evp.h"
  14. #include "third_party/boringssl/src/include/openssl/rsa.h"
  15. namespace webcrypto {
  16. namespace {
  17. // Extracts the OpenSSL key and digest from a WebCrypto key. The returned
  18. // pointers will remain valid as long as |key| is alive.
  19. Status GetPKeyAndDigest(const blink::WebCryptoKey& key,
  20. EVP_PKEY** pkey,
  21. const EVP_MD** digest) {
  22. *pkey = GetEVP_PKEY(key);
  23. *digest = GetDigest(key.Algorithm().RsaHashedParams()->GetHash());
  24. if (!*digest)
  25. return Status::ErrorUnsupported();
  26. return Status::Success();
  27. }
  28. // Sets the PSS parameters on |pctx| if the key is for RSA-PSS.
  29. //
  30. // Otherwise returns Success without doing anything.
  31. Status ApplyRsaPssOptions(const blink::WebCryptoKey& key,
  32. const EVP_MD* const mgf_digest,
  33. unsigned int salt_length_bytes,
  34. EVP_PKEY_CTX* pctx) {
  35. // Only apply RSA-PSS options if the key is for RSA-PSS.
  36. if (key.Algorithm().Id() != blink::kWebCryptoAlgorithmIdRsaPss) {
  37. DCHECK_EQ(0u, salt_length_bytes);
  38. DCHECK_EQ(blink::kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
  39. key.Algorithm().Id());
  40. return Status::Success();
  41. }
  42. // BoringSSL takes a signed int for the salt length, and interprets
  43. // negative values in a special manner. Make sure not to silently underflow.
  44. base::CheckedNumeric<int> salt_length_bytes_int(salt_length_bytes);
  45. if (!salt_length_bytes_int.IsValid()) {
  46. // TODO(eroman): Give a better error message.
  47. return Status::OperationError();
  48. }
  49. if (1 != EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
  50. 1 != EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf_digest) ||
  51. 1 != EVP_PKEY_CTX_set_rsa_pss_saltlen(
  52. pctx, salt_length_bytes_int.ValueOrDie())) {
  53. return Status::OperationError();
  54. }
  55. return Status::Success();
  56. }
  57. } // namespace
  58. Status RsaSign(const blink::WebCryptoKey& key,
  59. unsigned int pss_salt_length_bytes,
  60. base::span<const uint8_t> data,
  61. std::vector<uint8_t>* buffer) {
  62. if (key.GetType() != blink::kWebCryptoKeyTypePrivate)
  63. return Status::ErrorUnexpectedKeyType();
  64. crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  65. bssl::ScopedEVP_MD_CTX ctx;
  66. EVP_PKEY_CTX* pctx = nullptr; // Owned by |ctx|.
  67. EVP_PKEY* private_key = nullptr;
  68. const EVP_MD* digest = nullptr;
  69. Status status = GetPKeyAndDigest(key, &private_key, &digest);
  70. if (status.IsError())
  71. return status;
  72. // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter
  73. // returns a maximum allocation size, while the call without a NULL returns
  74. // the real one, which may be smaller.
  75. size_t sig_len = 0;
  76. if (!EVP_DigestSignInit(ctx.get(), &pctx, digest, nullptr, private_key)) {
  77. return Status::OperationError();
  78. }
  79. // Set PSS-specific options (if applicable).
  80. status = ApplyRsaPssOptions(key, digest, pss_salt_length_bytes, pctx);
  81. if (status.IsError())
  82. return status;
  83. if (!EVP_DigestSignUpdate(ctx.get(), data.data(), data.size()) ||
  84. !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) {
  85. return Status::OperationError();
  86. }
  87. buffer->resize(sig_len);
  88. if (!EVP_DigestSignFinal(ctx.get(), buffer->data(), &sig_len))
  89. return Status::OperationError();
  90. buffer->resize(sig_len);
  91. return Status::Success();
  92. }
  93. Status RsaVerify(const blink::WebCryptoKey& key,
  94. unsigned int pss_salt_length_bytes,
  95. base::span<const uint8_t> signature,
  96. base::span<const uint8_t> data,
  97. bool* signature_match) {
  98. if (key.GetType() != blink::kWebCryptoKeyTypePublic)
  99. return Status::ErrorUnexpectedKeyType();
  100. crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  101. bssl::ScopedEVP_MD_CTX ctx;
  102. EVP_PKEY_CTX* pctx = nullptr; // Owned by |ctx|.
  103. EVP_PKEY* public_key = nullptr;
  104. const EVP_MD* digest = nullptr;
  105. Status status = GetPKeyAndDigest(key, &public_key, &digest);
  106. if (status.IsError())
  107. return status;
  108. if (!EVP_DigestVerifyInit(ctx.get(), &pctx, digest, nullptr, public_key))
  109. return Status::OperationError();
  110. // Set PSS-specific options (if applicable).
  111. status = ApplyRsaPssOptions(key, digest, pss_salt_length_bytes, pctx);
  112. if (status.IsError())
  113. return status;
  114. if (!EVP_DigestVerifyUpdate(ctx.get(), data.data(), data.size()))
  115. return Status::OperationError();
  116. *signature_match =
  117. 1 == EVP_DigestVerifyFinal(ctx.get(), signature.data(), signature.size());
  118. return Status::Success();
  119. }
  120. } // namespace webcrypto