| 80 | | |
|---|
| 81 | | |
|---|
| 82 | | |
|---|
| 83 | | |
|---|
| 84 | | |
|---|
| | 88 | System.out.println("Private key read and created as object ..."); |
|---|
| | 89 | |
|---|
| | 90 | // Signature stuff |
|---|
| | 91 | org.opensaml.xml.signature.impl.SignatureBuilder signatureBuilder = new org.opensaml.xml.signature.impl.SignatureBuilder(); |
|---|
| | 92 | org.opensaml.xml.signature.impl.SignatureImpl signature = signatureBuilder.buildObject(); |
|---|
| | 93 | org.opensaml.xml.security.x509.BasicX509Credential credential; |
|---|
| | 94 | credential = new org.opensaml.xml.security.x509.BasicX509Credential(); |
|---|
| | 95 | credential.setPrivateKey(privateKey); |
|---|
| | 96 | //signature.setSigningCredential(credential); |
|---|
| | 97 | System.out.println("Set private key into credential object"); |
|---|
| | 98 | |
|---|
| | 99 | // build the AuthnRequest |
|---|
| | 100 | net.clareitysecurity.websso.sp.HttpPost post = new net.clareitysecurity.websso.sp.HttpPost(); |
|---|
| | 101 | post.setActionURL("xxx"); |
|---|
| | 102 | post.setAssertionConsumerServiceURL("yyy"); |
|---|
| | 103 | post.setIssuerName("test"); |
|---|
| | 104 | post.setProviderName("provider"); |
|---|
| | 105 | AuthnRequest authnRequest = post.getAuthnRequest(); |
|---|
| | 106 | System.out.println("Built AuthnRequest object ..."); |
|---|
| | 107 | |
|---|
| | 108 | // Now the response to our AuthnRequest |
|---|
| | 109 | SAMLResponse samlResponse = new SAMLResponse(); |
|---|
| | 110 | samlResponse.setActionURL("http://dev.acmemls.com/recv-response.jsp"); |
|---|
| | 111 | samlResponse.setIssuerName("dev.acmeidp.com"); |
|---|
| | 112 | samlResponse.setLoginId("acme"); |
|---|
| | 113 | samlResponse.setAuthnRequest(authnRequest); |
|---|
| | 114 | Response rsp = samlResponse.getSuccessResponse(); |
|---|
| | 115 | System.out.println("Built Response object ..."); |
|---|
| | 116 | |
|---|
| | 117 | // Now we need to try and sign the response object |
|---|
| | 118 | org.opensaml.common.impl.SAMLObjectContentReference socr = new org.opensaml.common.impl.SAMLObjectContentReference(rsp); |
|---|
| | 119 | signature.getContentReferences().add(socr); |
|---|
| | 120 | signature.setSigningCredential(credential); |
|---|
| | 121 | signature.setSignatureAlgorithm( SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1 ); |
|---|
| | 122 | signature.setCanonicalizationAlgorithm( SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS ); |
|---|
| | 123 | rsp.setSignature(signature); |
|---|
| | 124 | System.out.println("Signature object created and added to Response object ..."); |
|---|
| | 125 | // Now sign it |
|---|
| | 126 | org.opensaml.xml.signature.Signer.signObject(signature); |
|---|
| | 127 | System.out.println("Response now signed ..."); |
|---|
| | 128 | |
|---|
| | 129 | // Ok. We should now have a signed Response object. |
|---|
| | 130 | |
|---|
| | 131 | |
|---|
| | 132 | // Now let's find the public key stuff and verify our signed Response object |
|---|
| 119 | | KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder(); |
|---|
| 120 | | KeyInfo keyInfoDupe = keyInfoBuilder.buildObject(); |
|---|
| 121 | | keyInfoDupe.setDOM(keyInfo.getDOM()); |
|---|
| 122 | | |
|---|
| 123 | | // Signature stuff |
|---|
| 124 | | org.opensaml.xml.signature.impl.SignatureBuilder signatureBuilder = new org.opensaml.xml.signature.impl.SignatureBuilder(); |
|---|
| 125 | | org.opensaml.xml.signature.impl.SignatureImpl signature = signatureBuilder.buildObject(); |
|---|
| 126 | | signature.setKeyInfo(keyInfoDupe); |
|---|
| 127 | | System.out.println("Set KeyInfo"); |
|---|
| 128 | | |
|---|
| 129 | | |
|---|
| 130 | | /* |
|---|
| | 167 | // KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder(); |
|---|
| | 168 | // KeyInfo keyInfoDupe = keyInfoBuilder.buildObject(); |
|---|
| | 169 | // keyInfoDupe.setDOM(keyInfo.getDOM()); |
|---|
| | 170 | |
|---|
| | 198 | String encodedPublicKey = x509Cert.getValue(); |
|---|
| | 199 | byte[] x509KeyBytes = Base64.decode(encodedPublicKey); |
|---|
| | 200 | X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec( x509KeyBytes ); |
|---|
| | 201 | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
|---|
| | 202 | PublicKey publicKey = keyFactory.generatePublic(pubKeySpec); |
|---|
| | 203 | System.out.println("PublicKey finally created"); |
|---|
| | 204 | |
|---|
| | 205 | org.opensaml.xml.security.x509.BasicX509Credential publicCredential = new org.opensaml.xml.security.x509.BasicX509Credential(); |
|---|
| | 206 | publicCredential.setPublicKey(publicKey); |
|---|
| | 207 | org.opensaml.xml.signature.SignatureValidator signatureValidator = new org.opensaml.xml.signature.SignatureValidator(publicCredential); |
|---|
| | 208 | |
|---|
| | 209 | // Now try to validate |
|---|
| | 210 | try { |
|---|
| | 211 | signatureValidator.validate(signature); |
|---|
| | 212 | } catch (org.opensaml.xml.validation.ValidationException ve) { |
|---|
| | 213 | System.out.println("Signature is NOT valid."); |
|---|
| | 214 | return; |
|---|
| | 215 | } |
|---|
| | 216 | System.out.println("Signature is valid."); |
|---|
| | 217 | |
|---|