Why use CNG instead of CryptoAPI to store keys (2024)

In the last posts, I talked a bit about usingCryptoAPI andCryptoNG (CNG) to manage encryption keys, and how using CNGsometimes requires some extra work. That begs the question: is that extra work justified? If CryptoAPI is easier to use,why not just use that?

Obviously, one good reason to prefer CNG is that CryptoAPI is deprecated.

Another reason has to do with how CNG and CryptoAPI protect encryption keys: The two APIs have a very differentarchitecture and this difference has a profound impact on how difficult (or easy) it is for a bad actor to access those keys.

To see what that means in practice, let’s use Process Monitorto see how CryptoAPI and CNG store their keys.

Tracing CryptoAPI key access

First, we need a key and store it using CryptoAPI. An easy way to do this is to create a new self-signed certificate and usethe Microsoft Base Cryptographic Provider v1.0, which is one of the CryptoAPIcryptographic service providers.

New-SelfSignedCertificate ` -KeyUsage DigitalSignature ` -FriendlyName "Sample CryptoAPI key" ` -KeyExportPolicy NonExportable ` -Subject "Sample CryptoAPI key" ` -CertStoreLocation "cert:\CurrentUser\My" ` -Provider "Microsoft Base Cryptographic Provider v1.0" ` -KeySpec Signature

CryptoAPI stores RSA keysin %APPDATA%\Microsoft\Crypto\RSA\[User-SID]\, but these files are encrypted, so they need to be read via CryptoAPI.

To see how CryptoAPI accesses the stored keys, let’s use Process Monitor to see when and how it accesses files in%APPDATA%\Microsoft\Crypto\RSA\[User-SID]\. To trigger a key access, we use a little .NET test program(CryptoApiNetFx.exe) that uses our self-signed certificate to sign a piece of data.

As expected, we see several CreateFile calls that access files in %APPDATA%\Microsoft\Crypto\RSA\[User-SID]\.The calls originate from CryptoApiNetFx.exe and if we look at the stack trace, we can see how the CLR (clr.dll)calls into CryptoAPI (cryptsp.dll, rsaenh.dll) which then makes a system call to access the file:

This stack trace isn’t particularly exciting – but the key thing to notice is: Our process directly accesses the stored keys.As a result, the private key ends up beingloaded into our address space. If the key is marked as non-exportable, CryptoAPI won’t allow us to access the privatekey – but because it’s in our process address space, we can access it anywayas explained in this whitepaper.

The takeaway here is that CryptoAPI doesn’t provide particularly strong protections for stored keys: For a bad actorto exfiltrate a user’s private key, all they need to do is run a process as that user and extract the private key from memory.

Let’s see how CNG compares.

Tracing CNG key access

First, we create another self-signed certificate, this time using the Microsoft Software Key Storage Provider whichis a CNG key storage provider:

New-SelfSignedCertificate ` -KeyUsage DigitalSignature ` -FriendlyName "Sample CNG key" ` -Subject "Sample CNG key" ` -KeyExportPolicy NonExportable ` -CertStoreLocation "cert:\CurrentUser\My" ` -Provider "Microsoft Software Key Storage Provider" ` -KeyAlgorithm RSA ` -KeyLength 2048

The Microsoft Software Key Storage Provider stores its keysin %APPDATA%\Microsoft\Crypto\Keys. Following the same approach as before, let’s use a test program andProcess Monitor to see how our key is being accessed.

First thing to notice in this Process Monitor trace is that the file isn’t accessed by CryptoApiNetFx.exe, but by LSASS.If we look at the stack trace, we can also notice that the file accesses are made by keyiso.dll, which is servicing an LRPC (rpcrt4.dll):

keyiso.dll is the CNG key isolation service:

The CNG key isolation service is hosted in the LSA process. The service provides key process isolation to private keys and associated cryptographic operations as required by the Common Criteria. The service stores and uses long-lived keys in a secure process complying with Common Criteria requirements.

To access a CNG key, our process has to make an LRPC call to LSASS – and that’s important: LSASS is not only adifferent process, it also runs as a different user – so there is a security boundary that separates our process(CryptoApiNetFx.exe) from the private key. If the CNG key is marked as non-exportable, LSASS won’t give us accessto it and it’ll never be loaded into the address space of our process.

For a bad actor to exfiltrate a private key from CNG, they have to compromise LSASS. That’s possible, butnot exactly easy as LSASS runs as SYSTEM.

LSA protection

If you hold the debug privilege,you can attach a debugger (or mimikatz) to LSASS and thus undermine the extra protection that CNG provides over CryptoAPI.To protect against this attack vector, Windows 8.1 introduced LSA protection,which sets up LSASS to run as Protected Process Lite (PPL):

[...] a Protected Process can be accessed by an unprotected process only with very limited privileges: PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SET_LIMITED_INFORMATION, PROCESS_TERMINATE and PROCESS_SUSPEND_RESUME. This set can even be reduced for some highly-sensitive processes.

LSA protection is off by default, but when enabled,it makes it significantly more difficult for a bad actor to compromise LSASS and extract private keys from CNGsince they first have to find a way to disable PPL.

Credential Guard

LSA protection makes it more difficult to compromise LSASS, but it’s still possible.To further raise the bar, Windows 10 therefore introduced Credential Guard:

Protected processes are a kernel construct; malware running in the kernel can modify the structures necessary to mark a process as protected and, well, unprotect them. […] That’s not strictly true anymore with the introduction of Hypervisor-protected code integrity (HVCI), which is specifically designed to protect the kernel against tampering.

Credential Guard uses virtualization-based security (VBS) and runs parts of LSASS (LsaIso) at a different trust level,which is separated from the rest of the kernel by the hypervisor. When Credential Guard is enabled, even LSASS can’taccess non-exportable private keys anymore.

Takeaway

Any software-based key store has its limitations, and if you’re really concerned about the safety of encryption keys,you might be better off with a hardware-based solution. But some software-based key stores clearly provide betterprotection than others. CNG goes through much greater lengths to protect encryption keys than CryptoAPI, and withLSA protection and Credential Guard, you can further strengthen these protections. Whenever we need to store encryptionkeys, we should therefore prefer CNG over CryptoAPI.

Related posts

  • Exporting RSA public keys in .NET and .NET Framework
  • Importing RSA public keys in downlevel .NET and .NET Framework versions
  • Best practices for using workload identity federation
  • Encoding public keys in PEM format
  • What's inside a PEM file

Any opinions expressed on this blog are Johannes' own. Refer to the respective vendor’s product documentation for authoritative information.

« Back to home
Why use CNG instead of CryptoAPI to store keys (2024)
Top Articles
Federal Student Aid
Fish Shimmies: What It Is and How To Treat It
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5867

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.