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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chromeos/ash/components/kcer/helpers/pkcs12_reader.h"
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "base/containers/span.h"
#include "base/logging.h"
#include "chromeos/ash/components/kcer/helpers/key_helper.h"
#include "crypto/nss_util.h"
#include "crypto/openssl_util.h"
#include "net/cert/x509_util_nss.h"
#include "third_party/boringssl/src/include/openssl/base.h"
#include "third_party/boringssl/src/include/openssl/bn.h"
#include "third_party/boringssl/src/include/openssl/mem.h"
#include "third_party/boringssl/src/include/openssl/pkcs8.h"
#include "third_party/boringssl/src/include/openssl/stack.h"
#include "third_party/boringssl/src/include/openssl/x509.h"
namespace kcer::internal {
CertData::CertData() = default;
CertData::CertData(CertData&& other) {
x509 = std::move(other.x509);
nickname = std::move(other.nickname);
}
CertData::~CertData() = default;
KeyData::KeyData() = default;
KeyData::KeyData(KeyData&&) = default;
KeyData::~KeyData() = default;
std::vector<uint8_t> Pkcs12Reader::BignumToBytes(const BIGNUM* bignum) const {
if (!bignum) {
return {};
}
std::vector<uint8_t> result(BN_num_bytes(bignum));
BN_bn2bin(bignum, result.data());
return result;
}
Pkcs12Reader::Pkcs12Reader() {
// Make sure that NSS is initialized.
crypto::EnsureNSSInit();
}
Pkcs12ReaderStatusCode Pkcs12Reader::GetPkcs12KeyAndCerts(
const std::vector<uint8_t>& pkcs12_data,
const std::string& password,
bssl::UniquePtr<EVP_PKEY>& key,
bssl::UniquePtr<STACK_OF(X509)>& certs) const {
crypto::ClearOpenSSLERRStack(FROM_HERE);
CBS pkcs12;
CBS_init(&pkcs12, reinterpret_cast<const uint8_t*>(pkcs12_data.data()),
pkcs12_data.size());
if (!pkcs12.data || pkcs12.len <= 0) {
return Pkcs12ReaderStatusCode::kMissedPkcs12Data;
}
EVP_PKEY* key_ptr = nullptr;
certs = bssl::UniquePtr<STACK_OF(X509)>(sk_X509_new_null());
const int get_key_and_cert_result = PKCS12_get_key_and_certs(
&key_ptr, certs.get(), &pkcs12, password.c_str());
key = bssl::UniquePtr<EVP_PKEY>(key_ptr);
if (get_key_and_cert_result && key_ptr) {
return Pkcs12ReaderStatusCode::kSuccess;
}
uint32_t error = ERR_get_error();
if (ERR_GET_LIB(error) != ERR_LIB_PKCS8) {
LOG(ERROR) << "Failed to parse PKCS#12, bssl error: " << error;
return Pkcs12ReaderStatusCode::kFailedToParsePkcs12Data;
}
switch (ERR_GET_REASON(error)) {
case PKCS8_R_INCORRECT_PASSWORD:
return Pkcs12ReaderStatusCode::kPkcs12WrongPassword;
case PKCS8_R_MISSING_MAC:
return Pkcs12ReaderStatusCode::kPkcs12InvalidMac;
case PKCS8_R_BAD_PKCS12_DATA:
return Pkcs12ReaderStatusCode::kPkcs12InvalidFile;
case PKCS8_R_BAD_PKCS12_VERSION:
case PKCS8_R_PKCS12_PUBLIC_KEY_INTEGRITY_NOT_SUPPORTED:
return Pkcs12ReaderStatusCode::kPkcs12UnsupportedFile;
default:
LOG(ERROR) << "Failed to parse PKCS#12, bssl error: " << error;
return Pkcs12ReaderStatusCode::kFailedToParsePkcs12Data;
}
}
Pkcs12ReaderStatusCode Pkcs12Reader::GetDerEncodedCert(
X509* cert,
bssl::UniquePtr<uint8_t>& cert_der,
int& cert_der_size) const {
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
uint8_t* cert_der_ptr = nullptr;
cert_der_size = i2d_X509(cert, &cert_der_ptr);
cert_der = bssl::UniquePtr<uint8_t>(cert_der_ptr);
// Fail if error happened or if size is 0.
if (cert_der_size <= 0) {
return Pkcs12ReaderStatusCode::kPkcs12CertDerFailed;
}
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::GetIssuerNameDer(
X509* cert,
base::span<const uint8_t>& issuer_name_data) const {
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
X509_NAME* issuer_name = X509_get_issuer_name(cert);
if (!issuer_name) {
return Pkcs12ReaderStatusCode::kPkcs12CertIssuerNameMissed;
}
const uint8_t* name_der;
size_t name_der_size;
if (!X509_NAME_get0_der(issuer_name, &name_der, &name_der_size)) {
return Pkcs12ReaderStatusCode::kPkcs12CertIssuerDerNameFailed;
}
issuer_name_data = {name_der, name_der_size};
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::FindRawCertsWithSubject(
PK11SlotInfo* slot,
base::span<const uint8_t> required_subject_name,
CERTCertificateList** found_certs) const {
if (!slot) {
return Pkcs12ReaderStatusCode::kMissedSlotInfo;
}
if (required_subject_name.empty()) {
return Pkcs12ReaderStatusCode::kPkcs12CertSubjectNameMissed;
}
SECItem subject_item;
subject_item.len = required_subject_name.size();
subject_item.data = const_cast<uint8_t*>(required_subject_name.data());
// This is a call to NSS, replace it later with a call to Chaps.
SECStatus fetch_cert_with_same_subject_status =
PK11_FindRawCertsWithSubject(slot, &subject_item, found_certs);
if (fetch_cert_with_same_subject_status != SECSuccess) {
return Pkcs12ReaderStatusCode::kPkcs12FindCertsWithSubjectFailed;
}
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::GetSubjectNameDer(
X509* cert,
base::span<const uint8_t>& subject_name_data) const {
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
X509_NAME* subject_name = X509_get_subject_name(cert);
if (!subject_name) {
return Pkcs12ReaderStatusCode::kPkcs12CertSubjectNameMissed;
}
const uint8_t* name_der;
size_t name_der_size;
if (!X509_NAME_get0_der(subject_name, &name_der, &name_der_size)) {
return Pkcs12ReaderStatusCode::kPkcs12CertSubjectNameDerFailed;
}
subject_name_data = {name_der, name_der_size};
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::GetSerialNumberDer(
X509* cert,
bssl::UniquePtr<uint8_t>& der_serial_number,
int& der_serial_number_size) const {
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
const ASN1_INTEGER* serial_number = X509_get0_serialNumber(cert);
uint8_t* der_serial_number_ptr = nullptr;
der_serial_number_size =
i2d_ASN1_INTEGER(serial_number, &der_serial_number_ptr);
der_serial_number = bssl::UniquePtr<uint8_t>(der_serial_number_ptr);
if (der_serial_number_size < 0) {
return Pkcs12ReaderStatusCode::kPkcs12CertSerialNumberDerFailed;
}
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::GetLabel(X509* cert,
std::string& label) const {
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
int alias_len = 0;
const unsigned char* parsed_alias = X509_alias_get0(cert, &alias_len);
if (parsed_alias) {
label = std::string(reinterpret_cast<const char*>(parsed_alias),
static_cast<size_t>(alias_len));
return Pkcs12ReaderStatusCode::kSuccess;
}
X509_NAME* subject_name = X509_get_subject_name(cert);
if (!subject_name) {
return Pkcs12ReaderStatusCode::kPkcs12CertSubjectNameMissed;
}
char temp_label[512];
int get_common_name = X509_NAME_get_text_by_NID(
subject_name, NID_commonName, temp_label, sizeof(temp_label));
if (get_common_name < 0) {
return Pkcs12ReaderStatusCode::kPkcs12CNExtractionFailed;
}
label = temp_label;
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::IsCertWithNicknameInSlots(
const std::string& nickname,
bool& is_nickname_present) const {
if (nickname.empty()) {
return Pkcs12ReaderStatusCode::kPkcs12MissedNickname;
}
CERTCertList* results =
PK11_FindCertsFromNickname(nickname.c_str(), /*wincx=*/nullptr);
is_nickname_present = results && results->list.next != NULL;
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::DoesKeyForCertExist(
PK11SlotInfo* slot,
const Pkcs12ReaderCertSearchType cert_type,
const scoped_refptr<net::X509Certificate>& cert) const {
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
if (!slot) {
return Pkcs12ReaderStatusCode::kMissedSlotInfo;
}
net::ScopedCERTCertificate nss_cert =
net::x509_util::CreateCERTCertificateFromX509Certificate(cert.get());
SECKEYPrivateKey* private_key = nullptr;
switch (cert_type) {
case Pkcs12ReaderCertSearchType::kDerType:
private_key = PK11_FindKeyByDERCert(slot, nss_cert.get(), nullptr);
break;
case Pkcs12ReaderCertSearchType::kPlainType:
private_key = PK11_FindPrivateKeyFromCert(slot, nss_cert.get(), nullptr);
break;
}
if (private_key) {
SECKEY_DestroyPrivateKey(private_key);
return Pkcs12ReaderStatusCode::kSuccess;
}
return Pkcs12ReaderStatusCode::kKeyDataMissed;
}
Pkcs12ReaderStatusCode Pkcs12Reader::EnrichKeyData(KeyData& key_data) const {
if (!key_data.key) {
return Pkcs12ReaderStatusCode::kKeyDataMissed;
}
if (IsKeyRsaType(key_data.key)) {
const RSA* rsa_key = EVP_PKEY_get0_RSA(key_data.key.get());
if (!rsa_key) {
return Pkcs12ReaderStatusCode::kRsaKeyExtractionFailed;
}
std::vector<uint8_t> rsa_key_modulus_bytes =
BignumToBytes(RSA_get0_n(rsa_key));
if (rsa_key_modulus_bytes.empty()) {
return Pkcs12ReaderStatusCode::kPkcs12RsaModulusEmpty;
}
crypto::ScopedSECItem cka_id_item =
MakeIdFromPubKeyNss(rsa_key_modulus_bytes);
key_data.cka_id_value = SECItemToBytes(cka_id_item);
if (key_data.cka_id_value.empty()) {
return Pkcs12ReaderStatusCode::kRsaCkaIdExtractionFailed;
}
return Pkcs12ReaderStatusCode::kSuccess;
}
if (IsKeyEcType(key_data.key)) {
const EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key_data.key.get());
if (!ec_key) {
return Pkcs12ReaderStatusCode::kEcKeyExtractionFailed;
}
std::vector<uint8_t> ec_key_public_bytes = GetEcPublicKeyBytes(ec_key);
if (ec_key_public_bytes.empty()) {
return Pkcs12ReaderStatusCode::kEcKeyBytesEmpty;
}
key_data.cka_id_value = MakePkcs11IdForEcKey(ec_key_public_bytes);
if (key_data.cka_id_value.empty()) {
return Pkcs12ReaderStatusCode::kEcCkaIdExtractionFailed;
}
return Pkcs12ReaderStatusCode::kSuccess;
}
return Pkcs12ReaderStatusCode::kPkcs12NotSupportedKeyType;
}
Pkcs12ReaderStatusCode Pkcs12Reader::CheckRelation(const KeyData& key_data,
X509* cert,
bool& is_related) const {
is_related = false;
if (!key_data.key) {
return Pkcs12ReaderStatusCode::kKeyDataMissed;
}
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
bssl::UniquePtr<EVP_PKEY> evp_pub_key(X509_get_pubkey(cert));
if (!evp_pub_key) {
return Pkcs12ReaderStatusCode::kPKeyExtractionFailed;
}
if (!(IsKeyRsaType(key_data.key) || IsKeyEcType(key_data.key))) {
return Pkcs12ReaderStatusCode::kPkcs12NotSupportedKeyType;
}
int result = EVP_PKEY_cmp(key_data.key.get(), evp_pub_key.get());
if (result == 1) {
is_related = true;
return Pkcs12ReaderStatusCode::kSuccess;
}
if (result < 0) {
return Pkcs12ReaderStatusCode::kPkeyComparisonFailure;
}
return Pkcs12ReaderStatusCode::kPkcs12NoValidCertificatesFound;
}
Pkcs12ReaderStatusCode Pkcs12Reader::GetCertFromDerData(
const unsigned char* der_cert_data,
int der_cert_len,
bssl::UniquePtr<X509>& x509) const {
if (!der_cert_data || !der_cert_len) {
return Pkcs12ReaderStatusCode::kPkcs12NoValidCertificatesFound;
};
X509* cert = d2i_X509(NULL, &der_cert_data, der_cert_len);
if (!cert) {
return Pkcs12ReaderStatusCode::kCreateCertFailed;
}
x509 = bssl::UniquePtr<X509>(cert);
return Pkcs12ReaderStatusCode::kSuccess;
}
Pkcs12ReaderStatusCode Pkcs12Reader::IsCertInSlot(
PK11SlotInfo* slot,
const scoped_refptr<net::X509Certificate>& cert,
bool& is_cert_present) const {
is_cert_present = false;
if (!cert) {
return Pkcs12ReaderStatusCode::kCertificateDataMissed;
}
if (!slot) {
return Pkcs12ReaderStatusCode::kMissedSlotInfo;
}
net::ScopedCERTCertificate nss_cert =
net::x509_util::CreateCERTCertificateFromX509Certificate(cert.get());
CERTCertificate* result =
PK11_FindCertFromDERCert(slot, nss_cert.get(), /*wincx=*/nullptr);
if (result) {
is_cert_present = true;
}
return Pkcs12ReaderStatusCode::kSuccess;
}
} // namespace kcer::internal
|