xref: /minix3/crypto/external/bsd/openssl/dist/demos/sign/sign.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /* demos/sign/sign.c */
2*ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*ebfedea0SLionel Sambuc  * All rights reserved.
4*ebfedea0SLionel Sambuc  *
5*ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6*ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7*ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8*ebfedea0SLionel Sambuc  *
9*ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10*ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11*ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12*ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14*ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*ebfedea0SLionel Sambuc  *
16*ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17*ebfedea0SLionel Sambuc  * the code are not to be removed.
18*ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19*ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20*ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21*ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25*ebfedea0SLionel Sambuc  * are met:
26*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31*ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32*ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33*ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34*ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35*ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36*ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37*ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38*ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39*ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*ebfedea0SLionel Sambuc  *
41*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52*ebfedea0SLionel Sambuc  *
53*ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54*ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56*ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57*ebfedea0SLionel Sambuc  */
58*ebfedea0SLionel Sambuc 
59*ebfedea0SLionel Sambuc /* sign-it.cpp  -  Simple test app using SSLeay envelopes to sign data
60*ebfedea0SLionel Sambuc    29.9.1996, Sampo Kellomaki <sampo@iki.fi> */
61*ebfedea0SLionel Sambuc 
62*ebfedea0SLionel Sambuc /* converted to C - eay :-) */
63*ebfedea0SLionel Sambuc 
64*ebfedea0SLionel Sambuc /* reformated a bit and converted to use the more common functions: this was
65*ebfedea0SLionel Sambuc  * initially written at the dawn of time :-) - Steve.
66*ebfedea0SLionel Sambuc  */
67*ebfedea0SLionel Sambuc 
68*ebfedea0SLionel Sambuc #include <stdio.h>
69*ebfedea0SLionel Sambuc #include <openssl/rsa.h>
70*ebfedea0SLionel Sambuc #include <openssl/evp.h>
71*ebfedea0SLionel Sambuc #include <openssl/objects.h>
72*ebfedea0SLionel Sambuc #include <openssl/x509.h>
73*ebfedea0SLionel Sambuc #include <openssl/err.h>
74*ebfedea0SLionel Sambuc #include <openssl/pem.h>
75*ebfedea0SLionel Sambuc #include <openssl/ssl.h>
76*ebfedea0SLionel Sambuc 
77*ebfedea0SLionel Sambuc int main ()
78*ebfedea0SLionel Sambuc {
79*ebfedea0SLionel Sambuc   int err;
80*ebfedea0SLionel Sambuc   int sig_len;
81*ebfedea0SLionel Sambuc   unsigned char sig_buf [4096];
82*ebfedea0SLionel Sambuc   static char certfile[] = "cert.pem";
83*ebfedea0SLionel Sambuc   static char keyfile[]  = "key.pem";
84*ebfedea0SLionel Sambuc   static char data[]     = "I owe you...";
85*ebfedea0SLionel Sambuc   EVP_MD_CTX     md_ctx;
86*ebfedea0SLionel Sambuc   EVP_PKEY *      pkey;
87*ebfedea0SLionel Sambuc   FILE *          fp;
88*ebfedea0SLionel Sambuc   X509 *	x509;
89*ebfedea0SLionel Sambuc 
90*ebfedea0SLionel Sambuc   /* Just load the crypto library error strings,
91*ebfedea0SLionel Sambuc    * SSL_load_error_strings() loads the crypto AND the SSL ones */
92*ebfedea0SLionel Sambuc   /* SSL_load_error_strings();*/
93*ebfedea0SLionel Sambuc   ERR_load_crypto_strings();
94*ebfedea0SLionel Sambuc 
95*ebfedea0SLionel Sambuc   /* Read private key */
96*ebfedea0SLionel Sambuc 
97*ebfedea0SLionel Sambuc   fp = fopen (keyfile, "r");
98*ebfedea0SLionel Sambuc   if (fp == NULL) exit (1);
99*ebfedea0SLionel Sambuc   pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
100*ebfedea0SLionel Sambuc   fclose (fp);
101*ebfedea0SLionel Sambuc 
102*ebfedea0SLionel Sambuc   if (pkey == NULL) {
103*ebfedea0SLionel Sambuc 	ERR_print_errors_fp (stderr);
104*ebfedea0SLionel Sambuc 	exit (1);
105*ebfedea0SLionel Sambuc   }
106*ebfedea0SLionel Sambuc 
107*ebfedea0SLionel Sambuc   /* Do the signature */
108*ebfedea0SLionel Sambuc 
109*ebfedea0SLionel Sambuc   EVP_SignInit   (&md_ctx, EVP_sha1());
110*ebfedea0SLionel Sambuc   EVP_SignUpdate (&md_ctx, data, strlen(data));
111*ebfedea0SLionel Sambuc   sig_len = sizeof(sig_buf);
112*ebfedea0SLionel Sambuc   err = EVP_SignFinal (&md_ctx, sig_buf, &sig_len, pkey);
113*ebfedea0SLionel Sambuc 
114*ebfedea0SLionel Sambuc   if (err != 1) {
115*ebfedea0SLionel Sambuc 	ERR_print_errors_fp(stderr);
116*ebfedea0SLionel Sambuc 	exit (1);
117*ebfedea0SLionel Sambuc   }
118*ebfedea0SLionel Sambuc 
119*ebfedea0SLionel Sambuc   EVP_PKEY_free (pkey);
120*ebfedea0SLionel Sambuc 
121*ebfedea0SLionel Sambuc   /* Read public key */
122*ebfedea0SLionel Sambuc 
123*ebfedea0SLionel Sambuc   fp = fopen (certfile, "r");
124*ebfedea0SLionel Sambuc   if (fp == NULL) exit (1);
125*ebfedea0SLionel Sambuc   x509 = PEM_read_X509(fp, NULL, NULL, NULL);
126*ebfedea0SLionel Sambuc   fclose (fp);
127*ebfedea0SLionel Sambuc 
128*ebfedea0SLionel Sambuc   if (x509 == NULL) {
129*ebfedea0SLionel Sambuc 	ERR_print_errors_fp (stderr);
130*ebfedea0SLionel Sambuc 	exit (1);
131*ebfedea0SLionel Sambuc   }
132*ebfedea0SLionel Sambuc 
133*ebfedea0SLionel Sambuc   /* Get public key - eay */
134*ebfedea0SLionel Sambuc   pkey=X509_get_pubkey(x509);
135*ebfedea0SLionel Sambuc   if (pkey == NULL) {
136*ebfedea0SLionel Sambuc 	ERR_print_errors_fp (stderr);
137*ebfedea0SLionel Sambuc 	exit (1);
138*ebfedea0SLionel Sambuc   }
139*ebfedea0SLionel Sambuc 
140*ebfedea0SLionel Sambuc   /* Verify the signature */
141*ebfedea0SLionel Sambuc 
142*ebfedea0SLionel Sambuc   EVP_VerifyInit   (&md_ctx, EVP_sha1());
143*ebfedea0SLionel Sambuc   EVP_VerifyUpdate (&md_ctx, data, strlen((char*)data));
144*ebfedea0SLionel Sambuc   err = EVP_VerifyFinal (&md_ctx, sig_buf, sig_len, pkey);
145*ebfedea0SLionel Sambuc   EVP_PKEY_free (pkey);
146*ebfedea0SLionel Sambuc 
147*ebfedea0SLionel Sambuc   if (err != 1) {
148*ebfedea0SLionel Sambuc 	ERR_print_errors_fp (stderr);
149*ebfedea0SLionel Sambuc 	exit (1);
150*ebfedea0SLionel Sambuc   }
151*ebfedea0SLionel Sambuc   printf ("Signature Verified Ok.\n");
152*ebfedea0SLionel Sambuc   return(0);
153*ebfedea0SLionel Sambuc }
154