2014-09-22 96 views
9

我遇到了一個問題,即Spring SAML集成爲我的IdP生成正確的元數據文件。我獲得了新的SHA256 SSL證書。我已經完成了所有的步驟來創建適當的keyStore,並且我的Spring安全配置文件都已經設置好了。我從字面上看似98%的方式,但是在生成的元數據文件中缺少一件事,我無法爲我的生活找出爲什麼它沒有設置。設置ExtendedMetadata'signingAlgorithm'字段

這裏是我的MetadataGeneratorFilter ExtendedMetadata配置:

<bean id="metadataGeneratorFilter" class="org.springframework.security.saml.metadata.MetadataGeneratorFilter"> 
<constructor-arg> 
    <bean class="org.springframework.security.saml.metadata.MetadataGenerator"> 
     <property name="entityId" value="urn:myentityidhere"/> 
     <property name="entityBaseURL" value="https://${saml.url}"/> 
     <property name="extendedMetadata"> 
      <bean class="org.springframework.security.saml.metadata.ExtendedMetadata"> 
       <property name="signMetadata" value="true"/> 
       <property name="signingAlgorithm" value="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> 
       <property name="alias" value="ceo"/> 
       <property name="signingKey" value="${saml.sp.alias}"/> 
       <property name="encryptionKey" value="${saml.sp.alias}"/> 
      </bean> 
     </property> 
    </bean> 
</constructor-arg> 

當我運行我的應用程序,並轉到/ SAML /元數據URI讓Spring生成的元數據文件,我需要發送到我的IdP,SHA256算法在SignatureMethod上得到了正確設置,但子DigestMethod標籤的算法值仍然設置爲SHA1,當我需要將SHA256和DigestValue一起設置爲SHA256值而不是SHA1值時。

<ds:SignedInfo> 
    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> 
    <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> 
    <ds:Reference URI="#urn_myentityidhere"> 
     <ds:Transforms> 
      <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
      <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> 
     </ds:Transforms> 
     <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
     <ds:DigestValue>xxxxxxx</ds:DigestValue> 
    </ds:Reference> 
</ds:SignedInfo> 

有人可以指導我如何/我需要設置將DigestMethod算法值設置爲256也是什麼?我想,因爲它是SignedInfo標記的子元素,它將繼承Extendedmetadata配置中的signingAlgorithm值,但事實並非如此。

任何幫助將非常感激。非常感謝。

的解決方案 - 萬一有人關心

所以,一天的價值挖掘後,我決定只實現這個自己。我通過添加字段,digestMethodAlgorithm延長ExtendedMetadata類和加入適當的吸氣/ setter方法:

/** 
* Algorithm used for creation of digest method of this entity. At the moment only used for metadata signatures. 
* Only valid for local entities. 
*/ 
private String digestMethodAlgorithm; 

/** 
* Returns digest method algorithm value 
* @return String 
*/ 
public String getDigestMethodAlgorithm() 
{ 
    return digestMethodAlgorithm; 
} 

/** 
* Sets the digest method algorithm to use when signing the SAML messages. 
* This can be used, for example, when a strong algorithm is required (e.g. SHA 256 instead of SHA 128). 
* If this property is null, then the {@link org.opensaml.xml.Configuration} default algorithm will be used instead. 
* 
* Value only applies to local entities. 
* 
* At the moment the value is only used for signatures on metadata. 
* 
* Typical values are: 
* http://www.w3.org/2001/04/xmlenc#sha1 
* http://www.w3.org/2001/04/xmlenc#sha256 
* http://www.w3.org/2001/04/xmlenc#sha384 
* http://www.w3.org/2001/04/xmlenc#sha512 
* http://www.w3.org/2001/04/xmlenc#ripemd160 
* 
* @param digestMethodAlgorithm The new digest method algorithm to use 
* @see org.opensaml.xml.signature.SignatureConstants 
*/ 
public void setDigestMethodAlgorithm(String digestMethodAlgorithm) 
{ 
    this.digestMethodAlgorithm = digestMethodAlgorithm; 
} 

然後我修改從我的上述彈簧安全配置,以包括該新bean屬性在我MetadataGenerator配置進行設置:

<bean id="metadataGeneratorFilter" class="org.springframework.security.saml.metadata.MetadataGeneratorFilter"> 
<constructor-arg> 
    <bean class="org.springframework.security.saml.metadata.MetadataGenerator"> 
     <property name="entityId" value="urn:myentityidhere"/> 
     <property name="entityBaseURL" value="https://${saml.url}"/> 
     <property name="extendedMetadata"> 
      <bean class="org.springframework.security.saml.metadata.ExtendedMetadata"> 
       <property name="signMetadata" value="true"/> 
       <property name="signingAlgorithm" value="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> 
       <property name="digestMethodAlgorithm" value="http://www.w3.org/2001/04/xmlenc#sha256"/> 
       <property name="alias" value="ceo"/> 
       <property name="signingKey" value="${saml.sp.alias}"/> 
       <property name="encryptionKey" value="${saml.sp.alias}"/> 
      </bean> 
     </property> 
    </bean> 
</constructor-arg> 

然後,我也不得不提出兩個更改SAMLUtil類。在getSignadataAsString中,在isSignMetadata()if-子句中,我提取了由上述配置設置的digestMethodAlgorithm的注入值,然後進一步修改了marshallAndSignMessage方法以接受一個新的輸入參數,我進一步使用它來獲取DigestMethod算法集合。

內SAMLUtil.getMetaDataAsString的,線572

... 
String digestMethodAlgorithm = extendedMetadata.getDigestMethodAlgorithm(); 
element = SAMLUtil.marshallAndSignMessage(descriptor, credential, signingAlgorithm, digestMethodAlgorithm, keyGenerator); 
... 

內SAMLUtil.marshallAndSignMessage,線437之後,我添加/更改如下:

... 
BasicSecurityConfiguration secConfig = null; 

if (digestMethodAlgorithm != null) 
{ 
    secConfig = (BasicSecurityConfiguration) Configuration.getGlobalSecurityConfiguration(); 

    secConfig.setSignatureReferenceDigestMethod(digestMethodAlgorithm); 
} 

try { 
    SecurityHelper.prepareSignatureParams(signature, signingCredential, secConfig, keyInfoGenerator); 
} catch (org.opensaml.xml.security.SecurityException e) { 
    throw new MessageEncodingException("Error preparing signature for signing", e); 
} 
... 

我重新編譯了整個春天SAML通過Gradle,spring-security-saml-1.0.0.RELEASE,將新jar從build/libs目錄複製到我的項目中,部署webapp,將瀏覽器指向/ saml/metadata併成功獲取元數據文件meta的正確SHA256簽名部分數據文件。

我會看看我能做些什麼來實現這個項目的git repo,因爲我不想失去這個能力,因爲這個項目將來會發布。以前從來沒有爲這樣的開源項目做過貢獻。

<ds:SignedInfo> 
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> 
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> 
<ds:Reference URI="#urn_myentityidhere"> 
    <ds:Transforms> 
     <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> 
    </ds:Transforms> 
    <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/> 
    <ds:DigestValue>xxxxxx</ds:DigestValue> 
</ds:Reference> 

回答

9

可以春SAML初始化過程中配置消化方法,通過使下面的調用數字簽名的計算:

// Use SHA-256 signatures for RSA keys 
BasicSecurityConfiguration config = (BasicSecurityConfiguration) Configuration.getGlobalSecurityConfiguration(); 
config.setSignatureReferenceDigestMethod(SignatureConstants.ALGO_ID_DIGEST_SHA256); 

例如擴展默認org.springframework.security.saml.SAMLBootstrap和代碼添加到調用super後覆蓋postProcessBeanFactory方法:

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 
    super.postProcessBeanFactory(beanFactory); 
    BasicSecurityConfiguration config = (BasicSecurityConfiguration) Configuration.getGlobalSecurityConfiguration(); 
    config.setSignatureReferenceDigestMethod(SignatureConstants.ALGO_ID_DIGEST_SHA256); 
} 

此更改影響生成的元數據中的簽名和生成的SAML消息中的簽名。

12

自@VladimírSchäfer回答以來事情似乎發生了變化;它不適用於AD FS 2.0和SHA-256。我們必須添加一個額外的設置才能使其運行(請參閱下面的代碼)。

的問題似乎是在OpenSAML的xmltooling庫,特別是org.opensaml.xml.security.BasicSecurityConfiguration.getSignatureAlgorithmURI(Credential)方法 - 而不是僅僅使用證書的簽名算法(在本例中,SHA256withRSA),它獲得證書的關鍵,然後查看該密鑰的算法並使用已註冊URI的映射來查找簽名URI。如果他們只是將JCA 簽名算法的映射映射到URI,而不是關鍵算法映射到URI,則一切正常。

解決方法是在Spring連線期間註冊正確的簽名算法URI和BasicSecurityConfiguration,覆蓋已存在的(不需要的)URI http://www.w3.org/2000/09/xmldsig#rsa-sha1http://www.w3.org/2001/04/xmldsig-more#rsa-sha256)。

我們還必須刪除setSignatureReferenceDigestMethod()調用或將元數據導入AD FS將失敗。

import org.opensaml.Configuration;
import org.opensaml.xml.security.BasicSecurityConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.security.saml.SAMLBootstrap;

public class CustomSamlBootstrap extends SAMLBootstrap {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
super.postProcessBeanFactory(beanFactory); BasicSecurityConfiguration config = (BasicSecurityConfiguration) Configuration.getGlobalSecurityConfiguration(); config.registerSignatureAlgorithmURI("RSA", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"); } }
+0

你使用了哪個spring-security-saml2-core版本?我不能使你的建議... – nuvio 2016-07-29 07:01:24

+0

這是使用'org.springframework.security.extensions:spring-security-saml2-core:1.0.1.RELEASE' with'org.opensaml:opensaml:2.6.4' 。 – 2016-07-29 13:12:30

0

作出改變SAMLBootstrap全球安全配置後,我遇到了以下異常:

org.apache.xml.security.signature.XMLSignatureException:請求 算法SHA256withRSA不存在。原始消息是: SHA256withRSA消息摘要不提供 org.apache.xml.security.algorithms.MessageDigestAlgorithm.getDigestInstance(未知 來源)在 org.apache.xml.security.algorithms.MessageDigestAlgorithm.getInstance(未知 來源)在org.apache.xml.security.signature.Reference。(Unknown Source)at org.apache.xml.security.signature.Manifest.addDocument(Unknown Source) at org.apache.xml.security.signature.XMLSignature。 addDocument(未知 來源)

經過進一步調查發現,Apache XML Security xmlsec-1.4.3.jar不支持底層的SHA256withRSA算法。

解決方法:使用xmlsec-2.0.2.jarhttps://mvnrepository.com/artifact/org.apache.santuario/xmlsec/2.0.2

這個新的JAR解決了這個問題。