
Implementing XML Signatures in WSS4J
In the first column of this series, I introduced the WSS4J API. In the second column, I demonstrated the use of XSS4J for XML encryption. In the third column, I implemented the encryption features of WSS4J using the concepts discussed in the second column. And the fourth column of this series demonstrated the use of XSS4J to author XML digital signatures.
In this column, I'll use the concepts discussed in the previous columns to implement XML signature support in our WSS4J API.
As you know from previous columns, the WSS4J API implements the Token interface separately for different types of security tokens in WSS. In this column, I'll discuss six types of WSS signature tokens:
- X509 binary security token
- Username password text token
- Username password digest token
- X509 key name token
- X509 pointer token
- X509 key identifier token
Let's start our discussion by considering a WSS message before and after XML signatures.
A WSS Message Before and After XML Signature
In this section I'll present an input message (i.e. a SOAP message containing data to be signed) that we will use to demonstrate our WSS4J API for signing. Then I'll discuss the output after signing of the input message with six different tokens supported by the WSS specifications.
Listing 1 shows the SOAP input message that we will use as input for the XML signature process. Listings 2, 3, 4, 5, 6, and 7 show the output after signing the input SOAP message with different tokens.
You can see from Listings 2, 3, 4, 5, 6, and 7 that they are not very different from each other. Therefore, we will first explain Listing 2 and then outline the differences between Listing 2 and other tokens.
Listing 2 shows the result after
signing the input SOAP message using
the wsse:BinarySecurityToken element. I covered the
details of using the wsse:BinarySecurityToken element in
our web services security series (especially in Part 2). Notice the following points from Listing 2:
- You can see in Listing 2 that the
wsse:BinarySecurityTokenelement is included inside thewsse:Securityelement in the SOAP Header. - This
wsse:BinarySecurityTokenelement is actually a security token and in order to act as a security token, thewsse:BinarySecurityTokenelement has to wrap authentication data. - The
wsse:BinarySecurityTokencan wrap different types of authentication data in binary format (e.g. X509 certificates and Kerberos tickets, etc.). - You can see in Listing 2 that the
wsse:BinarySecurityTokenelement contains three attributes, namelyValueType,EncodeType, andwsu:Id. - The
ValueTypeattribute of thewsse:BinarySecurityTokenelement in Listing 2 is used to specify the type of the security token wrapped inside thiswsse:BinarySecurityTokenelement. Here theValueTypeattribute contains"X509v3", which means this binary security token wraps an X509 version 3 certificate. - The
wsu:Idattribute is used to specify an identifier for the token. Here we specified its value as "BinarySecurityTokenWithReference." Whenever we need this token for signing a message, we will refer to the token using itswsu:Id. - The
EncodeTypeattribute is used to specify the encoding type of the token. In Listing 2 the value of theEncodeTypeattribute iswsse:Base64Binary. The binary data in raw form cannot be wrapped inside XML markup as such; it may produce problems while XML parsing. Therefore we encode it into base-64 before wrapping inside XML markup. The result of base-64 encoding does not contain any byte that conflicts with XML processing. - Now look at the
ds:Signatureelement inwsse:Securityelement. We discussed the details of theds:Signatureformat in the second article of the Web Services Security series. - How will we specify the token that a particular signature element uses for signature? Look at the
ds:KeyInfoelement inside theds:Signatureelement in Listing 2. It contains awsse:SecurityTokenReferenceelement. Thiswsse:SecurityTokenReferenceelement is used to specify the token that we have used to produce the signature. In Listing 2 we have included ads:Referenceelement inside thewsse:SecurityTokenReferenceelement. TheURIattribute of theds:Referenceelement specifies the ID of the token we want to sign our message. For example in Listing 2 the value of theURIattribute is "#BinarySecurityTokenWithReference". This attribute value points to the token whosewsu:Idis "BinarySecurityTokenWithReference."
We will call the wsse:BinarySecurityToken element that wraps an X509 certificate as X509 binary security token.
Now look at Listing 3, the output in Listing 3 is generated after signing the SOAP message with another type of token named UsernameToken, which is also defined by the WSS Specification. The only difference between Listings 2 and 3 is that the wsse:BinarySecurityToken element in Listing 3 is replaced with wsse:UsernameToken element. The following points explain the wsse:UsernameToken security token:
- You can see in Listing 3 that a
wsse:UsernameTokenelement contains ausernamein thewsse:Usernameelement and a password wrapped in thewsse:Passwordelement. - The
UsernameTokensigns the SOAP message with the password specified in thewsse:Passwordelement. - The
wsse:Passwordelement contains a Type attribute, which specifies the form in which the password is wrapped inside thewsse:Passwordelement. Here in Listing 3 its value is "PasswordText", which tells that the password is in plain-text form. Some users might not like to send their password in plain-text form. They can use another type of security token named Username password digest token, which I will explain shortly.
We will call the username token with password in text form (Listing 3) as Username password text token.
Now look at Listing 4, which is a very slightly modified form of Listing 3. In Listing 4 the Type attribute of wsse:Password element is specified as "PasswordDigest." Moreover, two elements named wsse:Nonce and wsse:Created are included in the wsse:UsernameToken element. In this case the password (which is a secret key), wsse:Nonce (which wraps a random number), and wsse:Created (which is a timestamp) will be combined to form the digest value corresponding to the password.
We have already covered the details of password digests as well as wsse:Nonce and wsse:Created elements while discussing the wsse:UsernameToken element in Listing 1 of the fourth article of the Web Services Security series.
We will call the username token of type "PasswordDigest" (Listing 4) as Username password digest token.
Listing 5 shows the output after signing the message with the key name token. You can see in Listing 5 that it contains a ds:KeyName element as child of the wsse:SecurityTokenReference element. This ds:KeyName element contains the alias of the X509 certificate used to sign the WSS message.
Notice that unlike Listings 2, 3, and 4, Listing 5 does not contain any token. This is because, in Listing 5 a reference to the token is directly included in the wsse:SecurityTokenReference child of the ds:KeyInfo element. In this case we are assuming that the application that receives this WSS message can itself find the certificate using the alias. Therefore, we do not need to send the certificate along with the message.
We will call the key name token (Listing 5) as X509 key name token.
Now have a look at Listing 6, which is a slightly modified form of Listing 5. In Listing 6 the ds:KeyName element inside the wsse:SecurityTokenReference is replaced with the ds:X509Data element structure. The following points explain the ds:X509Data element:
-
The
ds:X509Dataelement wraps an element namedX509IssureSerial. - The
X509IssureSerialelement contains two child elements namedX509IssuerNameandX509SerialNumber. These two elements uniquely identify an X509 certificate used for signing.
Also notice that in the case of Listing 6, we are assuming that the recipient can find the certificate with the information provided in the ds:X509Data element. Therefore, we do not need to send the certificate along with the message.
We will refer to the token with the X509 issuer serial (Listing 6) as X509 pointer token.
Listing 7 shows the output after signing the message with the key identifier token. You can see that Listing 7 is a slightly modified form of Listing 5. In Listing 7 the ds:KeyName element is replaced with the wsse:KeyIdentifier element as the child of the wsse:SecurityTokenReference element. The wsse:KeyIdentifier wraps an identifier of a key.
The following points explain the wsse:KeyIdentifier element:
-
You can see in Listing 7 that the
wsse:KeyIdentifierelement contains two attributes, namelyEncodeTypeandValueType. - The
ValueTypeattribute of thewsse:KeyIdentifierelement in Listing 7 is used to specify the type of the identifier wrapped inside thiswsse:KeyIdentifierelement. Here the value of theValueTypeattribute iswsse:X509SubjectKeyIdentifier, which means this key identifier identifies the signer's X509 certificate. - The
EncodeTypeattribute is used to specify the encoding type of the token. In our case we are using base-64 encoding.
Also notice that in case of Listing 7, we are assuming that the recipient can find the certificate with the information provided in the wsse:KeyIdentifier element. Therefore, we do not need to send the certificate along with the message.
We will refer to the key identifier token (Listing 7) as X509 key identifier token.
Pages: 1, 2 |