File: client_cert_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 (439 lines) | stat: -rw-r--r-- 15,454 bytes parent folder | download | duplicates (7)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2012 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/ash/components/network/client_cert_util.h"

#include <cert.h>
#include <pk11pub.h>
#include <stddef.h>

#include <list>
#include <optional>
#include <variant>

#include "base/check.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "components/onc/onc_constants.h"
#include "net/base/net_errors.h"
#include "net/cert/cert_database.h"
#include "net/cert/nss_cert_database.h"
#include "net/cert/scoped_nss_types.h"
#include "net/cert/x509_certificate.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash::client_cert {

const char kDefaultTPMPin[] = "111111";

namespace {

// Extracts the type and descriptor (referenced GUID or client cert pattern
// or provisioning profile id) of a ONC-specified client certificate
// specification for a network
// (|dict_with_client_cert|) and stores it in |cert_config|.
void GetClientCertTypeAndDescriptor(
    onc::ONCSource onc_source,
    const base::Value::Dict& dict_with_client_cert,
    ClientCertConfig* cert_config) {
  cert_config->onc_source = onc_source;

  const std::string* identity =
      dict_with_client_cert.FindString(::onc::eap::kIdentity);
  if (identity)
    cert_config->policy_identity = *identity;

  const std::string* client_cert_type =
      dict_with_client_cert.FindString(::onc::client_cert::kClientCertType);
  if (client_cert_type)
    cert_config->client_cert_type = *client_cert_type;

  if (cert_config->client_cert_type == ::onc::client_cert::kPattern) {
    const base::Value::Dict* pattern_value =
        dict_with_client_cert.FindDict(::onc::client_cert::kClientCertPattern);
    if (pattern_value) {
      std::optional<OncCertificatePattern> pattern =
          OncCertificatePattern::ReadFromONCDictionary(*pattern_value);
      if (!pattern.has_value()) {
        LOG(ERROR) << "ClientCertPattern invalid";
        return;
      }
      cert_config->pattern = pattern.value();
    }
  } else if (cert_config->client_cert_type == ::onc::client_cert::kRef) {
    const std::string* client_cert_ref_key =
        dict_with_client_cert.FindString(::onc::client_cert::kClientCertRef);
    if (client_cert_ref_key)
      cert_config->guid = *client_cert_ref_key;
  } else if (cert_config->client_cert_type ==
             ::onc::client_cert::kProvisioningProfileId) {
    const std::string* provisioning_profile_id =
        dict_with_client_cert.FindString(
            ::onc::client_cert::kClientCertProvisioningProfileId);
    if (!provisioning_profile_id) {
      LOG(ERROR) << "ProvisioningProfileId missing";
      return;
    }
    cert_config->provisioning_profile_id = *provisioning_profile_id;
  }
}

}  // namespace

std::string GetPkcs11AndSlotIdFromEapCertId(const std::string& cert_id,
                                            int* slot_id) {
  *slot_id = -1;
  if (cert_id.empty())
    return std::string();

  size_t delimiter_pos = cert_id.find(':');
  if (delimiter_pos == std::string::npos) {
    // No delimiter found, so |cert_id| only contains the PKCS11 id.
    return cert_id;
  }
  if (delimiter_pos + 1 >= cert_id.size()) {
    LOG(ERROR) << "Empty PKCS11 id in cert id.";
    return std::string();
  }
  int parsed_slot_id;
  if (base::StringToInt(cert_id.substr(0, delimiter_pos), &parsed_slot_id))
    *slot_id = parsed_slot_id;
  else
    LOG(ERROR) << "Slot ID is not an integer. Cert ID is: " << cert_id << ".";
  return cert_id.substr(delimiter_pos + 1);
}

void GetClientCertFromShillProperties(const base::Value::Dict& shill_properties,
                                      ConfigType* cert_config_type,
                                      int* tpm_slot,
                                      std::string* pkcs11_id) {
  *cert_config_type = ConfigType::kNone;
  *tpm_slot = -1;
  pkcs11_id->clear();

  // Look for VPN specific client certificate properties.
  //
  // VPN Provider values are read from the "Provider" dictionary, not the
  // "Provider.Type", etc keys (which are used only to set the values).
  const base::Value::Dict* provider_properties =
      shill_properties.FindDict(shill::kProviderProperty);
  if (provider_properties) {
    const std::string* pkcs11_id_str = nullptr;

    // Look for OpenVPN specific properties.
    // Note: OpenVPN does not have a slot property, see crbug.com/769550.
    pkcs11_id_str =
        provider_properties->FindString(shill::kOpenVPNClientCertIdProperty);
    if (pkcs11_id_str) {
      *pkcs11_id = *pkcs11_id_str;
      *cert_config_type = ConfigType::kOpenVpn;
      return;
    }

    // Look for L2TP-IPsec specific properties.
    pkcs11_id_str =
        provider_properties->FindString(shill::kL2TPIPsecClientCertIdProperty);
    if (pkcs11_id_str) {
      *pkcs11_id = *pkcs11_id_str;

      const std::string* cert_slot = provider_properties->FindString(
          shill::kL2TPIPsecClientCertSlotProperty);
      if (cert_slot && !cert_slot->empty() &&
          !base::StringToInt(*cert_slot, tpm_slot)) {
        LOG(ERROR) << "Cert slot is not an integer: " << *cert_slot << ".";
        return;
      }

      *cert_config_type = ConfigType::kL2tpIpsec;
    }

    // Look for IKEv2 specific properties.
    pkcs11_id_str =
        provider_properties->FindString(shill::kIKEv2ClientCertIdProperty);
    if (pkcs11_id_str) {
      *pkcs11_id = *pkcs11_id_str;

      const std::string* cert_slot =
          provider_properties->FindString(shill::kIKEv2ClientCertSlotProperty);
      if (cert_slot && !cert_slot->empty() &&
          !base::StringToInt(*cert_slot, tpm_slot)) {
        LOG(ERROR) << "Cert slot is not an integer: " << *cert_slot << ".";
        return;
      }

      *cert_config_type = ConfigType::kIkev2;
    }
    return;
  }

  // Look for EAP specific client certificate properties, which can either be
  // part of a WiFi or EthernetEAP configuration.
  const std::string* cert_id =
      shill_properties.FindString(shill::kEapCertIdProperty);
  if (cert_id) {
    // Shill requires both CertID and KeyID for TLS connections, despite the
    // fact that by convention they are the same ID, because one identifies
    // the certificate and the other the private key.
    std::string key_id;
    const std::string* key_id_str =
        shill_properties.FindString(shill::kEapKeyIdProperty);
    if (key_id_str)
      key_id = *key_id_str;
    // Assume the configuration to be invalid, if the two IDs are not identical.
    if (*cert_id != key_id) {
      LOG(ERROR) << "EAP CertID differs from KeyID";
      return;
    }
    *pkcs11_id = GetPkcs11AndSlotIdFromEapCertId(*cert_id, tpm_slot);
    *cert_config_type = ConfigType::kEap;
  }
}

void SetShillProperties(const ConfigType cert_config_type,
                        const int tpm_slot,
                        const std::string& pkcs11_id,
                        base::Value::Dict& properties) {
  switch (cert_config_type) {
    case ConfigType::kNone: {
      return;
    }
    case ConfigType::kOpenVpn: {
      properties.Set(shill::kOpenVPNPinProperty, kDefaultTPMPin);
      // Note: OpenVPN does not have a slot property, see crbug.com/769550.
      properties.Set(shill::kOpenVPNClientCertIdProperty, pkcs11_id);
      break;
    }
    case ConfigType::kL2tpIpsec: {
      properties.Set(shill::kL2TPIPsecPinProperty, kDefaultTPMPin);
      properties.Set(shill::kL2TPIPsecClientCertSlotProperty,
                     base::NumberToString(tpm_slot));
      properties.Set(shill::kL2TPIPsecClientCertIdProperty, pkcs11_id);
      break;
    }
    case ConfigType::kIkev2: {
      // PIN property is not used by shill for a IKEv2 service since it is a
      // fixed value.
      properties.Set(shill::kIKEv2ClientCertSlotProperty,
                     base::NumberToString(tpm_slot));
      properties.Set(shill::kIKEv2ClientCertIdProperty, pkcs11_id);
      break;
    }
    case ConfigType::kEap: {
      properties.Set(shill::kEapPinProperty, kDefaultTPMPin);
      std::string key_id =
          base::StringPrintf("%i:%s", tpm_slot, pkcs11_id.c_str());

      // Shill requires both CertID and KeyID for TLS connections, despite the
      // fact that by convention they are the same ID, because one identifies
      // the certificate and the other the private key.
      properties.Set(shill::kEapCertIdProperty, key_id);
      properties.Set(shill::kEapKeyIdProperty, key_id);
      break;
    }
  }
}

void SetEmptyShillProperties(const ConfigType cert_config_type,
                             base::Value::Dict& properties) {
  switch (cert_config_type) {
    case ConfigType::kNone: {
      return;
    }
    case ConfigType::kOpenVpn: {
      properties.Set(shill::kOpenVPNPinProperty, std::string());
      properties.Set(shill::kOpenVPNClientCertIdProperty, std::string());
      break;
    }
    case ConfigType::kL2tpIpsec: {
      properties.Set(shill::kL2TPIPsecPinProperty, std::string());
      properties.Set(shill::kL2TPIPsecClientCertSlotProperty, std::string());
      properties.Set(shill::kL2TPIPsecClientCertIdProperty, std::string());
      break;
    }
    case ConfigType::kIkev2: {
      // PIN property is not used by shill for a IKEv2 service since it is a
      // fixed value.
      properties.Set(shill::kIKEv2ClientCertSlotProperty, std::string());
      properties.Set(shill::kIKEv2ClientCertIdProperty, std::string());
      break;
    }
    case ConfigType::kEap: {
      properties.Set(shill::kEapPinProperty, std::string());
      // Shill requires both CertID and KeyID for TLS connections, despite the
      // fact that by convention they are the same ID, because one identifies
      // the certificate and the other the private key.
      properties.Set(shill::kEapCertIdProperty, std::string());
      properties.Set(shill::kEapKeyIdProperty, std::string());
      break;
    }
  }
}

ClientCertConfig::ClientCertConfig()
    : location(ConfigType::kNone),
      client_cert_type(onc::client_cert::kClientCertTypeNone) {}

ClientCertConfig::ClientCertConfig(const ClientCertConfig& other) = default;

ClientCertConfig::~ClientCertConfig() = default;

ResolvedCert::ResolvedCert(
    Status status,
    int slot_id,
    const std::string& pkcs11_id,
    base::flat_map<std::string, std::string> variable_expansions)
    : status_(status),
      slot_id_(slot_id),
      pkcs11_id_(pkcs11_id),
      variable_expansions_(variable_expansions) {}

ResolvedCert::~ResolvedCert() = default;

ResolvedCert::ResolvedCert(ResolvedCert&& other) = default;

ResolvedCert& ResolvedCert::operator=(ResolvedCert&& other) = default;

// static
ResolvedCert ResolvedCert::NotKnownYet() {
  return ResolvedCert(Status::kNotKnownYet, -1, std::string(), {});
}

// static
ResolvedCert ResolvedCert::NothingMatched() {
  return ResolvedCert(Status::kNothingMatched, -1, std::string(), {});
}

// static
ResolvedCert ResolvedCert::CertMatched(
    int slot_id,
    const std::string& pkcs11_id,
    base::flat_map<std::string, std::string> variable_expansions) {
  return ResolvedCert(Status::kCertMatched, slot_id, pkcs11_id,
                      std::move(variable_expansions));
}

ResolvedCert::Status ResolvedCert::status() const {
  return status_;
}

int ResolvedCert::slot_id() const {
  DCHECK_EQ(status(), Status::kCertMatched);
  return slot_id_;
}

const std::string& ResolvedCert::pkcs11_id() const {
  DCHECK_EQ(status(), Status::kCertMatched);
  return pkcs11_id_;
}

const base::flat_map<std::string, std::string>&
ResolvedCert::variable_expansions() const {
  DCHECK_EQ(status(), Status::kCertMatched);
  return variable_expansions_;
}

bool operator==(const ResolvedCert& lhs, const ResolvedCert& rhs) {
  if (lhs.status() != rhs.status())
    return false;

  if (lhs.status() == ResolvedCert::Status::kCertMatched) {
    // Compare attributes of the matched certificate.
    return lhs.slot_id() == rhs.slot_id() &&
           lhs.pkcs11_id() == rhs.pkcs11_id() &&
           lhs.variable_expansions() == rhs.variable_expansions();
  }

  return true;
}

// Uses a template type to easily implement a const and a non-const version.
template <typename DictType>
DictType* GetOncClientCertConfigDict(DictType& network_config,
                                     ConfigType* out_config_type) {
  DictType* wifi = network_config.FindDict(::onc::network_config::kWiFi);
  if (wifi) {
    DictType* eap = wifi->FindDict(::onc::wifi::kEAP);
    if (!eap)
      return nullptr;
    if (out_config_type)
      *out_config_type = ConfigType::kEap;
    return eap;
  }

  DictType* ethernet =
      network_config.FindDict(::onc::network_config::kEthernet);
  if (ethernet) {
    DictType* eap = ethernet->FindDict(::onc::wifi::kEAP);
    if (!eap)
      return nullptr;
    if (out_config_type)
      *out_config_type = ConfigType::kEap;
    return eap;
  }

  DictType* vpn = network_config.FindDict(::onc::network_config::kVPN);
  if (vpn) {
    DictType* openvpn = vpn->FindDict(::onc::vpn::kOpenVPN);
    DictType* ipsec = vpn->FindDict(::onc::vpn::kIPsec);
    DictType* l2tp = vpn->FindDict(::onc::vpn::kL2TP);
    if (openvpn) {
      if (out_config_type)
        *out_config_type = ConfigType::kOpenVpn;
      return openvpn;
    }
    if (ipsec) {
      // Currently we support two kinds of IPsec-based VPN:
      // - L2TP/IPsec: IKE version is 1 and |l2tp| is set;
      // - IKEv2: IKE version is 2 and |l2tp| is not set.
      // Thus we only use |l2tp| to distinguish between these two cases.
      if (out_config_type)
        *out_config_type = l2tp ? ConfigType::kL2tpIpsec : ConfigType::kIkev2;
      return ipsec;
    }
  }

  return nullptr;
}

void OncToClientCertConfig(::onc::ONCSource onc_source,
                           const base::Value::Dict& network_config,
                           ClientCertConfig* cert_config) {
  *cert_config = ClientCertConfig();

  const base::Value::Dict* dict_with_client_cert =
      GetOncClientCertConfigDict(network_config, &(cert_config->location));
  if (dict_with_client_cert) {
    GetClientCertTypeAndDescriptor(onc_source, *dict_with_client_cert,
                                   cert_config);
  }
}

void SetResolvedCertInOnc(const ResolvedCert& resolved_cert,
                          base::Value::Dict& network_config) {
  if (resolved_cert.status() == ResolvedCert::Status::kNotKnownYet)
    return;

  base::Value::Dict* dict_with_client_cert =
      GetOncClientCertConfigDict(network_config, /*out_config_type=*/nullptr);
  if (!dict_with_client_cert)
    return;
  dict_with_client_cert->Set(::onc::client_cert::kClientCertType,
                             ::onc::client_cert::kPKCS11Id);
  if (resolved_cert.status() == ResolvedCert::Status::kNothingMatched) {
    // Empty PKCS11Id means that no certificate has been selected and it
    // should be cleared in shill.
    dict_with_client_cert->Set(::onc::client_cert::kClientCertPKCS11Id,
                               std::string());
  } else {
    dict_with_client_cert->Set(
        ::onc::client_cert::kClientCertPKCS11Id,
        base::StringPrintf("%i:%s", resolved_cert.slot_id(),
                           resolved_cert.pkcs11_id().c_str()));
  }
  dict_with_client_cert->Remove(::onc::client_cert::kClientCertPattern);
}

}  // namespace ash::client_cert