Reformat and "modernise" the sign.c demo.

This commit is contained in:
Dr. Stephen Henson 1999-06-09 23:33:48 +00:00
parent 5a2e24bad8
commit 5f6d0ea210
2 changed files with 46 additions and 25 deletions

View File

@ -5,6 +5,12 @@
Changes between 0.9.3a and 0.9.4 Changes between 0.9.3a and 0.9.4
*) Fix demos/sign/sign.c: well there wasn't anything strictly speaking
wrong with it but it was very old and did things like calling
PEM_ASN1_read() directly and used MD5 for the hash not to mention some
unusual formatting.
[Steve Henson]
*) Fix demos/selfsign.c: it used obsolete and deleted functions, changed *) Fix demos/selfsign.c: it used obsolete and deleted functions, changed
to use the new extension code. to use the new extension code.
[Steve Henson] [Steve Henson]

View File

@ -61,6 +61,10 @@
/* converted to C - eay :-) */ /* converted to C - eay :-) */
/* reformated a bit and converted to use the more common functions: this was
* initially written at the dawn of time :-) - Steve.
*/
#include <stdio.h> #include <stdio.h>
#include <openssl/rsa.h> #include <openssl/rsa.h>
#include <openssl/evp.h> #include <openssl/evp.h>
@ -90,49 +94,60 @@ int main ()
/* Read private key */ /* Read private key */
fp = fopen (keyfile, "r"); if (fp == NULL) exit (1); fp = fopen (keyfile, "r");
pkey = (EVP_PKEY*)PEM_ASN1_read ((char *(*)())d2i_PrivateKey, if (fp == NULL) exit (1);
PEM_STRING_EVP_PKEY, pkey = PEM_read_PrivateKey(fp, NULL, NULL);
fp,
NULL, NULL);
if (pkey == NULL) { ERR_print_errors_fp (stderr); exit (1); }
fclose (fp); fclose (fp);
if (pkey == NULL) {
ERR_print_errors_fp (stderr);
exit (1);
}
/* Do the signature */ /* Do the signature */
EVP_SignInit (&md_ctx, EVP_md5()); EVP_SignInit (&md_ctx, EVP_sha1());
EVP_SignUpdate (&md_ctx, data, strlen(data)); EVP_SignUpdate (&md_ctx, data, strlen(data));
sig_len = sizeof(sig_buf); sig_len = sizeof(sig_buf);
err = EVP_SignFinal (&md_ctx, err = EVP_SignFinal (&md_ctx, sig_buf, &sig_len, pkey);
sig_buf,
&sig_len, if (err != 1) {
pkey); ERR_print_errors_fp(stderr);
if (err != 1) { ERR_print_errors_fp (stderr); exit (1); } exit (1);
}
EVP_PKEY_free (pkey); EVP_PKEY_free (pkey);
/* Read public key */ /* Read public key */
fp = fopen (certfile, "r"); if (fp == NULL) exit (1); fp = fopen (certfile, "r");
x509 = (X509 *)PEM_ASN1_read ((char *(*)())d2i_X509, if (fp == NULL) exit (1);
PEM_STRING_X509, x509 = PEM_read_X509(fp, NULL, NULL);
fp, NULL, NULL);
if (x509 == NULL) { ERR_print_errors_fp (stderr); exit (1); }
fclose (fp); fclose (fp);
if (x509 == NULL) {
ERR_print_errors_fp (stderr);
exit (1);
}
/* Get public key - eay */ /* Get public key - eay */
pkey=X509_extract_key(x509); pkey=X509_get_pubkey(x509);
if (pkey == NULL) { ERR_print_errors_fp (stderr); exit (1); } if (pkey == NULL) {
ERR_print_errors_fp (stderr);
exit (1);
}
/* Verify the signature */ /* Verify the signature */
EVP_VerifyInit (&md_ctx, EVP_md5()); EVP_VerifyInit (&md_ctx, EVP_sha1());
EVP_VerifyUpdate (&md_ctx, data, strlen((char*)data)); EVP_VerifyUpdate (&md_ctx, data, strlen((char*)data));
err = EVP_VerifyFinal (&md_ctx, err = EVP_VerifyFinal (&md_ctx, sig_buf, sig_len, pkey);
sig_buf,
sig_len,
pkey);
if (err != 1) { ERR_print_errors_fp (stderr); exit (1); }
EVP_PKEY_free (pkey); EVP_PKEY_free (pkey);
if (err != 1) {
ERR_print_errors_fp (stderr);
exit (1);
}
printf ("Signature Verified Ok.\n"); printf ("Signature Verified Ok.\n");
return(0); return(0);
} }