|
21 | 21 | #include "third_party/boringssl/src/include/openssl/pkcs8.h" |
22 | 22 | #include "third_party/boringssl/src/include/openssl/stack.h" |
23 | 23 |
|
| 24 | +namespace owt { |
24 | 25 | namespace quic { |
25 | 26 |
|
26 | 27 | ProofSourceOwt::ProofSourceOwt() {} |
27 | 28 |
|
28 | 29 | ProofSourceOwt::~ProofSourceOwt() {} |
29 | 30 |
|
30 | | -ProofSource::TicketCrypter* ProofSourceOwt::GetTicketCrypter() { |
31 | | - return nullptr; |
| 31 | +bool ProofSourceOwt::Initialize(const base::FilePath& pfx_path, |
| 32 | + const std::string& password) { |
| 33 | + crypto::EnsureOpenSSLInit(); |
| 34 | + std::string pfx_data; |
| 35 | + if (!base::ReadFileToString(pfx_path, &pfx_data)) { |
| 36 | + DLOG(FATAL) << "Unable to read pfx file."; |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + EVP_PKEY* key = nullptr; |
| 41 | + bssl::UniquePtr<STACK_OF(X509)> certs(sk_X509_new_null()); |
| 42 | + CBS pkcs12; |
| 43 | + CBS_init(&pkcs12, reinterpret_cast<const uint8_t*>(pfx_data.c_str()), |
| 44 | + pfx_data.size()); |
| 45 | + if (PKCS12_get_key_and_certs(&key, certs.get(), &pkcs12, password.c_str()) == |
| 46 | + 0) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + std::vector<std::string> certs_string; |
| 50 | + for (X509* cert : certs.get()) { |
| 51 | + int len(0); |
| 52 | + unsigned char* buffer(nullptr); |
| 53 | + len = i2d_X509(cert, &buffer); |
| 54 | + if (len < 0) { |
| 55 | + LOG(ERROR) << "Failed to get X509 certificate."; |
| 56 | + return false; |
| 57 | + } |
| 58 | + auto cert_list = net::X509Certificate::CreateCertificateListFromBytes( |
| 59 | + base::as_bytes(base::span<unsigned char>(buffer, len)), |
| 60 | + net::X509Certificate::FORMAT_AUTO); |
| 61 | + certs_in_file_.insert(certs_in_file_.end(), cert_list.begin(), |
| 62 | + cert_list.end()); |
| 63 | + bssl::UniquePtr<CRYPTO_BUFFER> crypto_buffer = |
| 64 | + net::x509_util::CreateCryptoBuffer(base::make_span(buffer, len)); |
| 65 | + certs_string.emplace_back( |
| 66 | + net::x509_util::CryptoBufferAsStringPiece(crypto_buffer.get())); |
| 67 | + } |
| 68 | + |
| 69 | + if (certs_in_file_.empty()) { |
| 70 | + DLOG(FATAL) << "No certificates."; |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + chain_ = new ::quic::ProofSource::Chain(certs_string); |
| 75 | + private_key_ = std::make_unique<::quic::CertificatePrivateKey>( |
| 76 | + bssl::UniquePtr<EVP_PKEY>(key)); |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +absl::InlinedVector<uint16_t, 8> |
| 81 | +ProofSourceOwt::SupportedTlsSignatureAlgorithms() const { |
| 82 | + // Allow all signature algorithms that BoringSSL allows. |
| 83 | + return {}; |
| 84 | +} |
| 85 | + |
| 86 | +::quic::ProofSource::TicketCrypter* ProofSourceOwt::GetTicketCrypter() { |
| 87 | + return ticket_crypter_.get(); |
| 88 | +} |
| 89 | + |
| 90 | +void ProofSourceOwt::SetTicketCrypter( |
| 91 | + std::unique_ptr<::quic::ProofSource::TicketCrypter> ticket_crypter) { |
| 92 | + ticket_crypter_ = std::move(ticket_crypter); |
32 | 93 | } |
33 | 94 |
|
34 | | -void ProofSourceOwt::GetProof(const QuicSocketAddress& server_address, |
35 | | - const QuicSocketAddress& client_address, |
| 95 | +bool ProofSourceOwt::GetProofInner( |
| 96 | + const ::quic::QuicSocketAddress& server_addr, |
| 97 | + const std::string& hostname, |
| 98 | + const std::string& server_config, |
| 99 | + ::quic::QuicTransportVersion quic_version, |
| 100 | + absl::string_view chlo_hash, |
| 101 | + ::quiche::QuicheReferenceCountedPointer<::quic::ProofSource::Chain>* out_chain, |
| 102 | + ::quic::QuicCryptoProof* proof) { |
| 103 | + // This function is copied from `ProofSourceChromium`, but `leaf_cert_scts` is |
| 104 | + // not set. |
| 105 | + DCHECK(proof); |
| 106 | + DCHECK(private_key_); |
| 107 | + |
| 108 | + crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 109 | + bssl::ScopedEVP_MD_CTX sign_context; |
| 110 | + EVP_PKEY_CTX* pkey_ctx; |
| 111 | + |
| 112 | + uint32_t len_tmp = chlo_hash.length(); |
| 113 | + if (!EVP_DigestSignInit(sign_context.get(), &pkey_ctx, EVP_sha256(), nullptr, |
| 114 | + private_key_->private_key()) || |
| 115 | + (EVP_PKEY_id(private_key_->private_key()) == EVP_PKEY_RSA && |
| 116 | + (!EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) || |
| 117 | + !EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1))) || |
| 118 | + !EVP_DigestSignUpdate( |
| 119 | + sign_context.get(), |
| 120 | + reinterpret_cast<const uint8_t*>(::quic::kProofSignatureLabel), |
| 121 | + sizeof(::quic::kProofSignatureLabel)) || |
| 122 | + !EVP_DigestSignUpdate(sign_context.get(), |
| 123 | + reinterpret_cast<const uint8_t*>(&len_tmp), |
| 124 | + sizeof(len_tmp)) || |
| 125 | + !EVP_DigestSignUpdate(sign_context.get(), |
| 126 | + reinterpret_cast<const uint8_t*>(chlo_hash.data()), |
| 127 | + len_tmp) || |
| 128 | + !EVP_DigestSignUpdate( |
| 129 | + sign_context.get(), |
| 130 | + reinterpret_cast<const uint8_t*>(server_config.data()), |
| 131 | + server_config.size())) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + // Determine the maximum length of the signature. |
| 135 | + size_t len = 0; |
| 136 | + if (!EVP_DigestSignFinal(sign_context.get(), nullptr, &len)) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + std::vector<uint8_t> signature(len); |
| 140 | + // Sign it. |
| 141 | + if (!EVP_DigestSignFinal(sign_context.get(), signature.data(), &len)) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + signature.resize(len); |
| 145 | + proof->signature.assign(reinterpret_cast<const char*>(signature.data()), |
| 146 | + signature.size()); |
| 147 | + *out_chain = chain_; |
| 148 | + VLOG(1) << "signature: " |
| 149 | + << base::HexEncode(proof->signature.data(), proof->signature.size()); |
| 150 | + return true; |
| 151 | +} |
| 152 | + |
| 153 | +void ProofSourceOwt::GetProof(const ::quic::QuicSocketAddress& server_address, |
| 154 | + const ::quic::QuicSocketAddress& client_address, |
36 | 155 | const std::string& hostname, |
37 | 156 | const std::string& server_config, |
38 | | - QuicTransportVersion quic_version, |
| 157 | + ::quic::QuicTransportVersion quic_version, |
39 | 158 | absl::string_view chlo_hash, |
40 | 159 | std::unique_ptr<Callback> callback) { |
41 | | - bool cert_matched_sni; |
42 | | - ::quiche::QuicheReferenceCountedPointer<ProofSource::Chain> chain = |
43 | | - GetCertChain(server_address, client_address, hostname, &cert_matched_sni); |
44 | | - QuicCryptoProof proof; |
45 | | - proof.signature = "fake signature"; |
46 | | - proof.leaf_cert_scts = "fake timestamp"; |
47 | | - callback->Run(true, chain, proof, nullptr); |
| 160 | + // As a transitional implementation, just call the synchronous version of |
| 161 | + // GetProof, then invoke the callback with the results and destroy it. |
| 162 | + ::quiche::QuicheReferenceCountedPointer<::quic::ProofSource::Chain> chain; |
| 163 | + std::string signature; |
| 164 | + std::string leaf_cert_sct; |
| 165 | + ::quic::QuicCryptoProof out_proof; |
| 166 | + |
| 167 | + const bool ok = GetProofInner(server_address, hostname, server_config, |
| 168 | + quic_version, chlo_hash, &chain, &out_proof); |
| 169 | + callback->Run(ok, chain, out_proof, nullptr /* details */); |
48 | 170 | } |
49 | 171 |
|
50 | | -::quiche::QuicheReferenceCountedPointer<ProofSource::Chain> |
51 | | -ProofSourceOwt::GetCertChain(const QuicSocketAddress& server_address, |
52 | | - const QuicSocketAddress& client_address, |
| 172 | +::quiche::QuicheReferenceCountedPointer<::quic::ProofSource::Chain> |
| 173 | +ProofSourceOwt::GetCertChain(const ::quic::QuicSocketAddress& server_address, |
| 174 | + const ::quic::QuicSocketAddress& client_address, |
53 | 175 | const std::string& hostname, |
54 | | - bool* cert_matched_sni) { |
55 | | - std::vector<std::string> certs; |
56 | | - certs.push_back("fake cert"); |
57 | | - return ::quiche::QuicheReferenceCountedPointer<ProofSource::Chain>( |
58 | | - new ProofSource::Chain(certs)); |
| 176 | + bool* cert_matched_sni) { |
| 177 | + *cert_matched_sni = false; |
| 178 | + if (!hostname.empty()) { |
| 179 | + for (const scoped_refptr<net::X509Certificate>& cert : certs_in_file_) { |
| 180 | + if (cert->VerifyNameMatch(hostname)) { |
| 181 | + *cert_matched_sni = true; |
| 182 | + break; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + return chain_; |
59 | 187 | } |
60 | 188 |
|
61 | 189 | void ProofSourceOwt::ComputeTlsSignature( |
62 | | - const QuicSocketAddress& server_address, |
63 | | - const QuicSocketAddress& client_address, |
| 190 | + const ::quic::QuicSocketAddress& server_address, |
| 191 | + const ::quic::QuicSocketAddress& client_address, |
64 | 192 | const std::string& hostname, |
65 | 193 | uint16_t signature_algorithm, |
66 | 194 | absl::string_view in, |
67 | 195 | std::unique_ptr<SignatureCallback> callback) { |
68 | | - callback->Run(true, "fake signature", nullptr); |
| 196 | + crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 197 | + bssl::ScopedEVP_MD_CTX sign_context; |
| 198 | + EVP_PKEY_CTX* pkey_ctx; |
| 199 | + |
| 200 | + size_t siglen; |
| 201 | + std::string sig; |
| 202 | + if (!EVP_DigestSignInit(sign_context.get(), &pkey_ctx, EVP_sha256(), nullptr, |
| 203 | + private_key_->private_key()) || |
| 204 | + (EVP_PKEY_id(private_key_->private_key()) == EVP_PKEY_RSA && |
| 205 | + (!EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) || |
| 206 | + !EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1))) || |
| 207 | + !EVP_DigestSignUpdate(sign_context.get(), |
| 208 | + reinterpret_cast<const uint8_t*>(in.data()), |
| 209 | + in.size()) || |
| 210 | + !EVP_DigestSignFinal(sign_context.get(), nullptr, &siglen)) { |
| 211 | + callback->Run(false, sig, nullptr); |
| 212 | + return; |
| 213 | + } |
| 214 | + sig.resize(siglen); |
| 215 | + if (!EVP_DigestSignFinal( |
| 216 | + sign_context.get(), |
| 217 | + reinterpret_cast<uint8_t*>(const_cast<char*>(sig.data())), &siglen)) { |
| 218 | + callback->Run(false, sig, nullptr); |
| 219 | + return; |
| 220 | + } |
| 221 | + sig.resize(siglen); |
| 222 | + |
| 223 | + callback->Run(true, sig, nullptr); |
69 | 224 | } |
70 | 225 |
|
71 | 226 | } // namespace quic |
| 227 | +} // namespace owt |
0 commit comments