Class RsaSignatureContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, SignatureContext, TagEngine<Signature>
This context binds an RSA key and a signature specification, exposes a
passthrough view via wrap(java.io.InputStream), and finalizes at
end-of-stream. In produce (SIGN) mode the signature is emitted as a trailer.
In verify (VERIFY) mode the computed signature is compared against an
expected tag using a configured verification approach.
Modes
- SIGN - consumes body bytes and appends the computed signature as a trailer after the original data has been read.
- VERIFY - consumes body bytes and compares the computed signature
at EOF to the value provided via
setExpectedTag(byte[]). The outcome is handled by the verification approach set withsetVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate).
Usage
Sign with RSASSA-PSS (SHA-256, salt 32)
RsaSigSpec spec = RsaSigSpec.pss(RsaSigSpec.Hash.SHA256, 32);
try (RsaSignatureContext ctx = new RsaSignatureContext(algo, privateKey, spec);
InputStream in = Files.newInputStream(msgPath);
InputStream out = ctx.wrap(in)) {
// out yields message bytes, then signature trailer of length ctx.tagLength()
Files.copy(out, signedPayloadPath);
}
Verify a detached RSA signature
byte[] sig = Files.readAllBytes(sigPath);
RsaSigSpec spec = RsaSigSpec.pss(RsaSigSpec.Hash.SHA256, 32);
try (RsaSignatureContext ctx = new RsaSignatureContext(algo, publicKey, spec);
InputStream in = Files.newInputStream(bodyWithoutTrailer);
InputStream out = ctx.wrap(in)) {
ctx.setExpectedTag(sig);
// Throw on mismatch at EOF:
ctx.setVerificationApproach(ctx.getVerificationCore().getThrowOnMismatch());
Files.copy(out, recoveredMsgPath);
}
Notes
- Supported modes: PKCS#1 v1.5 and RSASSA-PSS. If
pssSaltLenin the spec is -1, the salt length defaults to the digest size. tagLength()equals the RSA modulus length in bytes.- Instances are single-use and not thread-safe. Calling
wrap(InputStream)more than once throwsIllegalStateException.
-
Constructor Summary
ConstructorsConstructorDescriptionRsaSignatureContext(CryptoAlgorithm alg, PrivateKey k, RsaSigSpec spec) Constructs a signing context using the given private key and signature specification.RsaSignatureContext(CryptoAlgorithm alg, PublicKey k, RsaSigSpec spec) Constructs a verification context using the given public key and signature specification. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm descriptor associated with this context.voidclose()Closes the context and its active stream if present.Returns the core verification predicate for RSA signatures.key()Returns the key bound to this context.voidsetExpectedTag(byte[] expected) Sets the expected signature for verification mode.voidSets the verification approach used to handle comparison outcomes in VERIFY mode.intReturns the signature length in bytes.wrap(InputStream upstream) Wraps an upstream stream and returns a passthrough stream that performs signing or verification.
-
Constructor Details
-
RsaSignatureContext
public RsaSignatureContext(CryptoAlgorithm alg, PrivateKey k, RsaSigSpec spec) throws GeneralSecurityException Constructs a signing context using the given private key and signature specification.The context operates in SIGN mode. At end-of-stream, a signature of length
tagLength()is produced and emitted as a trailer by the wrapped stream.- Parameters:
alg- algorithm descriptor owning this context; must not benullk- private key used for signing; must not benullspec- RSA signature specification (PKCS#1 v1.5 or PSS); must not benull- Throws:
GeneralSecurityException- if the JCA signature engine cannot be initializedNullPointerException- if any argument isnull
-
RsaSignatureContext
public RsaSignatureContext(CryptoAlgorithm alg, PublicKey k, RsaSigSpec spec) throws GeneralSecurityException Constructs a verification context using the given public key and signature specification.The context operates in VERIFY mode. Provide the expected signature via
setExpectedTag(byte[])before the wrapped stream reaches EOF. Final verification occurs at EOF using the configured verification approach.- Parameters:
alg- algorithm descriptor owning this context; must not benullk- public key used for verification; must not benullspec- RSA signature specification (PKCS#1 v1.5 or PSS); must not benull- Throws:
GeneralSecurityException- if the JCA signature engine cannot be initializedNullPointerException- if any argument isnull
-
-
Method Details
-
algorithm
Returns the algorithm descriptor associated with this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the owning algorithm
-
key
Returns the key bound to this context.This is a private key in SIGN mode and a public key in VERIFY mode.
- Specified by:
keyin interfaceCryptoContext- Returns:
- the RSA key used by this context
-
close
public void close()Closes the context and its active stream if present.If a wrapped stream is active, this method attempts to close it, which triggers finalization of signing or verification.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
wrap
Wraps an upstream stream and returns a passthrough stream that performs signing or verification.The returned stream relays bytes from
upstreamwhile updating the signature engine. On end-of-stream:- SIGN: computes the signature and emits it as a trailer.
- VERIFY: verifies against the expected tag and signals the outcome according to
setVerificationApproach(VerificationBiPredicate).
A context instance is single-use; calling this method twice throws
IllegalStateException.- Specified by:
wrapin interfaceTagEngine<Signature>- Parameters:
upstream- source stream to wrap- Returns:
- a new input stream that performs signing or verification as data is read
- Throws:
NullPointerException- ifupstreamisnullIllegalStateException- if this context was already used for a previous streamIOException- if the signature engine update or finalization fails
-
tagLength
-
setExpectedTag
public void setExpectedTag(byte[] expected) Sets the expected signature for verification mode.Only meaningful in VERIFY mode. The array is copied defensively. If set to
null, verification will fail unless the chosen verification approach records and ignores failures.- Specified by:
setExpectedTagin interfaceTagEngine<Signature>- Parameters:
expected- expected signature bytes ornullto clear
-
setVerificationApproach
public void setVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate<Signature> strategy) Sets the verification approach used to handle comparison outcomes in VERIFY mode.Common strategies include constant-time byte comparison via
ByteVerificationStrategyand JCA-signature-based checks viaSignatureVerificationStrategy. Decorators such asThrowingBiPredicate.VerificationBiPredicate.getThrowOnMismatch()andThrowingBiPredicate.VerificationBiPredicate.getFlagOkInCtx(conflux.CtxInterface, conflux.Key)can be used to throw on mismatch or to record a boolean in a context.- Specified by:
setVerificationApproachin interfaceTagEngine<Signature>- Parameters:
strategy- verification predicate; may benullto keep the default
-
getVerificationCore
Returns the core verification predicate for RSA signatures.The default behavior is to throw on mismatch, implemented by decorating the returned predicate with
getThrowOnMismatch()when the context is used and no custom strategy has been supplied.- Specified by:
getVerificationCorein interfaceTagEngine<Signature>- Returns:
- a predicate that delegates to
Signature.verify(byte[])
-