File: rsa_sign.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (148 lines) | stat: -rw-r--r-- 5,222 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 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 <stddef.h>

#include "base/numerics/safe_math.h"
#include "components/webcrypto/algorithms/rsa_sign.h"
#include "components/webcrypto/algorithms/util.h"
#include "components/webcrypto/blink_key_handle.h"
#include "components/webcrypto/crypto_data.h"
#include "components/webcrypto/status.h"
#include "crypto/openssl_util.h"
#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
#include "third_party/boringssl/src/include/openssl/digest.h"
#include "third_party/boringssl/src/include/openssl/evp.h"
#include "third_party/boringssl/src/include/openssl/rsa.h"

namespace webcrypto {

namespace {

// Extracts the OpenSSL key and digest from a WebCrypto key. The returned
// pointers will remain valid as long as |key| is alive.
Status GetPKeyAndDigest(const blink::WebCryptoKey& key,
                        EVP_PKEY** pkey,
                        const EVP_MD** digest) {
  *pkey = GetEVP_PKEY(key);

  *digest = GetDigest(key.algorithm().rsaHashedParams()->hash());
  if (!*digest)
    return Status::ErrorUnsupported();

  return Status::Success();
}

// Sets the PSS parameters on |pctx| if the key is for RSA-PSS.
//
// Otherwise returns Success without doing anything.
Status ApplyRsaPssOptions(const blink::WebCryptoKey& key,
                          const EVP_MD* const mgf_digest,
                          unsigned int salt_length_bytes,
                          EVP_PKEY_CTX* pctx) {
  // Only apply RSA-PSS options if the key is for RSA-PSS.
  if (key.algorithm().id() != blink::WebCryptoAlgorithmIdRsaPss) {
    DCHECK_EQ(0u, salt_length_bytes);
    DCHECK_EQ(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, key.algorithm().id());
    return Status::Success();
  }

  // BoringSSL takes a signed int for the salt length, and interprets
  // negative values in a special manner. Make sure not to silently underflow.
  base::CheckedNumeric<int> salt_length_bytes_int(salt_length_bytes);
  if (!salt_length_bytes_int.IsValid()) {
    // TODO(eroman): Give a better error message.
    return Status::OperationError();
  }

  if (1 != EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
      1 != EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf_digest) ||
      1 != EVP_PKEY_CTX_set_rsa_pss_saltlen(
               pctx, salt_length_bytes_int.ValueOrDie())) {
    return Status::OperationError();
  }

  return Status::Success();
}

}  // namespace

Status RsaSign(const blink::WebCryptoKey& key,
               unsigned int pss_salt_length_bytes,
               const CryptoData& data,
               std::vector<uint8_t>* buffer) {
  if (key.type() != blink::WebCryptoKeyTypePrivate)
    return Status::ErrorUnexpectedKeyType();

  crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  bssl::ScopedEVP_MD_CTX ctx;
  EVP_PKEY_CTX* pctx = NULL;  // Owned by |ctx|.

  EVP_PKEY* private_key = NULL;
  const EVP_MD* digest = NULL;
  Status status = GetPKeyAndDigest(key, &private_key, &digest);
  if (status.IsError())
    return status;

  // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter
  // returns a maximum allocation size, while the call without a NULL returns
  // the real one, which may be smaller.
  size_t sig_len = 0;
  if (!EVP_DigestSignInit(ctx.get(), &pctx, digest, NULL, private_key)) {
    return Status::OperationError();
  }

  // Set PSS-specific options (if applicable).
  status = ApplyRsaPssOptions(key, digest, pss_salt_length_bytes, pctx);
  if (status.IsError())
    return status;

  if (!EVP_DigestSignUpdate(ctx.get(), data.bytes(), data.byte_length()) ||
      !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) {
    return Status::OperationError();
  }

  buffer->resize(sig_len);
  if (!EVP_DigestSignFinal(ctx.get(), buffer->data(), &sig_len))
    return Status::OperationError();

  buffer->resize(sig_len);
  return Status::Success();
}

Status RsaVerify(const blink::WebCryptoKey& key,
                 unsigned int pss_salt_length_bytes,
                 const CryptoData& signature,
                 const CryptoData& data,
                 bool* signature_match) {
  if (key.type() != blink::WebCryptoKeyTypePublic)
    return Status::ErrorUnexpectedKeyType();

  crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  bssl::ScopedEVP_MD_CTX ctx;
  EVP_PKEY_CTX* pctx = NULL;  // Owned by |ctx|.

  EVP_PKEY* public_key = NULL;
  const EVP_MD* digest = NULL;
  Status status = GetPKeyAndDigest(key, &public_key, &digest);
  if (status.IsError())
    return status;

  if (!EVP_DigestVerifyInit(ctx.get(), &pctx, digest, NULL, public_key))
    return Status::OperationError();

  // Set PSS-specific options (if applicable).
  status = ApplyRsaPssOptions(key, digest, pss_salt_length_bytes, pctx);
  if (status.IsError())
    return status;

  if (!EVP_DigestVerifyUpdate(ctx.get(), data.bytes(), data.byte_length()))
    return Status::OperationError();

  *signature_match = 1 == EVP_DigestVerifyFinal(ctx.get(), signature.bytes(),
                                                signature.byte_length());
  return Status::Success();
}

}  // namespace webcrypto