Guys,
I'm new to OpenSSL. I'm trying to write a short program signing the message
digest I specify using
RSA_sign(). Could someone tell me how to use RSA_sign() function from
Crypro library.
Do I understand right from the documentation on the web that the first
parameter could be NID_md5 my digest method, second -my digest to sign,
3rd - its length, 4th - is OUTPUT parameter: signature itself, 5th - another
OUTPUT: pointer to signature's length, 6th - ptr to RSA structure with
signing key etc., that I should generate by RSA_generate_key() function in
advance? Is my logic correct?
For some reason I'm getting Segmentation Fault all the time.
I'd appreciate your help.
- Anton
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openss...@openssl.org
Automated List Manager majo...@openssl.org
1) Sounds correct, but note that you must allocate the memory for sigret
prior to calling the function.
2) If you can, use the EVP_* functions
(http://www.openssl.org/docs/crypto/EVP_SignInit.html#).
====================
Greg Stark
ghs...@pobox.com
====================
| int RSA_sign(int type, unsigned char *m, unsigned int m_len,
| unsigned char *sigret, unsigned int *siglen, RSA
| *rsa);
|
| Guys,
| I'm new to OpenSSL. I'm trying to write a short program signing the message
| digest I specify using
| RSA_sign(). Could someone tell me how to use RSA_sign() function from
| Crypro library.
|
| Do I understand right from the documentation on the web that the first
| parameter could be NID_md5 my digest method, second -my digest to sign,
| 3rd - its length, 4th - is OUTPUT parameter: signature itself, 5th - another
| OUTPUT: pointer to signature's length, 6th - ptr to RSA structure with
| signing key etc., that I should generate by RSA_generate_key() function in
| advance? Is my logic correct?
|
| For some reason I'm getting Segmentation Fault all the time.
|
| I'd appreciate your help.
|
Hi, Anton,
Here I have an example file coded long time ago, hopefully it is helpful.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <openssl/md5.h>
#include <openssl/rsa.h>
#include <openssl/obj_mac.h>
int main(int argc, char **argv)
{
int fd, i, size;
unsigned char md[MD5_DIGEST_LENGTH];
unsigned char buffer[1024], *rsaret;
BIO *in;
RSA *rsa=NULL;
MD5_CTX c;
signal(SIGPIPE, SIG_IGN);
CRYPTO_malloc_init();
OpenSSL_add_all_algorithms();
in = BIO_new(BIO_s_file());
MD5_Init(&c);
if (argc==1) {
fd = 0;
rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
}
else if (argc==2) {
fd = open(argv[1], O_RDONLY);
if (fd<=0) {
perror(argv[1]);
exit(-1);
}
rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
}
else if (argc==3){
fd = open(argv[1], O_RDONLY);
if (fd<=0) {
perror(argv[1]);
exit(-1);
}
in = BIO_new(BIO_s_file());
if ((BIO_read_filename(in, argv[2]))<=0) {
perror(argv[1]);
exit(-1);
}
rsa = PEM_read_bio_RSAPrivateKey(in, NULL, NULL, NULL);
}
else {
fprintf(stderr, "Usage: %s [<filename> [RSA private key in
PEM format]]");
exit(-1);
}
rsaret = malloc(RSA_size(rsa));
bzero(rsaret, RSA_size(rsa));
while ((i=read(fd, buffer, 1024))>0) {
MD5_Update(&c, buffer, i);
}
MD5_Final(&(md[0]), &c);
RSA_sign(NID_md5, md, MD5_DIGEST_LENGTH, rsaret, &size, rsa);
for (i=0;i<size;i++)
printf("%02X", (unsigned char)(*(rsaret+i)));
printf("\n");
BIO_free(in);
RSA_free(rsa);
free(rsaret);
close(fd);
}
--
___ ___|~|_ _ ____ _ ___ __|~| Edward...@oracle.com
/ _ \/ _` | \ /\ / / _ `| '__/ _` | 6501 E Belleview Avenue
| __/| (_| |\ V V /| (_| | | | (_| | Englewood, CO 80111, US
\___|\___,_| \_/\_/ \___,_|_| \___,_| Telephone: 720.489.6000