Skip to content

Commit

Permalink
Made CRL downloading customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Jan 10, 2024
1 parent bf08381 commit 5bdd331
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ They depend on several other libraries so I suggest you are going for the Maven

# News and noteworthy

* v9.2.4 - work in progress
* Via `CRLCache.setDownloader` a custom downloader can be configured. The default solution uses the Java runtime HttpURLConnection.
* v9.2.3 - 2024-01-10
* Changed the default checking mode in `CertificateRevocationChecker` from `OCSP` to `CRL_BEFORE_OCSP` due to https://github.com/phax/phase4/issues/124#issuecomment-1884398195
* v9.2.2 - 2024-01-08
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,73 @@ public static TimedCRL ofNow (@Nonnull final CRL aCRL)
}

/**
* A cache for CRLs read from remote locations.
* Callback interface to download CRL data. Use it globally with
* {@link CRLCache#setDownloader(ICRLDownloader)}.
*
* @author Philip Helger
* @since 9.2.4
*/
@FunctionalInterface
public interface ICRLDownloader
{
/**
* Download the content of the provided URL
*
* @param sURL
* The CRL URL to download. Neither <code>null</code> nor empty.
* @return <code>null</code> if no payload was returned
* @throws Exception
* In case of error
*/
@Nullable
byte [] downloadURL (@Nonnull @Nonempty String sURL) throws Exception;
}

/**
* A cache for CRLs read from remote locations. The remote reading can be
* customized by setting a specific CRL downloader via
* {@link #setDownloader(ICRLDownloader)}
*
* @author Philip Helger
*/
public static final class CRLCache extends Cache <String, TimedCRL>
{
public static final CRLCache INSTANCE = new CRLCache ();
public static final ICRLDownloader DEFAULT_DOWNLOADER = sURL -> {
// Use the built in HTTP client here (global proxy, etc.)
try (final InputStream aIS = new URL (sURL).openStream ())
{
return StreamHelper.getAllBytes (aIS);
}
};

private static ICRLDownloader s_aDownloader = DEFAULT_DOWNLOADER;

@Nonnull
public static ICRLDownloader getDownloader ()
{
return s_aDownloader;
}

public static void setDownloader (@Nonnull final ICRLDownloader aDownloader)
{
ValueEnforcer.notNull (aDownloader, "Downloader");
s_aDownloader = aDownloader;
LOGGER.info ("Set the global CRL Downloader to be " + aDownloader);
}

@Nullable
private static TimedCRL _loadCRL (@Nonnull final String sCRLURL)
{
if (EURLProtocol.HTTP.isUsedInURL (sCRLURL) ||
EURLProtocol.HTTPS.isUsedInURL (sCRLURL) ||
EURLProtocol.FTP.isUsedInURL (sCRLURL))
if (EURLProtocol.HTTP.isUsedInURL (sCRLURL) || EURLProtocol.HTTPS.isUsedInURL (sCRLURL))
{
// Try to download from remote URL
LOGGER.info ("Trying to download CRL from URL '" + sCRLURL + "'");
LOGGER.info ("Downloading CRL from URL '" + sCRLURL + "'");
final StopWatch aSW = StopWatch.createdStarted ();
int nByteCount = 0;
// Use the built in HTTP client here (global proxy, etc.)
try (final InputStream aIS = new URL (sCRLURL).openStream ())
try
{
final byte [] aCRLBytes = StreamHelper.getAllBytes (aIS);
final byte [] aCRLBytes = s_aDownloader.downloadURL (sCRLURL);
if (aCRLBytes != null)
{
nByteCount = aCRLBytes.length;
Expand All @@ -164,7 +208,8 @@ private static TimedCRL _loadCRL (@Nonnull final String sCRLURL)
finally
{
aSW.stop ();
LOGGER.info ("Downloading the CRL took " + aSW.getMillis () + " milliseconds for " + nByteCount + " bytes");
if (aSW.getMillis () > 1_000)
LOGGER.info ("Downloading the CRL took " + aSW.getMillis () + " milliseconds for " + nByteCount + " bytes");
}
}

Expand Down

0 comments on commit 5bdd331

Please sign in to comment.