Skip to content

WIP: Add e_cng, a new engine that interfaces with MS CNG/Ncrypt and uses STORE#3481

Closed
levitte wants to merge 5 commits intoopenssl:masterfrom
levitte:store2-e_cng
Closed

WIP: Add e_cng, a new engine that interfaces with MS CNG/Ncrypt and uses STORE#3481
levitte wants to merge 5 commits intoopenssl:masterfrom
levitte:store2-e_cng

Conversation

@levitte
Copy link
Member

@levitte levitte commented May 16, 2017

This PR doesn't build on its own for now, it needs to be merged #2688 to build.

Checklist
  • documentation is added or updated
  • tests are added or updated

@levitte
Copy link
Member Author

levitte commented May 16, 2017

@snhenson, you might be interested in this

@levitte
Copy link
Member Author

levitte commented May 16, 2017

For those interested, I maintain a merge of this and #2688 here, and it gets built on my personal Travis CI

@mattcaswell
Copy link
Member

Awesome!

@levitte
Copy link
Member Author

levitte commented May 17, 2017

Maybe time to start reviewing the STORE project for real? (hint hint nudge nudge) ;-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tricky one. Formally there is no reason to link everything with ncrypt.lib. Well, of course in static build ncrypt.lib needs to be there for every .exe that pulls in e_cng.obj. But in shared build cng.dll would be the only one that needs to be linked with it. MSC has solution for that, and it's #pragma comment(lib,"ncrypt") that can be added directly to e_cng.c. And it's going to work out in both static and dynamic builds. [The pragma is tolerated by mingw compilers, but does nothing.] Another argument against adding it as above is that people still seem to be asking about compiling for legacy version, and there is no ncrypt.lib there. Even in such case above mentioned #pragma would do the trick, because it can be conditional...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be argued that crypt32.lib shouldn't be there with the argument that only capi.dll needs it as well, no? I was pondering another variant, to add language in build.info to specify extra external libraries to be used with specific programs or shared libraries. Would it be worth it? Maybe. For now, though, I'll use the pragma, thanks for pointing it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be argued that crypt32.lib shouldn't be there

I think so, yes. But there is difference. crypt32.lib is present in legacy SDKs, while ncrypt.lib is not. This is kind of the real concern. Having library on command line you don't link with has no side effects (on Windows), but lib-file needs to be present if you refer to in on command line irregardless. In other words it's availability of ncrypt.lib file in user's SDK that is concern.

engines/e_cng.c Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSDN says include ncrypt.h and I don't see no ncrypt.h. Well, nothing prevents wincrypt.h from including ncrypt.h, but then question would be if all SDKs do that... Maybe they all do, did you verify?

Copy link
Member Author

@levitte levitte May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have verified that wincrypt.h includes ncrypt.h indeed. The reason I do this is the lines following, where I detect if ncrypt is present or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have verified that wincrypt.h includes ncrypt.h indeed.

Question was if all of them do. But of course one can always say that if wincrypt.h doesn't include ncrypt.h, then we don't compile it. It would be appropriate to mention this in more verbose comment...

engines/e_cng.c Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest a little bit more verbose comment...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

engines/e_cng.c Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't follow the code thoroughly, but question is if CP_UTF8 would be more appropriate. Underlying question is if in can contain non-ASCII characters. Trouble is that CP_ACP can wipe some of them. I'd say that CP_UTF8 should be always preferred unless there is recognized reason to use CP_ACP.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used CP_ACP because e_capi.c does so. I was wondering, though, if that was the right choice. What can we expect from the command prompt these days?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used CP_ACP because e_capi.c does so.

It doesn't mean that it's right :-) I mean it surely needs an overhaul too.

What can we expect from the command prompt these days?

Command line options are UTF8-ised, and so should be console input. I'll double-check... And I'll have a look at e_capi.c. One can also put it this way. Let's aim for CP_UTF8, because it's the only one that should work, and fix remaining code paths. As opposite to trying to see what happens to work right now... Just to clarify, go for CP_UTF8 :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hear ya

@levitte levitte force-pushed the store2-e_cng branch 3 times, most recently from 3e2c984 to 0556a9b Compare May 18, 2017 06:58
@levitte
Copy link
Member Author

levitte commented May 18, 2017

From a STORE point of view, this is currently fairly complete. It enumerates key providers, cert stores, keys (I hope, I don't seem to have any) and certs (the last two given a provider or a store), and fetches keys (I hope) and certs (this I know for certain).

On to the key calculations part...

@levitte
Copy link
Member Author

levitte commented May 18, 2017

Also, fancier search capabilities will be added in not too long (I know @snhenson's wondering about that part)

@levitte levitte force-pushed the store2-e_cng branch 3 times, most recently from d7d841c to a34fd58 Compare May 26, 2017 21:14
@levitte levitte force-pushed the store2-e_cng branch 2 times, most recently from e6c67b6 to 8338528 Compare July 20, 2017 15:12
@adityadigumarti
Copy link

Any idea when store changes and the cng engine this will be merged?

@mattcaswell mattcaswell added this to the Post 1.1.1 milestone Jan 24, 2018
@levitte
Copy link
Member Author

levitte commented Jul 27, 2019

Any idea when store changes and the cng engine this will be merged?

Not really. Our major focus for 3.0 is replumbing and the FIPS module, so it's quite possible that this gets pushed to a later version, like 3.1.

goto err;
}
if (NCryptExportKey(khandle, 0, BCRYPT_PUBLIC_KEY_BLOB, NULL,
(PBYTE)exported, size, NULL, 0) != ERROR_SUCCESS) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing NULL to pcbResult at this point causes NCryptExportKey to fail with 0x800706F4 in my test case. Passing &size resolves that problem.

@levitte
Copy link
Member Author

levitte commented Apr 16, 2021

I had forgotten about this... right now, my thoughts are to throw it away and create a provider instead

return -1;
}

if (NCryptEncrypt(ctx->khandle, (PBYTE)from, flen, NULL, to, flen, &len,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I have seen, NCryptEncrypt will use the public key from the key handle instead of the private key, so NCryptDecrypt needs to be used here to perform the RSA operation with the private key.

NCryptDecrypt however probably can't be configured to add padding before the RSA operation with the private key as required here (RSA private enrypt) as it's designed to be used for decryption with the private key but not for signing/encryption and therefore removes the padding after the RSA operation (RSA private decrypt) instead of adding it before.
For that reason, NCryptDecrypt can only be used with RSA_NO_PADDING here, as with that RSA private encrypt and RSA private decrypt result in the same operation.

When OpenSSL uses RSA signing with PSS padding, this method is called with RSA_NO_PADDING as the padding has already be applied to the buffer before, so that is no problem in that case.

Client certificate authentication with TLS 1.2+ works in my test case, when NCryptDecrypt with RSA_NO_PADDING is used here (See #12859).

Please tell me if I am wrong.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is true that from my testing that when using TLSv1.3, the NCryptDecrypt should be used instead of NCryptEncrypt with the NCRYPT_NO_PADDING_FLAG as the padding of the buffer.

@t8m t8m modified the milestones: Post 1.1.1, Post 3.0.0 May 12, 2021
@t8m t8m added branch: master Applies to master branch triaged: feature The issue/pr requests/adds a feature labels May 12, 2021
@levitte
Copy link
Member Author

levitte commented Jul 2, 2021

I was informed just now that someone else has made a CNG engine: https://github.com/rticommunity/openssl-cng-engine
I'm closing this PR in its favor.

@levitte levitte closed this Jul 2, 2021
@levitte levitte deleted the store2-e_cng branch July 2, 2021 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

branch: master Applies to master branch triaged: feature The issue/pr requests/adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants