she = new GSHeaderElement(samlAssertion);
}else if (reader != null) {
she = new GSHeaderElement(buffer);
she.setId(id); // set the ID again to bring it to top
}
JAXBEncryptedKey ek;
String asID;
String idVal = "";
String keyEncAlgo = XMLCipher.RSA_v1dot5;
Key samlkey = null;
if(samlAssertion != null){
asID = samlAssertion.getAttributeNS(null,"AssertionID");
if(she == null){
logger.log(Level.SEVERE, LogStringsMessages.WSS_1811_NULL_SAML_ASSERTION());
throw new XWSSecurityException("SAML Assertion is NULL");
}
if(asID == null || asID.length() ==0){
idVal = samlAssertion.getAttributeNS(null,"ID");
she.setId(idVal);
}else{
she.setId(asID);
}
}else {
if (she == null) {
she = (SecurityHeaderElement) context.getExtraneousProperty(MessageConstants.INCOMING_SAML_ASSERTION);
}
if (she == null) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1811_NULL_SAML_ASSERTION());
throw new XWSSecurityException("SAML Assertion is NULL");
}
idVal = asID = she.getId();
}
if(logger.isLoggable(Level.FINEST)){
logger.log(Level.FINEST, "SAML Assertion id:{0}", asID);
}
Key dataProtectionKey;
if(forSign){
PrivateKeyBinding privKBinding = (PrivateKeyBinding)keyBinding.getKeyBinding();
dataProtectionKey = privKBinding.getPrivateKey();
if (dataProtectionKey == null) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1810_NULL_PRIVATEKEY_SAML());
throw new XWSSecurityException("PrivateKey null inside PrivateKeyBinding set for SAML Policy ");
}
if(context.getSecurityHeader().getChildElement(she.getId()) == null){
context.getSecurityHeader().add(she);
}
} else {
SecurityHeaderElement assertion = (SecurityHeaderElement) context.getExtraneousProperty(MessageConstants.INCOMING_SAML_ASSERTION);
samlkey = ((SAMLAssertion) assertion).getKey();
/*
x509Cert = context.getSecurityEnvironment().getCertificate(
context.getExtraneousProperties() ,(PublicKey)key, false);
if (x509Cert == null) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1812_MISSING_CERT_SAMLASSERTION());
throw new XWSSecurityException("Could not locate Certificate corresponding to Key in SubjectConfirmation of SAML Assertion");
}
*/
if (!"".equals(keyBinding.getKeyAlgorithm())) {
keyEncAlgo = keyBinding.getKeyAlgorithm();
}
String dataEncAlgo = SecurityUtil.getDataEncryptionAlgo(context);
dataProtectionKey = SecurityUtil.generateSymmetricKey(dataEncAlgo);
}
Element authorityBinding = keyBinding.getAuthorityBinding();
//assertionId = keyBinding.getAssertionId();
String referenceType = keyBinding.getReferenceType();
if (referenceType.equals(MessageConstants.EMBEDDED_REFERENCE_TYPE)) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1813_UNSUPPORTED_EMBEDDEDREFERENCETYPE_SAML());
throw new XWSSecurityException("Embedded Reference Type for SAML Assertions not supported yet");
}
assertionId = she.getId();
//todo reference different keyreference types.
SecurityTokenReference samlSTR;
if(authorityBinding == null){
KeyIdentifier keyIdentifier = new KeyIdentifier(context.getSOAPVersion());
keyIdentifier.setValue(assertionId);
if(MessageConstants.SAML_v2_0_NS.equals(she.getNamespaceURI())){
keyIdentifier.setValueType(MessageConstants.WSSE_SAML_v2_0_KEY_IDENTIFIER_VALUE_TYPE);
} else{
keyIdentifier.setValueType(MessageConstants.WSSE_SAML_KEY_IDENTIFIER_VALUE_TYPE);
}
samlSTR = elementFactory.createSecurityTokenReference(keyIdentifier);
if (idVal != null) {
samlSTR.setTokenType(MessageConstants.WSSE_SAML_v2_0_TOKEN_TYPE);
}else{
samlSTR.setTokenType(MessageConstants.WSSE_SAML_v1_1_TOKEN_TYPE);
}
//((SecurityTokenReferenceType)samlSTR).getAny().add(authorityBinding);
((NamespaceContextEx)context.getNamespaceContext()).addWSS11NS();
buildKeyInfo((SecurityTokenReference) samlSTR);
} else{
//TODO: handle authorityBinding != null
}
if(!forSign){
HashMap ekCache = context.getEncryptedKeyCache();
ek = (JAXBEncryptedKey)elementFactory.createEncryptedKey(context.generateID(),keyEncAlgo,super.keyInfo,samlkey,dataProtectionKey);
context.getSecurityHeader().add(ek);
String ekId = ek.getId();
DirectReference dr = buildDirectReference(ekId,MessageConstants.EncryptedKey_NS);
result.setKeyInfo(buildKeyInfo(dr,""));
}else{
result.setKeyInfo(super.keyInfo);
}