Private key of certificate in certificate-store not readable

前端 未结 1 421
一整个雨季
一整个雨季 2020-12-30 08:47

I think I\'ve got the same issue like this guy, but I wasn\'t as lucky as him/her since the solution provided doesn\'t work for me.

The solution provided looks for f

相关标签:
1条回答
  • 2020-12-30 09:14

    I got a solution by googleing up nearly everything ... as I understand there are some things to do:

    1. Generate a X509Certificate2
    2. Make sure the private key container is persistent (not temporary)
    3. Make sure to have acccess rules for authenticated users, so they can see the private key

    So the final code a came up with is the following:

    X509Certificate2 nonPersistentCert = CreateACertSomehow();
    
    // this is only required since there's no constructor for X509Certificate2 that uses X509KeyStorageFlags but a password
    // so we create a tmp password, which is not reqired to be secure since it's only used in memory
    // and the private key will be included (plain) in the final cert anyway
    const string TMP_PFX_PASSWORD = "password";
    
    // create a pfx in memory ...
    byte[] nonPersistentCertPfxBytes = nonPersistentCert.Export(X509ContentType.Pfx, TMP_PFX_PASSWORD);
    
    // ... to get an X509Certificate2 object with the X509KeyStorageFlags.PersistKeySet flag set
    X509Certificate2 serverCert = new X509Certificate2(nonPersistentCertPfxBytes, TMP_PFX_PASSWORD,
        X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); // use X509KeyStorageFlags.Exportable only if you want the private key to tbe exportable
    
    // get the private key, which currently only the SYSTEM-User has access to
    RSACryptoServiceProvider systemUserOnlyReadablePrivateKey = serverCert.PrivateKey as RSACryptoServiceProvider;
    
    // create cspParameters
    CspParameters cspParameters = new CspParameters(systemUserOnlyReadablePrivateKey.CspKeyContainerInfo.ProviderType, 
        systemUserOnlyReadablePrivateKey.CspKeyContainerInfo.ProviderName, 
        systemUserOnlyReadablePrivateKey.CspKeyContainerInfo.KeyContainerName)
    {
        // CspProviderFlags.UseArchivableKey means the key is exportable, if you don't want that use CspProviderFlags.UseExistingKey instead
        Flags = CspProviderFlags.UseMachineKeyStore | CspProviderFlags.UseArchivableKey,
        CryptoKeySecurity = systemUserOnlyReadablePrivateKey.CspKeyContainerInfo.CryptoKeySecurity
    };
    
    // add the access rules
    cspParameters.CryptoKeySecurity.AddAccessRule(new CryptoKeyAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), CryptoKeyRights.GenericRead, AccessControlType.Allow));
    
    // create a new RSACryptoServiceProvider from the cspParameters and assign that as the private key
    RSACryptoServiceProvider allUsersReadablePrivateKey = new RSACryptoServiceProvider(cspParameters);
    serverCert.PrivateKey = allUsersReadablePrivateKey;
    
    // finally place it into the cert store
    X509Store rootStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    rootStore.Open(OpenFlags.ReadWrite);
    rootStore.Add(serverCert);
    rootStore.Close();
    
    // :)
    
    0 讨论(0)
提交回复
热议问题