See also: The Genero Web Services XML Library
The Encryption class provides methods to encrypt and decrypt XML documents, nodes or symmetric keys. It follows the XML-Encryption specifications.
The status is set to zero after a successful method call.
Syntax
xml.Encryption
Class Methods | |
Name | Description |
xml.Encryption.Create() |
Constructor of an
Encryption object. Returns a Encryption object or NULL. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
setKey( |
Assigns a copy of the symmetric key
to this Encrytion object. Any further XML document or node encryption
or decryption will use that symmetric key. When decrypting a XML document that has an embedded symmetric key, the embedded key will be used instead. Throws an exception in case of errors, and updates status with an error code. |
getEmbeddedKey() |
Returns a copy of the embedded
symmetric key that was used in the last decryption operation, or NULL
if there is none. An embedded symmetric key is always encrypted, and needs therefore a key-encryption key to be set in order to decrypt it. Throws an exception in case of errors, and updates status with an error code. |
setKeyEncryptionKey( |
Assigns a copy of the key-encryption
key to this Encrytion object. Any further XML encryption will use that key-encryption key to encrypt the symmetric key set with setKey() within the resulting XML, and any further XML decryption will use that key-encryption key to decrypt the embedded symmetric key.
Throws an exception in case of errors, and updates status with an error code. |
setCertificate( |
Assigns a copy of the X509
certificate to this Encryption object. The certificate will then be
added to any further XML document or node encryption.
Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
encryptElement( |
Encrypts the ELEMENT DomNode node
and all its children using the symmetric key.
The ELEMENT DomNode node and all its children are replaced at the same place in the XML document with the resulting EncryptedData node. Throws an exception in case of errors, and updates status with an error code. |
decryptElement( |
Decrypts the EncryptedData
DomNode enc using the symmetric key.
The encrypted DomNode enc is replaced at the same place in the XML document with the resulting ELEMENT DomNode and its children. Throws an exception in case of errors, and updates status with an error code. |
encryptElementContent( |
Encrypts all child nodes of the
ELEMENT DomNode node using the symmetric key.
The child nodes of the ELEMENT DomNode node are replaced at the same place in the XML document with the resulting EncryptedData node. Throws an exception in case of errors, and updates status with an error code. |
decryptElementContent( |
Decrypts the EncryptedData
DomNode enc using the symmetric key.
The encrypted DomNode enc is replaced at the same place in the XML document with the resulting child nodes. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
encryptElementDetached( |
Encrypts the ELEMENT DomNode node
and all its children using the symmetric key, and returns them as one
new EncryptedData
node.
The resulting DomNode isn't added at any place in the XML document. It's up to the user to insert it at the right place, and to remove the nodes in clear form. Throws an exception in case of errors, and updates status with an error code. |
decryptElementDetached( |
Decrypts the EncryptedData
DomNode enc using the symmetric key, and returns it
in a new ELEMENT node.
The resulting DomNode and its children aren't added at any place in the XML document. It's up to the user to insert it at the right place, and to remove the encrypted node. Throws an exception in case of errors, and updates status with an error code. |
encryptElementContentDetached( |
Encrypts all child nodes of the
ELEMENT DomNode node using the symmetric key, and
returns them as one new EncryptedData
node.
The resulting DomNode isn't added at any place in the XML document. It's up to the user to insert it at the right place, and to remove the nodes in clear form. Throws an exception in case of errors, and updates status with an error code. |
decryptElementContentDetached( |
Decrypts the EncryptedData
DomNode enc using the symmetric key, and returns
all its children in one new DOCUMENT_FRAGMENT_NODE node. The resulting child nodes aren't added at any place in the XML document. It's up to the user to insert it at the right place, and to remove the encrypted node. Throws an exception in case of errors, and updates status with an error code. |
Object Methods | |
Name | Description |
encryptKey( |
Encrypts the given symmetric or hmac key as
an EncryptedKey
node and returns it as root node of a new XML document. The
key-encryption key must been set otherwise it will fail. Depending on the feature set on the key-encryption key, the returned XML document will contain an additional KeyInfo node. Throws an exception in case of errors, and updates status with an error code. |
decryptKey( |
Decrypts the EncryptedKey
as root in the given XML document, and returns a new CryptoKey of given kind. Only symmetric or hmac keys are allowed.
Throws an exception in case of errors, and updates status with an error code. |
The following methods don't belong to the XML encryption specification, but are helper functions to allow 4GL application to encrypt and decrypt short passwords with RSA keys, or big strings by using symmetric keys. Notice that a common way to encrypt data is to use symmetric keys, and to use RSA keys to encrypt the symmetric key value.
Class Methods | |
Name | Description |
xml.Encryption.EncryptString( |
Encrypts the string str,
using the symmetric key key, and returns the
encrypted string encoded in BASE64.
The key must be of usage: encryption. Throws an exception in case of errors, and updates status with an error code. |
xml.Encryption.DecryptString( |
Decrypts the encrypted string str
encoded in BASE64, using the symmetric key key, and
returns the string in clear text.
The key must be of usage: encryption. Throws an exception in case of errors, and updates status with an error code. |
xml.Encryption.RSAEncrypt( |
Encrypts the string str
using the RSA key key and returns it encoded in
BASE64;
where key is the filename of a RSA public or
private key in PEM format or an entry in the
FGLPROFILE file, and where str is the string to be
encrypted.
RSA encryption is only intended to short strings that cannot exceed the size of the RSA key minus 12 bytes. For instance, if you have a RSA key of 512 bits, you password cannot exceed 512/8-12 = 52 bytes. If you need to handle big strings, you must use symmetric keys and the method EncryptString(). However, you can use RSA keys to encrypt symmetric key values. Throws an exception in case of errors, and updates status with an error code. |
xml.Encryption.RSADecrypt( |
Decrypts the BASE64 encrypted string
enc using the RSA key key
and returns it in clear text;
where key is the filename of a RSA private key in
PEM format or an entry
in the FGLPROFILE file, and where enc is a string
that was encrypted with the fglpass
tool or with the above method.
RSA decryption is only intended to short strings that cannot exceed the size of the RSA key minus 12 bytes. For instance, if you have a RSA key of 512 bits, you password cannot exceed 512/8-12 = 52 bytes. If you need to handle big strings, you must use symmetric keys and the method DecryptString(). However, you can use RSA keys to decrypt symmetric key values. Warning! YOU MUST PAY ATTENTION TO RESTRICT ACCESS TO THAT RSA PRIVATE KEY FILE ONLY TO THE PERSON OR GROUP OF PERSON AUTHORIZED. If the 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. But you can also use a script or the fglpass agent to provide the password to the application. For example, you can encrypt a database password with the fglpass tool and store it in the FGLPROFILE file, then you can decrypt it with the base.Application.getResourceEntry() and the xml.Encryption.RSADecrypt() method to connect to the database. Throws an exception in case of errors, and updates status with an error code. |
01
IMPORT xml02
03
MAIN04
DEFINE doc xml.DomDocument05
DEFINE root xml.DomNode06
DEFINE enc xml.Encryption07
DEFINE symkey xml.CryptoKey08
LET doc = xml.DomDocument.Create()09
# Notice that white spaces are significant in crytography,10
# therefore it is recommended that you remove unnecessary ones11
CALL doc.setFeature("whitespace-in-element-content",FALSE)12
TRY13
# Load XML file to be encrypted14
CALL doc.load("XMLFileToBeEncrypted.xml")15
LET root = doc.getDocumentElement()16
# Create symmetric AES128 key for XML encryption purposes17
LET symkey = xml.CryptoKey.Create("http://www.w3.org/2001/04/xmlenc#aes128-cbc")18
CALL symkey.setKey(">secretpassword<") # password of 128 bits19
CALL symKey.setFeature("KeyName","MySecretKey") # Name the password in order to identify the key (Not mandatory)20
# Encrypt the entire document21
LET enc = xml.Encryption.Create()22
CALL enc.setKey(symkey) # Set the symmetric key to be used23
CALL enc.encryptElement(root) # Encrypt24
# Save encrypted document back to disk25
CALL doc.setFeature("format-pretty-print",TRUE)26
CALL doc.save("EncryptedXMLFile.xml")27
CATCH28
DISPLAY "Unable to encrypt XML file :",STATUS29
END TRY30
END MAIN
Note: All keys or certificates in PEM or DER format were created with the OpenSSL tool.
01
IMPORT xml02
03
MAIN04
DEFINE doc xml.DomDocument05
DEFINE node xml.DomNode06
DEFINE enc xml.Encryption07
DEFINE symkey xml.CryptoKey08
DEFINE list xml.DomNodeList09
DEFINE str String10
LET doc = xml.DomDocument.Create()11
# Notice that whitespaces are significants in crytography,12
# therefore it is recommended to remove unnecessary ones13
CALL doc.setFeature("whitespace-in-element-content",FALSE)14
TRY15
# Load encrypted XML file16
CALL doc.load("EncryptedXMLFile.xml")17
# Retrieve encrypted node (if any) from the document18
LET list = doc.getElementsByTagNameNS("EncryptedData","http://www.w3.org/2001/04/xmlenc#")19
IF list.getCount()==1 THEN20
LET node = list.getItem(1)21
ELSE22
DISPLAY "No encrypted node found"23
EXIT PROGRAM24
END IF25
# Check if symmetric key name matches the expected "MySecretKey" (Not mandatory)26
LET list = node.selectByXPath("dsig:KeyInfo/dsig:KeyName[position()=1 and text()=\"MySecretKey\"]","dsig","http://www.w3.org/2000/09/xmldsig#")27
IF list.getCount()!=1 THEN28
DISPLAY "Key name doesn't match"29
EXIT PROGRAM30
END IF31
# Create symmetric AES128 key for XML decryption purpose32
LET symkey = xml.CryptoKey.Create("http://www.w3.org/2001/04/xmlenc#aes128-cbc")33
CALL symkey.setKey(">secretpassword<") # password of 128 bits34
# Decrypt the entire document35
LET enc = xml.Encryption.Create()36
CALL enc.setKey(symkey) # Set the symmetric key to be used37
CALL enc.decryptElement(node) # Decrypt38
# Save encrypted document back to disk39
CALL doc.setFeature("format-pretty-print",TRUE)40
CALL doc.save("DecryptedXMLFile.xml")41
CATCH42
DISPLAY "Unable to decrypt XML file :",STATUS43
END TRY44
END MAIN
Note: All keys or certificates in PEM or DER format were created with the OpenSSL tool.
01
IMPORT xml02
03
MAIN04
DEFINE doc xml.DomDocument05
DEFINE root xml.DomNode06
DEFINE enc xml.Encryption07
DEFINE symkey xml.CryptoKey08
DEFINE kek xml.CryptoKey09
DEFINE cert xml.CryptoX50910
LET doc = xml.DomDocument.Create()11
# Notice that whitespaces are significants in crytography,12
# therefore it is recommended to remove unnecessary ones13
CALL doc.setFeature("whitespace-in-element-content",FALSE)14
TRY15
# Load XML file to be encrypted16
CALL doc.load("XMLFileToBeEncrypted.xml")17
LET root = doc.getDocumentElement()18
# Load the X509 certificate and retrieve the public RSA key for key-encryption purpose19
LET cert = xml.CryptoX509.Create()20
CALL cert.loadPEM("RSA1024Certificate.crt")21
LET kek = cert.createPublicKey("http://www.w3.org/2001/04/xmlenc#rsa-1_5")22
# Generate symmetric key for XML encryption purpose23
LET symkey = xml.CryptoKey.Create("http://www.w3.org/2001/04/xmlenc#aes256-cbc")24
CALL symkey.generateKey(NULL)25
# Encrypt the entire document26
LET enc = xml.Encryption.Create()27
CALL enc.setKey(symkey) # Set the symmetric key to be used28
CALL enc.setKeyEncryptionKey(kek) # Set the key-encryption key to be used for protecting the symmetric key29
CALL enc.setCertificate(cert) # Set the certificate to be added (not mandatory)30
CALL enc.encryptElement(root) # Encrypt31
# Save encrypted document back to disk32
CALL doc.setFeature("format-pretty-print",TRUE)33
CALL doc.save("EncryptedXMLFile.xml")34
CATCH35
DISPLAY "Unable to encrypt XML file :",STATUS36
END TRY37
END MAIN
Note: All keys or certificates in PEM or DER format were created with the OpenSSL tool.
01
IMPORT xml02
03
MAIN04
DEFINE doc xml.DomDocument05
DEFINE node xml.DomNode06
DEFINE enc xml.Encryption07
DEFINE symkey xml.CryptoKey08
DEFINE kek xml.CryptoKey09
DEFINE list xml.DomNodeList10
LET doc = xml.DomDocument.Create()11
# Notice that whitespaces are significants in crytography,12
# therefore it is recommended to remove unnecessary ones13
CALL doc.setFeature("whitespace-in-element-content",FALSE)14
TRY15
# Load encrypted XML file16
CALL doc.load("EncryptedXMLFile.xml")17
# Retrieve encrypted node (if any) from the document18
LET list = doc.getElementsByTagNameNS("EncryptedData","http://www.w3.org/2001/04/xmlenc#")19
IF list.getCount()==1 THEN20
LET node = list.getItem(1)21
ELSE22
DISPLAY "No encrypted node found"23
EXIT PROGRAM24
END IF25
# Load the private RSA key26
LET kek = xml.CryptoKey.create("http://www.w3.org/2001/04/xmlenc#rsa-1_5")27
CALL kek.loadPEM("RSA1024Key.pem")28
# Decrypt the entire document29
LET enc = xml.Encryption.Create()30
CALL enc.setKeyEncryptionKey(kek) # Set the key-encryption key to decrypted the protected symmetric key31
CALL enc.decryptElement(node) # Decrypt32
# Retrieve the embedded symmetric key for futher usage and display info about it33
LET symkey = enc.getEmbeddedKey()34
DISPLAY "Key size (in bytes) : ",symkey.getSize() # displays 102435
DISPLAY "Key type : ",symkey.getType() # displays SYMMETRIC36
DISPLAY "Key usage : ",symkey.getUsage() # displays ENCRYPTION37
# Encrypted document back to disk38
CALL doc.setFeature("format-pretty-print",TRUE)39
CALL doc.save("DecryptedXMLFile.xml")40
CATCH41
DISPLAY "Unable to decrypt XML file :",STATUS42
END TRY43
END MAIN
Note: All keys or certificates in PEM or DER format were created with the OpenSSL tool.