sign and verify using openssl library functions instead of command line utilities - c

I am currently framing the openssl commands as a string and using linux system call int system(const char *command); for signing and verifying certain files.
For generating sha i use below command.
openssl dgst -sha256 > test_sha256.txt
After generating test_sha256.txt, i will sign this with my CA , which gives me signature.p7s, from which i extract CRLs(asn1parse -inform DER and dd ) and combining CRLs with my root CA, then i verify using below command.
openssl cms -verify -inform DER -in signature.p7s -binary -content test_sha256.txt -CAfile combined_CA.pem -purpose any -signer signer_file.crt
How can we achieve the same thing using the openssl library calls? Do we have separate openssl functions to achieve sign & verify.

Related

what openssl function to call for openssl cmd in c?

https://testnet.binance.vision/
bottom of it has the following :
# Sign the request:
timestamp=$(date +%s000)
api_params_with_timestamp="$API_PARAMS&timestamp=$timestamp"
signature=$(echo -n "$api_params_with_timestamp" \
| openssl dgst -sha256 -sign "$PRIVATE_KEY_PATH" \
| openssl enc -base64 -A)
I like to do signature sha256 sign with openssl function call in my c source, but I can not figure out how , is there any document that I can refer to ?!

Generating encrypted private key file with git-bash

I am trying to generate a encrytpted private/public key using the code example given here
https://docs.snowflake.com/en/user-guide/key-pair-auth.html
$ openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8
however it just generates an empty file. Is there something I'm missing perhaps?
Any tips would be appreciated

What are bag attributes and how can i generate them?

while converting some certificates from keystore to openssl/pem I noticed for the first time that there are "Bag Attributes" prepended to the certs.
The look like this:
Bag Attributes
friendlyName: CN=PositiveSSL CA,O=Comodo CA Limited,L=Salford,ST=Greater Manchester,C=GB
subject=/C=GB/ST=Greater Manchester/L=Salford/O=Comodo CA Limited/CN=PositiveSSL CA
issuer=/C=US/ST=UT/L=Salt Lake City/O=The USERTRUST Network/OU=http://www.usertrust.com/CN=UTN-USERFirst-Hardware
Do they serve any function?
I noticed that I like them because they make my chain-files (a concatenation of certificates) more clear. Sadly the ca certs I download don't have them.
So how do I generate them?
To be exact, you apparently mean converting (or just reading) with the openssl pkcs12 (import) utility a PKCS#12 file, which can be supported by Java as a keystore but was not the default (update) until Java9 in 2017. PKCS#12 was designed and normally is used for a privatekey and the cert(s) (usually multiple) for that key, although the format is flexible enough to allow lone cert(s). OpenSSL commandline pkcs12 -export requires a privatekey, although it will add "extra" certs, and a program calling the API can apparently do no privatekey. In my experience, Java didn't support lone cert(s) in PKCS#12 before version 8, and in my 8 and 9 has two attributes: pkcs9.friendlyName and 2.16.840.1.113894.746875.1.1 which is apparently an Oracle-defined trustedKeyUsage. Most lone certs are not stored, or downloaded, as PKCS#12.
PKCS#12 is defined in terms of several (slightly different) "bag" structures that contain various things, primarily privatekeys and certs with optional attributes attached that are unsurprisingly called "bag attributes"; your case (apparently) has only cert(s). These attributes follow the now-conventional structure of an arbitrary number of pairs of OID plus value depending on the OID. Note in your display only friendlyName is a bag attribute, indicated because it is indented under the heading.
The subject= and issuer= lines are fields from the cert itself which the openssl pkcs12 (import) utility extracts and prints for convenience. If that is sufficient, you can display them for any cert with the x509 utility; in particular if you want to have them before the PEM-encoded cert "blob" in the way pkcs12 output does, use openssl x509 -in infile -subject -issuer -out outfile. This does one cert, so if you have a chain in a PEM file you need to split it apart and do each cert separately, and possibly combine again afterwards; for example something like
# split into files cert_1, cert_2, etc.
$ awk <chain.pem -va="openssl x509 -subject -issuer >cert_"
'/^-----BEGIN/{b=a (++n);x=1}x{print|b}/^-----END/{close(b);x=0}'
# output entire "bag" to stdout (with blank lines between certs)
$ awk <chain.pem -va="openssl x509 -subject -issuer" \
'/^-----BEGIN/{b=a;x=1}x{print|b}/^-----END/{close(b);x=0;print""}'
As a comparison, openssl s_client -showcerts does something very similar: it outputs subject and issuer with each cert blob from the received chain, labelling them with a level number, "s:" and "i:".

to use RSA_PKCS1_OAEP_PADDING for RSA signature

Previously I was using rsa signature with no padding ,but now I was asked to add PKCS1_OAEP padding ,Initially I tried simple adding this flag "RSA_PKCS1_OAEP_PADDING", but it is giving error code while running like this
error:04066076:rsa routines:RSA_EAY_PRIVATE_ENCRYPT:unknown padding type
Further I googled about the rsa signature with PKCS1_OAEP padding but didn't get anything except it is said that for implementing RSA_PKCS1_OAEP padding you have to use this one
int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
unsigned char *f, int fl, unsigned char *p, int pl);
and then do private_encrypt with RSA_NO_padding but still having confusion as they have not explained clearly how to use this padding_add function explictly .can anyone provide help .would really appreciate that .
thanks in advance
According to RFC 3447 OAEP padding scheme can be used only with encryption operation and therefore it cannot be used with signatures. If standard RSASSA-PKCS1-v1_5 scheme is not good enough for you I believe you should use RSASSA-PSS which is recommended for new applications and have characteristics similar to OAEP scheme. See RFC 3447 for more details.
You can easily check out that my answer is correct with command line OpenSSL tool:
Generate private key:
openssl genrsa -out private.key 2048
Generate some input data:
echo "Hello world" > input.data
Try to generate signature with OAEP scheme:
openssl rsautl -sign -oaep -inkey private.key -in input.data -out output.data
RSA operation error
139655304349344:error:04066076:rsa routines:RSA_EAY_PRIVATE_ENCRYPT:unknown padding type:rsa_eay.c:389:
Try to encrypt data with OAEP scheme:
openssl rsautl -encrypt -oaep -inkey private.key -in input.data -out output.data
You can also take a look at "rsautl" module source code if you need more information but remember that RFC 3347 is your friend :)

How do I use libtomcrypt to import an RSA public key?

I am experimenting with using libtomcrypt to do RSA-2048 bit encryption. My current objective is to import a public key from a file. This file was generated using OpenSSL with the command:
$ openssl rsa -in private.pem -outform PEM -pubout -out public.pem
So I believe my public key is in PKCS#1 padding and in OpenSSL's PEM format.
I believe the function I need to use is rsa_import(), but that takes an in buffer, a length, and outputs an rsa_key pointer. Just to be clear, I believe what I need to do is as follows:
Read in the contents of public.pem to a buffer
Toss out the Header and Footers containing "Begin Public Key" etc.
Decode data from base64.
Pass in resulting data to rsa_import.
Is this correct? Can anyone who has used libtomcrypt for this purpose comment on this? Thanks.
So, upon digging into the source of rsa_import(), I figured out pretty quickly that it was expecting the key to be in DER format. Since I had access to the private key, I just made a DER file using this openssl command:
openssl rsa -in private.pem -outform DER -pubout -out public.der
Notably the argument for -outform is now DER rather than PEM. After this, I just read the file contents into a char buffer, then passed that in as the main argument for rsa_import. After that rsa_import made the key no problem and I was able to encrypt/decrypt from there.

Resources