Class GenericJcaMessageAgreementContext
- All Implemented Interfaces:
Closeable, AutoCloseable, AgreementContext, CryptoContext, MessageAgreementContext
This context provides a MessageAgreementContext view over a classical
JCA Diffie–Hellman style agreement (e.g., ECDH, XDH). The protocol
"to-be-sent" data for such agreements is the party's public key. This class
therefore maps:
getPeerMessage()to the local public key encoding (X.509 SubjectPublicKeyInfo),setPeerMessage(byte[])to importing the peer public key and binding it as the peer key.
Encoding
The message format is the standard X.509 SubjectPublicKeyInfo encoding
returned by Key.getEncoded(). This format is
stable, widely interoperable and avoids ad-hoc or algorithm-specific wire
formats.
Algorithm and provider selection
The underlying key agreement algorithm name (for
KeyAgreement) and the key factory algorithm name (for
KeyFactory) are supplied explicitly by the algorithm registration
code. This keeps algorithm-specific knowledge in *Algorithm classes
rather than embedding naming heuristics into shared code.
Lifecycle
- Create the context with the local key pair wrapper.
- Send
getPeerMessage()to the remote party. - Receive the remote party's message and call
setPeerMessage(byte[]). - Derive the raw shared secret with
deriveSecret().
Security considerations
- The message returned by
getPeerMessage()contains only public key material. - The output of
deriveSecret()is a raw shared secret and must be processed with a KDF by higher protocol layers before use as symmetric keying material. - This class does not log, persist, or otherwise expose private key material.
Thread safety
Instances are not thread-safe and are intended for single-use, single-threaded protocol executions.
- Since:
- 1.0
-
Constructor Summary
ConstructorsConstructorDescriptionGenericJcaMessageAgreementContext(CryptoAlgorithm alg, KeyPairKey keyPairKey, String jcaAgreementName, String agreementProvider, String keyFactoryAlg, String keyFactoryProvider) Creates a message-oriented agreement context over a JCA key agreement. -
Method Summary
Modifier and TypeMethodDescriptionReturns the algorithm that created this context.voidclose()Completes the context's implementation-specific cleanup.byte[]Performs the key agreement and returns the derived raw shared secret.byte[]Returns the local party's handshake message.key()Returns the key bound to this context.voidsetPeerMessage(byte[] message) Supplies the peer party's handshake message.voidsetPeerPublic(PublicKey peer) Sets the peer's public key to be used in the key agreement.
-
Constructor Details
-
GenericJcaMessageAgreementContext
public GenericJcaMessageAgreementContext(CryptoAlgorithm alg, KeyPairKey keyPairKey, String jcaAgreementName, String agreementProvider, String keyFactoryAlg, String keyFactoryProvider) Creates a message-oriented agreement context over a JCA key agreement.The wrapped
KeyPairKeysupplies both local private and public key components: the private key is used for the underlying agreement computation, while the public key is exported as the handshake message viagetPeerMessage().- Parameters:
alg- algorithm descriptor that owns this contextkeyPairKey- local key pair wrapper; must contain both a private and a public keyjcaAgreementName- JCAKeyAgreementalgorithm name (e.g.,"ECDH","X25519")agreementProvider- optional JCA provider name forKeyAgreement, ornullfor default provider selectionkeyFactoryAlg- JCAKeyFactoryalgorithm name used to import peer public keys (e.g.,"EC","XDH")keyFactoryProvider- optional JCA provider name forKeyFactory, ornullfor default provider selection- Throws:
NullPointerException- if any required argument isnull- Since:
- 1.0
-
-
Method Details
-
getPeerMessage
public byte[] getPeerMessage()Returns the local party's handshake message.For DH/XDH-style agreements, the handshake message is the local public key encoding. The returned array is a defensive copy and may be safely transmitted over untrusted channels.
The encoding is the standard X.509 SubjectPublicKeyInfo bytes returned by
Key.getEncoded().- Specified by:
getPeerMessagein interfaceMessageAgreementContext- Returns:
- a defensive copy of the local public key encoding (never
null) - Throws:
IllegalStateException- if the local public key does not provide an encoding- Since:
- 1.0
-
setPeerMessage
public void setPeerMessage(byte[] message) Supplies the peer party's handshake message.The provided message is interpreted as an X.509 SubjectPublicKeyInfo encoding of the peer public key. The key is imported using
KeyFactoryand then assigned as the peer key for the underlying agreement computation.Passing
nullresets the peer binding and makesderiveSecret()unusable until a new peer message is provided.- Specified by:
setPeerMessagein interfaceMessageAgreementContext- Parameters:
message- peer public key encoding (SPKI), ornullto reset the peer state- Throws:
IllegalArgumentException- if the message cannot be imported as a public key using the configuredKeyFactoryalgorithm/provider- Since:
- 1.0
-
setPeerPublic
Sets the peer's public key to be used in the key agreement.This method must be called before
AgreementContext.deriveSecret(). Implementations may validate that the provided key is suitable for the underlying algorithm (e.g., curve membership checks).- Specified by:
setPeerPublicin interfaceAgreementContext- Parameters:
peer- the peer's public key
-
deriveSecret
public byte[] deriveSecret()Performs the key agreement and returns the derived raw shared secret.The returned byte array contains secret material and should be used immediately in a KDF. Callers are responsible for clearing or zeroizing the array after use to reduce the risk of leakage.
- Specified by:
deriveSecretin interfaceAgreementContext- Returns:
- a new byte array containing the raw shared secret
-
algorithm
Returns the algorithm that created this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- the algorithm definition that owns this context
-
key
Returns the key bound to this context.For unkeyed contexts such as digests, this may be
NullKey.INSTANCE.- Specified by:
keyin interfaceCryptoContext- Returns:
- the cryptographic key or sentinel bound to this context
-
close
public void close()Completes the context's implementation-specific cleanup.Implementations release resources they explicitly own; cleanup can be a no-op for contexts holding only borrowed or garbage-collected state. The bound key remains caller-owned: closing a context never destroys the key. Callers that own a destroyable key must destroy it explicitly after all contexts and other consumers have released it. Streams returned by the context have independent lifecycles: callers must complete and close those streams before closing the context. This method does not promise to finalize stream output or verify authentication tags.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-