Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DSA Signing with OpenSSL

Tags:

c

key

openssl

load

dsa

I'm tryng to sign using DSA from OpenSSL. I have the files containing public and private keys.

First of all I make an unicast connection and every thing is fine. After that I need a multicast UDP connection and I want to sign the packets. I'm trying to use function PEM_read_DSA_PUBKEY() in order to load my public key from my cert but it doesn't work. It returns always NULL instead of a DSA struct.

Here you have a simplistic version of the code. I compile like this:

gcc -Wall -g -lm prueba.c -o prueba -lcrypto

Any idea? Thank you!

#include <stdio.h>
#include <openssl/dsa.h>
#include <openssl/pem.h>

int main()
{
    FILE *DSA_cert_file = fopen("./certs/cert.pem", "r");
    if (DSA_cert_file == NULL)
        return 1;

    printf("Certificate read\n");

    DSA *dsa = DSA_new();
    if((dsa = PEM_read_DSA_PUBKEY(DSA_cert_file, 0, 0, 0)) == NULL)
        return 1;

    printf("DSA public key read\n");

    return 0;
}
like image 629
calamares Avatar asked May 03 '11 10:05

calamares


People also ask

What is a DSA private key?

The DSA private key is used to generate digital signatures, and the DSA public key is used to verify digital signatures. The difficulty of the discrete logarithm problem is the basis for the NIST Digital Signature Standard (DSS) public key algorithm.

What is the size of signature in DSA?

For DSA, the size in bytes of the signature is N/4 bytes (e.g. 64 for N=256 ). For ECDSA, the signature is always twice the length of a point coordinate (e.g. 64 bytes for P-256).


1 Answers

Are you using a password-protected public key?

If yes, you are required to pass a callback function as the third argument to PEM_read_DSA_PUBKEY, so if the provided password matches, it will be able to properly load your key.

Update:

Alternatively, as pointed by Hasturkun, you can pass a null-terminated string as the fourth argument. Quoting the official documentation:

If the cb parameters is set to NULL and the u parameter is not NULL then the u parameter is interpreted as a null terminated string to use as the passphrase. If both cb and u are NULL then the default callback routine is used which will typically prompt for the passphrase on the current terminal with echoing turned off.

like image 164
jweyrich Avatar answered Sep 23 '22 23:09

jweyrich