]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
crypto-util: simplify pubkey_fingerprint()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 30 Jun 2026 14:56:43 +0000 (23:56 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 2 Jul 2026 18:02:56 +0000 (03:02 +0900)
There is no need to call i2d_PublicKey() twice. Passing a pointer
to NULL allows OpenSSL to automatically allocate the necessary buffer.

src/shared/crypto-util.c

index 68455abc64ca8b324d84af60b67e2f6009b7eba1..f99147d7bb046824708cc84ed5699fbf05bf2543 100644 (file)
@@ -1635,10 +1635,9 @@ int ecc_ecdh(const EVP_PKEY *private_pkey,
 
 int pubkey_fingerprint(EVP_PKEY *pk, const EVP_MD *md, void **ret, size_t *ret_size) {
         _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX* m = NULL;
-        _cleanup_free_ void *d = NULL, *h = NULL;
-        int sz, lsz, msz;
+        _cleanup_free_ void *h = NULL;
+        int lsz, msz;
         unsigned umsz;
-        unsigned char *dd;
         int r;
 
         /* Calculates a message digest of the DER encoded public key */
@@ -1652,15 +1651,8 @@ int pubkey_fingerprint(EVP_PKEY *pk, const EVP_MD *md, void **ret, size_t *ret_s
         if (r < 0)
                 return r;
 
-        sz = sym_i2d_PublicKey(pk, NULL);
-        if (sz < 0)
-                return log_openssl_errors(LOG_DEBUG, "Unable to convert public key to DER format");
-
-        dd = d = malloc(sz);
-        if (!d)
-                return log_oom_debug();
-
-        lsz = sym_i2d_PublicKey(pk, &dd);
+        _cleanup_(OPENSSL_freep) void *d = NULL;
+        lsz = sym_i2d_PublicKey(pk, (unsigned char**) &d);
         if (lsz < 0)
                 return log_openssl_errors(LOG_DEBUG, "Unable to convert public key to DER format");