Write P7M file Cades compliant - c#

I'm working with my smartcard and x509 cert through Net.Pkcs11.dll component.
I'm able to sign a file, but I don't know how can I create a P7M structure.
I've already seen many examples (BouncyCastle and so on) but all of theme use digital certificate with private key, while my private key is protected by smartcard.
Can you help me?

Related

Getting private key from ECC certificate

How can I get the private key from a certificate I imported with a .p12 file (contains private key too)? I am using .net
I have tried loading the certificate from the store and use .PrivateKey that is only for RSA and DSA because I get an error.
My certificate is ECC which I cannot find much information about. I tried using
certificate.GetECDsaPrivateKey()
but it returns null? What can I do to get the private key from the cert store? I need this private key to form a shared secret with a public key I have. Any help is appreciated. Thanks
edit: i converted the p12 into a .pem and can see that there is a private key in there

How to validate a signature using just the public key of the signing authority? C#

I have an encrypted XML which is signed using the private key of a signing certificate. I have decrypted the XML on another machine, revealing the public key of the same signing cert within it. Before I process the data that I have unwrapped, I need to validate the signature on the encrypted XML. How do I do this using just the public key of the signing cert that was used to sign it in the first place?
I am working with C# for this application.

How to encrypt/decrypt XMl wiith X.509 certificate correctly?

I want to encrypt a XML file by using a X.509 certificate and also decrypt it, too. As far as I know I need to use the public key (inside the certificate) to encrypt the XML and the private key to decrypt the XML. Thus only the guy with private key is able to read the decrypted data.
Microsoft provides some code for encryption/decryption here:
Encryption: https://msdn.microsoft.com/en-us/library/ms229744(v=vs.110).aspx
Decryption: https://msdn.microsoft.com/en-us/library/ms229943(v=vs.110).aspx
As you can see from the first example a X.509 certificate will be loaded to encrypt the file. But the second example does not(!) load a certificate to decrypt the example. It seems that the encrypted file holds all the necessary data to be decrypted? Does this mean that the file can be decrypted by anybody? I think I have a lag of understanding here - why is it not necessary to use a certificate to decrypt the data?
Regards,
Michael
On decryption, the certificate is loaded from the computers certificate store. From your second link:
The code example in this procedure decrypts an XML element using an X.509 certificate from the local certificate store of the current user account.
In that example, the public key used to encrypt the XML is stored in the encrypted data and is used to look up the proper certificate from the store.
So the answer to your question -- "why is not necessary to use a certificate to decrypt the data" -- is: it is necessary. The certificate was loaded automatically.

Using a X509 certificate for decryption

I have some data of an X509v3 certificate that is used at a central licensing station. My question is is the following amount of information enough for me to decrypt data using C# code? And additionally, how are the certificate properties imported into a project? Do I have to create a certificate file in order to go on?
Known to me are:
Subject
Serial Number
Issuer
"root-ca"
Public Key Algorithm: "rsaEncryption"
RSA Modulus, 128 bytes
RSA Public Key Exponent
X509v3 Extended Key Usage: "critical"
Signature Algorithm: "md5WithRSAEncryption", followed by 256 untitled bytes
SHA1 Fingerprint
I do not have any certificate file. Excuse me if a similar question has already been answered, unfortunately I wasn't able to find one like mine.
No, your data is not enough. First of all, this is all public data. It doesn't contain a private key. A private key is used for decryption or signature generation. A public key is used for encryption and signature verification.
The .NET API is peculiar in that you can seemingly use a certificate to decrypt. This is not really the case; the certificate and private key pair are seen as one; only if the private key is included then you can actually decrypt. Personally I see this as a minor design mistake.
In principle you could create a certificate given the information. Basically you would have to generate a certificate with the same information and then replace the issuer and signature fields.
This is however not for the weak of heart; I recommend a few years of experience before you even try. If any information is missing from the list above you won't get a valid certificate / signature, and you won't get any warning what is wrong, just a failure. You've got one advantage though; if the signature verifies or fingerprint is identical to the one you've got then you know that you've succeeded.
You would not be able to decrypt of course; the private key would still be missing.
Note that the signature is the 256 untitled bytes.
This information is not enough. This data is a public key to encrypt data.
RSAParameters

Client certs without using the keystore

I'm trying to figure out if there is any way to have a .NET client use a client certificate without involving the Windows keystore in any way.
I have a code snippet like this:
test1.Service s = new test1.Service();
X509Certificate c = X509Certificate.CreateFromCertFile(#"C:\test.pem");
s.ClientCertificates.Add(c);
int result = s.TestMethod();
However, this only works if I also have the certificate referenced in "test.pem" installed in my certificate store. I assume this is because the key is necessary for the negotiation. If I don't have the cert/key in the store, the cert is not sent with the request.
What I want to do is be able to provide both the certificate and private key from a file or files and not involve the Windows certificate store in any way.
Is there any way to do this?
I'm going to post an answer to my own post, but will leave it open to see if others can solve it different.
Basically, I'm punting. If you have a PKCS12 file (with both key and cert in it) you can use the X509Certificate2 class to load that file and use it, and it won't interrogate the keystore.
I could not find a way to do this with flat PEM key and cert files (like what you'd need with Python, for example).

Categories

Resources