2014-06-23 144 views
2

我正在編寫一個接收加密的SAML斷言的Web服務。在SAML斷言被加密之前,它可以被驗證。SAML 2.0解密EncryptedAssertion刪除名稱空間聲明?

當我的服務密碼進行解密並不能驗證斷言簽名

要看看這是爲什麼了EncryptedAssertion,我創建了一個小測試:

  1. 創建一個簽名的聲明(經查證屬實) - assertion1
  2. 覈實assertion1簽名 - 這個測試通過
  3. 加密assertion1得到一個EncryptedAssertion
  4. Decryp TS的EncryptedAssertion找回斷言 - assertion2
  5. 驗證上assertion2簽名 - 此測試失敗

如果我比較assertion1和assertion2節點只有一個區別。在Assertion1中,xmldsig命名空間既在斷言根元素中又在ds:Signature元素中聲明,在Assertion2中,Signature元素上的xmldsig命名空間聲明已被刪除。

XML-wise這是一個非常有效的轉換,XML仍然有效。我的問題是,當進行這種更改時,簽名不再有效,因爲斷言上的簽名已將現在缺失的前綴聲明考慮在內。

有沒有一種方法可以指導OpenSAML加密器/解密器不對接收到的XML進行「改進」,並且最初回饋用於輸入加密器的內容?

更改構造包含xmldsig命名空間的兩個聲明的XML的客戶端對我們來說不是一個真正的選擇。當然,但是這項服務的客戶是由另一家公司開發的,如果可能的話,我們寧願讓我們的服務對輸入的這類問題保持穩健。

這裏是我的測試代碼,加密assertion1:

public static EncryptedAssertion encryptAssertion(Assertion assertion, Credential credential) { 
    EncryptionParameters encParams = new EncryptionParameters(); 
    encParams.setAlgorithm(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128); 

    KeyEncryptionParameters kekParams = new KeyEncryptionParameters(); 
    kekParams.setEncryptionCredential(credential); 
    kekParams.setAlgorithm(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP); 
    KeyInfoGeneratorFactory kigf = 
      Configuration.getGlobalSecurityConfiguration() 
        .getKeyInfoGeneratorManager().getDefaultManager() 
        .getFactory(credential); 
    kekParams.setKeyInfoGenerator(kigf.newInstance()); 

    Encrypter samlEncrypter = new Encrypter(encParams, kekParams); 
    samlEncrypter.setKeyPlacement(Encrypter.KeyPlacement.INLINE); 

    try { 
     return samlEncrypter.encrypt(assertion); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
} 

這是解密EncryptedAssertion測試代碼:

public static Assertion decryptEncryptedAssertion(EncryptedAssertion encryptedAssertion, Credential credentials) throws DecryptionException { 
     StaticKeyInfoCredentialResolver staticKeyResolver = new StaticKeyInfoCredentialResolver(credentials); 
     InlineEncryptedKeyResolver inlineEncryptedKeyResolver = new InlineEncryptedKeyResolver(); 

     Decrypter decrypter = new Decrypter(null, staticKeyResolver, inlineEncryptedKeyResolver); 

     return decrypter.decrypt(encryptedAssertion); 
} 

這是assertion1的開頭:

<?xml version="1.0" encoding="UTF-8"?><saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_ede6280e-d094-4b74-a67a-e70bbec6f3e9" IssueInstant="2014-06-23T09:42:33.970Z" Version="2.0" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
    <saml2:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameidformat:entity">https://sts.sundhed.dk</saml2:Issuer> 
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
     <ds:SignedInfo> 

這是assertion2的開始 - 注意,與assertion1相比,xmlns:ds聲明簽名節點上比丟失:

<?xml version="1.0" encoding="UTF-8"?><saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_ede6280e-d094-4b74-a67a-e70bbec6f3e9" IssueInstant="2014-06-23T09:42:33.970Z" Version="2.0" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
    <saml2:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameidformat:entity">https://sts.sundhed.dk</saml2:Issuer> 
    <ds:Signature> 
     <ds:SignedInfo> 

更新:這是例外當試圖覈實assertion2簽名,我得到(當的xmlns:DS解密後是不存在的)。當調用解密器時。作爲回答表明setRootInNewDocument(真)調用validate成功完成:

org.opensaml.xml.validation.ValidationException: Unable to evaluate key against signature 
    at org.opensaml.xml.signature.SignatureValidator.validate(SignatureValidator.java:74) 
    at dk.itst.oiosaml.sp.model.OIOSamlObject.verifySignature(OIOSamlObject.java:239) 
    at dk.medicinkortet.idws.impl.EncryptedAssertionHandlerImplTest.testDecrypt(EncryptedAssertionHandlerImplTest.java:152) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 
Caused by: org.apache.xml.security.signature.MissingResourceFailureException: The Reference for URI #_944e39b7-37e2-4cd1-baba-865fb17f645b has no XMLSignatureInput 
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
    at org.apache.xml.security.signature.Manifest.verifyReferences(Manifest.java:414) 
    at org.apache.xml.security.signature.SignedInfo.verify(SignedInfo.java:256) 
    at org.apache.xml.security.signature.XMLSignature.checkSignatureValue(XMLSignature.java:728) 
    at org.opensaml.xml.signature.SignatureValidator.validate(SignatureValidator.java:69) 
    ... 34 more 
Caused by: org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
    at org.apache.xml.security.signature.Reference.calculateDigest(Reference.java:732) 
    at org.apache.xml.security.signature.Reference.verify(Reference.java:775) 
    at org.apache.xml.security.signature.Manifest.verifyReferences(Manifest.java:336) 
    ... 37 more 
Caused by: org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
    at org.apache.xml.security.signature.Reference.dereferenceURIandPerformTransforms(Reference.java:604) 
    at org.apache.xml.security.signature.Reference.calculateDigest(Reference.java:706) 
    ... 39 more 
Caused by: org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
Original Exception was org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
    at org.apache.xml.security.signature.Reference.getContentsBeforeTransformation(Reference.java:419) 
    at org.apache.xml.security.signature.Reference.dereferenceURIandPerformTransforms(Reference.java:597) 
    ... 40 more 
Caused by: org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID _944e39b7-37e2-4cd1-baba-865fb17f645b 
    at org.apache.xml.security.utils.resolver.implementations.ResolverFragment.engineResolveURI(ResolverFragment.java:85) 
    at org.apache.xml.security.utils.resolver.ResourceResolver.resolve(ResourceResolver.java:298) 
    at org.apache.xml.security.signature.Reference.getContentsBeforeTransformation(Reference.java:417) 
    ... 41 more 
+0

你是否贊同XML解析隨OpenSAML庫,在https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaInstall描述? –

+0

你可以請你張貼你的簽名驗證失敗的確切例外嗎?這個問題實際上可能與名稱空間不同有關,因爲使用的c14n算法(http://www.w3.org/TR/xml-exc-c14n/)應該使簽名期間XML文檔的兩個變體等同驗證。 –

回答

1

下可能會解決您的問題:

  • 務必正確隨OpenSAML
  • 創建endorse庫您將rootInNewDocument屬性設置爲true的解密器:

    decrypter.setRootInNewDocument(true);

+0

我贊同opensaml附帶的libs,但這並不妨礙xmlns:ds從assertion2中移除。但是調用decrypter.setRootInNewDocument(true)。非常感謝你 :) – joensson