Advertisement
reidca

Find private key filename for X509Certificate2 (inc CNG)

Feb 28th, 2014
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using Security.Cryptography.X509Certificates; //uses CLR Security from https://clrsecurity.codeplex.com
  2.  
  3. //certificate is a standard  System.Security.Cryptography.X509Certificates.X509Certificate2
  4.  
  5. if (certificate.HasCngKey())
  6. {
  7.     System.Security.Cryptography.CngKey privateKey = certificate.GetCngPrivateKey();
  8.     bool privateKeyIsExportable = privateKey.ExportPolicy == CngExportPolicies.AllowExport;
  9.     string privateKeyFileName = FindPrivateKeyPath(privateKey.UniqueName);
  10. }
  11.  
  12.     private static string FindPrivateKeyPath(string keyFileName)
  13.         {
  14.  
  15.             IList<string> searchDirectories = new List<string>(new string[] { @"\Microsoft\Crypto\Keys", @"\Microsoft\Crypto\RSA\MachineKeys" });
  16.  
  17.             string keyDirectory = null;
  18.             foreach (string searchDirectory in searchDirectories)
  19.             {
  20.                 string machineKeyDir = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), searchDirectory);
  21.                 // If found
  22.                 if (Directory.GetFiles(machineKeyDir, keyFileName).Length > 0)
  23.                 {
  24.                     keyDirectory = machineKeyDir;
  25.                     break;
  26.                 }
  27.             }
  28.  
  29.             // Look up All User profile from environment variable
  30.             if (string.IsNullOrEmpty(keyDirectory))
  31.             {
  32.                 // Next try current user profile
  33.                 string userKeyDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Crypto\RSA\";
  34.  
  35.                 // for each sub keyDirectory
  36.                 foreach (string subDir in Directory.GetDirectories(userKeyDir))
  37.                 {
  38.                     // Seach the key file
  39.                     string[] fs = Directory.GetFiles(subDir, keyFileName);
  40.                     if (fs.Length == 0)
  41.                     {
  42.                         continue;
  43.                     }
  44.                     else
  45.                     {
  46.                         // found
  47.                         keyDirectory = subDir;
  48.                         break;
  49.                     }
  50.                 }
  51.             }
  52.  
  53.             if (string.IsNullOrEmpty(keyDirectory))
  54.             {
  55.                 return "Private key exists but is not accessible";
  56.             }
  57.  
  58.             StringBuilder sb = new StringBuilder();
  59.             sb.Append(keyDirectory);
  60.             sb.Append(Path.DirectorySeparatorChar);
  61.             sb.Append(keyFileName);
  62.             return sb.ToString();
  63.  
  64.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement