Class AnB_Crypto_Wrapper
- Direct Known Subclasses:
AnB_Session
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Crypto_EncryptionEnginethe cryptographic engineprotected final Crypto_KeyStoreTypethe crypto store typeprotected final AnBx_Agentthe identity of the agent -
Constructor Summary
ConstructorsConstructorDescriptionCreate a AnB_Crypto_Wrapper for a specified cryptographic engineAnB_Crypto_Wrapper(Crypto_EncryptionEngine ee, String myAlias) Create a AnB_Crypto_Wrapper for a specified cryptographic engineCreate a AnB_Crypto_Wrapper for a specified key store setting mapAnB_Crypto_Wrapper(Crypto_KeyStoreSettings_Map kssd, Crypto_Config config) Create a AnB_Crypto_Wrapper for a specified key store setting map and cryptographic configurationAnB_Crypto_Wrapper(String myAlias, Crypto_Config config) Create a AnB_Crypto_Wrapper for a specified key store setting map and cryptographic configuration -
Method Summary
Modifier and TypeMethodDescriptionbooleanaliasExists(String alias) Check is an agent's alias is stored in the key stores accessible to the cryptographic enginebooleancheckHmac(Object object, Crypto_ByteArray hmac, SecretKey sk) Check a hmac value of a given object computed with the default hashing algorithm and a specified secret keydecrypt(Crypto_SealedObject sealedObj) Decrypts aCrypto_SealedObject.decrypt(Crypto_SealedObject sealedObj, Crypto_KeyPair kp) Decrypts aCrypto_SealedObjectusing the private key from the specified key pair.decrypt(Crypto_SealedObject sealedObj, Crypto_KeyStoreType pk) Decrypts aCrypto_SealedObjectusing a specific key store type.decrypt(Crypto_SealedObject sealedObj, SecretKey key) Symmetric decryption that works identically for all serialisers.Decrypts a Crypto_SealedPair with the private key from the default key store associated to current agentdecrypt(Crypto_SealedPair sc, Crypto_KeyPair kp) Decrypts a Crypto_SealedPair with the private key from a specified key pairDecrypts a Crypto_SealedPair with the private key from a specified key store associated to current agentdecrypt(SealedObject so, SecretKey symmetricKey) Decrypts a SealedObject with a specified symmetric keyencrypt(Object object, Crypto_KeyPair kp) Encrypts an object using the public key extracted from the given key pair.Encrypts an object for the recipient identified by the given alias.encrypt(Object object, String alias, Crypto_KeyStoreType pk) Encrypts an object with a public key retrieved from a specified key store associated to an user identified by an aliasEncrypts an object using the given public key.Symmetric encryption that works identically for all serialisers.byte[]encryptSymmetric(byte[] plaintext, SecretKey key) Encrypts the given plaintext using the provided symmetric key.static Crypto_SealedObjectReconstructs aCrypto_SealedObjectfrom a ProtobufAnBx_WireFormats.SealedMessage.static Crypto_SignedObjectReconstructs aCrypto_SignedObjectfrom a ProtobufAnBx_WireFormats.SignedMessage.Returns the cryptographic engineGenerates a HMAC secret keystatic voidgetInfo()Prints information about the cryptographic engine including supported algorithmsGenerates a key exchange key pair for the default key agreement algorithmgetKeyEx_PublicKey(KeyPair keyPair) Retrieves the public key from a specified key exchange key pairgetKeyEx_SecretKey(PublicKey publicKey, KeyPair keyPair) Retrieves the secret key from a specified key exchange key pair and associated public keyGenerates a key pair for the default public key algorithmRetrieve the default Crypto_KeyStoreSettings_Map for the current cryptographic engineprotected AnBx_AgentRetrieve the identity of the current agentgetName()Returns the agent name associated with this for the current AnB_Crypto_WrappergetNonce()Generates a fresh noncegetPublicKey(Crypto_KeyPair pair) Retrieves the public key from a specified key pairgetPublicKey(String alias, Crypto_KeyStoreType kst) Retrieve a public key associated to an agent's alias for a specified Crypto_KeyStoreTypeprotected CertificategetRemoteCertificate(String alias, Crypto_KeyStoreType kst) Retrieve the certificate associated to an agent's alias for a specified Crypto_KeyStoreTypeprotected Map<Crypto_KeyStoreType, Certificate> getRemoteCertificates(String alias) Retrieve the certificates associated to an agent's aliasGenerates a sequence numberGenerates a symmetric keyGenerates a symmetric key specifying a key modegetSymmetricKeyPBE(String password, String salt) Generates a symmetric key for Password Based Encryption (PBE)Generates a time stampbyte[]Computes an HMAC (keyed-hash message authentication code) over the given data using the provided secret key.static booleanChecks whether logging is currently enabled.makeDigest(Object object) Computes the hash of a given object with the default hashing algorithmComputes the hmac of a given object with the default hashing algorithm and a specified secret keystatic ObjectreadObject(String filename) Writes a serialised object from the file systemprotected ObjectReceive an object from a channelprotected AnBx_AgentReceive the identity of an agent from a channelprotected voidSend(Object obj, Channel_Abstraction c) Send an object over a channelprotected voidSend_Id(AnBx_Agent id, Channel_Abstraction c) Send the identity of an agent over a channelstatic voidsetLoggingExecTimeEnabled(boolean enabled) Sets the logging state for performance measurements.Signs an object using the default private key of the current agent.sign(Object object, Crypto_KeyPair kp) Signs an object using the private key contained in the specified key pair.sign(Object object, Crypto_KeyStoreType sk) Signs an object using a private key retrieved from the specified key store.Converts aCrypto_SealedObjectto its ProtobufAnBx_WireFormats.SealedMessagerepresentation.Converts aCrypto_SignedObjectto its ProtobufAnBx_WireFormats.SignedMessagerepresentation.byte[]unsealMessage(AnBx_WireFormats.SealedMessage sealedMsg) Unseals (decrypts) aAnBx_WireFormats.SealedMessageusing the local private key.verify(Crypto_SignedObject sig, String alias) Verifies aCrypto_SignedObjectusing the public key of the agent identified by the given alias.verify(Crypto_SignedObject sig, PublicKey publicKey) Verifies aCrypto_SignedObjectusing a specific public key.verify(SignedObject so, String alias) Verifies a SignedObject with a private key retrieved from the default key store associated to an agent identified by an aliasverify(SignedObject so, String alias, Crypto_KeyStoreType sk) Verifies a SignedObject with a private key retrieved from a specified key store associated to an agent identified by an aliasverify(SignedObject so, PublicKey pk) Verifies a SignedObject with a specified public keybyte[]verifyMessage(AnBx_WireFormats.SignedMessage sm, String alias) Verifies aAnBx_WireFormats.SignedMessageusing the public key of the given alias (retrieved from the keystore).byte[]verifyMessage(AnBx_WireFormats.SignedMessage sm, PublicKey publicKey) Verifies aAnBx_WireFormats.SignedMessageusing the provided public key.static voidwriteObject(Object object, String filename) Writes a serialised object to the file systemxor(Crypto_ByteArray x1, Crypto_ByteArray x2) Computes the xor of two Crypto_ByteArrays
-
Field Details
-
ee
the cryptographic engine -
me
the identity of the agent -
ident_ks
the crypto store type
-
-
Constructor Details
-
AnB_Crypto_Wrapper
Create a AnB_Crypto_Wrapper for a specified cryptographic engine- Parameters:
ee- the specified cryptographic enginemyAlias- Alias of the principal- See Also:
-
AnB_Crypto_Wrapper
Create a AnB_Crypto_Wrapper for a specified cryptographic engine- Parameters:
ee- the specified cryptographic engine- See Also:
-
AnB_Crypto_Wrapper
Create a AnB_Crypto_Wrapper for a specified key store setting map- Parameters:
kssd- the specified key store setting map- See Also:
-
AnB_Crypto_Wrapper
Create a AnB_Crypto_Wrapper for a specified key store setting map and cryptographic configuration- Parameters:
kssd- the specified key store setting mapconfig- the specified cryptographic configuration- See Also:
-
AnB_Crypto_Wrapper
Create a AnB_Crypto_Wrapper for a specified key store setting map and cryptographic configuration- Parameters:
myAlias- Alias of the principalconfig- the specified cryptographic configuration- See Also:
-
-
Method Details
-
setLoggingExecTimeEnabled
public static void setLoggingExecTimeEnabled(boolean enabled) Sets the logging state for performance measurements.- Parameters:
enabled-trueto enable logging,falseto disable logging.
-
isLoggingExecTimeEnabled
public static boolean isLoggingExecTimeEnabled()Checks whether logging is currently enabled.- Returns:
trueif logging is enabled,falseotherwise.
-
getInfo
public static void getInfo()Prints information about the cryptographic engine including supported algorithms- See Also:
-
getKeyStoreSettings_Map
Retrieve the default Crypto_KeyStoreSettings_Map for the current cryptographic engine- Returns:
- the Crypto_KeyStoreSettings_Map
- See Also:
-
getCryptoEngine
Returns the cryptographic engine- Returns:
- the Crypto_KeyStoreSettings_Map
- See Also:
-
getName
Returns the agent name associated with this for the current AnB_Crypto_Wrapper- Returns:
- the agent's name
-
aliasExists
Check is an agent's alias is stored in the key stores accessible to the cryptographic engine- Parameters:
alias- the agent's alias- Returns:
- true if the agent's alias exists
-
getPublicKey
Retrieve a public key associated to an agent's alias for a specified Crypto_KeyStoreType- Parameters:
alias- the agent's aliaskst- the crypto key store type- Returns:
- the public key
-
getRemoteCertificate
Retrieve the certificate associated to an agent's alias for a specified Crypto_KeyStoreType- Parameters:
alias- the agent's aliaskst- the crypto key store type- Returns:
- the certificate
-
getRemoteCertificates
Retrieve the certificates associated to an agent's alias- Parameters:
alias- the agent's alias- Returns:
- the certificates
-
getMyIdentity
Retrieve the identity of the current agent- Returns:
- the identity
-
Send
Send an object over a channel- Parameters:
obj- the objectc- the channel abstraction
-
Receive
Receive an object from a channel- Parameters:
c- the channel abstraction- Returns:
- the object
-
Send_Id
Send the identity of an agent over a channel- Parameters:
id- the agent idc- the channel abstraction
-
Receive_RemoteId
Receive the identity of an agent from a channel- Parameters:
c- the channel abstraction- Returns:
- the agent identity
-
encrypt
Encrypts an object with a public key retrieved from a specified key store associated to an user identified by an alias- Parameters:
object- the object to encryptalias- the identifier of the agentpk- the specified key store- Returns:
- the encrypted object as Crypto_SealedPair
- See Also:
-
decrypt
Decrypts a Crypto_SealedPair with the private key from the default key store associated to current agent- Parameters:
sc- the Crypto_SealedPair to decrypt- Returns:
- the decrypted object
- See Also:
-
decrypt
Decrypts a Crypto_SealedPair with the private key from a specified key store associated to current agent- Parameters:
sc- the Crypto_SealedPair to decryptpk- the specified key store- Returns:
- the decrypted object
- See Also:
-
decrypt
Decrypts a Crypto_SealedPair with the private key from a specified key pair- Parameters:
sc- the Crypto_SealedPair to decryptkp- the specified key pair- Returns:
- the decrypted object
- See Also:
-
decrypt
Decrypts a SealedObject with a specified symmetric key- Parameters:
so- the SealedObject to encryptsymmetricKey- the specified symmetric key- Returns:
- the encrypted object as SealedObject
-
verify
Verifies a SignedObject with a private key retrieved from the default key store associated to an agent identified by an alias- Parameters:
so- the SignedObject to verifyalias- the identifier of the agent- Returns:
- the verified object
- See Also:
-
verify
Verifies a SignedObject with a private key retrieved from a specified key store associated to an agent identified by an alias- Parameters:
so- the SignedObject to verifyalias- the identifier of the agentsk- the specified key store- Returns:
- the verified object
- See Also:
-
verify
Verifies a SignedObject with a specified public key- Parameters:
so- the SignedObject to verifypk- the specified public key- Returns:
- the verified object
-
getNonce
Generates a fresh nonce- Returns:
- the fresh nonce
-
getSeqNumber
Generates a sequence number- Returns:
- the sequence number
-
getSymmetricKey
Generates a symmetric key- Returns:
- the symmetric key
-
getSymmetricKey
Generates a symmetric key specifying a key mode- Parameters:
km- the key mode for key generation- Returns:
- the symmetric key
-
getHmacKey
Generates a HMAC secret key- Returns:
- the HMAC secret key
-
getTimeStamp
Generates a time stamp- Returns:
- the time stamp
-
getSymmetricKeyPBE
Generates a symmetric key for Password Based Encryption (PBE)- Parameters:
password- the passwordsalt- the salting argument- Returns:
- the symmetric key
-
getKeyEx_KeyPair
Generates a key exchange key pair for the default key agreement algorithm- Returns:
- the key exchange key pair
-
getKeyEx_PublicKey
Retrieves the public key from a specified key exchange key pair- Parameters:
keyPair- the specified key pair- Returns:
- the key pair
-
getKeyEx_SecretKey
Retrieves the secret key from a specified key exchange key pair and associated public key- Parameters:
publicKey- the associated public keykeyPair- the specified key pair- Returns:
- the (symmetric) secret key
-
getKeyPair
Generates a key pair for the default public key algorithm- Returns:
- the key pair
- See Also:
-
getPublicKey
Retrieves the public key from a specified key pair- Parameters:
pair- the specified key pair- Returns:
- the public key
- See Also:
-
makeDigest
Computes the hash of a given object with the default hashing algorithm- Parameters:
object- the given object- Returns:
- the hash value as Crypto_ByteArray
-
makeHmac
Computes the hmac of a given object with the default hashing algorithm and a specified secret key- Parameters:
object- the given objectsk- the secret key- Returns:
- the hmac value as Crypto_ByteArray
- See Also:
-
checkHmac
Check a hmac value of a given object computed with the default hashing algorithm and a specified secret key- Parameters:
object- the given objecthmac- the hmac valuesk- the secret key- Returns:
- the hmac value as Crypto_ByteArray
- See Also:
-
xor
Computes the xor of two Crypto_ByteArrays- Parameters:
x1- the first Crypto_ByteArray argumentx2- the second Crypto_ByteArray argument- Returns:
- the computed xor value
- See Also:
-
writeObject
Writes a serialised object to the file system- Parameters:
object- the given objectfilename- the specified destination file
-
readObject
Writes a serialised object from the file system- Parameters:
filename- the specified source file- Returns:
- the retrieved object
-
verifyMessage
Verifies aAnBx_WireFormats.SignedMessageusing the public key of the given alias (retrieved from the keystore). If verification succeeds, the original signed content bytes are returned.- Parameters:
sm- the signed message wrapper (must not benull)alias- the alias of the signer (must not benull)- Returns:
- the original content bytes (the
contentfield of the wrapper) - Throws:
SecurityException- if the signature does not verifyAnBx_LibraryException- if verification fails due to a cryptographic errorNullPointerException- if any argument isnull
-
verifyMessage
Verifies aAnBx_WireFormats.SignedMessageusing the provided public key. If verification succeeds, the original signed content bytes are returned.This overload is used when the public key was received during the protocol (e.g., a freshly generated public key sent by the peer).
- Parameters:
sm- the signed message wrapper (must not benull)publicKey- the public key to verify the signature (must not benull)- Returns:
- the original content bytes (the
contentfield of the wrapper) - Throws:
SecurityException- if the signature does not verifyAnBx_LibraryException- if verification fails due to a cryptographic errorNullPointerException- if any argument isnull
-
unsealMessage
Unseals (decrypts) aAnBx_WireFormats.SealedMessageusing the local private key.This method reverses the hybrid encryption process:
- Decrypts the symmetric key using the local private key.
- Decrypts the message symmetrically with the recovered key.
- Parameters:
sealedMsg- the sealed message wrapper (must not benull)- Returns:
- the decrypted plaintext bytes
- Throws:
AnBx_LibraryException- if decryption fails (e.g., corrupted ciphertext)NullPointerException- ifsealedMsgisnull
-
encryptSymmetric
Encrypts the given plaintext using the provided symmetric key.This method performs raw symmetric encryption (no key transport). The cipher algorithm and mode are determined from the key's algorithm via the configuration mapping
keyTypeToCipher. An appropriate initialisation vector (IV) is generated automatically for modes that require one (e.g., CBC, GCM).- Parameters:
plaintext- the raw bytes to encrypt (must not benull)key- the symmetric encryption key (must not benull)- Returns:
- the ciphertext bytes
- Throws:
AnBx_LibraryException- if encryption failsNullPointerException- if any argument isnull
-
hmac
Computes an HMAC (keyed-hash message authentication code) over the given data using the provided secret key.The HMAC algorithm is taken from the cryptographic configuration (
hMacAlgorithm). This method is intended for raw byte-based HMAC operations in typed Protobuf mode.- Parameters:
data- the data to authenticate (must not benull)key- the HMAC secret key (must not benull)- Returns:
- the HMAC value (raw bytes)
- Throws:
AnBx_LibraryException- if HMAC computation failsNullPointerException- if any argument isnull
-
toProtobufSigned
Converts aCrypto_SignedObjectto its ProtobufAnBx_WireFormats.SignedMessagerepresentation.This method simply delegates to the instance method
Crypto_SignedObject.toSignedMessage().- Parameters:
obj- the signed object to convert (must not benull)- Returns:
- the corresponding
SignedMessage
-
fromProtobufSigned
Reconstructs aCrypto_SignedObjectfrom a ProtobufAnBx_WireFormats.SignedMessage.The three fields (content, signature, algorithm) are taken directly from the message and stored inside the new container.
- Parameters:
msg- the Protobuf message (must not benull)- Returns:
- the reconstructed container
-
sign
Signs an object using the default private key of the current agent.The result is a serializer-neutral
Crypto_SignedObjectthat can be used identically in all serialisation modes.- Parameters:
object- the object to sign- Returns:
- a
Crypto_SignedObjectcontaining the signature
-
sign
Signs an object using a private key retrieved from the specified key store.- Parameters:
object- the object to signsk- the key store type that identifies the signing key- Returns:
- a serializer-neutral
Crypto_SignedObject
-
sign
Signs an object using the private key contained in the specified key pair.- Parameters:
object- the object to signkp- the key pair whose private key is used for signing- Returns:
- a serializer-neutral
Crypto_SignedObject
-
verify
Verifies aCrypto_SignedObjectusing the public key of the agent identified by the given alias.If the container holds a real
SignedObject(JavaSer mode), the legacy verification path is used. Otherwise the raw content and signature are verified viaverifyMessage(AnBx_WireFormats.SignedMessage, String)and the result is deserialised before being returned.- Parameters:
sig- the signed object to verifyalias- the alias of the signer (used to look up the public key)- Returns:
- the verified (and deserialised) original object
- Throws:
AnBx_LibraryException- if verification or deserialisation fails
-
verify
Verifies aCrypto_SignedObjectusing a specific public key.The method behaves like
verify(Crypto_SignedObject, String)but uses the suppliedPublicKeyinstead of looking one up by alias.- Parameters:
sig- the signed object to verifypublicKey- the public key to use for verification- Returns:
- the verified (and deserialised) original object
- Throws:
AnBx_LibraryException- if verification or deserialisation fails
-
toProtobufSealed
Converts aCrypto_SealedObjectto its ProtobufAnBx_WireFormats.SealedMessagerepresentation.This method simply delegates to the instance method
Crypto_SealedObject.toSealedMessage().- Parameters:
obj- the sealed object to convert (must not benull)- Returns:
- the corresponding
SealedMessage
-
fromProtobufSealed
Reconstructs aCrypto_SealedObjectfrom a ProtobufAnBx_WireFormats.SealedMessage.The three fields (encrypted key, encrypted message, cipher scheme) are extracted from the message and used to construct the unified container.
- Parameters:
msg- the Protobuf message (must not benull)- Returns:
- the reconstructed container
-
encrypt
Encrypts an object for the recipient identified by the given alias.The legacy
Crypto_SealedPairproduced by the engine is immediately wrapped in aCrypto_SealedObjectfor use in all serializers.- Parameters:
object- the object to encryptalias- the alias of the recipient (the public key is retrieved from the default key store)- Returns:
- a
Crypto_SealedObjectcontaining the encrypted data - See Also:
-
encrypt
Encrypts an object using the given public key.The legacy
Crypto_SealedPairproduced by the engine is immediately wrapped in aCrypto_SealedObject.- Parameters:
object- the object to encryptpublicKey- the recipient's public key- Returns:
- a
Crypto_SealedObjectcontaining the encrypted data
-
encrypt
Encrypts an object using the public key extracted from the given key pair.The legacy
Crypto_SealedPairproduced by the engine is immediately wrapped in aCrypto_SealedObject.- Parameters:
object- the object to encryptkp- the key pair whose public key is used for encryption- Returns:
- a
Crypto_SealedObjectcontaining the encrypted data
-
decrypt
Decrypts aCrypto_SealedObject.The method first tries to reconstruct a legacy
Crypto_SealedPairand decrypt it using the Java serialisation path. If that fails (e.g., because the sealed object was built from raw bytes in ProtoBuf mode), it falls back to the Protobuf path viaunsealMessage(AnBx_WireFormats.SealedMessage)and deserialises the result.- Parameters:
sealedObj- the sealed object to decrypt (must not benull)- Returns:
- the decrypted (and deserialised) object
- Throws:
AnBx_LibraryException- if decryption fails on both paths
-
encrypt
Symmetric encryption that works identically for all serialisers.The object is serialised, encrypted with the given key, and returned as a
Crypto_SealedObjectwhoseencryptedKeyisnull(symmetric mode).- Parameters:
object- the object to encryptkey- the symmetric key- Returns:
- the sealed container (cipher scheme is taken from the crypto config)
-
decrypt
Symmetric decryption that works identically for all serialisers.The method expects a
Crypto_SealedObjectwith anullencrypted key (symmetric mode). It decrypts the raw ciphertext and deserialises the result.- Parameters:
sealedObj- the sealed container (encryptedKey must benull)key- the symmetric key- Returns:
- the deserialised original object
- Throws:
AnBx_LibraryException- if the encryptedKey is notnullor decryption/deserialisation fails
-
decrypt
Decrypts aCrypto_SealedObjectusing the private key from the specified key pair.If the container holds a legacy
Crypto_SealedPair, that pair is used directly; otherwise the raw bytes are extracted and the Protobuf path is taken.- Parameters:
sealedObj- the sealed object to decrypt (must not benull)kp- the key pair whose private key is used for decryption- Returns:
- the decrypted (and deserialised) object
- Throws:
AnBx_LibraryException- if decryption fails
-
decrypt
Decrypts aCrypto_SealedObjectusing a specific key store type.If the container holds a legacy
Crypto_SealedPair, that pair is used directly with the provided key store; otherwise the raw bytes are extracted and the Protobuf path is taken (the key store is ignored for symmetric / pure byte-based objects).- Parameters:
sealedObj- the sealed object to decrypt (must not benull)pk- the key store type that identifies the private key to use- Returns:
- the decrypted (and deserialised) object
- Throws:
AnBx_LibraryException- if decryption fails
-