-
Notifications
You must be signed in to change notification settings - Fork 149
Improve CRL retrieval and fix poisoned cache #833
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
readium/lcp/src/main/java/org/readium/r2/lcp/service/Crl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2026 Readium Foundation. All rights reserved. | ||
| * Use of this source code is governed by the BSD-style license | ||
| * available in the top-level LICENSE file of the project. | ||
| */ | ||
|
|
||
| package org.readium.r2.lcp.service | ||
|
|
||
| import java.io.ByteArrayInputStream | ||
| import java.security.cert.CertificateFactory | ||
| import kotlin.io.encoding.Base64 | ||
| import org.readium.r2.lcp.BuildConfig.DEBUG | ||
| import timber.log.Timber | ||
|
mickael-menu marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * A genuine X.509 certificate revocation list, in its PEM encoding. | ||
| * | ||
| * An instance can only be created from a payload which was successfully parsed as an X.509 CRL. | ||
| */ | ||
| @JvmInline | ||
| internal value class Crl private constructor(val pem: String) { | ||
|
|
||
| companion object { | ||
| private const val PEM_HEADER = "-----BEGIN X509 CRL-----" | ||
| private const val PEM_FOOTER = "-----END X509 CRL-----" | ||
|
|
||
| /** | ||
| * Creates a [Crl] from its DER encoding, or returns null if [data] is not an X.509 CRL. | ||
| */ | ||
| fun fromDer(data: ByteArray): Crl? { | ||
| if (!isX509Crl(data)) { | ||
| return null | ||
| } | ||
| return Crl("$PEM_HEADER${Base64.encode(data)}$PEM_FOOTER") | ||
| } | ||
|
|
||
| /** | ||
| * Parses a PEM-encoded [Crl], or returns null if [pem] is not an X.509 CRL. | ||
| */ | ||
| fun parsePem(pem: String): Crl? { | ||
| if (!pem.startsWith(PEM_HEADER) || !pem.endsWith(PEM_FOOTER)) { | ||
| return null | ||
| } | ||
| val base64 = pem.substring(PEM_HEADER.length, pem.length - PEM_FOOTER.length) | ||
| val data = try { | ||
| // Decoding with [Base64.Mime] instead of [Base64.Default], as it ignores the line | ||
| // breaks found in the CRLs cached by previous versions of the toolkit on API level | ||
| // 25 and below. | ||
| Base64.Mime.decode(base64) | ||
| } catch (e: IllegalArgumentException) { | ||
| if (DEBUG) Timber.e(e) | ||
| return null | ||
| } | ||
| if (!isX509Crl(data)) { | ||
| return null | ||
| } | ||
| return Crl(pem) | ||
| } | ||
|
|
||
| /** | ||
| * Checks that [data] contains a DER-encoded X.509 CRL. | ||
| */ | ||
| private fun isX509Crl(data: ByteArray): Boolean = | ||
| try { | ||
| CertificateFactory.getInstance("X.509") | ||
| .generateCRL(ByteArrayInputStream(data)) != null | ||
| } catch (e: Exception) { | ||
| if (DEBUG) Timber.e(e) | ||
| false | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.