<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Surajkumar Gunjal on Medium]]></title>
        <description><![CDATA[Stories by Surajkumar Gunjal on Medium]]></description>
        <link>https://medium.com/@suraj0208?source=rss-8471074b6239------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aQE5W3QuQK3e4K-LYD6csQ.jpeg</url>
            <title>Stories by Surajkumar Gunjal on Medium</title>
            <link>https://medium.com/@suraj0208?source=rss-8471074b6239------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:47:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suraj0208/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Updating ACLs of the private key of the certificate in Windows certificate store]]></title>
            <link>https://medium.com/@suraj0208/updating-acls-of-the-private-key-of-the-certificate-in-windows-certificate-store-2f83bbad827e?source=rss-8471074b6239------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f83bbad827e</guid>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[windows-security]]></category>
            <category><![CDATA[windows]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Surajkumar Gunjal]]></dc:creator>
            <pubDate>Wed, 20 Oct 2021 11:46:35 GMT</pubDate>
            <atom:updated>2021-10-21T14:40:34.353Z</atom:updated>
            <content:encoded><![CDATA[<p>Some applications may have different services/processes running in different user accounts. All processes of the application may need access to the private key in Windows certificate store. In that case in which user’s certificate store you keep the certificate ?</p><p>One option is to deploy certificate in all certificate stores, i.e. the <em>Current User </em>store of each user. <br>If your application deploys its own certificate somehow, the application needs to add the certificate to each store. Other option is to ask user to import the certificate in all certificate stores.</p><p>What if your services are running is shared account like Local Service or Network Service? If you import it in the corresponding user account store, the certificate and private key will be accessible to all the processes running in that account. This doesn’t sound secure.</p><p>We can fix this by using Windows service accounts.<br>We will keep our certificate in the Local Machine store. Certificates in Local Machine store can be accessed by any user on the system but not the private key.<br>We can allow only our application services access to the private key. Each service has it’s own service account what that means is even if your service is running in Local Service / Network Service account, it has it’s own service account and hence it’s own security context.</p><p>You can find more info about service accounts <a href="https://docs.microsoft.com/en-us/windows/security/identity-protection/access-control/service-accounts">here</a>.</p><p>As each service account has it’s own security context, it has it’s own SID ( security identifier ). The service account SID begins with S-1–5–80-. We will allow only our service SIDs access to the key. Not Local Service/Network Service account.</p><ol><li>When the service is started we need to tell Windows to put it’s own SID in it’s process token. We do that by setting the SID type of service to unrestricted. Refer <a href="https://docs.microsoft.com/en-us/windows/win32/api/winsvc/ns-winsvc-service_sid_info">this</a> for more info.<br>Use this command to set the sid type to unrestricted and restart the service:<em><br>sc sidtype “service name” unrestricted</em></li><li>Next task is to set ACLs on private key so that the services have permissions to access the key.<br>Right click on a certificate inside Local Machine store ( open certlm.msc via run ) then<br>Click all tasks -&gt; manage private keys. Here you can add / remove access to user accounts including service accounts.<br>To add ACLs for service account type: NT Service\&lt;your service name&gt; while selecting user account.</li></ol><p>Note that all <a href="https://en.wikipedia.org/wiki/Cryptographic_Service_Provider">CSPs </a>don’t support security descriptors ( setting ACLs on the private key )</p><h3>Programmatically updating the ACLs of the private key</h3><p>Since our certificate is in Local Machine store, only Administrators can update the ACLs.</p><p>There are two APIs <em>CryptSetProvParam </em>and <em>NCryptSetProperty </em>which can be used to set the Security descriptor on the private key depending on the CSP.<br>Read more about CSPs <a href="https://en.wikipedia.org/wiki/Cryptographic_Service_Provider">here</a>.</p><p>We cannot use the usual file system APIs to set security descriptor as the implementation / location of the private key is abstracted by the CSP.</p><p>The API you need to use depends on the type of the CSP. If you have a legacy CSP, you need to use the <a href="https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptsetprovparam"><em>CryptSetProvParam </em></a>API, if you have a CNG CSP you need to use <a href="https://docs.microsoft.com/en-us/windows/win32/api/ncrypt/nf-ncrypt-ncryptsetproperty"><em>NCryptSetProperty </em></a>API.</p><p>We can get a any type of handle to the key irrespective of your provider using <a href="https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptacquirecertificateprivatekey"><em>CryptAcquireCertificatePrivateKey </em></a>but the APIs such as <em>CryptSetProvParam</em> and <em>NCryptSetProperty </em>don’t work well with translated handles. So the rule is to acquire the same type of handle as your CSP.</p><ol><li><strong>Determine the type of CSP:</strong></li></ol><pre>DWORD c_prov_info = 0;<br>if (!CerGetCertificateContextProperty(context,<br>                                      CERT_KEY_PROV_INFO_PROP_ID,<br>                                      NULL,<br>                                      &amp;c_prov_info))<br>{<br>    return GetLastError();<br>}</pre><pre>PCRYPT_KEY_PROV_INFO prov_info = (PCRYPT_KEY_PROV_INFO) LocalAlloc(LMEM_ZEROINIT, c_prov_info);</pre><pre>if (!CerGetCertificateContextProperty(context,<br>                                      CERT_KEY_PROV_INFO_PROP_ID,<br>                                      prov_info,<br>                                      &amp;c_prov_info))<br>{<br>    LocalFree(prov_info);<br>    return GetLastError();<br>}<br>bool is_legacy_provider = false;<br>bool is_cng_provider = false;</pre><pre>if (prov_info-&gt;dwProvType == 0)<br>{<br>    is_cng_provider = true<br>}<br>else<br>{<br>    is_legacy_provider = true;<br>}<br>LocalFree(prov_info);</pre><p><strong>2. Checking if the CSP supports security descriptors:</strong></p><ol><li>If CSP is CNG, use <em>NCRYPT_PROVIDER_HANDLE_PROPERTY </em>constant to get the provider handle from the certificate context using the API <em>NCryptGetProperty</em>.<br>If you have the provider handle obtained from some other way that should work too. <br>Use the <em>NCRYPT_SECURITY_DESCR_SUPPORT_PROPERTY </em>constant to know if the security descriptors are supported by passing the CSP handle ( not the key handle ) to <em>NCryptGetProperty API. <br></em>If the property is 1, security descriptors are supported by the CSP, if it is 0 or the API fails with error <em>NTE_NOT_SUPPORTED</em>, security descriptors are not supported by the CSP.</li><li>If the CSP is legacy, there is no clear documentation by Microsoft about the security descriptor support. So we wouldn’t know if security descriptors are supported unless we try getting/setting one. A non-standard way to determine the support in this case will be reading the security descriptor and trying to set the same descriptor again on the key, using the method stated below.</li></ol><p><strong>3. Setting the security descriptors:</strong></p><p>Once we have identified the CSP type and we know security descriptors are supported / might be supported, we need to acquire the right handle to the key using API <em>CryptAcquireCertificatePrivateKey </em>before setting the security descriptors. Pass the correct flags to this API:</p><ul><li>If your CSP is CNG use the flag <em>CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG</em></li><li>If your CSP is legacy use the flag <em>CRYPT_ACQUIRE_ALLOW_NCRYPT_KEY_FLAG.</em></li></ul><p>Building security descriptor for the private key is like building security descriptor for any file system object. Except the supported access type depends on the CSP. Generally providers support <em>GENERIC_ALL </em>and <em>GENERIC_READ </em>access types.</p><p>Once we’ve the security descriptor</p><ul><li>Call <em>CryptSetProvParam </em>if you have legacy key.</li><li>Call <em>NCryptSetProperty </em>if you have CNG key.<br>Use the API GetSecurityDescriptorLength to get the length of security descriptor to pass it as input to <em>NCryptSetProperty.</em></li></ul><p>Now the services will be able to access the private key of the certificate which is in Local Machine store.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f83bbad827e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting ACLs on files with reparse points: symbolic links, junctions and NTFS mounted folders.]]></title>
            <link>https://medium.com/@suraj0208/setting-acls-on-reparse-points-symbolic-links-junctions-and-ntfs-mounted-folders-93f60e1dda6f?source=rss-8471074b6239------2</link>
            <guid isPermaLink="false">https://medium.com/p/93f60e1dda6f</guid>
            <category><![CDATA[hard-link]]></category>
            <category><![CDATA[windows]]></category>
            <category><![CDATA[symbolic-link]]></category>
            <category><![CDATA[ntfs]]></category>
            <category><![CDATA[windows-api]]></category>
            <dc:creator><![CDATA[Surajkumar Gunjal]]></dc:creator>
            <pubDate>Tue, 19 Oct 2021 18:52:28 GMT</pubDate>
            <atom:updated>2021-10-29T11:39:43.652Z</atom:updated>
            <content:encoded><![CDATA[<p>The Windows API SetNamedSecurityInfo is used to set ACLs on a file or directory. Windows supports inheritable ACLs, that means any ACLs set on a container ( like a directory ) can be automatically inherited by the containing objects ( sub directories or files ). This feature is very useful while dealing with large set of files, while creating file system objects on the fly etc.</p><p>SetNamedSecurityInfo when called on a directory symbolic link / junction /NTFS mounted folder, the API sets ACLs only on the file (link / junction / mounted folder), not it’s target. SetNamedSecurityInfo doesn’t provide a way to set permissions on the target of such files. So even if try to set inheritable ACLs on a link, the inheritable ACLs are not set on the target directory and hence it’s sub directories and files. Note that the link points to the directory and does not contain the directory.</p><p>To set the ACLs on the target of the link, we first need to get the target path from the link we have. But let’s discuss few concepts before we do that.</p><p><strong>What is a reparse point ?</strong></p><p>A file or directory can contain a reparse point. It is nothing but user-defined data structure that your application can interpret, the application itself will be storing it. There’s a file system filter, which you have to install to interpret the data. The reparse point has a reparse tag, which uniquely identifies the data it is storing. When the file system opens a file with a reparse point, it attempts to find the file system filter associated with the data format identified by the reparse tag. There are few standard reparse tags used by Microsoft. IsReparseTagMicrosoft macro can be used to determine if the reparse tag is by Microsoft.<br>NTFS implements symbolic links, junctions and mounted folders using reparse points. Every reparse point doesn’t have to be a symbolic link / junction / mounted folder but these objects are reparse points.<br><a href="https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/b41f1cbf-10df-4a47-98d4-1c52a833d913">Here</a> is example of Microsoft reparse point for symbolic links.</p><p><strong>What is a symbolic link?</strong></p><p>A symbolic link is essentially a pointer to a file or directory located elsewhere. It consumes little space and is very fast to create. You can use mklink command to create a symbolic link to a file. The /D option can be used to create symbolic link to a directory. We can have only symbolic links to directories not hard links.</p><p><strong>What is a junction?</strong></p><p>junctions and symbolic links are same as long as you’re not looking at the remote server.<br><a href="https://superuser.com/questions/343074/directory-junction-vs-directory-symbolic-link">This</a> article has a good explanation about the difference between the two.</p><p><strong>What is NTFS mounted folder?</strong></p><p>A mounted folder is an association between a volume and a directory on another volume. When a mounted folder is created, users and applications can access the target volume by using the path to the mounted folder.<br><a href="https://www.windowscentral.com/how-mount-hard-drive-folder-windows-10">This</a> article shows how to mount drive as a folder.</p><p>Now, back to SetNamedSecurityInfo.</p><p>First step is to detect if we have a symbolic link / junction /mounted folder.</p><pre>DWORD attributes = GetFileAttributes(_T(&quot;C:\\test_mount_pt&quot;));<br>      <br>if (attributes &amp; FILE_ATTRIBUTE_REPARSE_POINT)<br>{<br>        std::cout &lt;&lt; &quot;It is a reparse point&quot; &lt;&lt; std::endl;<br>}</pre><p>Now we have to determine if it’s really a link/junction or mounted folder and not other reparse point.</p><pre>WIN32_FIND_DATA data = { 0 };<br>HANDLE handle = FindFirstFile(_T(&quot;C:\\test_mount_pt&quot;), &amp;data);<br>bool is_mounted_folder = data.dwReserved0  == IO_REPARSE_TAG_MOUNT_POINT;<br>bool is_symlink = data.dwReserved0  == IO_REPARSE_TAG_SYMLINK</pre><p>Once we know it is a reparse point and is a link or a junction or a mounted folder, We can get the target path.</p><pre>// This API will fail if the user running the command does not have SE_BACKUP_NAME privileges.<br>HANDLE directory_handle = CreateFile(_T(&quot;C:\\test_mount_pt&quot;,<br>                                     GENERIC_READ,<br>                                     FILE_SHARE_READ,<br>                                     NULL  /* security descriptor */,<br>                                     OPEN_EXISTING,<br>                                     FILE_FLAG_BACKUP_SEMANTICS,<br>                                     NULL  /* template handle */);<br>if (directory_handle == INVALID_HANDLE_VALUE)<br>{<br>    return GetLastError();<br>}<br>DWORD required_buffer_size = GetFinalPathNameByHandle(directory_handle, NULL, 0, FILE_NAME_NORMALIZED);<br>if (required_buffer_size == 0)<br>{<br>    CloseHandle(directory_handle);<br>    return GetLastError();<br>}<br>required_buffer_size++;<br>std::vector&lt;TCHAR&gt; target_path(required_buffer_size);<br>required_buffer_size <br>    = GetFinalPathNameByHandle(directory_handle,<br>                               &amp;target_path[0],<br>                               required_buffer_size,<br>                               FILE_NAME_NORMALIZED);<br>if (required_buffer_size == 0)<br>{<br>    CloseHandle(directory_handle);<br>    return GetLastError();<br>}<br>CloseHandle(directory_handle);</pre><p>If we have links like A-&gt;B-&gt;C-&gt;D-&gt;&lt;target directory&gt; then the API GetFinalPathNameByHandle will return the the target path directly.</p><p>To traverse the symbolic links one by one you will need file system APIs / structures.</p><pre>HANDLE link_handle = CreateFile(_T(&quot;C:\\test_mount_pt&quot;),<br>                                0,<br>                                FILE_SHARE_READ,<br>                                0,<br>                                OPEN_EXISTING,<br>                                FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,<br>                                0);<br>if (link_handle == INVALID_HANDLE_VALUE)<br>{<br>    return GetLastError();<br>}<br>BYTE buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE] = { 0 };<br>DWORD size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;<br>PREPARSE_DATA_BUFFER reparsedataBuffer = (PREPARSE_DATA_BUFFER)buffer;<br>BOOL ret = DeviceIoControl(link_handle,<br>                           FSCTL_GET_REPARSE_POINT,<br>                           NULL,<br>                           0,<br>                           buffer,<br>                           size,<br>                           &amp;size,<br>                           NULL);<br>TCHAR* full_path = reparsedataBuffer-&gt;SymbolicLinkReparseBuffer.PathBuffer;</pre><p><em>reparsedataBuffer-&gt;SymbolicLinkReparseBuffer.PathBuffer</em> will have the path to next link. We can continue to parse the reparse the reparse data buffer in this way till we reach an actual directory. Maybe call SetNamedSecurityInfo on each such file we encounter along the way.</p><p>Note that this will not cover the scenario where your target path contains symlinks in subdirectories or inside itself. To handle this, you need use TreeSetNamedSecurityInfo API. For each file you encounter, you need to check if it is a symbolic link and repeat the process again.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93f60e1dda6f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>