Copilot commented on code in PR #3779:
URL: https://github.com/apache/brpc/pull/3779#discussion_r4717670434


##########
src/brpc/policy/mysql/mysql_auth_scramble.cpp:
##########
@@ -38,6 +40,55 @@ bool Sha256Bytes(const unsigned char* data, size_t len, 
unsigned char out[32]) {
         && digest_len == 32;
 }
 
+// Parses a PEM-encoded public key and returns its SubjectPublicKeyInfo DER
+// encoding, or an empty string when |pem| is not a parseable public key.
+// Both "BEGIN PUBLIC KEY" (SubjectPublicKeyInfo) and "BEGIN RSA PUBLIC KEY"
+// (PKCS#1) are accepted, and both are re-encoded as SubjectPublicKeyInfo so
+// that they compare equal.
+std::string PublicKeyDer(const butil::StringPiece& pem) {
+    if (pem.empty()) {
+        return std::string();
+    }
+    unsigned char* der = nullptr;
+    int len = 0;
+    {
+        BIO* bio = BIO_new_mem_buf(pem.data(), static_cast<int>(pem.size()));
+        if (bio == nullptr) {
+            return std::string();
+        }
+        EVP_PKEY* pkey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
+        BIO_free(bio);
+        if (pkey != nullptr) {
+            len = i2d_PUBKEY(pkey, &der);
+            EVP_PKEY_free(pkey);
+        }
+    }
+#if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_NUMBER < 0x30000000L
+    // Before OpenSSL 3.0, PEM_read_bio_PUBKEY() only reads
+    // SubjectPublicKeyInfo; fall back to the PKCS#1 reader there.
+    if (len <= 0) {

Review Comment:
   The PKCS#1 fallback is still compiled only for OpenSSL versions below 3.0. 
`PEM_read_bio_PUBKEY` remains the SubjectPublicKeyInfo reader on OpenSSL 3, so 
a valid `BEGIN RSA PUBLIC KEY` input is still rejected there; the new 
`Pkcs1AndSpkiEncodingsOfSameKeyMatch` case will fail on the OpenSSL 3 builds 
used by this project, and pins in that encoding cannot be accepted. Keep the 
PKCS#1 fallback available for OpenSSL 3 as well (or use a version-independent 
DER parser).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to