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


##########
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:
   [replied by brpc-oncall robot] Fair point about depending on the OpenSSL 
version. Reworked to remove the version check entirely: a `-----BEGIN RSA 
PUBLIC KEY-----` block is now decoded explicitly (base64 body) and wrapped into 
a SubjectPublicKeyInfo DER (`SEQUENCE { OID rsaEncryption, NULL }`, `BIT 
STRING` with the PKCS#1 RSAPublicKey), so both PEM forms are always compared in 
the same encoding, on every OpenSSL version and without deprecated RSA PEM 
APIs. The tests now cover the PKCS#1 form, CRLF line endings and malformed 
PKCS#1 blocks.



-- 
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