2012-02-23 24 views
10

का उपयोग कर जावा में SAML 2.0 का उपयोग करके एन्क्रिप्टेड दावे को डिक्रिप्ट करना मुझे SAML 2.0 का उपयोग करके एन्क्रिप्टेड दावे को डिक्रिप्ट करने का प्रयास करते समय कोई समस्या है। मैं जिस पुस्तकालय का उपयोग कर रहा हूं वह ओपनएसएमएल जावा पुस्तकालय 2.5.2 है।ओपनएसएमएल

एन्क्रिप्टेड जोर इस तरह दिखता है:

<EncryptedAssertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion"> 
<enc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" 
    xmlns:enc="http://www.w3.org/2001/04/xmlenc#"> 
    <enc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /> 
    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> 
    <e:EncryptedKey xmlns:e="http://www.w3.org/2001/04/xmlenc#"> 
     <e:EncryptionMethod 
     Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"> 
     <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
     </e:EncryptionMethod> 
     <KeyInfo> 
     <o:SecurityTokenReference 
      xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext- 
        1.0.xsd"> 
      <o:KeyIdentifier 
      ValueType="http://docs.oasis-open.org/wss/oasis-wss-soap-message-security- 
         1.1#ThumbprintSHA1" 
      EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap- 
         message-security-1.0#Base64Binary"> 
      1H3mV/pJAlVZAst/Dt0rqbBd67g= 
      </o:KeyIdentifier> 
     </o:SecurityTokenReference> 
     </KeyInfo> 
     <e:CipherData> 
     <e:CipherValue> 
    ... ENCRYPTED KEY HERE ... 
     </e:CipherValue> 
     </e:CipherData> 
    </e:EncryptedKey> 
    </KeyInfo> 
    <enc:CipherData> 
    <enc:CipherValue> 
    ... ENCRYPTED ASSERTIONS HERE ... 
    </enc:CipherValue> 
    </enc:CipherData> 
</enc:EncryptedData> 
</EncryptedAssertion> 

मैं PEM प्रारूप में है कि निम्नलिखित openssl आदेश का उपयोग प्रारूप pkcs8 करने के लिए अपने निजी कुंजी परिवर्तित किया:

openssl pkcs8 -topk8 -nocrypt -inform PEM -in rsa_private_key.key -outform DER -out rsa_private_key.pk8 

मैं तो के लिए तैयार हूँ एन्क्रिप्टेड दावे को डिक्रिप्ट करने का प्रयास करें। यहां मेरा जावा कोड है:

... 
// Load the XML file and parse it. 
File xmlFile = new File("data\\token.xml"); 
InputStream inputStream = new FileInputStream(xmlFile); 
Document document = parserPoolManager.parse(inputStream); 
Element metadataRoot = document.getDocumentElement(); 

// Unmarshall 
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); 
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot); 
EncryptedAssertion encryptedAssertion = (EncryptedAssertion)unmarshaller.unmarshall(metadataRoot); 

// Load the private key file. 
File privateKeyFile = new File("data\\rsa_private_key.pk8"); 
FileInputStream inputStreamPrivateKey = new FileInputStream(privateKeyFile); 
byte[] encodedPrivateKey = new byte[(int)privateKeyFile.length()]; 
inputStreamPrivateKey.read(encodedPrivateKey); 
inputStreamPrivateKey.close(); 

// Create the private key. 
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); 
RSAPrivateKey privateKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(privateKeySpec); 

// Create the credentials. 
BasicX509Credential decryptionCredential = new BasicX509Credential(); 
decryptionCredential.setPrivateKey(privateKey); 

// Create a decrypter. 
Decrypter decrypter = new Decrypter(null, new StaticKeyInfoCredentialResolver(decryptionCredential), new InlineEncryptedKeyResolver()); 

// Decrypt the assertion. 
Assertion decryptedAssertion; 

try 
{ 
    decryptedAssertion = decrypter.decrypt(encryptedAssertion); 
} 
... 

इस कोड को चलाने से हमेशा निर्णय को डिक्रिप्ट करने में असमर्थ होने के परिणामस्वरूप परिणाम होता है। मुझे निम्न त्रुटियां मिलती हैं:

5473 [main] ERROR org.opensaml.xml.encryption.Decrypter - Error decrypting encrypted key 
org.apache.xml.security.encryption.XMLEncryptionException: Key is too long for unwrapping 
Original Exception was java.security.InvalidKeyException: Key is too long for unwrapping 
    at org.apache.xml.security.encryption.XMLCipher.decryptKey(Unknown Source) 
    at org.opensaml.xml.encryption.Decrypter.decryptKey(Decrypter.java:681) 
    at org.opensaml.xml.encryption.Decrypter.decryptKey(Decrypter.java:612) 
    at org.opensaml.xml.encryption.Decrypter.decryptUsingResolvedEncryptedKey(Decrypter.java:762) 
    at org.opensaml.xml.encryption.Decrypter.decryptDataToDOM(Decrypter.java:513) 
    at org.opensaml.xml.encryption.Decrypter.decryptDataToList(Decrypter.java:440) 
    at org.opensaml.xml.encryption.Decrypter.decryptData(Decrypter.java:401) 
    at org.opensaml.saml2.encryption.Decrypter.decryptData(Decrypter.java:141) 
    at org.opensaml.saml2.encryption.Decrypter.decrypt(Decrypter.java:69) 
    at DecrypterTool.main(DecrypterTool.java:121) 
java.security.InvalidKeyException: Key is too long for unwrapping 
    at com.sun.crypto.provider.RSACipher.engineUnwrap(DashoA13*..) 
    at javax.crypto.Cipher.unwrap(DashoA13*..) 
    at org.apache.xml.security.encryption.XMLCipher.decryptKey(Unknown Source) 
    at org.opensaml.xml.encryption.Decrypter.decryptKey(Decrypter.java:681) 
    at org.opensaml.xml.encryption.Decrypter.decryptKey(Decrypter.java:612) 
    at org.opensaml.xml.encryption.Decrypter.decryptUsingResolvedEncryptedKey(Decrypter.java:762) 
    at org.opensaml.xml.encryption.Decrypter.decryptDataToDOM(Decrypter.java:513) 
    at org.opensaml.xml.encryption.Decrypter.decryptDataToList(Decrypter.java:440) 
    at org.opensaml.xml.encryption.Decrypter.decryptData(Decrypter.java:401) 
    at org.opensaml.saml2.encryption.Decrypter.decryptData(Decrypter.java:141) 
    at org.opensaml.saml2.encryption.Decrypter.decrypt(Decrypter.java:69) 
    at DecrypterTool.main(DecrypterTool.java:121) 
5477 [main] ERROR org.opensaml.xml.encryption.Decrypter - Failed to decrypt EncryptedKey, valid decryption key could not be resolved 
5477 [main] ERROR org.opensaml.xml.encryption.Decrypter - Failed to decrypt EncryptedData using either EncryptedData KeyInfoCredentialResolver or EncryptedKeyResolver + EncryptedKey KeyInfoCredentialResolver 
5478 [main] ERROR org.opensaml.saml2.encryption.Decrypter - SAML Decrypter encountered an error decrypting element content 
org.opensaml.xml.encryption.DecryptionException: Failed to decrypt EncryptedData 
    at org.opensaml.xml.encryption.Decrypter.decryptDataToDOM(Decrypter.java:524) 
    at org.opensaml.xml.encryption.Decrypter.decryptDataToList(Decrypter.java:440) 
    at org.opensaml.xml.encryption.Decrypter.decryptData(Decrypter.java:401) 
    at org.opensaml.saml2.encryption.Decrypter.decryptData(Decrypter.java:141) 
    at org.opensaml.saml2.encryption.Decrypter.decrypt(Decrypter.java:69) 
    at DecrypterTool.main(DecrypterTool.java:121) 

मुझे सच में नहीं पता कि मैं इस मामले में क्या गलत कर रहा हूं। मैंने अपनी निजी कुंजी को pkcs8 में परिवर्तित कर दिया, मैंने अपना एसएएमएल एक्सएमएल डेटा लोड किया और इसे वैध प्रकार (एन्क्रिप्टेडएस्सेशन) में अनारक्षित कर दिया और मैंने अपनी निजी कुंजी के आधार पर एक डिक्रिप्ट बनाया।

क्या यह संभव है कि यह आरएसए के लिए ओएपी प्रारूप से संबंधित है? मैं डिफ़ॉल्ट जावा क्रिप्टोग्राफी पुस्तकालय का उपयोग कर रहा हूँ।

धन्यवाद!

+0

मैं अपने सटीक समस्या पता नहीं है, लेकिन मैं अपने सिर टकरा था साथ काम करते हुए [टैग: saml] मैं 'अपाचे camel' का उपयोग करके महान आसानी पाया। – Shahzeb

+0

@Shahzeb मुझे कुछ और उपयोग करना अच्छा लगेगा, लेकिन मेरा ग्राहक सैम का उपयोग कर रहा है और मैं वास्तव में इसे बदल नहीं सकता।:( – thewalrusnp

उत्तर

17

आप में से उन लोगों के लिए जो इस समस्या को प्राप्त करेंगे, यह इस तथ्य से संबंधित था कि जावा क्रिप्टोग्राफी एक्सटेंशन (जेसीई) असीमित शक्ति क्षेत्राधिकार नीति फ़ाइलों को स्थापित नहीं किया गया था और यह मुझे एईएस-128 से बेहतर एन्क्रिप्शन का उपयोग नहीं करने दे रहा था। जेसीई नीति फ़ाइलों के साथ नीति फ़ाइलों को प्रतिस्थापित करना, मैं अपने एन्क्रिप्टेड दावे को सफलतापूर्वक डिक्रिप्ट करने में सक्षम था।

+1

माइंड साझा करना आप इस खोज पर कैसे पहुंचे? – Zoomzoom

2

@thwalrusnp से सहमत हैं। बस पॉलिसी जार डाउनलोड कर सकते हैं, जहां से सटीक स्थान जोड़ना चाहता था।

जावा रनटाइम एनवायरनमेंट के डिफ़ॉल्ट वितरण में क्रिप्टोग्राफी शक्ति की सीमा के कारण Error while decrypting assertion sent from IDP

यह तब होता है करने के लिए answer पर मिले।

  1. डाउनलोड जावा क्रिप्टोग्राफी एक्सटेंशन (JCE) असीमित शक्ति क्षेत्राधिकार नीति फ़ाइलें (for Java 7) (for Java 8)

  2. निकालें ज़िप संग्रह और वहाँ local_policy.jar और US_export_policy.jar पाते हैं।

  3. इन फ़ाइलों के अपने जेआरई संस्करण को $ JAVA_HOME/jre {version_number}/lib/security/डाउनलोड किए गए लोगों के साथ बदलें।

  4. किसी भी चल रहे जेआरई प्रक्रिया को पुनरारंभ करें। अब आप लंबी चाबियों का उपयोग कर सकते हैं।

+0

इसमें शामिल होने पर, ऐसा लगता है कि असीमित ताकत नीति फ़ाइलों को जावा 9 के साथ डिफ़ॉल्ट रूप से भेज दिया जाता है। –