Class GenericJcaSignatureContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, SignatureContext, TagEngine<Signature>
Signature to the streaming
SignatureContext/TagEngine model.
GenericJcaSignatureContext binds a key and a
Signature engine to a pull-based pipeline: the wrapped
stream forwards bytes unchanged while updating the engine. In SIGN mode a
fixed-length trailer containing the signature is appended at end-of-stream;
in VERIFY mode the computed signature is compared at end-of-stream to an
expected tag supplied by the caller. The trailer length is determined up
front so pipelines can append or strip trailers without buffering the whole
stream.
Modes and usage
- SIGN: consume body bytes; on EOF call
Signature.sign()and append a trailer oftagLength()bytes. - VERIFY: consume body bytes; on EOF compare against the expected
tag set via
setExpectedTag(byte[])using the verification approach configured withsetVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate).
Length resolvers
A fixed tag length is required in both modes. For SIGN, a
GenericJcaSignatureContext.SignLengthResolver supplies the produced length (either a constant or
by probing a provider). For VERIFY, a GenericJcaSignatureContext.VerifyLengthResolver supplies
the expected length derived from key parameters or a known constant.
Examples
Sign with RSA-PSS
GenericJcaSignatureContext ctx = new GenericJcaSignatureContext(
algorithm,
privateKey,
GenericJcaSignatureContext.jcaFactory("RSASSA-PSS", null),
GenericJcaSignatureContext.SignLengthResolver.probeWith("RSASSA-PSS", null));
try (InputStream in = ctx.wrap(sourceStream)) {
in.transferTo(out); // body, then trailer of ctx.tagLength() bytes
}
Verify detached RSA signature
GenericJcaSignatureContext vctx = new GenericJcaSignatureContext(
algorithm,
publicKey,
GenericJcaSignatureContext.jcaFactory("SHA256withRSA", null),
GenericJcaSignatureContext.VerifyLengthResolver.fixed(256)); // 2048-bit modulus
vctx.setVerificationApproach(vctx.getVerificationCore().getThrowOnMismatch());
vctx.setExpectedTag(signatureBytes);
try (InputStream verified = vctx.wrap(bodyWithoutTrailer)) {
verified.transferTo(java.io.OutputStream.nullOutputStream()); // throws on mismatch at EOF
}
Thread-safety
Instances are stateful, single-use, and not thread-safe. Call
wrap(InputStream) at most once per instance.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFactory of initializedSignatureengines.static interfaceStrategy for determining the signature trailer length in SIGN mode.static interfaceStrategy for determining the expected signature length in VERIFY mode. -
Constructor Summary
ConstructorsConstructorDescriptionGenericJcaSignatureContext(CryptoAlgorithm algorithm, PrivateKey privateKey, GenericJcaSignatureContext.EngineFactory engineFactory, GenericJcaSignatureContext.SignLengthResolver lengthResolver) Constructs a SIGN-mode context.GenericJcaSignatureContext(CryptoAlgorithm algorithm, PublicKey publicKey, GenericJcaSignatureContext.EngineFactory engineFactory, GenericJcaSignatureContext.VerifyLengthResolver verifyLengthResolver) Constructs a VERIFY-mode context. -
Method Summary
Modifier and TypeMethodDescriptionReturns the logical algorithm associated with this context.voidclose()Closes the active wrapped stream, if any, suppressingIOException.Returns the core verification predicate for signature comparison.jcaFactory(String jcaAlg, String provider) Convenience factory that produces a JCASignatureinitialized for sign or verify.key()Returns the key bound to this context.voidsetExpectedTag(byte[] expected) Sets the expected verification tag to be checked when the wrapped stream finishes.voidSets the verification approach used in VERIFY mode to compare the expected and computed signatures.intReturns the declared tag length in bytes.wrap(InputStream upstream) Wraps the supplied upstream stream so the underlyingSignatureis updated as data flows.
-
Constructor Details
-
GenericJcaSignatureContext
public GenericJcaSignatureContext(CryptoAlgorithm algorithm, PrivateKey privateKey, GenericJcaSignatureContext.EngineFactory engineFactory, GenericJcaSignatureContext.SignLengthResolver lengthResolver) throws GeneralSecurityException Constructs a SIGN-mode context.First resolves the produced signature length via
lengthResolver, then creates and initializes the signing engine viaengineFactory.- Parameters:
algorithm- logical algorithm descriptor associated with this context; must not benullprivateKey- private key used for signing; must not benullengineFactory- factory creating an initializedSignaturein sign mode; must not benulllengthResolver- strategy to resolve the produced signature length up front; must not benull- Throws:
GeneralSecurityException- if length resolution or engine initialization failsNullPointerException- if any required argument isnullUnsupportedRoleException- if the algorithm or key is LMS/XMSS; rejection precedes resolver and factory invocation
-
GenericJcaSignatureContext
public GenericJcaSignatureContext(CryptoAlgorithm algorithm, PublicKey publicKey, GenericJcaSignatureContext.EngineFactory engineFactory, GenericJcaSignatureContext.VerifyLengthResolver verifyLengthResolver) throws GeneralSecurityException Constructs a VERIFY-mode context.First resolves the expected tag length via
verifyLengthResolver, then creates and initializes the verifying engine viaengineFactory.- Parameters:
algorithm- logical algorithm descriptor associated with this context; must not benullpublicKey- public key used for verification; must not benullengineFactory- factory creating an initializedSignaturein verify mode; must not benullverifyLengthResolver- strategy to resolve the expected signature length up front; must not benull- Throws:
GeneralSecurityException- if length resolution or engine initialization failsNullPointerException- if any required argument isnull
-
-
Method Details
-
jcaFactory
Convenience factory that produces a JCASignatureinitialized for sign or verify.The returned factory performs
Signature.getInstance(String)(optionally with a provider) and then callsSignature.initSign(PrivateKey)orSignature.initVerify(PublicKey).EngineFactory f = GenericJcaSignatureContext.jcaFactory("SHA256withRSA", null); Signature signer = f.create(privateKey, true);- Implementation Note:
- In signing mode, the returned factory rejects LMS/XMSS with
UnsupportedRoleExceptionbefore provider lookup or key use. - Parameters:
jcaAlg- JCA signature algorithm name; must not benullprovider- optional provider name;nullselects the highest-priority provider- Returns:
- factory creating initialized
Signatureengines for the specified algorithm/provider
-
algorithm
Returns the logical algorithm associated with this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the algorithm descriptor
-
key
Returns the key bound to this context.- Specified by:
keyin interfaceCryptoContext- Returns:
- the signing key in SIGN mode or the verification key in VERIFY mode
-
close
public void close()Closes the active wrapped stream, if any, suppressingIOException.Keeping
close()non-throwing simplifies pipeline cleanup.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
wrap
Wraps the supplied upstream stream so the underlyingSignatureis updated as data flows.This method may be called only once per instance. In SIGN mode the returned stream appends a trailer of
tagLength()bytes when the upstream finishes. In VERIFY mode the returned stream performs verification at EOF against the expected tag configured viasetExpectedTag(byte[])and surfaces the outcome using the verification approach set viasetVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate).- Specified by:
wrapin interfaceTagEngine<Signature>- Parameters:
upstream- source stream whose bytes will be fed into the signature; must not benull- Returns:
- stream that must be fully consumed to trigger signing or verification
- Throws:
NullPointerException- ifupstreamisnullIllegalStateException- if this context has already wrapped a streamIOException- if signing or verification fails during processing or finalization
-
tagLength
-
setExpectedTag
public void setExpectedTag(byte[] expected) Sets the expected verification tag to be checked when the wrapped stream finishes.Applicable only in VERIFY mode. Passing
nullclears the tag. The array is defensively copied. If a wrapped stream is already active, its expected tag is updated as well.- Specified by:
setExpectedTagin interfaceTagEngine<Signature>- Parameters:
expected- expected signature bytes ornullto clear- Throws:
UnsupportedOperationException- if called in SIGN mode
-
setVerificationApproach
public void setVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate<Signature> strategy) Sets the verification approach used in VERIFY mode to compare the expected and computed signatures.- Specified by:
setVerificationApproachin interfaceTagEngine<Signature>- Parameters:
strategy- verification predicate; may be decorated to throw or to flag into a context;nullkeeps the default
-
getVerificationCore
Returns the core verification predicate for signature comparison.- Specified by:
getVerificationCorein interfaceTagEngine<Signature>- Returns:
- predicate that delegates to
Signature.verify(byte[])
-