File: keystore_service_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (158 lines) | stat: -rw-r--r-- 5,590 bytes parent folder | download | duplicates (6)
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
149
150
151
152
153
154
155
156
157
158
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/crosapi/cpp/keystore_service_util.h"

#include <optional>
#include <vector>

#include "base/numerics/safe_math.h"
#include "base/values.h"
#include "chromeos/crosapi/mojom/keystore_service.mojom.h"

namespace crosapi {
namespace keystore_service_util {

const char kWebCryptoEcdsa[] = "ECDSA";
const char kWebCryptoRsassaPkcs1v15[] = "RSASSA-PKCS1-v1_5";
const char kWebCryptoRsaOaep[] = "RSA-OAEP";
const char kWebCryptoNamedCurveP256[] = "P-256";

// Converts a keystore algorithm into a WebCrypto dictionary.
std::optional<base::Value::Dict> MakeDictionaryFromKeystoreAlgorithm(
    const crosapi::mojom::KeystoreAlgorithmPtr& algorithm) {
  base::Value::Dict value;
  switch (algorithm->which()) {
    case crosapi::mojom::KeystoreAlgorithm::Tag::kRsassaPkcs115:
      value.Set("name", kWebCryptoRsassaPkcs1v15);

      if (!base::IsValueInRangeForNumericType<int>(
              algorithm->get_rsassa_pkcs115()->modulus_length)) {
        return std::nullopt;
      }

      value.Set(
          "modulusLength",
          static_cast<int>(algorithm->get_rsassa_pkcs115()->modulus_length));

      if (!algorithm->get_rsassa_pkcs115()->public_exponent) {
        return std::nullopt;
      }
      value.Set("publicExponent",
                base::Value::BlobStorage(
                    algorithm->get_rsassa_pkcs115()->public_exponent.value()));
      return value;
    case crosapi::mojom::KeystoreAlgorithm::Tag::kEcdsa:
      value.Set("name", kWebCryptoEcdsa);
      value.Set("namedCurve", algorithm->get_ecdsa()->named_curve);
      return value;
    case crosapi::mojom::KeystoreAlgorithm::Tag::kRsaOaep:
      value.Set("name", kWebCryptoRsaOaep);

      if (!base::IsValueInRangeForNumericType<int>(
              algorithm->get_rsa_oaep()->modulus_length)) {
        return std::nullopt;
      }

      value.Set("modulusLength",
                static_cast<int>(algorithm->get_rsa_oaep()->modulus_length));

      if (!algorithm->get_rsa_oaep()->public_exponent) {
        return std::nullopt;
      }
      value.Set("publicExponent",
                base::Value::BlobStorage(
                    algorithm->get_rsa_oaep()->public_exponent.value()));
      return value;
    default:
      return std::nullopt;
  }
}

// Converts a WebCrypto dictionary into a keystore algorithm.
std::optional<crosapi::mojom::KeystoreAlgorithmPtr>
MakeKeystoreAlgorithmFromDictionary(const base::Value::Dict& dictionary) {
  const std::string* name = dictionary.FindString("name");
  if (!name) {
    return std::nullopt;
  }

  if (*name == kWebCryptoRsassaPkcs1v15) {
    std::optional<int> modulus_length = dictionary.FindInt("modulusLength");
    const std::vector<uint8_t>* public_exponent =
        dictionary.FindBlob("publicExponent");
    if (!modulus_length || !public_exponent) {
      return std::nullopt;
    }
    if (!base::IsValueInRangeForNumericType<uint32_t>(modulus_length.value())) {
      return std::nullopt;
    }
    crosapi::mojom::KeystoreRsaParamsPtr params =
        crosapi::mojom::KeystoreRsaParams::New();
    params->modulus_length =
        base::checked_cast<uint32_t>(modulus_length.value());
    params->public_exponent = *public_exponent;
    return crosapi::mojom::KeystoreAlgorithm::NewRsassaPkcs115(
        std::move(params));
  }

  if (*name == kWebCryptoEcdsa) {
    const std::string* named_curve = dictionary.FindString("namedCurve");
    if (!named_curve) {
      return std::nullopt;
    }
    crosapi::mojom::KeystoreEcdsaParamsPtr params =
        crosapi::mojom::KeystoreEcdsaParams::New();
    params->named_curve = *named_curve;
    return crosapi::mojom::KeystoreAlgorithm::NewEcdsa(std::move(params));
  }

  if (*name == kWebCryptoRsaOaep) {
    std::optional<int> modulus_length = dictionary.FindInt("modulusLength");
    const std::vector<uint8_t>* public_exponent =
        dictionary.FindBlob("publicExponent");
    if (!modulus_length || !public_exponent) {
      return std::nullopt;
    }
    if (!base::IsValueInRangeForNumericType<uint32_t>(modulus_length.value())) {
      return std::nullopt;
    }
    crosapi::mojom::KeystoreRsaParamsPtr params =
        crosapi::mojom::KeystoreRsaParams::New();
    params->modulus_length =
        base::checked_cast<uint32_t>(modulus_length.value());
    params->public_exponent = *public_exponent;
    return crosapi::mojom::KeystoreAlgorithm::NewRsaOaep(std::move(params));
  }

  return std::nullopt;
}

mojom::KeystoreAlgorithmPtr MakeRsassaPkcs1v15KeystoreAlgorithm(
    unsigned int modulus_length,
    bool sw_backed) {
  mojom::KeystoreRsaParamsPtr params = mojom::KeystoreRsaParams::New();
  params->modulus_length = modulus_length;
  params->sw_backed = sw_backed;
  return mojom::KeystoreAlgorithm::NewRsassaPkcs115(std::move(params));
}

mojom::KeystoreAlgorithmPtr MakeEcdsaKeystoreAlgorithm(
    const std::string& named_curve) {
  mojom::KeystoreEcdsaParamsPtr params = mojom::KeystoreEcdsaParams::New();
  params->named_curve = named_curve;
  return mojom::KeystoreAlgorithm::NewEcdsa(std::move(params));
}

mojom::KeystoreAlgorithmPtr MakeRsaOaepKeystoreAlgorithm(
    unsigned int modulus_length,
    bool sw_backed) {
  mojom::KeystoreRsaParamsPtr params = mojom::KeystoreRsaParams::New();
  params->modulus_length = modulus_length;
  params->sw_backed = sw_backed;
  return mojom::KeystoreAlgorithm::NewRsaOaep(std::move(params));
}

}  // namespace keystore_service_util
}  // namespace crosapi