etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / openssl / dist / demos / smime / smsign.c
blobc7bf86d37062eaa4e13ef3429d6fbcc42f1e9f70
1 /* Simple S/MIME signing example */
2 #include <openssl/pem.h>
3 #include <openssl/pkcs7.h>
4 #include <openssl/err.h>
6 int main(int argc, char **argv)
8 BIO *in = NULL, *out = NULL, *tbio = NULL;
9 X509 *scert = NULL;
10 EVP_PKEY *skey = NULL;
11 PKCS7 *p7 = NULL;
12 int ret = 1;
15 * For simple S/MIME signing use PKCS7_DETACHED. On OpenSSL 0.9.9 only:
16 * for streaming detached set PKCS7_DETACHED|PKCS7_STREAM for streaming
17 * non-detached set PKCS7_STREAM
19 int flags = PKCS7_DETACHED | PKCS7_STREAM;
21 OpenSSL_add_all_algorithms();
22 ERR_load_crypto_strings();
24 /* Read in signer certificate and private key */
25 tbio = BIO_new_file("signer.pem", "r");
27 if (!tbio)
28 goto err;
30 scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
32 BIO_reset(tbio);
34 skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
36 if (!scert || !skey)
37 goto err;
39 /* Open content being signed */
41 in = BIO_new_file("sign.txt", "r");
43 if (!in)
44 goto err;
46 /* Sign content */
47 p7 = PKCS7_sign(scert, skey, NULL, in, flags);
49 if (!p7)
50 goto err;
52 out = BIO_new_file("smout.txt", "w");
53 if (!out)
54 goto err;
56 if (!(flags & PKCS7_STREAM))
57 BIO_reset(in);
59 /* Write out S/MIME message */
60 if (!SMIME_write_PKCS7(out, p7, in, flags))
61 goto err;
63 ret = 0;
65 err:
67 if (ret) {
68 fprintf(stderr, "Error Signing Data\n");
69 ERR_print_errors_fp(stderr);
72 if (p7)
73 PKCS7_free(p7);
74 if (scert)
75 X509_free(scert);
76 if (skey)
77 EVP_PKEY_free(skey);
79 if (in)
80 BIO_free(in);
81 if (out)
82 BIO_free(out);
83 if (tbio)
84 BIO_free(tbio);
86 return ret;