-
Create a stream that contains an XML document.
To do this, you typically use %XML.WriterOpens in a new tab to write output for an XML-enabled object to a stream.
-
Create at least one instance of %SYS.X509CredentialsOpens in a new tab that accesses the InterSystems IRIS credential set of the entity to whom you are going to give the encrypted document. To do so, call the GetByAlias() class method of this class. For example:
set credset=##class(%SYS.X509Credentials).GetByAlias("recipient")
To run this method, you must be logged in as a user included in the OwnerList for that credential set, or the OwnerList must be null. Also see Retrieving a Credential Set Programmatically in Securing Web Services.
-
Create at least one instance of %XML.Security.EncryptedKeyOpens in a new tab. To create an instance of this class, use the CreateX509() class method of this class. For example:
set enckey=##class(%XML.Security.EncryptedKey).Createx509(credset,encryptionOptions,referenceOption)
-
credset is the instance of %SYS.X509CredentialsOpens in a new tab that you just created.
-
encryptionOptions is $$$SOAPWSIncludeNone (there are other options, but they do not apply in this scenario).
This macro is defined in the %soap.inc include file.
-
referenceOption specifies the nature of the reference to the encrypted element. For permitted values, see Reference Options for X.509 Certificates in Securing Web Services.
The macros used here are defined in the %soap.inc include file.
-
Create an instance of %Library.ListOfObjectsOpens in a new tab and use its Insert() method to insert the instances of %XML.Security.EncryptedKeyOpens in a new tab that you just created.
-
Create an instance of %XML.Security.EncryptedDataOpens in a new tab by using the %New() method. For example:
set encdata=##class(%XML.Security.EncryptedData).%New()
-
Use the EncryptStream() instance method of %XML.Security.EncryptedDataOpens in a new tab to encrypt the stream that you created in step 2. For example:
set status=encdata.EncryptStream(stream,encryptedKeys)
-
Create and update an instance of your container class.
The details depend on your class.
-
Use %XML.WriterOpens in a new tab to generate output for your container class. See Writing XML Output from Objects.
For example, the container class shown previously also includes the following method:
ClassMethod Demo(filename = "",obj="")
{
#include %soap
if (obj="") {
set obj=##class(XMLEncryption.Person).GetPerson()
}
//create stream from this XML-enabled object
set writer=##class(%XML.Writer).%New()
set stream=##class(%GlobalCharacterStream).%New()
set status=writer.OutputToStream(stream)
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit }
set status=writer.RootObject(obj)
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit }
do stream.Rewind()
set container=..%New() ; this is the object we will write out
set cred=##class(%SYS.X509Credentials).GetByAlias("servercred")
set parts=$$$SOAPWSIncludeNone
set ref=$$$KeyInfoX509Certificate
set key=##class(%XML.Security.EncryptedKey).CreateX509(cred,parts,ref)
set container.Key=key ; this detail depends on the class
//need to create a list of keys (just one in this example)
set keys=##class(%Collection.ListOfObj).%New()
do keys.Insert(key)
set encdata=##class(%XML.Security.EncryptedData).%New()
set status=encdata.EncryptStream(stream,keys)
set container.Data=encdata ; this detail depends on the class
// write output for the container
set writer=##class(%XML.Writer).%New()
set writer.Indent=1
if (filename'="") {
set status=writer.OutputToFile(filename)
if $$$ISERR(status) {do $system.OBJ.DisplayError(status) quit}
}
set status=writer.RootObject(container)
if $$$ISERR(status) {do $system.OBJ.DisplayError(status) quit}
}
This method can accept the OREF of any XML-enabled class; if none is provided, a default is used.