Skip to content

Add finalizer for x509_Revoked #1346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions common/src/jni/main/cpp/conscrypt/native_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6019,6 +6019,21 @@ static jlong NativeCrypto_X509_REVOKED_dup(JNIEnv* env, jclass, jlong x509Revoke
return reinterpret_cast<uintptr_t>(dup);
}

static void NativeCrypto_X509_REVOKED_free(JNIEnv* env, jclass, jlong x509RevokedRef) {
CHECK_ERROR_QUEUE_ON_RETURN;
X509_REVOKED* revoked = reinterpret_cast<X509_REVOKED*>(static_cast<uintptr_t>(x509RevokedRef));
JNI_TRACE("X509_REVOKED_free(%p)", revoked);

if (revoked == nullptr) {
conscrypt::jniutil::throwNullPointerException(env, "revoked == null");
JNI_TRACE("X509_REVOKED_free(%p) => revoked == null", revoked);
return;
}

X509_REVOKED_free(revoked);
revoked = nullptr;
}

static jlong NativeCrypto_get_X509_REVOKED_revocationDate(JNIEnv* env, jclass,
jlong x509RevokedRef) {
CHECK_ERROR_QUEUE_ON_RETURN;
Expand Down Expand Up @@ -11849,6 +11864,7 @@ static JNINativeMethod sNativeCryptoMethods[] = {
CONSCRYPT_NATIVE_METHOD(X509_CRL_get_ext, "(J" REF_X509_CRL "Ljava/lang/String;)J"),
CONSCRYPT_NATIVE_METHOD(X509_REVOKED_get_ext, "(JLjava/lang/String;)J"),
CONSCRYPT_NATIVE_METHOD(X509_REVOKED_dup, "(J)J"),
CONSCRYPT_NATIVE_METHOD(X509_REVOKED_free, "(J)V"),
CONSCRYPT_NATIVE_METHOD(i2d_X509_REVOKED, "(J)[B"),
CONSCRYPT_NATIVE_METHOD(X509_supported_extension, "(J)I"),
CONSCRYPT_NATIVE_METHOD(ASN1_TIME_to_Calendar, "(JLjava/util/Calendar;)V"),
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/org/conscrypt/NativeCrypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,8 @@ static native long X509_CRL_get_nextUpdate(long x509CrlCtx, OpenSSLX509CRL holde

@FastNative static native void X509_REVOKED_print(long bioRef, long x509RevokedCtx);

@FastNative static native void X509_REVOKED_free(long x509RevokedCtx);

// --- X509_EXTENSION ------------------------------------------------------

@FastNative static native int X509_supported_extension(long x509ExtensionRef);
Expand Down
22 changes: 20 additions & 2 deletions common/src/main/java/org/conscrypt/OpenSSLX509CRLEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.conscrypt;

import org.conscrypt.OpenSSLX509CertificateFactory.ParsingException;

import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.cert.CRLException;
Expand All @@ -24,12 +26,11 @@
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.conscrypt.OpenSSLX509CertificateFactory.ParsingException;

/**
* An implementation of {@link X509CRLEntry} based on BoringSSL.
*/
final class OpenSSLX509CRLEntry extends X509CRLEntry {
final class OpenSSLX509CRLEntry extends X509CRLEntry implements AutoCloseable {
private final long mContext;
private final Date revocationDate;

Expand Down Expand Up @@ -135,4 +136,21 @@ public String toString() {
NativeCrypto.BIO_free_all(bioCtx);
}
}

@Override
public void close() {
if (mContext != 0) {
NativeCrypto.X509_REVOKED_free(mContext);
}
}

@Override
@SuppressWarnings("Finalize")
protected void finalize() throws Throwable {
try {
close();
} finally {
super.finalize();
}
}
}