The fix I need introduces the use of EVP_PKEY_get0_RSA().

Ownership handling in this scope is a bit wonky: X509_get_pubkey()
increments the refcount of pPublicKey. What we actually want is a
reference of its pkey.rsa. So use X509_get0_pubkey() instead and up the
refcount of the RSA. Finally, let's not leak the cert on success.

Index: signature.c
===================================================================
RCS file: /cvs/src/lib/libkeynote/signature.c,v
retrieving revision 1.26
diff -u -p -r1.26 signature.c
--- signature.c 9 May 2017 13:52:45 -0000       1.26
+++ signature.c 21 Nov 2021 22:41:00 -0000
@@ -522,7 +522,7 @@ kn_decode_key(struct keynote_deckey *dc,
            return -1;
        }
 
-       if ((pPublicKey = X509_get_pubkey(px509Cert)) == NULL) {
+       if ((pPublicKey = X509_get0_pubkey(px509Cert)) == NULL) {
            free(ptr);
            X509_free(px509Cert);
            keynote_errno = ERROR_SYNTAX;
@@ -530,9 +530,11 @@ kn_decode_key(struct keynote_deckey *dc,
        }
 
        /* RSA-specific */
-       dc->dec_key = pPublicKey->pkey.rsa;
+       dc->dec_key = EVP_PKEY_get0_RSA(pPublicKey);
+       RSA_up_ref(dc->dec_key);
 
        free(ptr);
+       X509_free(px509Cert);
        return 0;
     }    
 

Reply via email to