
{"id":2081,"date":"2016-08-10T15:41:13","date_gmt":"2016-08-10T19:41:13","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=2081"},"modified":"2016-08-10T15:41:13","modified_gmt":"2016-08-10T19:41:13","slug":"amusing-cryptography-on-windows","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2081","title":{"rendered":"Amusing Cryptography on Windows"},"content":{"rendered":"<p>Amusing fact #1: .NET framework does not have built-in class to load RSA private key from PKCS#1 (PEM) representation.<br \/>\nI borrowed this: http:\/\/www.codeproject.com\/Articles\/162194\/Certificates-to-DB-and-Back<\/p>\n<p>Amusing fact #2: creating an RSA key requires file system access. To the user profile. If you are running under an ASP.NET app pool user that has no profile, you get this:<\/p>\n<p><code><\/p>\n<pre>\nSystem.Security.Cryptography.CryptographicException: The system cannot find the file specified.\n\n   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)\n   at System.Security.Cryptography.Utils._CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv)\n   at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)\n   at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)\n   at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()\n   at {my method}\n<\/pre>\n<p><\/code><\/p>\n<p>To fix, one must either create the user profile, or use <code>CspProviderFlags.UseMachineKeyStore<\/code> when creating <code>RSACryptoServiceProvider<\/code>. If you choose the latter, make sure the user has write access to <code>C:\\ProgramData\\Microsoft\\Crypto\\RSA\\MachineKeys<\/code>. I believe <code>%AllUsersProfile%\\Microsoft\\Crypto\\RSA\\MachineKeys<\/code> should be used to accommodate customized or non-English systems.<\/p>\n<p>If you use machine storage, but the user does not have write access to the above folder, you&#8217;ll get<br \/>\n<code>System.Security.Cryptography.CryptographicException: Access denied.<\/code><\/p>\n<p>I am not certain which exact calls raises the exception: first Microsoft function on the call stack is <code>GetKeyPair()<\/code>, but I am not calling it: the actual culprit must be inlined. I suspect it is either <code>new RSACryptoServiceProvider(CspParameters)<\/code> or <code>RSACryptoServiceProvider.ImportParameters(RsaParameterTraits)<\/code>, I did not have time to investigate which one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Amusing fact #1: .NET framework does not have built-in class to load RSA private key from PKCS#1 (PEM) representation. I borrowed this: http:\/\/www.codeproject.com\/Articles\/162194\/Certificates-to-DB-and-Back Amusing fact #2: creating an RSA key <a href=\"https:\/\/ikriv.com\/blog\/?p=2081\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-2081","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2081"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2081\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}