2017-08-29 10 views
5

पर Asp.Net कोर 2 पर प्रमाण पत्र के साथ डेटा सुरक्षा कुंजी फ़ाइलों की सुरक्षा कैसे करें मैं डेटा सुरक्षा को कॉन्फ़िगर करने और कुंजी फ़ाइलों की सुरक्षा के लिए प्रमाणपत्र का उपयोग करने की कोशिश कर रहा हूं। यहाँ एमएस प्रलेखन Configuring data protectionडेबियन/लिनक्स

यहाँ मैं क्या करने की कोशिश कर रहा हूँ है:

services 
    .AddDataProtection() 
    .SetApplicationName("test server") 
    .PersistKeysToFileSystem("/home/www-data/config") 
    .ProtectKeysWithCertificate(
     new X509Certificate2("/home/www-data/config/"keyprotection.pfx); 

जब मैं आवेदन मैं स्टार्टअप पर निम्न त्रुटि प्राप्त लांच:

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58] 
    Creating key {71e2c23f-448b-49c9-984f-3c8d7227c904} with 
    creation date 2017-08-29 18:53:51Z, activation date 2017-08-29 18:53:51Z, and expiration date 2017-11-27 18:53:51Z. 
info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[39] 
    Writing data to file '/home/www-data/config/key-71e2c23f-448b-49c9-984f-3c8d7227c904.xml'. 
fail: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[24] 
    An exception occurred while processing the key element '<key id="71e2c23f-448b-49c9-984f-3c8d7227c904" version="1" />'. 
System.Security.Cryptography.CryptographicException: Unable to retrieve the decryption key. 
    at System.Security.Cryptography.Xml.EncryptedXml.GetDecryptionKey(EncryptedData encryptedData, String symmetricAlgorithmUri) 
    at System.Security.Cryptography.Xml.EncryptedXml.DecryptDocument() 
    at Microsoft.AspNetCore.DataProtection.XmlEncryption.EncryptedXmlDecryptor.Decrypt(XElement encryptedElement) 
    at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.DecryptElement(XElement element, IActivator activator) 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.DeserializeDescriptorFromKeyElement(XElement keyElement) 
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[12] 
    Key {71e2c23f-448b-49c9-984f-3c8d7227c904} is ineligible to be the default key because its CreateEncryptor method failed. 
System.Security.Cryptography.CryptographicException: Unable to retrieve the decryption key. 
    at System.Security.Cryptography.Xml.EncryptedXml.GetDecryptionKey(EncryptedData encryptedData, String symmetricAlgorithmUri) 
    at System.Security.Cryptography.Xml.EncryptedXml.DecryptDocument() 
    at Microsoft.AspNetCore.DataProtection.XmlEncryption.EncryptedXmlDecryptor.Decrypt(XElement encryptedElement) 
    at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.DecryptElement(XElement element, IActivator activator) 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.DeserializeDescriptorFromKeyElement(XElement keyElement) 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.DeferredKey.<>c__DisplayClass1_0.<GetLazyDescriptorDelegate>b__0() 
    at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode) 
    at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) 
    at System.Lazy`1.CreateValue() 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyBase.get_Descriptor() 
    at Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngGcmAuthenticatedEncryptorFactory.CreateEncryptorInstance(IKey key) 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyBase.CreateEncryptor() 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver.CanCreateAuthenticatedEncryptor(IKey key) 
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[12] 
    Key {71e2c23f-448b-49c9-984f-3c8d7227c904} is ineligible to be the default key because its CreateEncryptor method failed. 
System.Security.Cryptography.CryptographicException: Unable to retrieve the decryption key. 
    at System.Security.Cryptography.Xml.EncryptedXml.GetDecryptionKey(EncryptedData encryptedData, String symmetricAlgorithmUri) 
    at System.Security.Cryptography.Xml.EncryptedXml.DecryptDocument() 
    at Microsoft.AspNetCore.DataProtection.XmlEncryption.EncryptedXmlDecryptor.Decrypt(XElement encryptedElement) 
    at Microsoft.AspNetCore.DataProtection.XmlEncryption.XmlEncryptionExtensions.DecryptElement(XElement element, IActivator activator) 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.IInternalXmlKeyManager.DeserializeDescriptorFromKeyElement(XElement keyElement) 
    at Microsoft.AspNetCore.DataProtection.KeyManagement.DeferredKey.<>c__DisplayClass1_0.<GetLazyDescriptorDelegate>b__0() 
    at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode) 
--- End of stack trace from previous location where exception was thrown --- 

तो कुंजी बनाया गया है और अच्छी तरह से एन्क्रिप्टेड है। लेकिन ऐसा लगता है किसी भी तरह यह है कि यह कैसे डिक्रिप्ट करने के लिए त्रुटि में के रूप में यह कहते हैं पता नहीं है कि:

System.Security.Cryptography.CryptographicException: 
    Unable to retrieve the decryption key. 

अगर मैं इसे सही ढंग से समझते हैं, यह प्रमाण पत्र मैं कुंजी एन्क्रिप्ट करने के लिए प्रदान करता है। लेकिन ऐसा लगता है कि यह किसी कारण से डिक्रिप्शन के लिए एक ही प्रमाण का उपयोग नहीं करता है (ऐसा लगता है कि यह कहीं और [स्टोर?] से इसे पुनः प्राप्त करने का प्रयास करता है)।

क्या गलत हो रहा है?

मैं भी यहां बताए अनुसार सीए की दुकान में प्रमाणपत्र डालने की कोशिश की: Create a Self-Signed Certificate and trust it on Ubuntu Linux

तब मैं उन्हें इस तरह कोड से वापस पता लगाने की कोशिश:

var cert = new CertificateResolver().ResolveCertificate(CertThumbprint); 

लेकिन यह काम नहीं किया (यह इसे नहीं मिल सकता है)।

मैं भी कोशिश की निम्नलिखित दृष्टिकोण का उपयोग कर उन्हें खोजने के लिए करने की कोशिश की:

var store = new X509Store(StoreName.CertificateAuthority, 
    StoreLocation.LocalMachine); 

store.Open(OpenFlags.ReadOnly); 

var collection = store.Certificates.Find(
    X509FindType.FindByThumbprint, 
    CertThumbprint, false); 

store.Close(); 

var x509Cert = collection.Count > 0 ? collection[0] : null; 

लेकिन यह न तो काम नहीं किया।

तो सही तरीका क्या है?

उत्तर

2

केवल माइक्रोसॉफ्ट के लिए जाना जाता कारणों के लिए, ProtectKeysWithCertificate ओवरराइड कि वास्तविक प्रमाण पत्र (PFX फाइल या X509Certificate2 वस्तुओं) को स्वीकार केवल एन्क्रिप्ट DPAPI डेटा में सक्षम हैं। डिक्रिप्शन केवल तभी काम करता है जब एक ही सर्टिफिकेट मशीन के सर्टिफिकेट स्टोर में संग्रहीत होता है, जो उनको अपेक्षाकृत व्यर्थ बनाता है।

क्यों? कौन जाने। यह विशेष रूप से उपयोगी जानकारी नहीं है, लेकिन यह "अंतर्निहित ढांचे की सीमा" के रूप में here को अस्पष्ट रूप से खारिज कर दिया गया है।

this संबंधित चर्चा (जो कि किसी भी Microsoft सहायता या सगाई के बिना बंद था), उपयोगकर्ता एक कस्टम दृढ़ता वर्ग साझा करता है जो इस रहस्यमय "सीमा" को प्रभावित नहीं करता है। नीचे गिटहब रेपो लिंक, मुझे पता है कि यह एक पुराना सवाल है, लेकिन शायद यह किसी और की मदद करेगा।

https://github.com/tillig/DataProtection

अद्यतन: यह आगामी कोर में तय हो जाएगा 2.1.0 रिलीज: https://github.com/aspnet/Home/issues/2759#issuecomment-367157751