Summary:
See also: The Genero Web Services XML Library
The CryptoKey class provides methods to manipulate HMAC, symmetric and asymmetric keys needed for signing, verifying, encrypting and decrypting XML documents or document fragments. It follows the XML-Signature and XML-Encryption specifications.
The status is set to zero after a successful method call.
Syntax
xml.CryptoKey
Class Methods | |
Name | Description |
xml.CryptoKey.Create(
|
Constructor of an
empty CryptoKey object depending on a url,
where url
defines a key identifier according to the XML-Signature and
XML-Encryption specification.
Returns a CryptoKey object or NULL. Throws an exception in case of errors, and updates status with an error code. |
xml.CryptoKey.CreateFromNode(
|
Constructor of a
new CryptoKey object depending on a url
and from a XML node,
according to the XML-Signature and XML-Encryption specification;
where url
defines a key identifier restricted to PUBLIC/PRIVATE keys,
and node is an ELEMENT node whose local name is either:
If the local name is RSAKeyValue or RSAKeyPair, the URL must be a RSA key. If the local name is DSAKeyValue, the URL must be a DSA key. Throws an exception in case of errors, and updates status with an error code. |
xml.CryptoKey.CreateDerivedKey(
|
Constructor of an
empty CryptoKey object intended to be derived before use,
and depending on a url;
where url
defines a key identifier according to the XML-Signature and
XML-Encryption specification.
Returns a CryptoKey object or NULL. Only symmetric and HMAC keys can be derived. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
getUrl()
|
Returns the key identifier as an URL as defined in the
XML-Signature and XML-Encryption specification. Throws an exception in case of errors, and updates status with an error code. |
getType() |
Returns the type
of key.
Throws an exception in case of errors, and updates status with an error code. |
getUsage() |
Returns the usage
of the key.
Throws an exception in case of errors, and updates status with an error code. |
getSize() |
Returns the size of the key in bits.
Throws an exception in case of errors, and updates status with an error code. |
compareTo( |
Compares this CryptoKey object to
the given one, and returns true if they are identical. It verifies if the keys URL, type, size, usage and value are the same. In other words, two identical keys will produce the same encryption cipher. Note: The key features are not taken into account during comparison. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
setKey( |
Defines the value of a HMAC or
Symmetric key. The value can be a password and must be of the size corresponding to the key identifier. Throws an exception in case of errors, and updates status with an error code. |
generateKey( |
Generates a random key of given size (in bits). For symmetric keys, the size is fixed by the key identifier, and you can't change it. The only authorized values are the real key size or NULL. Throws an exception in case of errors, and updates status with an error code. |
deriveKey( |
Derives the symmetric or HMAC CryptoKey object using given method identifier and concatenating the optional label, the mandatory seed value and the optional created date as initial random value;
where size defines the number of bytes of the resulting derived key, offset the number of bytes the resulting octet stream must be shifted to obtain the derived key
and seed a valid Base64 string representing a random binary data you can obtain with the
CreateRandomString() helper method. If it is a symmetric key, the size can be 0, or must match the original key according to key identifier. See derived keys for more details. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
loadPEM( |
Loads an asymmetric DSA or RSA key
from a file in PEM format, where file is the
filename or an entry
in the FGLPROFILE file.
If the DSA or RSA private key is protected with a password, the recommended way is to unprotect it with the openssl tool and to put the key file on a restricted file system. However, you can use a script or the fglpass agent to provide the password to the application. Throws an exception in case of errors, and updates status with an error code. |
loadDER( |
Loads an asymmetric DSA or RSA key
from a file in DER format, where file is the
filename or an entry
in the FGLPROFILE file.
If the DSA or RSA private key is protected with a password, the recommended way is to unprotect it with the openssl tool and to put the key file on a restricted file system. However, you can use a script or the fglpass agent to provide the password to the application. Throws an exception in case of errors, and updates status with an error code. |
loadBIN(
|
Loads a symmetric or HMAC key from a
file in raw format, where file is the filename or
an entry
in the FGLPROFILE file.
Raw format means that the data in the file are read without any transformation, and will be stored as it in the key. For instance, if you file contains hello, it has the same effect as calling setKey() with hello as parameter. Throws an exception in case of errors, and updates status with an error code. |
savePublic() |
Saves the public part of an
asymmetric RSA or DSA CryptoKey object into a XML document according to
the XML-Signature specification for DSA
and RSA
key value.
See also the RetrievalMethod feature. Throws an exception in case of errors, and updates status with an error code. |
savePrivate() |
Saves the private part of an
asymmetric RSA CryptoKey object into a XML document according to the XKMS
2.0 specification.
Throws an exception in case of errors, and updates status with an error code. |
saveToString()
|
Saves the CryptoKey object into a
BASE64 string format.
Throws an exception in case of errors, and updates status with an error code. |
loadPublic( |
Loads the public asymmetric RSA or
DSA key in the given XML document into the public part of this
CryptoKey object, according to the XML-Signature specification for DSA
and RSA
key value.
Throws an exception in case of errors, and updates status with an error code. |
loadPrivate( |
Loads the private asymmetric RSA key
in the given XML document into the private part of this CryptoKey
object, according to the XKMS
2.0 specification.
Throws an exception in case of errors, and updates status with an error code. |
loadFromString( |
Loads the given key in BASE64 string
format into this CryptoKey object.
Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
setFeature(
|
Sets or resets the given feature for this CryptoKey
object.
Throws an exception in case of errors, and updates status with an error code. |
getFeature( |
Returns the value of the given feature for this CryptoKey
object, or NULL if the feature is not set.
Throws an exception in case of errors, and updates status with an error code. |
Identifier | Description | Usage | Type |
http://www.w3.org/2000/09/xmldsig#dsa-sha1 | Asymmetric DSA key with SHA1 for signature purposes. Uses a private DSA key for signature and needs an associated public DSA key or X509 certificate containing it, to verify it. See specification for details. |
SIGNATURE | PUBLIC or PRIVATE |
http://www.w3.org/2000/09/xmldsig#rsa-sha1 | Asymmetric RSA key with SHA1 for signature purposes. Uses a private RSA key for signature and needs an associated public RSA key or X509 certificate containing it, to verify it. See specification for details. |
SIGNATURE | PUBLIC or PRIVATE |
http://www.w3.org/2000/09/xmldsig#hmac-sha1 | Message Authentication Code key with SHA1 for signature
purposes. Uses a same password for signature and to verify it, and key size is free. See specification for details. |
SIGNATURE | HMAC |
http://www.w3.org/2001/04/xmlenc#aes128-cbc | Symmectric AES128 key for encryption purposes. Uses a common key of 128bits for encrypting and decrypting XML documents. See specification for details. |
ENCRYPTION | SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#aes192-cbc | Symmetric AES192 key for encryption purposes. Uses a common key of 192bits for encrypting and decrypting XML documents. See specification for details. |
ENCRYPTION | SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#aes256-cbc | Symmetric AES256 key for encryption purposes. Uses a common key of 256bits for encrypting and decrypting XML documents. See specification for details. |
ENCRYPTION | SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#tripledes-cbc | Symmetric TripleDes key for encryption purposes. Uses a common key of 192bits for encrypting and decrypting XML documents. See specification for details. |
ENCRYPTION | SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#kw-aes128 | Symmetric AES128 key wrap for key encryption purposes. Uses a common key of 128bits for encrypting and decrypting a symmetric key. See specification for details. |
KEY ENCRYPTION |
SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#kw-aes192 | Symmetric AES192 key wrap for key encryption purposes. Uses a common key of 192bits for encrypting and decrypting a symmetric key. See specification for details. |
KEY ENCRYPTION |
SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#kw-aes256 | Symmetric AES256 key wrap for key encryption purposes. Uses a common key of 256bits for encrypting and decrypting a symmetric key. See specification for details. |
KEY ENCRYPTION |
SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#kw-tripledes | Symmetric TripleDes key wrap for key encryption
purposes. Uses a common key of 192bits for encrypting and decrypting a symmetric key. See specification for details. |
KEY ENCRYPTION |
SYMMETRIC |
http://www.w3.org/2001/04/xmlenc#rsa-1_5 | Asymmetric RSA key for key encryption purposes. Uses a public RSA key or a X509 certificate containing it to encrypt a symmetric key, and needs the associated private RSA key to decrypt it. See specification for details. |
KEY ENCRYPTION |
PUBLIC or PRIVATE |
http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p | Asymmetric RSA key for key encryption purposes. Uses a public RSA key or a X509 certificate containing it to encrypt a symmetric key, and needs the associated private RSA key to decrypt it. See specification for details. |
KEY ENCRYPTION | PUBLIC or PRIVATE |
Key derivation is used on symmetric or HMAC keys to avoid the direct usage of a shared secret password in secured operations. If two parties share a secret password that is successfully hacked by a third party, any future operation becomes unsecure, and the initial two parties do not even realize that their exchanges are unsafe. However, if a different password based on that shared secret password is used for each new secured operation, even if one operation is compromised, it will only unsecure that operation, but not other operations.
The derivation consists of applying an algorithm with some additional inputs (such as a random seed value) to a password in order to obtain another password that is then used in one secured operation. Of course, the algorithm and its additional inputs must also be shared to enable the computation of the same derived key by someone that is intended to decrypt the message.
Notice also that passwords are often only composed of alpha-numeric characters that eases a bit more the job of a hacker, whereas a derived key is composed of any binary data produced by the algorithm used for the derivation.
Method | Description |
http://schemas.xmlsoap.org/ws/2005/02/sc/dk/p_sha1 | Only algorithm supported. See specification for details. |
Feature | Description |
KeyName (See specification for details) |
Defines or
returns whether a user-defined key name is added during a XML signature
or encryption in order to identify it to other
applications, or by the key
store. The default value is NULL, meaning that no key name is used. |
KeyValue (See specification for details) |
Defines or
returns whether the public part of the asymmetric key is added during a
XML signature or encryption. Only for RSA and DSA keys. The default value is FALSE, meaning that no key value is used. |
RetrievalMethod (See specification for details) |
Defines or
returns the URL where the XML form of
The XML form of a DSA or RSA public key can be obtain by the savePublic() method. The XML form of a symmetric key can be
obtain by the encryptKey()
method |
|
01
IMPORT xml02
03
MAIN04
DEFINE key xml.CryptoKey05
LET key = xml.CryptoKey.Create("http://www.w3.org/2001/04/xmlenc#aes256-cbc")06
TRY07
CALL key.generateKey(NULL)08
DISPLAY "Key size (in bits) : ",key.getSize() # displays 256 (bits)09
DISPLAY "Key type : ",key.getType() # displays SYMMETRIC10
DISPLAY "Key usage : ",key.getUsage() # displays ENCRYPTION11
CATCH12
DISPLAY "Unable to generate key :",STATUS13
END TRY14
END MAIN
01
IMPORT xml02
03
MAIN04
DEFINE key xml.CryptoKey05
LET key = xml.CryptoKey.Create("http://www.w3.org/2000/09/xmldsig#hmac-sha1")06
TRY07
CALL key.setKey("secretpassword")08
DISPLAY "Key size (in bits) : ",key.getSize() # displays 112 (size of secretpassword in bits)09
DISPLAY "Key type : ",key.getType() # displays HMAC10
DISPLAY "Key usage : ",key.getUsage() # displays SIGNATURE11
CATCH12
DISPLAY "Unable to set key :",STATUS13
END TRY14
END MAIN
01
IMPORT xml02
IMPORT com03
04
MAIN05
DEFINE key xml.CryptoKey06
DEFINE seedBase64 STRING # will contain a random binary data encoded in Base6407
LET key = xml.CryptoKey.CreateDerivedKey("http://www.w3.org/2000/09/xmldsig#hmac-sha1")08
TRY09
CALL key.setKey("secretpassword")10
# Creates a random 24 bytes long binary data encoded into a Base64 form string11
LET seedBase64 = com.Util.CreateRandomString(24)12
# Derives the 14 bytes long "secretpassword" into a 64 bytes long key13
# from a random 24 bytes long seed value and shifting the resulting key from 255 bytes14
CALL key.deriveKey("http://schemas.xmlsoap.org/ws/2005/02/sc/dk/p_sha1",NULL,seedBase64,NULL,255,64)15
DISPLAY "Key size (in bits) : ",key.getSize() # displays 512 (size of 'secretpassword' derivation in bits)16
# Note: from now key is derived and can be used in any encryption or signature function17
CATCH18
DISPLAY "Unable to derive key :",STATUS19
END TRY20
END MAIN
Note: All keys in PEM or DER format were created with the OpenSSL tool.