diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/pkcs7/enc.c | 86 |
1 files changed, 58 insertions, 28 deletions
diff --git a/src/lib/libcrypto/pkcs7/enc.c b/src/lib/libcrypto/pkcs7/enc.c index 625a7c2285..7417f8a4e0 100644 --- a/src/lib/libcrypto/pkcs7/enc.c +++ b/src/lib/libcrypto/pkcs7/enc.c | |||
| @@ -56,61 +56,91 @@ | |||
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | #include <stdio.h> | 58 | #include <stdio.h> |
| 59 | #include "bio.h" | 59 | #include <string.h> |
| 60 | #include "x509.h" | 60 | #include <openssl/bio.h> |
| 61 | #include "pem.h" | 61 | #include <openssl/x509.h> |
| 62 | #include <openssl/pem.h> | ||
| 63 | #include <openssl/err.h> | ||
| 62 | 64 | ||
| 63 | main(argc,argv) | 65 | int main(argc,argv) |
| 64 | int argc; | 66 | int argc; |
| 65 | char *argv[]; | 67 | char *argv[]; |
| 66 | { | 68 | { |
| 67 | X509 *x509; | 69 | X509 *x509; |
| 68 | EVP_PKEY *pkey; | ||
| 69 | PKCS7 *p7; | 70 | PKCS7 *p7; |
| 70 | PKCS7 *p7_data; | ||
| 71 | PKCS7_SIGNER_INFO *si; | ||
| 72 | BIO *in; | 71 | BIO *in; |
| 73 | BIO *data,*p7bio; | 72 | BIO *data,*p7bio; |
| 74 | char buf[1024*4]; | 73 | char buf[1024*4]; |
| 75 | int i,j; | 74 | int i; |
| 76 | int nodetach=0; | 75 | int nodetach=1; |
| 76 | char *keyfile = NULL; | ||
| 77 | const EVP_CIPHER *cipher=NULL; | ||
| 78 | STACK_OF(X509) *recips=NULL; | ||
| 77 | 79 | ||
| 78 | EVP_add_digest(EVP_sha1()); | 80 | OpenSSL_add_all_algorithms(); |
| 79 | EVP_add_cipher(EVP_des_cbc()); | ||
| 80 | 81 | ||
| 81 | data=BIO_new(BIO_s_file()); | 82 | data=BIO_new(BIO_s_file()); |
| 82 | again: | 83 | while(argc > 1) |
| 83 | if (argc > 1) | ||
| 84 | { | 84 | { |
| 85 | if (strcmp(argv[1],"-nd") == 0) | 85 | if (strcmp(argv[1],"-nd") == 0) |
| 86 | { | 86 | { |
| 87 | nodetach=1; | 87 | nodetach=1; |
| 88 | argv++; argc--; | 88 | argv++; argc--; |
| 89 | goto again; | ||
| 90 | } | 89 | } |
| 91 | if (!BIO_read_filename(data,argv[1])) | 90 | else if ((strcmp(argv[1],"-c") == 0) && (argc >= 2)) { |
| 92 | goto err; | 91 | if(!(cipher = EVP_get_cipherbyname(argv[2]))) { |
| 93 | } | 92 | fprintf(stderr, "Unknown cipher %s\n", argv[2]); |
| 94 | else | 93 | goto err; |
| 95 | BIO_set_fp(data,stdin,BIO_NOCLOSE); | 94 | } |
| 95 | argc-=2; | ||
| 96 | argv+=2; | ||
| 97 | } else if ((strcmp(argv[1],"-k") == 0) && (argc >= 2)) { | ||
| 98 | keyfile = argv[2]; | ||
| 99 | argc-=2; | ||
| 100 | argv+=2; | ||
| 101 | if (!(in=BIO_new_file(keyfile,"r"))) goto err; | ||
| 102 | if (!(x509=PEM_read_bio_X509(in,NULL,NULL,NULL))) | ||
| 103 | goto err; | ||
| 104 | if(!recips) recips = sk_X509_new_null(); | ||
| 105 | sk_X509_push(recips, x509); | ||
| 106 | BIO_free(in); | ||
| 107 | } else break; | ||
| 108 | } | ||
| 96 | 109 | ||
| 97 | if ((in=BIO_new_file("server.pem","r")) == NULL) goto err; | 110 | if(!recips) { |
| 98 | if ((x509=PEM_read_bio_X509(in,NULL,NULL)) == NULL) goto err; | 111 | fprintf(stderr, "No recipients\n"); |
| 112 | goto err; | ||
| 113 | } | ||
| 114 | |||
| 115 | if (!BIO_read_filename(data,argv[1])) goto err; | ||
| 116 | |||
| 117 | p7=PKCS7_new(); | ||
| 118 | #if 0 | ||
| 99 | BIO_reset(in); | 119 | BIO_reset(in); |
| 100 | if ((pkey=PEM_read_bio_PrivateKey(in,NULL,NULL)) == NULL) goto err; | 120 | if ((pkey=PEM_read_bio_PrivateKey(in,NULL,NULL)) == NULL) goto err; |
| 101 | BIO_free(in); | 121 | BIO_free(in); |
| 102 | |||
| 103 | p7=PKCS7_new(); | ||
| 104 | PKCS7_set_type(p7,NID_pkcs7_signedAndEnveloped); | 122 | PKCS7_set_type(p7,NID_pkcs7_signedAndEnveloped); |
| 105 | 123 | ||
| 106 | if (PKCS7_add_signature(p7,x509,pkey,EVP_sha1()) == NULL) goto err; | 124 | if (PKCS7_add_signature(p7,x509,pkey,EVP_sha1()) == NULL) goto err; |
| 107 | |||
| 108 | if (!PKCS7_set_cipher(p7,EVP_des_cbc())) goto err; | ||
| 109 | if (PKCS7_add_recipient(p7,x509) == NULL) goto err; | ||
| 110 | |||
| 111 | /* we may want to add more */ | 125 | /* we may want to add more */ |
| 112 | PKCS7_add_certificate(p7,x509); | 126 | PKCS7_add_certificate(p7,x509); |
| 127 | #else | ||
| 128 | PKCS7_set_type(p7,NID_pkcs7_enveloped); | ||
| 129 | #endif | ||
| 130 | if(!cipher) { | ||
| 131 | #ifndef OPENSSL_NO_DES | ||
| 132 | cipher = EVP_des_ede3_cbc(); | ||
| 133 | #else | ||
| 134 | fprintf(stderr, "No cipher selected\n"); | ||
| 135 | goto err; | ||
| 136 | #endif | ||
| 137 | } | ||
| 113 | 138 | ||
| 139 | if (!PKCS7_set_cipher(p7,cipher)) goto err; | ||
| 140 | for(i = 0; i < sk_X509_num(recips); i++) { | ||
| 141 | if (!PKCS7_add_recipient(p7,sk_X509_value(recips, i))) goto err; | ||
| 142 | } | ||
| 143 | sk_X509_pop_free(recips, X509_free); | ||
| 114 | 144 | ||
| 115 | /* Set the content of the signed to 'data' */ | 145 | /* Set the content of the signed to 'data' */ |
| 116 | /* PKCS7_content_new(p7,NID_pkcs7_data); not used in envelope */ | 146 | /* PKCS7_content_new(p7,NID_pkcs7_data); not used in envelope */ |
| @@ -129,7 +159,7 @@ again: | |||
| 129 | } | 159 | } |
| 130 | BIO_flush(p7bio); | 160 | BIO_flush(p7bio); |
| 131 | 161 | ||
| 132 | if (!PKCS7_dataSign(p7,p7bio)) goto err; | 162 | if (!PKCS7_dataFinal(p7,p7bio)) goto err; |
| 133 | BIO_free(p7bio); | 163 | BIO_free(p7bio); |
| 134 | 164 | ||
| 135 | PEM_write_PKCS7(stdout,p7); | 165 | PEM_write_PKCS7(stdout,p7); |
