Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by dami » Thu, 12 Apr 2007 11:42:51


I would like to make a signed and then enveloped s/mime compatible
message using either CryptEncryptMessage or CryptMsgOpenToEncode
APIs. CryptMsgOpenToEncode is pretty low level and I understand that
pretty well. My questions are:

Is CryptEncryptMessage a higher level wrapper for doing this?
If so, how does it encrypt the message using multiple recipient keys
(certificates)? My understanding is that a single message is encrypted
to single public key. ??
Also, then CryptDecryptMessage doesn't seem to take any private to
decrypt the message. Am I missing something here.

Thanks in advance for the clarification,

D
 
 
 

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by Mitch Gall » Fri, 13 Apr 2007 00:07:30


Yes CryptEncryptMessage is a higher-level wrapper.
This is how it works:
- a cryptographically random symmetric key is created
- that symmetric key is used to bulk-encrypted the content data
- then, that symmetric key is RSA (typically) encrypted using the PUBLIC
key of EACH of the recipients, so if you have 3 recipients, then the same
symmetric key is RSA-encyrpted 3 different times with each recipient's
PUBLIC key.
- these 3 RSA encrypted blobs are included with the "enveloped" message
- the symmetric key (for decrypting the original content data) can be
decrypted by the PRIVATE key of any of the 3 recipients.

- Mitch

 
 
 

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by dami » Sat, 14 Apr 2007 15:28:54

On Apr 12, 12:07 am, "Mitch Gallant" < XXXX@XXXXX.COM >




Great. Thanks for the explanation. A few follow up questions:

1. So, if I want to sign the message, I would have to do this
separately?

2. These functions take certificates. I have public keys. How can I
transform the public keys to certificates?

3. Also, for CryptDecryptMessage, it seems as if it implicitly using a
private key of mine, how do I get the corresponding public key to
distribute to others?

Thanks in advance, D
 
 
 

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by Mitch Gall » Sat, 14 Apr 2007 20:44:33


There are some C examples in MSDN crypto area on Signing and Enveloping
messages. The CryptoAPI message functions mostly use keys that have
associated certificates (and hence reference stores for recipients etc..).
For example, to Envelope (encrypt to a number of recipients) a "hint" to the
recipients is included in the pkcs7 message produced by including the
recipients certificate issuer and serial number.
This page has a good description of the algorithm:
http://www.yqcomputer.com/
Do you have keypairs without associated certificates?

CryptDecryptMessage requires one or more cert stores be specified in the
pDecryptPara struct. The function internally searches the specified stores
for certs that match the recipients "hint" included in the enveloped message
blob and if a match is found attempts to de-envelope the message using the
private key associated with that "hint" certificate.

Do you mean redistrubuting just the public key? If you key has an associated
cert you should distribute the entire certificate obviously.
- Mitch
 
 
 

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by dami » Tue, 17 Apr 2007 17:44:02

n Apr 13, 8:44 pm, "Mitch Gallant" < XXXX@XXXXX.COM >
wrote:

Hi Mitch,

Once again, thank you for the help.


Yes, this exactly currently the case. We are still trying to figure
out what we want to do in regards to certificates and verification.
But until then, I have RSA base64 encoded public keys. How could I put
these into a format that I use withe these functions?

Thanks, D


 
 
 

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by Mitch Gall » Tue, 17 Apr 2007 20:21:02

lt; XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

You could wrap a temporary self-signed certificate around they keypair. You
can do this in different ways. One easy way if you know the keycontainer
name is to use makecert.exe and specify the keycontainer name using the -sk
switch. See MSDN docs for makecert.exe

Related to this, the following .NET utility exports public keys (in various
binary or PEM formats) to PKCS12 and if the keypair doesn't have an
associated certificate, creates a dummy one for export or raw keypairs in
pkcs12 format.
http://www.jensign.com/opensslkey

- MItch

- Mitch


 
 
 

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by dami » Wed, 18 Apr 2007 16:14:45

n Apr 16, 8:21 pm, "Mitch Gallant" < XXXX@XXXXX.COM >
wrote:

Ok. I think I'm getting it. Will makecert automatically add this to
the default system store? Is this where I want the certificate (on the
recipient side) for the eventual call to CryptDecryptMessage call?

Yes, that OpenSsl.cs program is quite handy. Unfortunately, I'm have
to stay in the unmanaged world.

Thanks, D

 
 
 

Questions about s/mime, CryptEncryptMessage & CryptMsgOpenToEncode

Post by Mitch Gall » Wed, 18 Apr 2007 20:38:38

lt; XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

Default store and store location for makecert are MY (Personal) store and
CurrentUser storelocation.
You can change those respectively with the ss and sr switches.
Certificates with associated private keys are almost always stored in the MY
store and that is where CryptDecryptMessage will look for certs used to
decrypt enveloped messages.
- Mitch