diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs7')
| -rw-r--r-- | src/lib/libcrypto/pkcs7/Makefile.ssl | 195 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_asn1.c | 213 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_attr.c | 139 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_doit.c | 979 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_lib.c | 477 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_mime.c | 685 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_smime.c | 441 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pkcs7.h | 449 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pkcs7err.c | 160 |
9 files changed, 3738 insertions, 0 deletions
diff --git a/src/lib/libcrypto/pkcs7/Makefile.ssl b/src/lib/libcrypto/pkcs7/Makefile.ssl new file mode 100644 index 0000000000..d2afd977e1 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/Makefile.ssl | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | # | ||
| 2 | # SSLeay/crypto/pkcs7/Makefile | ||
| 3 | # | ||
| 4 | |||
| 5 | DIR= pkcs7 | ||
| 6 | TOP= ../.. | ||
| 7 | CC= cc | ||
| 8 | INCLUDES= -I.. -I$(TOP) -I../../include | ||
| 9 | CFLAG=-g | ||
| 10 | INSTALL_PREFIX= | ||
| 11 | OPENSSLDIR= /usr/local/ssl | ||
| 12 | INSTALLTOP=/usr/local/ssl | ||
| 13 | MAKE= make -f Makefile.ssl | ||
| 14 | MAKEDEPPROG= makedepend | ||
| 15 | MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) | ||
| 16 | MAKEFILE= Makefile.ssl | ||
| 17 | AR= ar r | ||
| 18 | |||
| 19 | PEX_LIBS= | ||
| 20 | EX_LIBS= | ||
| 21 | |||
| 22 | CFLAGS= $(INCLUDES) $(CFLAG) | ||
| 23 | |||
| 24 | GENERAL=Makefile README | ||
| 25 | TEST= | ||
| 26 | APPS= | ||
| 27 | |||
| 28 | LIB=$(TOP)/libcrypto.a | ||
| 29 | LIBSRC= pk7_asn1.c pk7_lib.c pkcs7err.c pk7_doit.c pk7_smime.c pk7_attr.c \ | ||
| 30 | pk7_mime.c | ||
| 31 | LIBOBJ= pk7_asn1.o pk7_lib.o pkcs7err.o pk7_doit.o pk7_smime.o pk7_attr.o \ | ||
| 32 | pk7_mime.o | ||
| 33 | |||
| 34 | SRC= $(LIBSRC) | ||
| 35 | |||
| 36 | EXHEADER= pkcs7.h | ||
| 37 | HEADER= $(EXHEADER) | ||
| 38 | |||
| 39 | ALL= $(GENERAL) $(SRC) $(HEADER) | ||
| 40 | |||
| 41 | top: | ||
| 42 | (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) | ||
| 43 | |||
| 44 | test: | ||
| 45 | |||
| 46 | all: lib | ||
| 47 | |||
| 48 | testapps: enc dec sign verify | ||
| 49 | |||
| 50 | enc: enc.o lib | ||
| 51 | $(CC) $(CFLAGS) -o enc enc.o $(PEX_LIBS) $(LIB) $(EX_LIBS) | ||
| 52 | |||
| 53 | dec: dec.o lib | ||
| 54 | $(CC) $(CFLAGS) -o dec dec.o $(PEX_LIBS) $(LIB) $(EX_LIBS) | ||
| 55 | |||
| 56 | sign: sign.o lib | ||
| 57 | $(CC) $(CFLAGS) -o sign sign.o $(PEX_LIBS) $(LIB) $(EX_LIBS) | ||
| 58 | |||
| 59 | verify: verify.o example.o lib | ||
| 60 | $(CC) $(CFLAGS) -o verify verify.o $(PEX_LIBS) example.o $(LIB) $(EX_LIBS) | ||
| 61 | |||
| 62 | lib: $(LIBOBJ) | ||
| 63 | $(AR) $(LIB) $(LIBOBJ) | ||
| 64 | $(RANLIB) $(LIB) || echo Never mind. | ||
| 65 | @touch lib | ||
| 66 | |||
| 67 | files: | ||
| 68 | $(PERL) $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO | ||
| 69 | |||
| 70 | links: | ||
| 71 | @sh $(TOP)/util/point.sh Makefile.ssl Makefile | ||
| 72 | @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER) | ||
| 73 | @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST) | ||
| 74 | @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS) | ||
| 75 | |||
| 76 | install: | ||
| 77 | @for i in $(EXHEADER) ; \ | ||
| 78 | do \ | ||
| 79 | (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \ | ||
| 80 | chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \ | ||
| 81 | done; | ||
| 82 | |||
| 83 | tags: | ||
| 84 | ctags $(SRC) | ||
| 85 | |||
| 86 | tests: | ||
| 87 | |||
| 88 | lint: | ||
| 89 | lint -DLINT $(INCLUDES) $(SRC)>fluff | ||
| 90 | |||
| 91 | depend: | ||
| 92 | $(MAKEDEPEND) $(INCLUDES) $(DEPFLAG) $(PROGS) $(LIBSRC) | ||
| 93 | |||
| 94 | dclean: | ||
| 95 | $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new | ||
| 96 | mv -f Makefile.new $(MAKEFILE) | ||
| 97 | |||
| 98 | clean: | ||
| 99 | rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff enc dec sign verify | ||
| 100 | |||
| 101 | # DO NOT DELETE THIS LINE -- make depend depends on it. | ||
| 102 | |||
| 103 | pk7_asn1.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 104 | pk7_asn1.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h | ||
| 105 | pk7_asn1.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 106 | pk7_asn1.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 107 | pk7_asn1.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 108 | pk7_asn1.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 109 | pk7_asn1.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
| 110 | pk7_asn1.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
| 111 | pk7_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 112 | pk7_asn1.o: ../../include/openssl/pkcs7.h ../../include/openssl/rsa.h | ||
| 113 | pk7_asn1.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 114 | pk7_asn1.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 115 | pk7_asn1.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 116 | pk7_asn1.o: ../cryptlib.h pk7_asn1.c | ||
| 117 | pk7_attr.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 118 | pk7_attr.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 119 | pk7_attr.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 120 | pk7_attr.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 121 | pk7_attr.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 122 | pk7_attr.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
| 123 | pk7_attr.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
| 124 | pk7_attr.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 125 | pk7_attr.o: ../../include/openssl/pem.h ../../include/openssl/pem2.h | ||
| 126 | pk7_attr.o: ../../include/openssl/pkcs7.h ../../include/openssl/rsa.h | ||
| 127 | pk7_attr.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 128 | pk7_attr.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 129 | pk7_attr.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 130 | pk7_attr.o: pk7_attr.c | ||
| 131 | pk7_doit.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 132 | pk7_doit.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 133 | pk7_doit.o: ../../include/openssl/buffer.h ../../include/openssl/conf.h | ||
| 134 | pk7_doit.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 135 | pk7_doit.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 136 | pk7_doit.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 137 | pk7_doit.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
| 138 | pk7_doit.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
| 139 | pk7_doit.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 140 | pk7_doit.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h | ||
| 141 | pk7_doit.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 142 | pk7_doit.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
| 143 | pk7_doit.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
| 144 | pk7_doit.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h | ||
| 145 | pk7_doit.o: ../cryptlib.h pk7_doit.c | ||
| 146 | pk7_lib.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 147 | pk7_lib.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 148 | pk7_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 149 | pk7_lib.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
| 150 | pk7_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 151 | pk7_lib.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
| 152 | pk7_lib.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
| 153 | pk7_lib.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
| 154 | pk7_lib.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
| 155 | pk7_lib.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 156 | pk7_lib.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
| 157 | pk7_lib.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
| 158 | pk7_lib.o: ../../include/openssl/x509_vfy.h ../cryptlib.h pk7_lib.c | ||
| 159 | pk7_mime.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 160 | pk7_mime.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 161 | pk7_mime.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 162 | pk7_mime.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
| 163 | pk7_mime.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 164 | pk7_mime.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
| 165 | pk7_mime.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
| 166 | pk7_mime.o: ../../include/openssl/opensslconf.h | ||
| 167 | pk7_mime.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 168 | pk7_mime.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h | ||
| 169 | pk7_mime.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 170 | pk7_mime.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
| 171 | pk7_mime.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
| 172 | pk7_mime.o: ../../include/openssl/x509_vfy.h ../cryptlib.h pk7_mime.c | ||
| 173 | pk7_smime.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 174 | pk7_smime.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 175 | pk7_smime.o: ../../include/openssl/buffer.h ../../include/openssl/conf.h | ||
| 176 | pk7_smime.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 177 | pk7_smime.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 178 | pk7_smime.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 179 | pk7_smime.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
| 180 | pk7_smime.o: ../../include/openssl/objects.h | ||
| 181 | pk7_smime.o: ../../include/openssl/opensslconf.h | ||
| 182 | pk7_smime.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 183 | pk7_smime.o: ../../include/openssl/pkcs7.h ../../include/openssl/rsa.h | ||
| 184 | pk7_smime.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 185 | pk7_smime.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 186 | pk7_smime.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 187 | pk7_smime.o: ../../include/openssl/x509v3.h ../cryptlib.h pk7_smime.c | ||
| 188 | pkcs7err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 189 | pkcs7err.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h | ||
| 190 | pkcs7err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 191 | pkcs7err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 192 | pkcs7err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 193 | pkcs7err.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
| 194 | pkcs7err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 195 | pkcs7err.o: pkcs7err.c | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_asn1.c b/src/lib/libcrypto/pkcs7/pk7_asn1.c new file mode 100644 index 0000000000..46f0fc9375 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_asn1.c | |||
| @@ -0,0 +1,213 @@ | |||
| 1 | /* pk7_asn.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 2000. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2000 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/asn1t.h> | ||
| 62 | #include <openssl/pkcs7.h> | ||
| 63 | #include <openssl/x509.h> | ||
| 64 | |||
| 65 | /* PKCS#7 ASN1 module */ | ||
| 66 | |||
| 67 | /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */ | ||
| 68 | |||
| 69 | ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0); | ||
| 70 | |||
| 71 | ASN1_ADB(PKCS7) = { | ||
| 72 | ADB_ENTRY(NID_pkcs7_data, ASN1_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING, 0)), | ||
| 73 | ADB_ENTRY(NID_pkcs7_signed, ASN1_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)), | ||
| 74 | ADB_ENTRY(NID_pkcs7_enveloped, ASN1_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)), | ||
| 75 | ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)), | ||
| 76 | ADB_ENTRY(NID_pkcs7_digest, ASN1_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)), | ||
| 77 | ADB_ENTRY(NID_pkcs7_encrypted, ASN1_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0)) | ||
| 78 | } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL); | ||
| 79 | |||
| 80 | ASN1_SEQUENCE(PKCS7) = { | ||
| 81 | ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT), | ||
| 82 | ASN1_ADB_OBJECT(PKCS7) | ||
| 83 | }ASN1_SEQUENCE_END(PKCS7) | ||
| 84 | |||
| 85 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7) | ||
| 86 | IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7) | ||
| 87 | |||
| 88 | ASN1_SEQUENCE(PKCS7_SIGNED) = { | ||
| 89 | ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER), | ||
| 90 | ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR), | ||
| 91 | ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7), | ||
| 92 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0), | ||
| 93 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1), | ||
| 94 | ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO) | ||
| 95 | } ASN1_SEQUENCE_END(PKCS7_SIGNED) | ||
| 96 | |||
| 97 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED) | ||
| 98 | |||
| 99 | /* Minor tweak to operation: free up EVP_PKEY */ | ||
| 100 | static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 101 | { | ||
| 102 | if(operation == ASN1_OP_FREE_POST) { | ||
| 103 | PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval; | ||
| 104 | EVP_PKEY_free(si->pkey); | ||
| 105 | } | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = { | ||
| 110 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER), | ||
| 111 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | ||
| 112 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR), | ||
| 113 | /* NB this should be a SET OF but we use a SEQUENCE OF so the | ||
| 114 | * original order * is retained when the structure is reencoded. | ||
| 115 | * Since the attributes are implicitly tagged this will not affect | ||
| 116 | * the encoding. | ||
| 117 | */ | ||
| 118 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0), | ||
| 119 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR), | ||
| 120 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING), | ||
| 121 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1) | ||
| 122 | } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO) | ||
| 123 | |||
| 124 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) | ||
| 125 | |||
| 126 | ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = { | ||
| 127 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME), | ||
| 128 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER) | ||
| 129 | } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL) | ||
| 130 | |||
| 131 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) | ||
| 132 | |||
| 133 | ASN1_SEQUENCE(PKCS7_ENVELOPE) = { | ||
| 134 | ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER), | ||
| 135 | ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | ||
| 136 | ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT) | ||
| 137 | } ASN1_SEQUENCE_END(PKCS7_ENVELOPE) | ||
| 138 | |||
| 139 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE) | ||
| 140 | |||
| 141 | /* Minor tweak to operation: free up X509 */ | ||
| 142 | static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 143 | { | ||
| 144 | if(operation == ASN1_OP_FREE_POST) { | ||
| 145 | PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval; | ||
| 146 | X509_free(ri->cert); | ||
| 147 | } | ||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = { | ||
| 152 | ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER), | ||
| 153 | ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | ||
| 154 | ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR), | ||
| 155 | ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING) | ||
| 156 | } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO) | ||
| 157 | |||
| 158 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) | ||
| 159 | |||
| 160 | ASN1_SEQUENCE(PKCS7_ENC_CONTENT) = { | ||
| 161 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT), | ||
| 162 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR), | ||
| 163 | ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING, 0) | ||
| 164 | } ASN1_SEQUENCE_END(PKCS7_ENC_CONTENT) | ||
| 165 | |||
| 166 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) | ||
| 167 | |||
| 168 | ASN1_SEQUENCE(PKCS7_SIGN_ENVELOPE) = { | ||
| 169 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER), | ||
| 170 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | ||
| 171 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR), | ||
| 172 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT), | ||
| 173 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0), | ||
| 174 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1), | ||
| 175 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO) | ||
| 176 | } ASN1_SEQUENCE_END(PKCS7_SIGN_ENVELOPE) | ||
| 177 | |||
| 178 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) | ||
| 179 | |||
| 180 | ASN1_SEQUENCE(PKCS7_ENCRYPT) = { | ||
| 181 | ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER), | ||
| 182 | ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT) | ||
| 183 | } ASN1_SEQUENCE_END(PKCS7_ENCRYPT) | ||
| 184 | |||
| 185 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT) | ||
| 186 | |||
| 187 | ASN1_SEQUENCE(PKCS7_DIGEST) = { | ||
| 188 | ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER), | ||
| 189 | ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR), | ||
| 190 | ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7), | ||
| 191 | ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING) | ||
| 192 | } ASN1_SEQUENCE_END(PKCS7_DIGEST) | ||
| 193 | |||
| 194 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST) | ||
| 195 | |||
| 196 | /* Specials for authenticated attributes */ | ||
| 197 | |||
| 198 | /* When signing attributes we want to reorder them to match the sorted | ||
| 199 | * encoding. | ||
| 200 | */ | ||
| 201 | |||
| 202 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) = | ||
| 203 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | ||
| 204 | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN) | ||
| 205 | |||
| 206 | /* When verifying attributes we need to use the received order. So | ||
| 207 | * we use SEQUENCE OF and tag it to SET OF | ||
| 208 | */ | ||
| 209 | |||
| 210 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) = | ||
| 211 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, | ||
| 212 | V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | ||
| 213 | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY) | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_attr.c b/src/lib/libcrypto/pkcs7/pk7_attr.c new file mode 100644 index 0000000000..5ff5a88b5c --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_attr.c | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | /* pk7_attr.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 2001. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2001 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <stdlib.h> | ||
| 61 | #include <openssl/bio.h> | ||
| 62 | #include <openssl/asn1.h> | ||
| 63 | #include <openssl/pem.h> | ||
| 64 | #include <openssl/pkcs7.h> | ||
| 65 | #include <openssl/x509.h> | ||
| 66 | #include <openssl/err.h> | ||
| 67 | |||
| 68 | int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, STACK_OF(X509_ALGOR) *cap) | ||
| 69 | { | ||
| 70 | ASN1_STRING *seq; | ||
| 71 | unsigned char *p, *pp; | ||
| 72 | int len; | ||
| 73 | len=i2d_ASN1_SET_OF_X509_ALGOR(cap,NULL,i2d_X509_ALGOR, | ||
| 74 | V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, | ||
| 75 | IS_SEQUENCE); | ||
| 76 | if(!(pp=(unsigned char *)OPENSSL_malloc(len))) { | ||
| 77 | PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | p=pp; | ||
| 81 | i2d_ASN1_SET_OF_X509_ALGOR(cap,&p,i2d_X509_ALGOR, V_ASN1_SEQUENCE, | ||
| 82 | V_ASN1_UNIVERSAL, IS_SEQUENCE); | ||
| 83 | if(!(seq = ASN1_STRING_new())) { | ||
| 84 | PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 85 | return 0; | ||
| 86 | } | ||
| 87 | if(!ASN1_STRING_set (seq, pp, len)) { | ||
| 88 | PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | OPENSSL_free (pp); | ||
| 92 | return PKCS7_add_signed_attribute(si, NID_SMIMECapabilities, | ||
| 93 | V_ASN1_SEQUENCE, seq); | ||
| 94 | } | ||
| 95 | |||
| 96 | STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si) | ||
| 97 | { | ||
| 98 | ASN1_TYPE *cap; | ||
| 99 | unsigned char *p; | ||
| 100 | cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities); | ||
| 101 | if (!cap) return NULL; | ||
| 102 | p = cap->value.sequence->data; | ||
| 103 | return d2i_ASN1_SET_OF_X509_ALGOR(NULL, &p, | ||
| 104 | cap->value.sequence->length, | ||
| 105 | d2i_X509_ALGOR, X509_ALGOR_free, | ||
| 106 | V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); | ||
| 107 | } | ||
| 108 | |||
| 109 | /* Basic smime-capabilities OID and optional integer arg */ | ||
| 110 | int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) | ||
| 111 | { | ||
| 112 | X509_ALGOR *alg; | ||
| 113 | |||
| 114 | if(!(alg = X509_ALGOR_new())) { | ||
| 115 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 116 | return 0; | ||
| 117 | } | ||
| 118 | ASN1_OBJECT_free(alg->algorithm); | ||
| 119 | alg->algorithm = OBJ_nid2obj (nid); | ||
| 120 | if (arg > 0) { | ||
| 121 | ASN1_INTEGER *nbit; | ||
| 122 | if(!(alg->parameter = ASN1_TYPE_new())) { | ||
| 123 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 124 | return 0; | ||
| 125 | } | ||
| 126 | if(!(nbit = ASN1_INTEGER_new())) { | ||
| 127 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 128 | return 0; | ||
| 129 | } | ||
| 130 | if(!ASN1_INTEGER_set (nbit, arg)) { | ||
| 131 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 132 | return 0; | ||
| 133 | } | ||
| 134 | alg->parameter->value.integer = nbit; | ||
| 135 | alg->parameter->type = V_ASN1_INTEGER; | ||
| 136 | } | ||
| 137 | sk_X509_ALGOR_push (sk, alg); | ||
| 138 | return 1; | ||
| 139 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_doit.c b/src/lib/libcrypto/pkcs7/pk7_doit.c new file mode 100644 index 0000000000..4a4ff340ce --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_doit.c | |||
| @@ -0,0 +1,979 @@ | |||
| 1 | /* crypto/pkcs7/pk7_doit.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/rand.h> | ||
| 62 | #include <openssl/objects.h> | ||
| 63 | #include <openssl/x509.h> | ||
| 64 | #include <openssl/x509v3.h> | ||
| 65 | |||
| 66 | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, | ||
| 67 | void *value); | ||
| 68 | static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid); | ||
| 69 | |||
| 70 | static int PKCS7_type_is_other(PKCS7* p7) | ||
| 71 | { | ||
| 72 | int isOther=1; | ||
| 73 | |||
| 74 | int nid=OBJ_obj2nid(p7->type); | ||
| 75 | |||
| 76 | switch( nid ) | ||
| 77 | { | ||
| 78 | case NID_pkcs7_data: | ||
| 79 | case NID_pkcs7_signed: | ||
| 80 | case NID_pkcs7_enveloped: | ||
| 81 | case NID_pkcs7_signedAndEnveloped: | ||
| 82 | case NID_pkcs7_digest: | ||
| 83 | case NID_pkcs7_encrypted: | ||
| 84 | isOther=0; | ||
| 85 | break; | ||
| 86 | default: | ||
| 87 | isOther=1; | ||
| 88 | } | ||
| 89 | |||
| 90 | return isOther; | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | static int PKCS7_type_is_octet_string(PKCS7* p7) | ||
| 95 | { | ||
| 96 | if ( 0==PKCS7_type_is_other(p7) ) | ||
| 97 | return 0; | ||
| 98 | |||
| 99 | return (V_ASN1_OCTET_STRING==p7->d.other->type) ? 1 : 0; | ||
| 100 | } | ||
| 101 | |||
| 102 | BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) | ||
| 103 | { | ||
| 104 | int i,j; | ||
| 105 | BIO *out=NULL,*btmp=NULL; | ||
| 106 | X509_ALGOR *xa; | ||
| 107 | const EVP_MD *evp_md; | ||
| 108 | const EVP_CIPHER *evp_cipher=NULL; | ||
| 109 | STACK_OF(X509_ALGOR) *md_sk=NULL; | ||
| 110 | STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; | ||
| 111 | X509_ALGOR *xalg=NULL; | ||
| 112 | PKCS7_RECIP_INFO *ri=NULL; | ||
| 113 | EVP_PKEY *pkey; | ||
| 114 | |||
| 115 | i=OBJ_obj2nid(p7->type); | ||
| 116 | p7->state=PKCS7_S_HEADER; | ||
| 117 | |||
| 118 | switch (i) | ||
| 119 | { | ||
| 120 | case NID_pkcs7_signed: | ||
| 121 | md_sk=p7->d.sign->md_algs; | ||
| 122 | break; | ||
| 123 | case NID_pkcs7_signedAndEnveloped: | ||
| 124 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 125 | md_sk=p7->d.signed_and_enveloped->md_algs; | ||
| 126 | xalg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 127 | evp_cipher=p7->d.signed_and_enveloped->enc_data->cipher; | ||
| 128 | if (evp_cipher == NULL) | ||
| 129 | { | ||
| 130 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 131 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 132 | goto err; | ||
| 133 | } | ||
| 134 | break; | ||
| 135 | case NID_pkcs7_enveloped: | ||
| 136 | rsk=p7->d.enveloped->recipientinfo; | ||
| 137 | xalg=p7->d.enveloped->enc_data->algorithm; | ||
| 138 | evp_cipher=p7->d.enveloped->enc_data->cipher; | ||
| 139 | if (evp_cipher == NULL) | ||
| 140 | { | ||
| 141 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 142 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 143 | goto err; | ||
| 144 | } | ||
| 145 | break; | ||
| 146 | default: | ||
| 147 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 148 | goto err; | ||
| 149 | } | ||
| 150 | |||
| 151 | if (md_sk != NULL) | ||
| 152 | { | ||
| 153 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 154 | { | ||
| 155 | xa=sk_X509_ALGOR_value(md_sk,i); | ||
| 156 | if ((btmp=BIO_new(BIO_f_md())) == NULL) | ||
| 157 | { | ||
| 158 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB); | ||
| 159 | goto err; | ||
| 160 | } | ||
| 161 | |||
| 162 | j=OBJ_obj2nid(xa->algorithm); | ||
| 163 | evp_md=EVP_get_digestbyname(OBJ_nid2sn(j)); | ||
| 164 | if (evp_md == NULL) | ||
| 165 | { | ||
| 166 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNKNOWN_DIGEST_TYPE); | ||
| 167 | goto err; | ||
| 168 | } | ||
| 169 | |||
| 170 | BIO_set_md(btmp,evp_md); | ||
| 171 | if (out == NULL) | ||
| 172 | out=btmp; | ||
| 173 | else | ||
| 174 | BIO_push(out,btmp); | ||
| 175 | btmp=NULL; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | if (evp_cipher != NULL) | ||
| 180 | { | ||
| 181 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| 182 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 183 | int keylen,ivlen; | ||
| 184 | int jj,max; | ||
| 185 | unsigned char *tmp; | ||
| 186 | EVP_CIPHER_CTX *ctx; | ||
| 187 | |||
| 188 | if ((btmp=BIO_new(BIO_f_cipher())) == NULL) | ||
| 189 | { | ||
| 190 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB); | ||
| 191 | goto err; | ||
| 192 | } | ||
| 193 | BIO_get_cipher_ctx(btmp, &ctx); | ||
| 194 | keylen=EVP_CIPHER_key_length(evp_cipher); | ||
| 195 | ivlen=EVP_CIPHER_iv_length(evp_cipher); | ||
| 196 | if (RAND_bytes(key,keylen) <= 0) | ||
| 197 | goto err; | ||
| 198 | xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher)); | ||
| 199 | if (ivlen > 0) RAND_pseudo_bytes(iv,ivlen); | ||
| 200 | EVP_CipherInit_ex(ctx, evp_cipher, NULL, key, iv, 1); | ||
| 201 | |||
| 202 | if (ivlen > 0) { | ||
| 203 | if (xalg->parameter == NULL) | ||
| 204 | xalg->parameter=ASN1_TYPE_new(); | ||
| 205 | if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0) | ||
| 206 | goto err; | ||
| 207 | } | ||
| 208 | |||
| 209 | /* Lets do the pub key stuff :-) */ | ||
| 210 | max=0; | ||
| 211 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 212 | { | ||
| 213 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 214 | if (ri->cert == NULL) | ||
| 215 | { | ||
| 216 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_MISSING_CERIPEND_INFO); | ||
| 217 | goto err; | ||
| 218 | } | ||
| 219 | pkey=X509_get_pubkey(ri->cert); | ||
| 220 | jj=EVP_PKEY_size(pkey); | ||
| 221 | EVP_PKEY_free(pkey); | ||
| 222 | if (max < jj) max=jj; | ||
| 223 | } | ||
| 224 | if ((tmp=(unsigned char *)OPENSSL_malloc(max)) == NULL) | ||
| 225 | { | ||
| 226 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_MALLOC_FAILURE); | ||
| 227 | goto err; | ||
| 228 | } | ||
| 229 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 230 | { | ||
| 231 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 232 | pkey=X509_get_pubkey(ri->cert); | ||
| 233 | jj=EVP_PKEY_encrypt(tmp,key,keylen,pkey); | ||
| 234 | EVP_PKEY_free(pkey); | ||
| 235 | if (jj <= 0) | ||
| 236 | { | ||
| 237 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_EVP_LIB); | ||
| 238 | OPENSSL_free(tmp); | ||
| 239 | goto err; | ||
| 240 | } | ||
| 241 | M_ASN1_OCTET_STRING_set(ri->enc_key,tmp,jj); | ||
| 242 | } | ||
| 243 | OPENSSL_free(tmp); | ||
| 244 | memset(key, 0, keylen); | ||
| 245 | |||
| 246 | if (out == NULL) | ||
| 247 | out=btmp; | ||
| 248 | else | ||
| 249 | BIO_push(out,btmp); | ||
| 250 | btmp=NULL; | ||
| 251 | } | ||
| 252 | |||
| 253 | if (bio == NULL) { | ||
| 254 | if (PKCS7_is_detached(p7)) | ||
| 255 | bio=BIO_new(BIO_s_null()); | ||
| 256 | else { | ||
| 257 | if (PKCS7_type_is_signed(p7) ) { | ||
| 258 | if ( PKCS7_type_is_data(p7->d.sign->contents)) { | ||
| 259 | ASN1_OCTET_STRING *os; | ||
| 260 | os=p7->d.sign->contents->d.data; | ||
| 261 | if (os->length > 0) | ||
| 262 | bio = BIO_new_mem_buf(os->data, os->length); | ||
| 263 | } | ||
| 264 | else if ( PKCS7_type_is_octet_string(p7->d.sign->contents) ) { | ||
| 265 | ASN1_OCTET_STRING *os; | ||
| 266 | os=p7->d.sign->contents->d.other->value.octet_string; | ||
| 267 | if (os->length > 0) | ||
| 268 | bio = BIO_new_mem_buf(os->data, os->length); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | if(bio == NULL) { | ||
| 272 | bio=BIO_new(BIO_s_mem()); | ||
| 273 | BIO_set_mem_eof_return(bio,0); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | BIO_push(out,bio); | ||
| 278 | bio=NULL; | ||
| 279 | if (0) | ||
| 280 | { | ||
| 281 | err: | ||
| 282 | if (out != NULL) | ||
| 283 | BIO_free_all(out); | ||
| 284 | if (btmp != NULL) | ||
| 285 | BIO_free_all(btmp); | ||
| 286 | out=NULL; | ||
| 287 | } | ||
| 288 | return(out); | ||
| 289 | } | ||
| 290 | |||
| 291 | /* int */ | ||
| 292 | BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) | ||
| 293 | { | ||
| 294 | int i,j; | ||
| 295 | BIO *out=NULL,*btmp=NULL,*etmp=NULL,*bio=NULL; | ||
| 296 | unsigned char *tmp=NULL; | ||
| 297 | X509_ALGOR *xa; | ||
| 298 | ASN1_OCTET_STRING *data_body=NULL; | ||
| 299 | const EVP_MD *evp_md; | ||
| 300 | const EVP_CIPHER *evp_cipher=NULL; | ||
| 301 | EVP_CIPHER_CTX *evp_ctx=NULL; | ||
| 302 | X509_ALGOR *enc_alg=NULL; | ||
| 303 | STACK_OF(X509_ALGOR) *md_sk=NULL; | ||
| 304 | STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; | ||
| 305 | X509_ALGOR *xalg=NULL; | ||
| 306 | PKCS7_RECIP_INFO *ri=NULL; | ||
| 307 | |||
| 308 | i=OBJ_obj2nid(p7->type); | ||
| 309 | p7->state=PKCS7_S_HEADER; | ||
| 310 | |||
| 311 | switch (i) | ||
| 312 | { | ||
| 313 | case NID_pkcs7_signed: | ||
| 314 | data_body=p7->d.sign->contents->d.data; | ||
| 315 | md_sk=p7->d.sign->md_algs; | ||
| 316 | break; | ||
| 317 | case NID_pkcs7_signedAndEnveloped: | ||
| 318 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 319 | md_sk=p7->d.signed_and_enveloped->md_algs; | ||
| 320 | data_body=p7->d.signed_and_enveloped->enc_data->enc_data; | ||
| 321 | enc_alg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 322 | evp_cipher=EVP_get_cipherbyname(OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm))); | ||
| 323 | if (evp_cipher == NULL) | ||
| 324 | { | ||
| 325 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 326 | goto err; | ||
| 327 | } | ||
| 328 | xalg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 329 | break; | ||
| 330 | case NID_pkcs7_enveloped: | ||
| 331 | rsk=p7->d.enveloped->recipientinfo; | ||
| 332 | enc_alg=p7->d.enveloped->enc_data->algorithm; | ||
| 333 | data_body=p7->d.enveloped->enc_data->enc_data; | ||
| 334 | evp_cipher=EVP_get_cipherbyname(OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm))); | ||
| 335 | if (evp_cipher == NULL) | ||
| 336 | { | ||
| 337 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 338 | goto err; | ||
| 339 | } | ||
| 340 | xalg=p7->d.enveloped->enc_data->algorithm; | ||
| 341 | break; | ||
| 342 | default: | ||
| 343 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 344 | goto err; | ||
| 345 | } | ||
| 346 | |||
| 347 | /* We will be checking the signature */ | ||
| 348 | if (md_sk != NULL) | ||
| 349 | { | ||
| 350 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 351 | { | ||
| 352 | xa=sk_X509_ALGOR_value(md_sk,i); | ||
| 353 | if ((btmp=BIO_new(BIO_f_md())) == NULL) | ||
| 354 | { | ||
| 355 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); | ||
| 356 | goto err; | ||
| 357 | } | ||
| 358 | |||
| 359 | j=OBJ_obj2nid(xa->algorithm); | ||
| 360 | evp_md=EVP_get_digestbyname(OBJ_nid2sn(j)); | ||
| 361 | if (evp_md == NULL) | ||
| 362 | { | ||
| 363 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNKNOWN_DIGEST_TYPE); | ||
| 364 | goto err; | ||
| 365 | } | ||
| 366 | |||
| 367 | BIO_set_md(btmp,evp_md); | ||
| 368 | if (out == NULL) | ||
| 369 | out=btmp; | ||
| 370 | else | ||
| 371 | BIO_push(out,btmp); | ||
| 372 | btmp=NULL; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | if (evp_cipher != NULL) | ||
| 377 | { | ||
| 378 | #if 0 | ||
| 379 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| 380 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 381 | unsigned char *p; | ||
| 382 | int keylen,ivlen; | ||
| 383 | int max; | ||
| 384 | X509_OBJECT ret; | ||
| 385 | #endif | ||
| 386 | int jj; | ||
| 387 | |||
| 388 | if ((etmp=BIO_new(BIO_f_cipher())) == NULL) | ||
| 389 | { | ||
| 390 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); | ||
| 391 | goto err; | ||
| 392 | } | ||
| 393 | |||
| 394 | /* It was encrypted, we need to decrypt the secret key | ||
| 395 | * with the private key */ | ||
| 396 | |||
| 397 | /* Find the recipientInfo which matches the passed certificate | ||
| 398 | * (if any) | ||
| 399 | */ | ||
| 400 | |||
| 401 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) { | ||
| 402 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 403 | if(!X509_NAME_cmp(ri->issuer_and_serial->issuer, | ||
| 404 | pcert->cert_info->issuer) && | ||
| 405 | !M_ASN1_INTEGER_cmp(pcert->cert_info->serialNumber, | ||
| 406 | ri->issuer_and_serial->serial)) break; | ||
| 407 | ri=NULL; | ||
| 408 | } | ||
| 409 | if (ri == NULL) { | ||
| 410 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 411 | PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); | ||
| 412 | goto err; | ||
| 413 | } | ||
| 414 | |||
| 415 | jj=EVP_PKEY_size(pkey); | ||
| 416 | tmp=(unsigned char *)OPENSSL_malloc(jj+10); | ||
| 417 | if (tmp == NULL) | ||
| 418 | { | ||
| 419 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_MALLOC_FAILURE); | ||
| 420 | goto err; | ||
| 421 | } | ||
| 422 | |||
| 423 | jj=EVP_PKEY_decrypt(tmp, M_ASN1_STRING_data(ri->enc_key), | ||
| 424 | M_ASN1_STRING_length(ri->enc_key), pkey); | ||
| 425 | if (jj <= 0) | ||
| 426 | { | ||
| 427 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_EVP_LIB); | ||
| 428 | goto err; | ||
| 429 | } | ||
| 430 | |||
| 431 | evp_ctx=NULL; | ||
| 432 | BIO_get_cipher_ctx(etmp,&evp_ctx); | ||
| 433 | EVP_CipherInit_ex(evp_ctx,evp_cipher,NULL,NULL,NULL,0); | ||
| 434 | if (EVP_CIPHER_asn1_to_param(evp_ctx,enc_alg->parameter) < 0) | ||
| 435 | goto err; | ||
| 436 | |||
| 437 | if (jj != EVP_CIPHER_CTX_key_length(evp_ctx)) { | ||
| 438 | /* Some S/MIME clients don't use the same key | ||
| 439 | * and effective key length. The key length is | ||
| 440 | * determined by the size of the decrypted RSA key. | ||
| 441 | */ | ||
| 442 | if(!EVP_CIPHER_CTX_set_key_length(evp_ctx, jj)) | ||
| 443 | { | ||
| 444 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 445 | PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH); | ||
| 446 | goto err; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | EVP_CipherInit_ex(evp_ctx,NULL,NULL,tmp,NULL,0); | ||
| 450 | |||
| 451 | memset(tmp,0,jj); | ||
| 452 | |||
| 453 | if (out == NULL) | ||
| 454 | out=etmp; | ||
| 455 | else | ||
| 456 | BIO_push(out,etmp); | ||
| 457 | etmp=NULL; | ||
| 458 | } | ||
| 459 | |||
| 460 | #if 1 | ||
| 461 | if (PKCS7_is_detached(p7) || (in_bio != NULL)) | ||
| 462 | { | ||
| 463 | bio=in_bio; | ||
| 464 | } | ||
| 465 | else | ||
| 466 | { | ||
| 467 | #if 0 | ||
| 468 | bio=BIO_new(BIO_s_mem()); | ||
| 469 | /* We need to set this so that when we have read all | ||
| 470 | * the data, the encrypt BIO, if present, will read | ||
| 471 | * EOF and encode the last few bytes */ | ||
| 472 | BIO_set_mem_eof_return(bio,0); | ||
| 473 | |||
| 474 | if (data_body->length > 0) | ||
| 475 | BIO_write(bio,(char *)data_body->data,data_body->length); | ||
| 476 | #else | ||
| 477 | if (data_body->length > 0) | ||
| 478 | bio = BIO_new_mem_buf(data_body->data,data_body->length); | ||
| 479 | else { | ||
| 480 | bio=BIO_new(BIO_s_mem()); | ||
| 481 | BIO_set_mem_eof_return(bio,0); | ||
| 482 | } | ||
| 483 | #endif | ||
| 484 | } | ||
| 485 | BIO_push(out,bio); | ||
| 486 | bio=NULL; | ||
| 487 | #endif | ||
| 488 | if (0) | ||
| 489 | { | ||
| 490 | err: | ||
| 491 | if (out != NULL) BIO_free_all(out); | ||
| 492 | if (btmp != NULL) BIO_free_all(btmp); | ||
| 493 | if (etmp != NULL) BIO_free_all(etmp); | ||
| 494 | if (bio != NULL) BIO_free_all(bio); | ||
| 495 | out=NULL; | ||
| 496 | } | ||
| 497 | if (tmp != NULL) | ||
| 498 | OPENSSL_free(tmp); | ||
| 499 | return(out); | ||
| 500 | } | ||
| 501 | |||
| 502 | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio) | ||
| 503 | { | ||
| 504 | int ret=0; | ||
| 505 | int i,j; | ||
| 506 | BIO *btmp; | ||
| 507 | BUF_MEM *buf_mem=NULL; | ||
| 508 | BUF_MEM *buf=NULL; | ||
| 509 | PKCS7_SIGNER_INFO *si; | ||
| 510 | EVP_MD_CTX *mdc,ctx_tmp; | ||
| 511 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 512 | STACK_OF(PKCS7_SIGNER_INFO) *si_sk=NULL; | ||
| 513 | ASN1_OCTET_STRING *os=NULL; | ||
| 514 | |||
| 515 | EVP_MD_CTX_init(&ctx_tmp); | ||
| 516 | i=OBJ_obj2nid(p7->type); | ||
| 517 | p7->state=PKCS7_S_HEADER; | ||
| 518 | |||
| 519 | switch (i) | ||
| 520 | { | ||
| 521 | case NID_pkcs7_signedAndEnveloped: | ||
| 522 | /* XXXXXXXXXXXXXXXX */ | ||
| 523 | si_sk=p7->d.signed_and_enveloped->signer_info; | ||
| 524 | os=M_ASN1_OCTET_STRING_new(); | ||
| 525 | p7->d.signed_and_enveloped->enc_data->enc_data=os; | ||
| 526 | break; | ||
| 527 | case NID_pkcs7_enveloped: | ||
| 528 | /* XXXXXXXXXXXXXXXX */ | ||
| 529 | os=M_ASN1_OCTET_STRING_new(); | ||
| 530 | p7->d.enveloped->enc_data->enc_data=os; | ||
| 531 | break; | ||
| 532 | case NID_pkcs7_signed: | ||
| 533 | si_sk=p7->d.sign->signer_info; | ||
| 534 | os=p7->d.sign->contents->d.data; | ||
| 535 | /* If detached data then the content is excluded */ | ||
| 536 | if(p7->detached) { | ||
| 537 | M_ASN1_OCTET_STRING_free(os); | ||
| 538 | p7->d.sign->contents->d.data = NULL; | ||
| 539 | } | ||
| 540 | break; | ||
| 541 | } | ||
| 542 | |||
| 543 | if (si_sk != NULL) | ||
| 544 | { | ||
| 545 | if ((buf=BUF_MEM_new()) == NULL) | ||
| 546 | { | ||
| 547 | PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_BIO_LIB); | ||
| 548 | goto err; | ||
| 549 | } | ||
| 550 | for (i=0; i<sk_PKCS7_SIGNER_INFO_num(si_sk); i++) | ||
| 551 | { | ||
| 552 | si=sk_PKCS7_SIGNER_INFO_value(si_sk,i); | ||
| 553 | if (si->pkey == NULL) continue; | ||
| 554 | |||
| 555 | j=OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 556 | |||
| 557 | btmp=bio; | ||
| 558 | for (;;) | ||
| 559 | { | ||
| 560 | if ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) | ||
| 561 | == NULL) | ||
| 562 | { | ||
| 563 | PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 564 | goto err; | ||
| 565 | } | ||
| 566 | BIO_get_md_ctx(btmp,&mdc); | ||
| 567 | if (mdc == NULL) | ||
| 568 | { | ||
| 569 | PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_INTERNAL_ERROR); | ||
| 570 | goto err; | ||
| 571 | } | ||
| 572 | if (EVP_MD_CTX_type(mdc) == j) | ||
| 573 | break; | ||
| 574 | else | ||
| 575 | btmp=BIO_next(btmp); | ||
| 576 | } | ||
| 577 | |||
| 578 | /* We now have the EVP_MD_CTX, lets do the | ||
| 579 | * signing. */ | ||
| 580 | EVP_MD_CTX_copy_ex(&ctx_tmp,mdc); | ||
| 581 | if (!BUF_MEM_grow(buf,EVP_PKEY_size(si->pkey))) | ||
| 582 | { | ||
| 583 | PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_BIO_LIB); | ||
| 584 | goto err; | ||
| 585 | } | ||
| 586 | |||
| 587 | sk=si->auth_attr; | ||
| 588 | |||
| 589 | /* If there are attributes, we add the digest | ||
| 590 | * attribute and only sign the attributes */ | ||
| 591 | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) | ||
| 592 | { | ||
| 593 | unsigned char md_data[EVP_MAX_MD_SIZE], *abuf=NULL; | ||
| 594 | unsigned int md_len, alen; | ||
| 595 | ASN1_OCTET_STRING *digest; | ||
| 596 | ASN1_UTCTIME *sign_time; | ||
| 597 | const EVP_MD *md_tmp; | ||
| 598 | |||
| 599 | /* Add signing time if not already present */ | ||
| 600 | if (!PKCS7_get_signed_attribute(si, | ||
| 601 | NID_pkcs9_signingTime)) | ||
| 602 | { | ||
| 603 | sign_time=X509_gmtime_adj(NULL,0); | ||
| 604 | PKCS7_add_signed_attribute(si, | ||
| 605 | NID_pkcs9_signingTime, | ||
| 606 | V_ASN1_UTCTIME,sign_time); | ||
| 607 | } | ||
| 608 | |||
| 609 | /* Add digest */ | ||
| 610 | md_tmp=EVP_MD_CTX_md(&ctx_tmp); | ||
| 611 | EVP_DigestFinal_ex(&ctx_tmp,md_data,&md_len); | ||
| 612 | digest=M_ASN1_OCTET_STRING_new(); | ||
| 613 | M_ASN1_OCTET_STRING_set(digest,md_data,md_len); | ||
| 614 | PKCS7_add_signed_attribute(si, | ||
| 615 | NID_pkcs9_messageDigest, | ||
| 616 | V_ASN1_OCTET_STRING,digest); | ||
| 617 | |||
| 618 | /* Now sign the attributes */ | ||
| 619 | EVP_SignInit_ex(&ctx_tmp,md_tmp,NULL); | ||
| 620 | alen = ASN1_item_i2d((ASN1_VALUE *)sk,&abuf, | ||
| 621 | ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); | ||
| 622 | if(!abuf) goto err; | ||
| 623 | EVP_SignUpdate(&ctx_tmp,abuf,alen); | ||
| 624 | OPENSSL_free(abuf); | ||
| 625 | } | ||
| 626 | |||
| 627 | #ifndef OPENSSL_NO_DSA | ||
| 628 | if (si->pkey->type == EVP_PKEY_DSA) | ||
| 629 | ctx_tmp.digest=EVP_dss1(); | ||
| 630 | #endif | ||
| 631 | |||
| 632 | if (!EVP_SignFinal(&ctx_tmp,(unsigned char *)buf->data, | ||
| 633 | (unsigned int *)&buf->length,si->pkey)) | ||
| 634 | { | ||
| 635 | PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_EVP_LIB); | ||
| 636 | goto err; | ||
| 637 | } | ||
| 638 | if (!ASN1_STRING_set(si->enc_digest, | ||
| 639 | (unsigned char *)buf->data,buf->length)) | ||
| 640 | { | ||
| 641 | PKCS7err(PKCS7_F_PKCS7_DATASIGN,ERR_R_ASN1_LIB); | ||
| 642 | goto err; | ||
| 643 | } | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | if (!PKCS7_is_detached(p7)) | ||
| 648 | { | ||
| 649 | btmp=BIO_find_type(bio,BIO_TYPE_MEM); | ||
| 650 | if (btmp == NULL) | ||
| 651 | { | ||
| 652 | PKCS7err(PKCS7_F_PKCS7_DATASIGN,PKCS7_R_UNABLE_TO_FIND_MEM_BIO); | ||
| 653 | goto err; | ||
| 654 | } | ||
| 655 | BIO_get_mem_ptr(btmp,&buf_mem); | ||
| 656 | /* Mark the BIO read only then we can use its copy of the data | ||
| 657 | * instead of making an extra copy. | ||
| 658 | */ | ||
| 659 | BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY); | ||
| 660 | BIO_set_mem_eof_return(btmp, 0); | ||
| 661 | os->data = (unsigned char *)buf_mem->data; | ||
| 662 | os->length = buf_mem->length; | ||
| 663 | #if 0 | ||
| 664 | M_ASN1_OCTET_STRING_set(os, | ||
| 665 | (unsigned char *)buf_mem->data,buf_mem->length); | ||
| 666 | #endif | ||
| 667 | } | ||
| 668 | ret=1; | ||
| 669 | err: | ||
| 670 | EVP_MD_CTX_cleanup(&ctx_tmp); | ||
| 671 | if (buf != NULL) BUF_MEM_free(buf); | ||
| 672 | return(ret); | ||
| 673 | } | ||
| 674 | |||
| 675 | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio, | ||
| 676 | PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 677 | { | ||
| 678 | PKCS7_ISSUER_AND_SERIAL *ias; | ||
| 679 | int ret=0,i; | ||
| 680 | STACK_OF(X509) *cert; | ||
| 681 | X509 *x509; | ||
| 682 | |||
| 683 | if (PKCS7_type_is_signed(p7)) | ||
| 684 | { | ||
| 685 | cert=p7->d.sign->cert; | ||
| 686 | } | ||
| 687 | else if (PKCS7_type_is_signedAndEnveloped(p7)) | ||
| 688 | { | ||
| 689 | cert=p7->d.signed_and_enveloped->cert; | ||
| 690 | } | ||
| 691 | else | ||
| 692 | { | ||
| 693 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 694 | goto err; | ||
| 695 | } | ||
| 696 | /* XXXXXXXXXXXXXXXXXXXXXXX */ | ||
| 697 | ias=si->issuer_and_serial; | ||
| 698 | |||
| 699 | x509=X509_find_by_issuer_and_serial(cert,ias->issuer,ias->serial); | ||
| 700 | |||
| 701 | /* were we able to find the cert in passed to us */ | ||
| 702 | if (x509 == NULL) | ||
| 703 | { | ||
| 704 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE); | ||
| 705 | goto err; | ||
| 706 | } | ||
| 707 | |||
| 708 | /* Lets verify */ | ||
| 709 | if(!X509_STORE_CTX_init(ctx,cert_store,x509,cert)) | ||
| 710 | { | ||
| 711 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); | ||
| 712 | goto err; | ||
| 713 | } | ||
| 714 | X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN); | ||
| 715 | i=X509_verify_cert(ctx); | ||
| 716 | if (i <= 0) | ||
| 717 | { | ||
| 718 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); | ||
| 719 | X509_STORE_CTX_cleanup(ctx); | ||
| 720 | goto err; | ||
| 721 | } | ||
| 722 | X509_STORE_CTX_cleanup(ctx); | ||
| 723 | |||
| 724 | return PKCS7_signatureVerify(bio, p7, si, x509); | ||
| 725 | err: | ||
| 726 | return ret; | ||
| 727 | } | ||
| 728 | |||
| 729 | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, | ||
| 730 | X509 *x509) | ||
| 731 | { | ||
| 732 | ASN1_OCTET_STRING *os; | ||
| 733 | EVP_MD_CTX mdc_tmp,*mdc; | ||
| 734 | int ret=0,i; | ||
| 735 | int md_type; | ||
| 736 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 737 | BIO *btmp; | ||
| 738 | EVP_PKEY *pkey; | ||
| 739 | |||
| 740 | EVP_MD_CTX_init(&mdc_tmp); | ||
| 741 | |||
| 742 | if (!PKCS7_type_is_signed(p7) && | ||
| 743 | !PKCS7_type_is_signedAndEnveloped(p7)) { | ||
| 744 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 745 | PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 746 | goto err; | ||
| 747 | } | ||
| 748 | |||
| 749 | md_type=OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 750 | |||
| 751 | btmp=bio; | ||
| 752 | for (;;) | ||
| 753 | { | ||
| 754 | if ((btmp == NULL) || | ||
| 755 | ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL)) | ||
| 756 | { | ||
| 757 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 758 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 759 | goto err; | ||
| 760 | } | ||
| 761 | BIO_get_md_ctx(btmp,&mdc); | ||
| 762 | if (mdc == NULL) | ||
| 763 | { | ||
| 764 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 765 | ERR_R_INTERNAL_ERROR); | ||
| 766 | goto err; | ||
| 767 | } | ||
| 768 | if (EVP_MD_CTX_type(mdc) == md_type) | ||
| 769 | break; | ||
| 770 | btmp=BIO_next(btmp); | ||
| 771 | } | ||
| 772 | |||
| 773 | /* mdc is the digest ctx that we want, unless there are attributes, | ||
| 774 | * in which case the digest is the signed attributes */ | ||
| 775 | EVP_MD_CTX_copy_ex(&mdc_tmp,mdc); | ||
| 776 | |||
| 777 | sk=si->auth_attr; | ||
| 778 | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) | ||
| 779 | { | ||
| 780 | unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL; | ||
| 781 | unsigned int md_len, alen; | ||
| 782 | ASN1_OCTET_STRING *message_digest; | ||
| 783 | |||
| 784 | EVP_DigestFinal_ex(&mdc_tmp,md_dat,&md_len); | ||
| 785 | message_digest=PKCS7_digest_from_attributes(sk); | ||
| 786 | if (!message_digest) | ||
| 787 | { | ||
| 788 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 789 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 790 | goto err; | ||
| 791 | } | ||
| 792 | if ((message_digest->length != (int)md_len) || | ||
| 793 | (memcmp(message_digest->data,md_dat,md_len))) | ||
| 794 | { | ||
| 795 | #if 0 | ||
| 796 | { | ||
| 797 | int ii; | ||
| 798 | for (ii=0; ii<message_digest->length; ii++) | ||
| 799 | printf("%02X",message_digest->data[ii]); printf(" sent\n"); | ||
| 800 | for (ii=0; ii<md_len; ii++) printf("%02X",md_dat[ii]); printf(" calc\n"); | ||
| 801 | } | ||
| 802 | #endif | ||
| 803 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 804 | PKCS7_R_DIGEST_FAILURE); | ||
| 805 | ret= -1; | ||
| 806 | goto err; | ||
| 807 | } | ||
| 808 | |||
| 809 | EVP_VerifyInit_ex(&mdc_tmp,EVP_get_digestbynid(md_type), NULL); | ||
| 810 | |||
| 811 | alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, | ||
| 812 | ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); | ||
| 813 | EVP_VerifyUpdate(&mdc_tmp, abuf, alen); | ||
| 814 | |||
| 815 | OPENSSL_free(abuf); | ||
| 816 | } | ||
| 817 | |||
| 818 | os=si->enc_digest; | ||
| 819 | pkey = X509_get_pubkey(x509); | ||
| 820 | if (!pkey) | ||
| 821 | { | ||
| 822 | ret = -1; | ||
| 823 | goto err; | ||
| 824 | } | ||
| 825 | #ifndef OPENSSL_NO_DSA | ||
| 826 | if(pkey->type == EVP_PKEY_DSA) mdc_tmp.digest=EVP_dss1(); | ||
| 827 | #endif | ||
| 828 | |||
| 829 | i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length, pkey); | ||
| 830 | EVP_PKEY_free(pkey); | ||
| 831 | if (i <= 0) | ||
| 832 | { | ||
| 833 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 834 | PKCS7_R_SIGNATURE_FAILURE); | ||
| 835 | ret= -1; | ||
| 836 | goto err; | ||
| 837 | } | ||
| 838 | else | ||
| 839 | ret=1; | ||
| 840 | err: | ||
| 841 | EVP_MD_CTX_cleanup(&mdc_tmp); | ||
| 842 | return(ret); | ||
| 843 | } | ||
| 844 | |||
| 845 | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) | ||
| 846 | { | ||
| 847 | STACK_OF(PKCS7_RECIP_INFO) *rsk; | ||
| 848 | PKCS7_RECIP_INFO *ri; | ||
| 849 | int i; | ||
| 850 | |||
| 851 | i=OBJ_obj2nid(p7->type); | ||
| 852 | if (i != NID_pkcs7_signedAndEnveloped) return(NULL); | ||
| 853 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 854 | ri=sk_PKCS7_RECIP_INFO_value(rsk,0); | ||
| 855 | if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL); | ||
| 856 | ri=sk_PKCS7_RECIP_INFO_value(rsk,idx); | ||
| 857 | return(ri->issuer_and_serial); | ||
| 858 | } | ||
| 859 | |||
| 860 | ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 861 | { | ||
| 862 | return(get_attribute(si->auth_attr,nid)); | ||
| 863 | } | ||
| 864 | |||
| 865 | ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 866 | { | ||
| 867 | return(get_attribute(si->unauth_attr,nid)); | ||
| 868 | } | ||
| 869 | |||
| 870 | static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid) | ||
| 871 | { | ||
| 872 | int i; | ||
| 873 | X509_ATTRIBUTE *xa; | ||
| 874 | ASN1_OBJECT *o; | ||
| 875 | |||
| 876 | o=OBJ_nid2obj(nid); | ||
| 877 | if (!o || !sk) return(NULL); | ||
| 878 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 879 | { | ||
| 880 | xa=sk_X509_ATTRIBUTE_value(sk,i); | ||
| 881 | if (OBJ_cmp(xa->object,o) == 0) | ||
| 882 | { | ||
| 883 | if (!xa->single && sk_ASN1_TYPE_num(xa->value.set)) | ||
| 884 | return(sk_ASN1_TYPE_value(xa->value.set,0)); | ||
| 885 | else | ||
| 886 | return(NULL); | ||
| 887 | } | ||
| 888 | } | ||
| 889 | return(NULL); | ||
| 890 | } | ||
| 891 | |||
| 892 | ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 893 | { | ||
| 894 | ASN1_TYPE *astype; | ||
| 895 | if(!(astype = get_attribute(sk, NID_pkcs9_messageDigest))) return NULL; | ||
| 896 | return astype->value.octet_string; | ||
| 897 | } | ||
| 898 | |||
| 899 | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, | ||
| 900 | STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 901 | { | ||
| 902 | int i; | ||
| 903 | |||
| 904 | if (p7si->auth_attr != NULL) | ||
| 905 | sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr,X509_ATTRIBUTE_free); | ||
| 906 | p7si->auth_attr=sk_X509_ATTRIBUTE_dup(sk); | ||
| 907 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 908 | { | ||
| 909 | if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr,i, | ||
| 910 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) | ||
| 911 | == NULL) | ||
| 912 | return(0); | ||
| 913 | } | ||
| 914 | return(1); | ||
| 915 | } | ||
| 916 | |||
| 917 | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 918 | { | ||
| 919 | int i; | ||
| 920 | |||
| 921 | if (p7si->unauth_attr != NULL) | ||
| 922 | sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, | ||
| 923 | X509_ATTRIBUTE_free); | ||
| 924 | p7si->unauth_attr=sk_X509_ATTRIBUTE_dup(sk); | ||
| 925 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 926 | { | ||
| 927 | if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr,i, | ||
| 928 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) | ||
| 929 | == NULL) | ||
| 930 | return(0); | ||
| 931 | } | ||
| 932 | return(1); | ||
| 933 | } | ||
| 934 | |||
| 935 | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 936 | void *value) | ||
| 937 | { | ||
| 938 | return(add_attribute(&(p7si->auth_attr),nid,atrtype,value)); | ||
| 939 | } | ||
| 940 | |||
| 941 | int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 942 | void *value) | ||
| 943 | { | ||
| 944 | return(add_attribute(&(p7si->unauth_attr),nid,atrtype,value)); | ||
| 945 | } | ||
| 946 | |||
| 947 | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, | ||
| 948 | void *value) | ||
| 949 | { | ||
| 950 | X509_ATTRIBUTE *attr=NULL; | ||
| 951 | |||
| 952 | if (*sk == NULL) | ||
| 953 | { | ||
| 954 | *sk = sk_X509_ATTRIBUTE_new_null(); | ||
| 955 | new_attrib: | ||
| 956 | attr=X509_ATTRIBUTE_create(nid,atrtype,value); | ||
| 957 | sk_X509_ATTRIBUTE_push(*sk,attr); | ||
| 958 | } | ||
| 959 | else | ||
| 960 | { | ||
| 961 | int i; | ||
| 962 | |||
| 963 | for (i=0; i<sk_X509_ATTRIBUTE_num(*sk); i++) | ||
| 964 | { | ||
| 965 | attr=sk_X509_ATTRIBUTE_value(*sk,i); | ||
| 966 | if (OBJ_obj2nid(attr->object) == nid) | ||
| 967 | { | ||
| 968 | X509_ATTRIBUTE_free(attr); | ||
| 969 | attr=X509_ATTRIBUTE_create(nid,atrtype,value); | ||
| 970 | sk_X509_ATTRIBUTE_set(*sk,i,attr); | ||
| 971 | goto end; | ||
| 972 | } | ||
| 973 | } | ||
| 974 | goto new_attrib; | ||
| 975 | } | ||
| 976 | end: | ||
| 977 | return(1); | ||
| 978 | } | ||
| 979 | |||
diff --git a/src/lib/libcrypto/pkcs7/pk7_lib.c b/src/lib/libcrypto/pkcs7/pk7_lib.c new file mode 100644 index 0000000000..c00ed6833a --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_lib.c | |||
| @@ -0,0 +1,477 @@ | |||
| 1 | /* crypto/pkcs7/pk7_lib.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/objects.h> | ||
| 62 | #include <openssl/x509.h> | ||
| 63 | |||
| 64 | long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg) | ||
| 65 | { | ||
| 66 | int nid; | ||
| 67 | long ret; | ||
| 68 | |||
| 69 | nid=OBJ_obj2nid(p7->type); | ||
| 70 | |||
| 71 | switch (cmd) | ||
| 72 | { | ||
| 73 | case PKCS7_OP_SET_DETACHED_SIGNATURE: | ||
| 74 | if (nid == NID_pkcs7_signed) | ||
| 75 | { | ||
| 76 | ret=p7->detached=(int)larg; | ||
| 77 | } | ||
| 78 | else | ||
| 79 | { | ||
| 80 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); | ||
| 81 | ret=0; | ||
| 82 | } | ||
| 83 | break; | ||
| 84 | case PKCS7_OP_GET_DETACHED_SIGNATURE: | ||
| 85 | if (nid == NID_pkcs7_signed) | ||
| 86 | { | ||
| 87 | if(!p7->d.sign || !p7->d.sign->contents->d.ptr) | ||
| 88 | ret = 1; | ||
| 89 | else ret = 0; | ||
| 90 | |||
| 91 | p7->detached = ret; | ||
| 92 | } | ||
| 93 | else | ||
| 94 | { | ||
| 95 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); | ||
| 96 | ret=0; | ||
| 97 | } | ||
| 98 | |||
| 99 | break; | ||
| 100 | default: | ||
| 101 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_UNKNOWN_OPERATION); | ||
| 102 | ret=0; | ||
| 103 | } | ||
| 104 | return(ret); | ||
| 105 | } | ||
| 106 | |||
| 107 | int PKCS7_content_new(PKCS7 *p7, int type) | ||
| 108 | { | ||
| 109 | PKCS7 *ret=NULL; | ||
| 110 | |||
| 111 | if ((ret=PKCS7_new()) == NULL) goto err; | ||
| 112 | if (!PKCS7_set_type(ret,type)) goto err; | ||
| 113 | if (!PKCS7_set_content(p7,ret)) goto err; | ||
| 114 | |||
| 115 | return(1); | ||
| 116 | err: | ||
| 117 | if (ret != NULL) PKCS7_free(ret); | ||
| 118 | return(0); | ||
| 119 | } | ||
| 120 | |||
| 121 | int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data) | ||
| 122 | { | ||
| 123 | int i; | ||
| 124 | |||
| 125 | i=OBJ_obj2nid(p7->type); | ||
| 126 | switch (i) | ||
| 127 | { | ||
| 128 | case NID_pkcs7_signed: | ||
| 129 | if (p7->d.sign->contents != NULL) | ||
| 130 | PKCS7_free(p7->d.sign->contents); | ||
| 131 | p7->d.sign->contents=p7_data; | ||
| 132 | break; | ||
| 133 | case NID_pkcs7_digest: | ||
| 134 | case NID_pkcs7_data: | ||
| 135 | case NID_pkcs7_enveloped: | ||
| 136 | case NID_pkcs7_signedAndEnveloped: | ||
| 137 | case NID_pkcs7_encrypted: | ||
| 138 | default: | ||
| 139 | PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 140 | goto err; | ||
| 141 | } | ||
| 142 | return(1); | ||
| 143 | err: | ||
| 144 | return(0); | ||
| 145 | } | ||
| 146 | |||
| 147 | int PKCS7_set_type(PKCS7 *p7, int type) | ||
| 148 | { | ||
| 149 | ASN1_OBJECT *obj; | ||
| 150 | |||
| 151 | /*PKCS7_content_free(p7);*/ | ||
| 152 | obj=OBJ_nid2obj(type); /* will not fail */ | ||
| 153 | |||
| 154 | switch (type) | ||
| 155 | { | ||
| 156 | case NID_pkcs7_signed: | ||
| 157 | p7->type=obj; | ||
| 158 | if ((p7->d.sign=PKCS7_SIGNED_new()) == NULL) | ||
| 159 | goto err; | ||
| 160 | ASN1_INTEGER_set(p7->d.sign->version,1); | ||
| 161 | break; | ||
| 162 | case NID_pkcs7_data: | ||
| 163 | p7->type=obj; | ||
| 164 | if ((p7->d.data=M_ASN1_OCTET_STRING_new()) == NULL) | ||
| 165 | goto err; | ||
| 166 | break; | ||
| 167 | case NID_pkcs7_signedAndEnveloped: | ||
| 168 | p7->type=obj; | ||
| 169 | if ((p7->d.signed_and_enveloped=PKCS7_SIGN_ENVELOPE_new()) | ||
| 170 | == NULL) goto err; | ||
| 171 | ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1); | ||
| 172 | p7->d.signed_and_enveloped->enc_data->content_type | ||
| 173 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 174 | break; | ||
| 175 | case NID_pkcs7_enveloped: | ||
| 176 | p7->type=obj; | ||
| 177 | if ((p7->d.enveloped=PKCS7_ENVELOPE_new()) | ||
| 178 | == NULL) goto err; | ||
| 179 | ASN1_INTEGER_set(p7->d.enveloped->version,0); | ||
| 180 | p7->d.enveloped->enc_data->content_type | ||
| 181 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 182 | break; | ||
| 183 | case NID_pkcs7_encrypted: | ||
| 184 | p7->type=obj; | ||
| 185 | if ((p7->d.encrypted=PKCS7_ENCRYPT_new()) | ||
| 186 | == NULL) goto err; | ||
| 187 | ASN1_INTEGER_set(p7->d.encrypted->version,0); | ||
| 188 | p7->d.encrypted->enc_data->content_type | ||
| 189 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 190 | break; | ||
| 191 | |||
| 192 | case NID_pkcs7_digest: | ||
| 193 | default: | ||
| 194 | PKCS7err(PKCS7_F_PKCS7_SET_TYPE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 195 | goto err; | ||
| 196 | } | ||
| 197 | return(1); | ||
| 198 | err: | ||
| 199 | return(0); | ||
| 200 | } | ||
| 201 | |||
| 202 | int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi) | ||
| 203 | { | ||
| 204 | int i,j,nid; | ||
| 205 | X509_ALGOR *alg; | ||
| 206 | STACK_OF(PKCS7_SIGNER_INFO) *signer_sk; | ||
| 207 | STACK_OF(X509_ALGOR) *md_sk; | ||
| 208 | |||
| 209 | i=OBJ_obj2nid(p7->type); | ||
| 210 | switch (i) | ||
| 211 | { | ||
| 212 | case NID_pkcs7_signed: | ||
| 213 | signer_sk= p7->d.sign->signer_info; | ||
| 214 | md_sk= p7->d.sign->md_algs; | ||
| 215 | break; | ||
| 216 | case NID_pkcs7_signedAndEnveloped: | ||
| 217 | signer_sk= p7->d.signed_and_enveloped->signer_info; | ||
| 218 | md_sk= p7->d.signed_and_enveloped->md_algs; | ||
| 219 | break; | ||
| 220 | default: | ||
| 221 | PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 222 | return(0); | ||
| 223 | } | ||
| 224 | |||
| 225 | nid=OBJ_obj2nid(psi->digest_alg->algorithm); | ||
| 226 | |||
| 227 | /* If the digest is not currently listed, add it */ | ||
| 228 | j=0; | ||
| 229 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 230 | { | ||
| 231 | alg=sk_X509_ALGOR_value(md_sk,i); | ||
| 232 | if (OBJ_obj2nid(alg->algorithm) == nid) | ||
| 233 | { | ||
| 234 | j=1; | ||
| 235 | break; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | if (!j) /* we need to add another algorithm */ | ||
| 239 | { | ||
| 240 | if(!(alg=X509_ALGOR_new()) | ||
| 241 | || !(alg->parameter = ASN1_TYPE_new())) { | ||
| 242 | PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE); | ||
| 243 | return(0); | ||
| 244 | } | ||
| 245 | alg->algorithm=OBJ_nid2obj(nid); | ||
| 246 | alg->parameter->type = V_ASN1_NULL; | ||
| 247 | sk_X509_ALGOR_push(md_sk,alg); | ||
| 248 | } | ||
| 249 | |||
| 250 | sk_PKCS7_SIGNER_INFO_push(signer_sk,psi); | ||
| 251 | return(1); | ||
| 252 | } | ||
| 253 | |||
| 254 | int PKCS7_add_certificate(PKCS7 *p7, X509 *x509) | ||
| 255 | { | ||
| 256 | int i; | ||
| 257 | STACK_OF(X509) **sk; | ||
| 258 | |||
| 259 | i=OBJ_obj2nid(p7->type); | ||
| 260 | switch (i) | ||
| 261 | { | ||
| 262 | case NID_pkcs7_signed: | ||
| 263 | sk= &(p7->d.sign->cert); | ||
| 264 | break; | ||
| 265 | case NID_pkcs7_signedAndEnveloped: | ||
| 266 | sk= &(p7->d.signed_and_enveloped->cert); | ||
| 267 | break; | ||
| 268 | default: | ||
| 269 | PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 270 | return(0); | ||
| 271 | } | ||
| 272 | |||
| 273 | if (*sk == NULL) | ||
| 274 | *sk=sk_X509_new_null(); | ||
| 275 | CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); | ||
| 276 | sk_X509_push(*sk,x509); | ||
| 277 | return(1); | ||
| 278 | } | ||
| 279 | |||
| 280 | int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) | ||
| 281 | { | ||
| 282 | int i; | ||
| 283 | STACK_OF(X509_CRL) **sk; | ||
| 284 | |||
| 285 | i=OBJ_obj2nid(p7->type); | ||
| 286 | switch (i) | ||
| 287 | { | ||
| 288 | case NID_pkcs7_signed: | ||
| 289 | sk= &(p7->d.sign->crl); | ||
| 290 | break; | ||
| 291 | case NID_pkcs7_signedAndEnveloped: | ||
| 292 | sk= &(p7->d.signed_and_enveloped->crl); | ||
| 293 | break; | ||
| 294 | default: | ||
| 295 | PKCS7err(PKCS7_F_PKCS7_ADD_CRL,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 296 | return(0); | ||
| 297 | } | ||
| 298 | |||
| 299 | if (*sk == NULL) | ||
| 300 | *sk=sk_X509_CRL_new_null(); | ||
| 301 | |||
| 302 | CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL); | ||
| 303 | sk_X509_CRL_push(*sk,crl); | ||
| 304 | return(1); | ||
| 305 | } | ||
| 306 | |||
| 307 | int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, | ||
| 308 | const EVP_MD *dgst) | ||
| 309 | { | ||
| 310 | char is_dsa; | ||
| 311 | if (pkey->type == EVP_PKEY_DSA) is_dsa = 1; | ||
| 312 | else is_dsa = 0; | ||
| 313 | /* We now need to add another PKCS7_SIGNER_INFO entry */ | ||
| 314 | ASN1_INTEGER_set(p7i->version,1); | ||
| 315 | X509_NAME_set(&p7i->issuer_and_serial->issuer, | ||
| 316 | X509_get_issuer_name(x509)); | ||
| 317 | |||
| 318 | /* because ASN1_INTEGER_set is used to set a 'long' we will do | ||
| 319 | * things the ugly way. */ | ||
| 320 | M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); | ||
| 321 | p7i->issuer_and_serial->serial= | ||
| 322 | M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)); | ||
| 323 | |||
| 324 | /* lets keep the pkey around for a while */ | ||
| 325 | CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); | ||
| 326 | p7i->pkey=pkey; | ||
| 327 | |||
| 328 | /* Set the algorithms */ | ||
| 329 | if (is_dsa) p7i->digest_alg->algorithm=OBJ_nid2obj(NID_sha1); | ||
| 330 | else | ||
| 331 | p7i->digest_alg->algorithm=OBJ_nid2obj(EVP_MD_type(dgst)); | ||
| 332 | |||
| 333 | if (p7i->digest_alg->parameter != NULL) | ||
| 334 | ASN1_TYPE_free(p7i->digest_alg->parameter); | ||
| 335 | if ((p7i->digest_alg->parameter=ASN1_TYPE_new()) == NULL) | ||
| 336 | goto err; | ||
| 337 | p7i->digest_alg->parameter->type=V_ASN1_NULL; | ||
| 338 | |||
| 339 | p7i->digest_enc_alg->algorithm=OBJ_nid2obj(EVP_PKEY_type(pkey->type)); | ||
| 340 | |||
| 341 | if (p7i->digest_enc_alg->parameter != NULL) | ||
| 342 | ASN1_TYPE_free(p7i->digest_enc_alg->parameter); | ||
| 343 | if(is_dsa) p7i->digest_enc_alg->parameter = NULL; | ||
| 344 | else { | ||
| 345 | if (!(p7i->digest_enc_alg->parameter=ASN1_TYPE_new())) | ||
| 346 | goto err; | ||
| 347 | p7i->digest_enc_alg->parameter->type=V_ASN1_NULL; | ||
| 348 | } | ||
| 349 | |||
| 350 | return(1); | ||
| 351 | err: | ||
| 352 | return(0); | ||
| 353 | } | ||
| 354 | |||
| 355 | PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey, | ||
| 356 | const EVP_MD *dgst) | ||
| 357 | { | ||
| 358 | PKCS7_SIGNER_INFO *si; | ||
| 359 | |||
| 360 | if ((si=PKCS7_SIGNER_INFO_new()) == NULL) goto err; | ||
| 361 | if (!PKCS7_SIGNER_INFO_set(si,x509,pkey,dgst)) goto err; | ||
| 362 | if (!PKCS7_add_signer(p7,si)) goto err; | ||
| 363 | return(si); | ||
| 364 | err: | ||
| 365 | return(NULL); | ||
| 366 | } | ||
| 367 | |||
| 368 | STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7) | ||
| 369 | { | ||
| 370 | if (PKCS7_type_is_signed(p7)) | ||
| 371 | { | ||
| 372 | return(p7->d.sign->signer_info); | ||
| 373 | } | ||
| 374 | else if (PKCS7_type_is_signedAndEnveloped(p7)) | ||
| 375 | { | ||
| 376 | return(p7->d.signed_and_enveloped->signer_info); | ||
| 377 | } | ||
| 378 | else | ||
| 379 | return(NULL); | ||
| 380 | } | ||
| 381 | |||
| 382 | PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509) | ||
| 383 | { | ||
| 384 | PKCS7_RECIP_INFO *ri; | ||
| 385 | |||
| 386 | if ((ri=PKCS7_RECIP_INFO_new()) == NULL) goto err; | ||
| 387 | if (!PKCS7_RECIP_INFO_set(ri,x509)) goto err; | ||
| 388 | if (!PKCS7_add_recipient_info(p7,ri)) goto err; | ||
| 389 | return(ri); | ||
| 390 | err: | ||
| 391 | return(NULL); | ||
| 392 | } | ||
| 393 | |||
| 394 | int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) | ||
| 395 | { | ||
| 396 | int i; | ||
| 397 | STACK_OF(PKCS7_RECIP_INFO) *sk; | ||
| 398 | |||
| 399 | i=OBJ_obj2nid(p7->type); | ||
| 400 | switch (i) | ||
| 401 | { | ||
| 402 | case NID_pkcs7_signedAndEnveloped: | ||
| 403 | sk= p7->d.signed_and_enveloped->recipientinfo; | ||
| 404 | break; | ||
| 405 | case NID_pkcs7_enveloped: | ||
| 406 | sk= p7->d.enveloped->recipientinfo; | ||
| 407 | break; | ||
| 408 | default: | ||
| 409 | PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 410 | return(0); | ||
| 411 | } | ||
| 412 | |||
| 413 | sk_PKCS7_RECIP_INFO_push(sk,ri); | ||
| 414 | return(1); | ||
| 415 | } | ||
| 416 | |||
| 417 | int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509) | ||
| 418 | { | ||
| 419 | ASN1_INTEGER_set(p7i->version,0); | ||
| 420 | X509_NAME_set(&p7i->issuer_and_serial->issuer, | ||
| 421 | X509_get_issuer_name(x509)); | ||
| 422 | |||
| 423 | M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); | ||
| 424 | p7i->issuer_and_serial->serial= | ||
| 425 | M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)); | ||
| 426 | |||
| 427 | X509_ALGOR_free(p7i->key_enc_algor); | ||
| 428 | p7i->key_enc_algor= X509_ALGOR_dup(x509->cert_info->key->algor); | ||
| 429 | |||
| 430 | CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); | ||
| 431 | p7i->cert=x509; | ||
| 432 | |||
| 433 | return(1); | ||
| 434 | } | ||
| 435 | |||
| 436 | X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 437 | { | ||
| 438 | if (PKCS7_type_is_signed(p7)) | ||
| 439 | return(X509_find_by_issuer_and_serial(p7->d.sign->cert, | ||
| 440 | si->issuer_and_serial->issuer, | ||
| 441 | si->issuer_and_serial->serial)); | ||
| 442 | else | ||
| 443 | return(NULL); | ||
| 444 | } | ||
| 445 | |||
| 446 | int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher) | ||
| 447 | { | ||
| 448 | int i; | ||
| 449 | ASN1_OBJECT *objtmp; | ||
| 450 | PKCS7_ENC_CONTENT *ec; | ||
| 451 | |||
| 452 | i=OBJ_obj2nid(p7->type); | ||
| 453 | switch (i) | ||
| 454 | { | ||
| 455 | case NID_pkcs7_signedAndEnveloped: | ||
| 456 | ec=p7->d.signed_and_enveloped->enc_data; | ||
| 457 | break; | ||
| 458 | case NID_pkcs7_enveloped: | ||
| 459 | ec=p7->d.enveloped->enc_data; | ||
| 460 | break; | ||
| 461 | default: | ||
| 462 | PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 463 | return(0); | ||
| 464 | } | ||
| 465 | |||
| 466 | /* Check cipher OID exists and has data in it*/ | ||
| 467 | i = EVP_CIPHER_type(cipher); | ||
| 468 | if(i == NID_undef) { | ||
| 469 | PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); | ||
| 470 | return(0); | ||
| 471 | } | ||
| 472 | objtmp = OBJ_nid2obj(i); | ||
| 473 | |||
| 474 | ec->cipher = cipher; | ||
| 475 | return 1; | ||
| 476 | } | ||
| 477 | |||
diff --git a/src/lib/libcrypto/pkcs7/pk7_mime.c b/src/lib/libcrypto/pkcs7/pk7_mime.c new file mode 100644 index 0000000000..086d394270 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_mime.c | |||
| @@ -0,0 +1,685 @@ | |||
| 1 | /* pk7_mime.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 1999. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <ctype.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include <openssl/rand.h> | ||
| 63 | #include <openssl/x509.h> | ||
| 64 | |||
| 65 | /* MIME and related routines */ | ||
| 66 | |||
| 67 | /* MIME format structures | ||
| 68 | * Note that all are translated to lower case apart from | ||
| 69 | * parameter values. Quotes are stripped off | ||
| 70 | */ | ||
| 71 | |||
| 72 | typedef struct { | ||
| 73 | char *param_name; /* Param name e.g. "micalg" */ | ||
| 74 | char *param_value; /* Param value e.g. "sha1" */ | ||
| 75 | } MIME_PARAM; | ||
| 76 | |||
| 77 | DECLARE_STACK_OF(MIME_PARAM) | ||
| 78 | IMPLEMENT_STACK_OF(MIME_PARAM) | ||
| 79 | |||
| 80 | typedef struct { | ||
| 81 | char *name; /* Name of line e.g. "content-type" */ | ||
| 82 | char *value; /* Value of line e.g. "text/plain" */ | ||
| 83 | STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */ | ||
| 84 | } MIME_HEADER; | ||
| 85 | |||
| 86 | DECLARE_STACK_OF(MIME_HEADER) | ||
| 87 | IMPLEMENT_STACK_OF(MIME_HEADER) | ||
| 88 | |||
| 89 | static int B64_write_PKCS7(BIO *bio, PKCS7 *p7); | ||
| 90 | static PKCS7 *B64_read_PKCS7(BIO *bio); | ||
| 91 | static char * strip_ends(char *name); | ||
| 92 | static char * strip_start(char *name); | ||
| 93 | static char * strip_end(char *name); | ||
| 94 | static MIME_HEADER *mime_hdr_new(char *name, char *value); | ||
| 95 | static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value); | ||
| 96 | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio); | ||
| 97 | static int mime_hdr_cmp(const MIME_HEADER * const *a, | ||
| 98 | const MIME_HEADER * const *b); | ||
| 99 | static int mime_param_cmp(const MIME_PARAM * const *a, | ||
| 100 | const MIME_PARAM * const *b); | ||
| 101 | static void mime_param_free(MIME_PARAM *param); | ||
| 102 | static int mime_bound_check(char *line, int linelen, char *bound, int blen); | ||
| 103 | static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret); | ||
| 104 | static int iscrlf(char c); | ||
| 105 | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name); | ||
| 106 | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name); | ||
| 107 | static void mime_hdr_free(MIME_HEADER *hdr); | ||
| 108 | |||
| 109 | #define MAX_SMLEN 1024 | ||
| 110 | #define mime_debug(x) /* x */ | ||
| 111 | |||
| 112 | |||
| 113 | typedef void (*stkfree)(); | ||
| 114 | |||
| 115 | /* Base 64 read and write of PKCS#7 structure */ | ||
| 116 | |||
| 117 | static int B64_write_PKCS7(BIO *bio, PKCS7 *p7) | ||
| 118 | { | ||
| 119 | BIO *b64; | ||
| 120 | if(!(b64 = BIO_new(BIO_f_base64()))) { | ||
| 121 | PKCS7err(PKCS7_F_B64_WRITE_PKCS7,ERR_R_MALLOC_FAILURE); | ||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | bio = BIO_push(b64, bio); | ||
| 125 | i2d_PKCS7_bio(bio, p7); | ||
| 126 | BIO_flush(bio); | ||
| 127 | bio = BIO_pop(bio); | ||
| 128 | BIO_free(b64); | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | static PKCS7 *B64_read_PKCS7(BIO *bio) | ||
| 133 | { | ||
| 134 | BIO *b64; | ||
| 135 | PKCS7 *p7; | ||
| 136 | if(!(b64 = BIO_new(BIO_f_base64()))) { | ||
| 137 | PKCS7err(PKCS7_F_B64_READ_PKCS7,ERR_R_MALLOC_FAILURE); | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | bio = BIO_push(b64, bio); | ||
| 141 | if(!(p7 = d2i_PKCS7_bio(bio, NULL))) | ||
| 142 | PKCS7err(PKCS7_F_B64_READ_PKCS7,PKCS7_R_DECODE_ERROR); | ||
| 143 | BIO_flush(bio); | ||
| 144 | bio = BIO_pop(bio); | ||
| 145 | BIO_free(b64); | ||
| 146 | return p7; | ||
| 147 | } | ||
| 148 | |||
| 149 | /* SMIME sender */ | ||
| 150 | |||
| 151 | int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags) | ||
| 152 | { | ||
| 153 | char linebuf[MAX_SMLEN]; | ||
| 154 | char bound[33], c; | ||
| 155 | int i; | ||
| 156 | if((flags & PKCS7_DETACHED) && data) { | ||
| 157 | /* We want multipart/signed */ | ||
| 158 | /* Generate a random boundary */ | ||
| 159 | RAND_pseudo_bytes((unsigned char *)bound, 32); | ||
| 160 | for(i = 0; i < 32; i++) { | ||
| 161 | c = bound[i] & 0xf; | ||
| 162 | if(c < 10) c += '0'; | ||
| 163 | else c += 'A' - 10; | ||
| 164 | bound[i] = c; | ||
| 165 | } | ||
| 166 | bound[32] = 0; | ||
| 167 | BIO_printf(bio, "MIME-Version: 1.0\n"); | ||
| 168 | BIO_printf(bio, "Content-Type: multipart/signed;"); | ||
| 169 | BIO_printf(bio, " protocol=\"application/x-pkcs7-signature\";"); | ||
| 170 | BIO_printf(bio, " micalg=sha1; boundary=\"----%s\"\n\n", bound); | ||
| 171 | BIO_printf(bio, "This is an S/MIME signed message\n\n"); | ||
| 172 | /* Now write out the first part */ | ||
| 173 | BIO_printf(bio, "------%s\n", bound); | ||
| 174 | if(flags & PKCS7_TEXT) BIO_printf(bio, "Content-Type: text/plain\n\n"); | ||
| 175 | while((i = BIO_read(data, linebuf, MAX_SMLEN)) > 0) | ||
| 176 | BIO_write(bio, linebuf, i); | ||
| 177 | BIO_printf(bio, "\n------%s\n", bound); | ||
| 178 | |||
| 179 | /* Headers for signature */ | ||
| 180 | |||
| 181 | BIO_printf(bio, "Content-Type: application/x-pkcs7-signature; name=\"smime.p7s\"\n"); | ||
| 182 | BIO_printf(bio, "Content-Transfer-Encoding: base64\n"); | ||
| 183 | BIO_printf(bio, "Content-Disposition: attachment; filename=\"smime.p7s\"\n\n"); | ||
| 184 | B64_write_PKCS7(bio, p7); | ||
| 185 | BIO_printf(bio,"\n------%s--\n\n", bound); | ||
| 186 | return 1; | ||
| 187 | } | ||
| 188 | /* MIME headers */ | ||
| 189 | BIO_printf(bio, "MIME-Version: 1.0\n"); | ||
| 190 | BIO_printf(bio, "Content-Disposition: attachment; filename=\"smime.p7m\"\n"); | ||
| 191 | BIO_printf(bio, "Content-Type: application/x-pkcs7-mime; name=\"smime.p7m\"\n"); | ||
| 192 | BIO_printf(bio, "Content-Transfer-Encoding: base64\n\n"); | ||
| 193 | B64_write_PKCS7(bio, p7); | ||
| 194 | BIO_printf(bio, "\n"); | ||
| 195 | return 1; | ||
| 196 | } | ||
| 197 | |||
| 198 | /* SMIME reader: handle multipart/signed and opaque signing. | ||
| 199 | * in multipart case the content is placed in a memory BIO | ||
| 200 | * pointed to by "bcont". In opaque this is set to NULL | ||
| 201 | */ | ||
| 202 | |||
| 203 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont) | ||
| 204 | { | ||
| 205 | BIO *p7in; | ||
| 206 | STACK_OF(MIME_HEADER) *headers = NULL; | ||
| 207 | STACK_OF(BIO) *parts = NULL; | ||
| 208 | MIME_HEADER *hdr; | ||
| 209 | MIME_PARAM *prm; | ||
| 210 | PKCS7 *p7; | ||
| 211 | int ret; | ||
| 212 | |||
| 213 | if(bcont) *bcont = NULL; | ||
| 214 | |||
| 215 | if (!(headers = mime_parse_hdr(bio))) { | ||
| 216 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_PARSE_ERROR); | ||
| 217 | return NULL; | ||
| 218 | } | ||
| 219 | |||
| 220 | if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { | ||
| 221 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 222 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_CONTENT_TYPE); | ||
| 223 | return NULL; | ||
| 224 | } | ||
| 225 | |||
| 226 | /* Handle multipart/signed */ | ||
| 227 | |||
| 228 | if(!strcmp(hdr->value, "multipart/signed")) { | ||
| 229 | /* Split into two parts */ | ||
| 230 | prm = mime_param_find(hdr, "boundary"); | ||
| 231 | if(!prm || !prm->param_value) { | ||
| 232 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 233 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BOUNDARY); | ||
| 234 | return NULL; | ||
| 235 | } | ||
| 236 | ret = multi_split(bio, prm->param_value, &parts); | ||
| 237 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 238 | if(!ret || (sk_BIO_num(parts) != 2) ) { | ||
| 239 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BODY_FAILURE); | ||
| 240 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 241 | return NULL; | ||
| 242 | } | ||
| 243 | |||
| 244 | /* Parse the signature piece */ | ||
| 245 | p7in = sk_BIO_value(parts, 1); | ||
| 246 | |||
| 247 | if (!(headers = mime_parse_hdr(p7in))) { | ||
| 248 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_SIG_PARSE_ERROR); | ||
| 249 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 250 | return NULL; | ||
| 251 | } | ||
| 252 | |||
| 253 | /* Get content type */ | ||
| 254 | |||
| 255 | if(!(hdr = mime_hdr_find(headers, "content-type")) || | ||
| 256 | !hdr->value) { | ||
| 257 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 258 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_SIG_CONTENT_TYPE); | ||
| 259 | return NULL; | ||
| 260 | } | ||
| 261 | |||
| 262 | if(strcmp(hdr->value, "application/x-pkcs7-signature") && | ||
| 263 | strcmp(hdr->value, "application/pkcs7-signature")) { | ||
| 264 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 265 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_SIG_INVALID_MIME_TYPE); | ||
| 266 | ERR_add_error_data(2, "type: ", hdr->value); | ||
| 267 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 268 | return NULL; | ||
| 269 | } | ||
| 270 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 271 | /* Read in PKCS#7 */ | ||
| 272 | if(!(p7 = B64_read_PKCS7(p7in))) { | ||
| 273 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_PKCS7_SIG_PARSE_ERROR); | ||
| 274 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 275 | return NULL; | ||
| 276 | } | ||
| 277 | |||
| 278 | if(bcont) { | ||
| 279 | *bcont = sk_BIO_value(parts, 0); | ||
| 280 | BIO_free(p7in); | ||
| 281 | sk_BIO_free(parts); | ||
| 282 | } else sk_BIO_pop_free(parts, BIO_vfree); | ||
| 283 | return p7; | ||
| 284 | } | ||
| 285 | |||
| 286 | /* OK, if not multipart/signed try opaque signature */ | ||
| 287 | |||
| 288 | if (strcmp (hdr->value, "application/x-pkcs7-mime") && | ||
| 289 | strcmp (hdr->value, "application/pkcs7-mime")) { | ||
| 290 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_INVALID_MIME_TYPE); | ||
| 291 | ERR_add_error_data(2, "type: ", hdr->value); | ||
| 292 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 293 | return NULL; | ||
| 294 | } | ||
| 295 | |||
| 296 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 297 | |||
| 298 | if(!(p7 = B64_read_PKCS7(bio))) { | ||
| 299 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_PKCS7_PARSE_ERROR); | ||
| 300 | return NULL; | ||
| 301 | } | ||
| 302 | return p7; | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | /* Copy text from one BIO to another making the output CRLF at EOL */ | ||
| 307 | int SMIME_crlf_copy(BIO *in, BIO *out, int flags) | ||
| 308 | { | ||
| 309 | char eol; | ||
| 310 | int len; | ||
| 311 | char linebuf[MAX_SMLEN]; | ||
| 312 | if(flags & PKCS7_BINARY) { | ||
| 313 | while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0) | ||
| 314 | BIO_write(out, linebuf, len); | ||
| 315 | return 1; | ||
| 316 | } | ||
| 317 | if(flags & PKCS7_TEXT) BIO_printf(out, "Content-Type: text/plain\r\n\r\n"); | ||
| 318 | while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) { | ||
| 319 | eol = 0; | ||
| 320 | while(iscrlf(linebuf[len - 1])) { | ||
| 321 | len--; | ||
| 322 | eol = 1; | ||
| 323 | } | ||
| 324 | BIO_write(out, linebuf, len); | ||
| 325 | if(eol) BIO_write(out, "\r\n", 2); | ||
| 326 | } | ||
| 327 | return 1; | ||
| 328 | } | ||
| 329 | |||
| 330 | /* Strip off headers if they are text/plain */ | ||
| 331 | int SMIME_text(BIO *in, BIO *out) | ||
| 332 | { | ||
| 333 | char iobuf[4096]; | ||
| 334 | int len; | ||
| 335 | STACK_OF(MIME_HEADER) *headers; | ||
| 336 | MIME_HEADER *hdr; | ||
| 337 | |||
| 338 | if (!(headers = mime_parse_hdr(in))) { | ||
| 339 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_PARSE_ERROR); | ||
| 340 | return 0; | ||
| 341 | } | ||
| 342 | if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { | ||
| 343 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_MIME_NO_CONTENT_TYPE); | ||
| 344 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 345 | return 0; | ||
| 346 | } | ||
| 347 | if (strcmp (hdr->value, "text/plain")) { | ||
| 348 | PKCS7err(PKCS7_F_SMIME_TEXT,PKCS7_R_INVALID_MIME_TYPE); | ||
| 349 | ERR_add_error_data(2, "type: ", hdr->value); | ||
| 350 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 351 | return 0; | ||
| 352 | } | ||
| 353 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 354 | while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) | ||
| 355 | BIO_write(out, iobuf, len); | ||
| 356 | return 1; | ||
| 357 | } | ||
| 358 | |||
| 359 | /* Split a multipart/XXX message body into component parts: result is | ||
| 360 | * canonical parts in a STACK of bios | ||
| 361 | */ | ||
| 362 | |||
| 363 | static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret) | ||
| 364 | { | ||
| 365 | char linebuf[MAX_SMLEN]; | ||
| 366 | int len, blen; | ||
| 367 | BIO *bpart = NULL; | ||
| 368 | STACK_OF(BIO) *parts; | ||
| 369 | char state, part, first; | ||
| 370 | |||
| 371 | blen = strlen(bound); | ||
| 372 | part = 0; | ||
| 373 | state = 0; | ||
| 374 | first = 1; | ||
| 375 | parts = sk_BIO_new_null(); | ||
| 376 | *ret = parts; | ||
| 377 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { | ||
| 378 | state = mime_bound_check(linebuf, len, bound, blen); | ||
| 379 | if(state == 1) { | ||
| 380 | first = 1; | ||
| 381 | part++; | ||
| 382 | } else if(state == 2) { | ||
| 383 | sk_BIO_push(parts, bpart); | ||
| 384 | return 1; | ||
| 385 | } else if(part) { | ||
| 386 | if(first) { | ||
| 387 | first = 0; | ||
| 388 | if(bpart) sk_BIO_push(parts, bpart); | ||
| 389 | bpart = BIO_new(BIO_s_mem()); | ||
| 390 | |||
| 391 | } else BIO_write(bpart, "\r\n", 2); | ||
| 392 | /* Strip CR+LF from linebuf */ | ||
| 393 | while(iscrlf(linebuf[len - 1])) len--; | ||
| 394 | BIO_write(bpart, linebuf, len); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | return 0; | ||
| 398 | } | ||
| 399 | |||
| 400 | static int iscrlf(char c) | ||
| 401 | { | ||
| 402 | if(c == '\r' || c == '\n') return 1; | ||
| 403 | return 0; | ||
| 404 | } | ||
| 405 | |||
| 406 | /* This is the big one: parse MIME header lines up to message body */ | ||
| 407 | |||
| 408 | #define MIME_INVALID 0 | ||
| 409 | #define MIME_START 1 | ||
| 410 | #define MIME_TYPE 2 | ||
| 411 | #define MIME_NAME 3 | ||
| 412 | #define MIME_VALUE 4 | ||
| 413 | #define MIME_QUOTE 5 | ||
| 414 | #define MIME_COMMENT 6 | ||
| 415 | |||
| 416 | |||
| 417 | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) | ||
| 418 | { | ||
| 419 | char *p, *q, c; | ||
| 420 | char *ntmp; | ||
| 421 | char linebuf[MAX_SMLEN]; | ||
| 422 | MIME_HEADER *mhdr = NULL; | ||
| 423 | STACK_OF(MIME_HEADER) *headers; | ||
| 424 | int len, state, save_state = 0; | ||
| 425 | |||
| 426 | headers = sk_MIME_HEADER_new(mime_hdr_cmp); | ||
| 427 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { | ||
| 428 | /* If whitespace at line start then continuation line */ | ||
| 429 | if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME; | ||
| 430 | else state = MIME_START; | ||
| 431 | ntmp = NULL; | ||
| 432 | /* Go through all characters */ | ||
| 433 | for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) { | ||
| 434 | |||
| 435 | /* State machine to handle MIME headers | ||
| 436 | * if this looks horrible that's because it *is* | ||
| 437 | */ | ||
| 438 | |||
| 439 | switch(state) { | ||
| 440 | case MIME_START: | ||
| 441 | if(c == ':') { | ||
| 442 | state = MIME_TYPE; | ||
| 443 | *p = 0; | ||
| 444 | ntmp = strip_ends(q); | ||
| 445 | q = p + 1; | ||
| 446 | } | ||
| 447 | break; | ||
| 448 | |||
| 449 | case MIME_TYPE: | ||
| 450 | if(c == ';') { | ||
| 451 | mime_debug("Found End Value\n"); | ||
| 452 | *p = 0; | ||
| 453 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | ||
| 454 | sk_MIME_HEADER_push(headers, mhdr); | ||
| 455 | ntmp = NULL; | ||
| 456 | q = p + 1; | ||
| 457 | state = MIME_NAME; | ||
| 458 | } else if(c == '(') { | ||
| 459 | save_state = state; | ||
| 460 | state = MIME_COMMENT; | ||
| 461 | } | ||
| 462 | break; | ||
| 463 | |||
| 464 | case MIME_COMMENT: | ||
| 465 | if(c == ')') { | ||
| 466 | state = save_state; | ||
| 467 | } | ||
| 468 | break; | ||
| 469 | |||
| 470 | case MIME_NAME: | ||
| 471 | if(c == '=') { | ||
| 472 | state = MIME_VALUE; | ||
| 473 | *p = 0; | ||
| 474 | ntmp = strip_ends(q); | ||
| 475 | q = p + 1; | ||
| 476 | } | ||
| 477 | break ; | ||
| 478 | |||
| 479 | case MIME_VALUE: | ||
| 480 | if(c == ';') { | ||
| 481 | state = MIME_NAME; | ||
| 482 | *p = 0; | ||
| 483 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | ||
| 484 | ntmp = NULL; | ||
| 485 | q = p + 1; | ||
| 486 | } else if (c == '"') { | ||
| 487 | mime_debug("Found Quote\n"); | ||
| 488 | state = MIME_QUOTE; | ||
| 489 | } else if(c == '(') { | ||
| 490 | save_state = state; | ||
| 491 | state = MIME_COMMENT; | ||
| 492 | } | ||
| 493 | break; | ||
| 494 | |||
| 495 | case MIME_QUOTE: | ||
| 496 | if(c == '"') { | ||
| 497 | mime_debug("Found Match Quote\n"); | ||
| 498 | state = MIME_VALUE; | ||
| 499 | } | ||
| 500 | break; | ||
| 501 | } | ||
| 502 | } | ||
| 503 | |||
| 504 | if(state == MIME_TYPE) { | ||
| 505 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | ||
| 506 | sk_MIME_HEADER_push(headers, mhdr); | ||
| 507 | } else if(state == MIME_VALUE) | ||
| 508 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | ||
| 509 | if(p == linebuf) break; /* Blank line means end of headers */ | ||
| 510 | } | ||
| 511 | |||
| 512 | return headers; | ||
| 513 | |||
| 514 | } | ||
| 515 | |||
| 516 | static char *strip_ends(char *name) | ||
| 517 | { | ||
| 518 | return strip_end(strip_start(name)); | ||
| 519 | } | ||
| 520 | |||
| 521 | /* Strip a parameter of whitespace from start of param */ | ||
| 522 | static char *strip_start(char *name) | ||
| 523 | { | ||
| 524 | char *p, c; | ||
| 525 | /* Look for first non white space or quote */ | ||
| 526 | for(p = name; (c = *p) ;p++) { | ||
| 527 | if(c == '"') { | ||
| 528 | /* Next char is start of string if non null */ | ||
| 529 | if(p[1]) return p + 1; | ||
| 530 | /* Else null string */ | ||
| 531 | return NULL; | ||
| 532 | } | ||
| 533 | if(!isspace((unsigned char)c)) return p; | ||
| 534 | } | ||
| 535 | return NULL; | ||
| 536 | } | ||
| 537 | |||
| 538 | /* As above but strip from end of string : maybe should handle brackets? */ | ||
| 539 | static char *strip_end(char *name) | ||
| 540 | { | ||
| 541 | char *p, c; | ||
| 542 | if(!name) return NULL; | ||
| 543 | /* Look for first non white space or quote */ | ||
| 544 | for(p = name + strlen(name) - 1; p >= name ;p--) { | ||
| 545 | c = *p; | ||
| 546 | if(c == '"') { | ||
| 547 | if(p - 1 == name) return NULL; | ||
| 548 | *p = 0; | ||
| 549 | return name; | ||
| 550 | } | ||
| 551 | if(isspace((unsigned char)c)) *p = 0; | ||
| 552 | else return name; | ||
| 553 | } | ||
| 554 | return NULL; | ||
| 555 | } | ||
| 556 | |||
| 557 | static MIME_HEADER *mime_hdr_new(char *name, char *value) | ||
| 558 | { | ||
| 559 | MIME_HEADER *mhdr; | ||
| 560 | char *tmpname, *tmpval, *p; | ||
| 561 | int c; | ||
| 562 | if(name) { | ||
| 563 | if(!(tmpname = BUF_strdup(name))) return NULL; | ||
| 564 | for(p = tmpname ; *p; p++) { | ||
| 565 | c = *p; | ||
| 566 | if(isupper(c)) { | ||
| 567 | c = tolower(c); | ||
| 568 | *p = c; | ||
| 569 | } | ||
| 570 | } | ||
| 571 | } else tmpname = NULL; | ||
| 572 | if(value) { | ||
| 573 | if(!(tmpval = BUF_strdup(value))) return NULL; | ||
| 574 | for(p = tmpval ; *p; p++) { | ||
| 575 | c = *p; | ||
| 576 | if(isupper(c)) { | ||
| 577 | c = tolower(c); | ||
| 578 | *p = c; | ||
| 579 | } | ||
| 580 | } | ||
| 581 | } else tmpval = NULL; | ||
| 582 | mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER)); | ||
| 583 | if(!mhdr) return NULL; | ||
| 584 | mhdr->name = tmpname; | ||
| 585 | mhdr->value = tmpval; | ||
| 586 | if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL; | ||
| 587 | return mhdr; | ||
| 588 | } | ||
| 589 | |||
| 590 | static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value) | ||
| 591 | { | ||
| 592 | char *tmpname, *tmpval, *p; | ||
| 593 | int c; | ||
| 594 | MIME_PARAM *mparam; | ||
| 595 | if(name) { | ||
| 596 | tmpname = BUF_strdup(name); | ||
| 597 | if(!tmpname) return 0; | ||
| 598 | for(p = tmpname ; *p; p++) { | ||
| 599 | c = *p; | ||
| 600 | if(isupper(c)) { | ||
| 601 | c = tolower(c); | ||
| 602 | *p = c; | ||
| 603 | } | ||
| 604 | } | ||
| 605 | } else tmpname = NULL; | ||
| 606 | if(value) { | ||
| 607 | tmpval = BUF_strdup(value); | ||
| 608 | if(!tmpval) return 0; | ||
| 609 | } else tmpval = NULL; | ||
| 610 | /* Parameter values are case sensitive so leave as is */ | ||
| 611 | mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM)); | ||
| 612 | if(!mparam) return 0; | ||
| 613 | mparam->param_name = tmpname; | ||
| 614 | mparam->param_value = tmpval; | ||
| 615 | sk_MIME_PARAM_push(mhdr->params, mparam); | ||
| 616 | return 1; | ||
| 617 | } | ||
| 618 | |||
| 619 | static int mime_hdr_cmp(const MIME_HEADER * const *a, | ||
| 620 | const MIME_HEADER * const *b) | ||
| 621 | { | ||
| 622 | return(strcmp((*a)->name, (*b)->name)); | ||
| 623 | } | ||
| 624 | |||
| 625 | static int mime_param_cmp(const MIME_PARAM * const *a, | ||
| 626 | const MIME_PARAM * const *b) | ||
| 627 | { | ||
| 628 | return(strcmp((*a)->param_name, (*b)->param_name)); | ||
| 629 | } | ||
| 630 | |||
| 631 | /* Find a header with a given name (if possible) */ | ||
| 632 | |||
| 633 | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name) | ||
| 634 | { | ||
| 635 | MIME_HEADER htmp; | ||
| 636 | int idx; | ||
| 637 | htmp.name = name; | ||
| 638 | idx = sk_MIME_HEADER_find(hdrs, &htmp); | ||
| 639 | if(idx < 0) return NULL; | ||
| 640 | return sk_MIME_HEADER_value(hdrs, idx); | ||
| 641 | } | ||
| 642 | |||
| 643 | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name) | ||
| 644 | { | ||
| 645 | MIME_PARAM param; | ||
| 646 | int idx; | ||
| 647 | param.param_name = name; | ||
| 648 | idx = sk_MIME_PARAM_find(hdr->params, ¶m); | ||
| 649 | if(idx < 0) return NULL; | ||
| 650 | return sk_MIME_PARAM_value(hdr->params, idx); | ||
| 651 | } | ||
| 652 | |||
| 653 | static void mime_hdr_free(MIME_HEADER *hdr) | ||
| 654 | { | ||
| 655 | if(hdr->name) OPENSSL_free(hdr->name); | ||
| 656 | if(hdr->value) OPENSSL_free(hdr->value); | ||
| 657 | if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); | ||
| 658 | OPENSSL_free(hdr); | ||
| 659 | } | ||
| 660 | |||
| 661 | static void mime_param_free(MIME_PARAM *param) | ||
| 662 | { | ||
| 663 | if(param->param_name) OPENSSL_free(param->param_name); | ||
| 664 | if(param->param_value) OPENSSL_free(param->param_value); | ||
| 665 | OPENSSL_free(param); | ||
| 666 | } | ||
| 667 | |||
| 668 | /* Check for a multipart boundary. Returns: | ||
| 669 | * 0 : no boundary | ||
| 670 | * 1 : part boundary | ||
| 671 | * 2 : final boundary | ||
| 672 | */ | ||
| 673 | static int mime_bound_check(char *line, int linelen, char *bound, int blen) | ||
| 674 | { | ||
| 675 | if(linelen == -1) linelen = strlen(line); | ||
| 676 | if(blen == -1) blen = strlen(bound); | ||
| 677 | /* Quickly eliminate if line length too short */ | ||
| 678 | if(blen + 2 > linelen) return 0; | ||
| 679 | /* Check for part boundary */ | ||
| 680 | if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) { | ||
| 681 | if(!strncmp(line + blen + 2, "--", 2)) return 2; | ||
| 682 | else return 1; | ||
| 683 | } | ||
| 684 | return 0; | ||
| 685 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_smime.c b/src/lib/libcrypto/pkcs7/pk7_smime.c new file mode 100644 index 0000000000..f0d071e282 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_smime.c | |||
| @@ -0,0 +1,441 @@ | |||
| 1 | /* pk7_smime.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 1999. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* Simple PKCS#7 processing functions */ | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include <openssl/x509.h> | ||
| 64 | #include <openssl/x509v3.h> | ||
| 65 | |||
| 66 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | ||
| 67 | BIO *data, int flags) | ||
| 68 | { | ||
| 69 | PKCS7 *p7; | ||
| 70 | PKCS7_SIGNER_INFO *si; | ||
| 71 | BIO *p7bio; | ||
| 72 | STACK_OF(X509_ALGOR) *smcap; | ||
| 73 | int i; | ||
| 74 | |||
| 75 | if(!X509_check_private_key(signcert, pkey)) { | ||
| 76 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | ||
| 77 | return NULL; | ||
| 78 | } | ||
| 79 | |||
| 80 | if(!(p7 = PKCS7_new())) { | ||
| 81 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 82 | return NULL; | ||
| 83 | } | ||
| 84 | |||
| 85 | PKCS7_set_type(p7, NID_pkcs7_signed); | ||
| 86 | |||
| 87 | PKCS7_content_new(p7, NID_pkcs7_data); | ||
| 88 | |||
| 89 | if (!(si = PKCS7_add_signature(p7,signcert,pkey,EVP_sha1()))) { | ||
| 90 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); | ||
| 91 | return NULL; | ||
| 92 | } | ||
| 93 | |||
| 94 | if(!(flags & PKCS7_NOCERTS)) { | ||
| 95 | PKCS7_add_certificate(p7, signcert); | ||
| 96 | if(certs) for(i = 0; i < sk_X509_num(certs); i++) | ||
| 97 | PKCS7_add_certificate(p7, sk_X509_value(certs, i)); | ||
| 98 | } | ||
| 99 | |||
| 100 | if(!(p7bio = PKCS7_dataInit(p7, NULL))) { | ||
| 101 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 102 | return NULL; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | SMIME_crlf_copy(data, p7bio, flags); | ||
| 107 | |||
| 108 | if(!(flags & PKCS7_NOATTR)) { | ||
| 109 | PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, | ||
| 110 | V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data)); | ||
| 111 | /* Add SMIMECapabilities */ | ||
| 112 | if(!(flags & PKCS7_NOSMIMECAP)) | ||
| 113 | { | ||
| 114 | if(!(smcap = sk_X509_ALGOR_new_null())) { | ||
| 115 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 116 | return NULL; | ||
| 117 | } | ||
| 118 | #ifndef OPENSSL_NO_DES | ||
| 119 | PKCS7_simple_smimecap (smcap, NID_des_ede3_cbc, -1); | ||
| 120 | #endif | ||
| 121 | #ifndef OPENSSL_NO_RC2 | ||
| 122 | PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 128); | ||
| 123 | PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 64); | ||
| 124 | #endif | ||
| 125 | #ifndef OPENSSL_NO_DES | ||
| 126 | PKCS7_simple_smimecap (smcap, NID_des_cbc, -1); | ||
| 127 | #endif | ||
| 128 | #ifndef OPENSSL_NO_RC2 | ||
| 129 | PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 40); | ||
| 130 | #endif | ||
| 131 | PKCS7_add_attrib_smimecap (si, smcap); | ||
| 132 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | if(flags & PKCS7_DETACHED)PKCS7_set_detached(p7, 1); | ||
| 137 | |||
| 138 | if (!PKCS7_dataFinal(p7,p7bio)) { | ||
| 139 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_DATASIGN); | ||
| 140 | return NULL; | ||
| 141 | } | ||
| 142 | |||
| 143 | BIO_free_all(p7bio); | ||
| 144 | return p7; | ||
| 145 | } | ||
| 146 | |||
| 147 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | ||
| 148 | BIO *indata, BIO *out, int flags) | ||
| 149 | { | ||
| 150 | STACK_OF(X509) *signers; | ||
| 151 | X509 *signer; | ||
| 152 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 153 | PKCS7_SIGNER_INFO *si; | ||
| 154 | X509_STORE_CTX cert_ctx; | ||
| 155 | char buf[4096]; | ||
| 156 | int i, j=0, k, ret = 0; | ||
| 157 | BIO *p7bio; | ||
| 158 | BIO *tmpout; | ||
| 159 | |||
| 160 | if(!p7) { | ||
| 161 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_INVALID_NULL_POINTER); | ||
| 162 | return 0; | ||
| 163 | } | ||
| 164 | |||
| 165 | if(!PKCS7_type_is_signed(p7)) { | ||
| 166 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 167 | return 0; | ||
| 168 | } | ||
| 169 | |||
| 170 | /* Check for no data and no content: no data to verify signature */ | ||
| 171 | if(PKCS7_get_detached(p7) && !indata) { | ||
| 172 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_CONTENT); | ||
| 173 | return 0; | ||
| 174 | } | ||
| 175 | #if 0 | ||
| 176 | /* NB: this test commented out because some versions of Netscape | ||
| 177 | * illegally include zero length content when signing data. | ||
| 178 | */ | ||
| 179 | |||
| 180 | /* Check for data and content: two sets of data */ | ||
| 181 | if(!PKCS7_get_detached(p7) && indata) { | ||
| 182 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CONTENT_AND_DATA_PRESENT); | ||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | #endif | ||
| 186 | |||
| 187 | sinfos = PKCS7_get_signer_info(p7); | ||
| 188 | |||
| 189 | if(!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) { | ||
| 190 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_SIGNATURES_ON_DATA); | ||
| 191 | return 0; | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | signers = PKCS7_get0_signers(p7, certs, flags); | ||
| 196 | |||
| 197 | if(!signers) return 0; | ||
| 198 | |||
| 199 | /* Now verify the certificates */ | ||
| 200 | |||
| 201 | if (!(flags & PKCS7_NOVERIFY)) for (k = 0; k < sk_X509_num(signers); k++) { | ||
| 202 | signer = sk_X509_value (signers, k); | ||
| 203 | if (!(flags & PKCS7_NOCHAIN)) { | ||
| 204 | if(!X509_STORE_CTX_init(&cert_ctx, store, signer, | ||
| 205 | p7->d.sign->cert)) | ||
| 206 | { | ||
| 207 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB); | ||
| 208 | sk_X509_free(signers); | ||
| 209 | return 0; | ||
| 210 | } | ||
| 211 | X509_STORE_CTX_set_purpose(&cert_ctx, | ||
| 212 | X509_PURPOSE_SMIME_SIGN); | ||
| 213 | } else if(!X509_STORE_CTX_init (&cert_ctx, store, signer, NULL)) { | ||
| 214 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB); | ||
| 215 | sk_X509_free(signers); | ||
| 216 | return 0; | ||
| 217 | } | ||
| 218 | i = X509_verify_cert(&cert_ctx); | ||
| 219 | if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx); | ||
| 220 | X509_STORE_CTX_cleanup(&cert_ctx); | ||
| 221 | if (i <= 0) { | ||
| 222 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CERTIFICATE_VERIFY_ERROR); | ||
| 223 | ERR_add_error_data(2, "Verify error:", | ||
| 224 | X509_verify_cert_error_string(j)); | ||
| 225 | sk_X509_free(signers); | ||
| 226 | return 0; | ||
| 227 | } | ||
| 228 | /* Check for revocation status here */ | ||
| 229 | } | ||
| 230 | |||
| 231 | p7bio=PKCS7_dataInit(p7,indata); | ||
| 232 | |||
| 233 | if(flags & PKCS7_TEXT) { | ||
| 234 | if(!(tmpout = BIO_new(BIO_s_mem()))) { | ||
| 235 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE); | ||
| 236 | goto err; | ||
| 237 | } | ||
| 238 | } else tmpout = out; | ||
| 239 | |||
| 240 | /* We now have to 'read' from p7bio to calculate digests etc. */ | ||
| 241 | for (;;) | ||
| 242 | { | ||
| 243 | i=BIO_read(p7bio,buf,sizeof(buf)); | ||
| 244 | if (i <= 0) break; | ||
| 245 | if (tmpout) BIO_write(tmpout, buf, i); | ||
| 246 | } | ||
| 247 | |||
| 248 | if(flags & PKCS7_TEXT) { | ||
| 249 | if(!SMIME_text(tmpout, out)) { | ||
| 250 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SMIME_TEXT_ERROR); | ||
| 251 | BIO_free(tmpout); | ||
| 252 | goto err; | ||
| 253 | } | ||
| 254 | BIO_free(tmpout); | ||
| 255 | } | ||
| 256 | |||
| 257 | /* Now Verify All Signatures */ | ||
| 258 | if (!(flags & PKCS7_NOSIGS)) | ||
| 259 | for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sinfos); i++) | ||
| 260 | { | ||
| 261 | si=sk_PKCS7_SIGNER_INFO_value(sinfos,i); | ||
| 262 | signer = sk_X509_value (signers, i); | ||
| 263 | j=PKCS7_signatureVerify(p7bio,p7,si, signer); | ||
| 264 | if (j <= 0) { | ||
| 265 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SIGNATURE_FAILURE); | ||
| 266 | goto err; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | ret = 1; | ||
| 271 | |||
| 272 | err: | ||
| 273 | |||
| 274 | if(indata) BIO_pop(p7bio); | ||
| 275 | BIO_free_all(p7bio); | ||
| 276 | sk_X509_free(signers); | ||
| 277 | |||
| 278 | return ret; | ||
| 279 | } | ||
| 280 | |||
| 281 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags) | ||
| 282 | { | ||
| 283 | STACK_OF(X509) *signers; | ||
| 284 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 285 | PKCS7_SIGNER_INFO *si; | ||
| 286 | PKCS7_ISSUER_AND_SERIAL *ias; | ||
| 287 | X509 *signer; | ||
| 288 | int i; | ||
| 289 | |||
| 290 | if(!p7) { | ||
| 291 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_INVALID_NULL_POINTER); | ||
| 292 | return NULL; | ||
| 293 | } | ||
| 294 | |||
| 295 | if(!PKCS7_type_is_signed(p7)) { | ||
| 296 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 297 | return NULL; | ||
| 298 | } | ||
| 299 | if(!(signers = sk_X509_new_null())) { | ||
| 300 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,ERR_R_MALLOC_FAILURE); | ||
| 301 | return NULL; | ||
| 302 | } | ||
| 303 | |||
| 304 | /* Collect all the signers together */ | ||
| 305 | |||
| 306 | sinfos = PKCS7_get_signer_info(p7); | ||
| 307 | |||
| 308 | if(sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) { | ||
| 309 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_NO_SIGNERS); | ||
| 310 | return 0; | ||
| 311 | } | ||
| 312 | |||
| 313 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) | ||
| 314 | { | ||
| 315 | si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | ||
| 316 | ias = si->issuer_and_serial; | ||
| 317 | signer = NULL; | ||
| 318 | /* If any certificates passed they take priority */ | ||
| 319 | if (certs) signer = X509_find_by_issuer_and_serial (certs, | ||
| 320 | ias->issuer, ias->serial); | ||
| 321 | if (!signer && !(flags & PKCS7_NOINTERN) | ||
| 322 | && p7->d.sign->cert) signer = | ||
| 323 | X509_find_by_issuer_and_serial (p7->d.sign->cert, | ||
| 324 | ias->issuer, ias->serial); | ||
| 325 | if (!signer) { | ||
| 326 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); | ||
| 327 | sk_X509_free(signers); | ||
| 328 | return 0; | ||
| 329 | } | ||
| 330 | |||
| 331 | sk_X509_push(signers, signer); | ||
| 332 | } | ||
| 333 | return signers; | ||
| 334 | } | ||
| 335 | |||
| 336 | |||
| 337 | /* Build a complete PKCS#7 enveloped data */ | ||
| 338 | |||
| 339 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | ||
| 340 | int flags) | ||
| 341 | { | ||
| 342 | PKCS7 *p7; | ||
| 343 | BIO *p7bio = NULL; | ||
| 344 | int i; | ||
| 345 | X509 *x509; | ||
| 346 | if(!(p7 = PKCS7_new())) { | ||
| 347 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 348 | return NULL; | ||
| 349 | } | ||
| 350 | |||
| 351 | PKCS7_set_type(p7, NID_pkcs7_enveloped); | ||
| 352 | if(!PKCS7_set_cipher(p7, cipher)) { | ||
| 353 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER); | ||
| 354 | goto err; | ||
| 355 | } | ||
| 356 | |||
| 357 | for(i = 0; i < sk_X509_num(certs); i++) { | ||
| 358 | x509 = sk_X509_value(certs, i); | ||
| 359 | if(!PKCS7_add_recipient(p7, x509)) { | ||
| 360 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, | ||
| 361 | PKCS7_R_ERROR_ADDING_RECIPIENT); | ||
| 362 | goto err; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | if(!(p7bio = PKCS7_dataInit(p7, NULL))) { | ||
| 367 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 368 | goto err; | ||
| 369 | } | ||
| 370 | |||
| 371 | SMIME_crlf_copy(in, p7bio, flags); | ||
| 372 | |||
| 373 | BIO_flush(p7bio); | ||
| 374 | |||
| 375 | if (!PKCS7_dataFinal(p7,p7bio)) { | ||
| 376 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_PKCS7_DATAFINAL_ERROR); | ||
| 377 | goto err; | ||
| 378 | } | ||
| 379 | BIO_free_all(p7bio); | ||
| 380 | |||
| 381 | return p7; | ||
| 382 | |||
| 383 | err: | ||
| 384 | |||
| 385 | BIO_free(p7bio); | ||
| 386 | PKCS7_free(p7); | ||
| 387 | return NULL; | ||
| 388 | |||
| 389 | } | ||
| 390 | |||
| 391 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) | ||
| 392 | { | ||
| 393 | BIO *tmpmem; | ||
| 394 | int ret, i; | ||
| 395 | char buf[4096]; | ||
| 396 | |||
| 397 | if(!p7) { | ||
| 398 | PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_INVALID_NULL_POINTER); | ||
| 399 | return 0; | ||
| 400 | } | ||
| 401 | |||
| 402 | if(!PKCS7_type_is_enveloped(p7)) { | ||
| 403 | PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 404 | return 0; | ||
| 405 | } | ||
| 406 | |||
| 407 | if(!X509_check_private_key(cert, pkey)) { | ||
| 408 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, | ||
| 409 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | ||
| 410 | return 0; | ||
| 411 | } | ||
| 412 | |||
| 413 | if(!(tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert))) { | ||
| 414 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR); | ||
| 415 | return 0; | ||
| 416 | } | ||
| 417 | |||
| 418 | if (flags & PKCS7_TEXT) { | ||
| 419 | BIO *tmpbuf, *bread; | ||
| 420 | /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ | ||
| 421 | if(!(tmpbuf = BIO_new(BIO_f_buffer()))) { | ||
| 422 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | ||
| 423 | return 0; | ||
| 424 | } | ||
| 425 | if(!(bread = BIO_push(tmpbuf, tmpmem))) { | ||
| 426 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | ||
| 427 | return 0; | ||
| 428 | } | ||
| 429 | ret = SMIME_text(bread, data); | ||
| 430 | BIO_free_all(bread); | ||
| 431 | return ret; | ||
| 432 | } else { | ||
| 433 | for(;;) { | ||
| 434 | i = BIO_read(tmpmem, buf, sizeof(buf)); | ||
| 435 | if(i <= 0) break; | ||
| 436 | BIO_write(data, buf, i); | ||
| 437 | } | ||
| 438 | BIO_free_all(tmpmem); | ||
| 439 | return 1; | ||
| 440 | } | ||
| 441 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pkcs7.h b/src/lib/libcrypto/pkcs7/pkcs7.h new file mode 100644 index 0000000000..5819700a85 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pkcs7.h | |||
| @@ -0,0 +1,449 @@ | |||
| 1 | /* crypto/pkcs7/pkcs7.h */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #ifndef HEADER_PKCS7_H | ||
| 60 | #define HEADER_PKCS7_H | ||
| 61 | |||
| 62 | #include <openssl/asn1.h> | ||
| 63 | #include <openssl/bio.h> | ||
| 64 | #include <openssl/e_os2.h> | ||
| 65 | |||
| 66 | #include <openssl/symhacks.h> | ||
| 67 | #include <openssl/ossl_typ.h> | ||
| 68 | |||
| 69 | #ifdef __cplusplus | ||
| 70 | extern "C" { | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #ifdef OPENSSL_SYS_WIN32 | ||
| 74 | /* Under Win32 thes are defined in wincrypt.h */ | ||
| 75 | #undef PKCS7_ISSUER_AND_SERIAL | ||
| 76 | #undef PKCS7_SIGNER_INFO | ||
| 77 | #endif | ||
| 78 | |||
| 79 | /* | ||
| 80 | Encryption_ID DES-CBC | ||
| 81 | Digest_ID MD5 | ||
| 82 | Digest_Encryption_ID rsaEncryption | ||
| 83 | Key_Encryption_ID rsaEncryption | ||
| 84 | */ | ||
| 85 | |||
| 86 | typedef struct pkcs7_issuer_and_serial_st | ||
| 87 | { | ||
| 88 | X509_NAME *issuer; | ||
| 89 | ASN1_INTEGER *serial; | ||
| 90 | } PKCS7_ISSUER_AND_SERIAL; | ||
| 91 | |||
| 92 | typedef struct pkcs7_signer_info_st | ||
| 93 | { | ||
| 94 | ASN1_INTEGER *version; /* version 1 */ | ||
| 95 | PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; | ||
| 96 | X509_ALGOR *digest_alg; | ||
| 97 | STACK_OF(X509_ATTRIBUTE) *auth_attr; /* [ 0 ] */ | ||
| 98 | X509_ALGOR *digest_enc_alg; | ||
| 99 | ASN1_OCTET_STRING *enc_digest; | ||
| 100 | STACK_OF(X509_ATTRIBUTE) *unauth_attr; /* [ 1 ] */ | ||
| 101 | |||
| 102 | /* The private key to sign with */ | ||
| 103 | EVP_PKEY *pkey; | ||
| 104 | } PKCS7_SIGNER_INFO; | ||
| 105 | |||
| 106 | DECLARE_STACK_OF(PKCS7_SIGNER_INFO) | ||
| 107 | DECLARE_ASN1_SET_OF(PKCS7_SIGNER_INFO) | ||
| 108 | |||
| 109 | typedef struct pkcs7_recip_info_st | ||
| 110 | { | ||
| 111 | ASN1_INTEGER *version; /* version 0 */ | ||
| 112 | PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; | ||
| 113 | X509_ALGOR *key_enc_algor; | ||
| 114 | ASN1_OCTET_STRING *enc_key; | ||
| 115 | X509 *cert; /* get the pub-key from this */ | ||
| 116 | } PKCS7_RECIP_INFO; | ||
| 117 | |||
| 118 | DECLARE_STACK_OF(PKCS7_RECIP_INFO) | ||
| 119 | DECLARE_ASN1_SET_OF(PKCS7_RECIP_INFO) | ||
| 120 | |||
| 121 | typedef struct pkcs7_signed_st | ||
| 122 | { | ||
| 123 | ASN1_INTEGER *version; /* version 1 */ | ||
| 124 | STACK_OF(X509_ALGOR) *md_algs; /* md used */ | ||
| 125 | STACK_OF(X509) *cert; /* [ 0 ] */ | ||
| 126 | STACK_OF(X509_CRL) *crl; /* [ 1 ] */ | ||
| 127 | STACK_OF(PKCS7_SIGNER_INFO) *signer_info; | ||
| 128 | |||
| 129 | struct pkcs7_st *contents; | ||
| 130 | } PKCS7_SIGNED; | ||
| 131 | /* The above structure is very very similar to PKCS7_SIGN_ENVELOPE. | ||
| 132 | * How about merging the two */ | ||
| 133 | |||
| 134 | typedef struct pkcs7_enc_content_st | ||
| 135 | { | ||
| 136 | ASN1_OBJECT *content_type; | ||
| 137 | X509_ALGOR *algorithm; | ||
| 138 | ASN1_OCTET_STRING *enc_data; /* [ 0 ] */ | ||
| 139 | const EVP_CIPHER *cipher; | ||
| 140 | } PKCS7_ENC_CONTENT; | ||
| 141 | |||
| 142 | typedef struct pkcs7_enveloped_st | ||
| 143 | { | ||
| 144 | ASN1_INTEGER *version; /* version 0 */ | ||
| 145 | STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; | ||
| 146 | PKCS7_ENC_CONTENT *enc_data; | ||
| 147 | } PKCS7_ENVELOPE; | ||
| 148 | |||
| 149 | typedef struct pkcs7_signedandenveloped_st | ||
| 150 | { | ||
| 151 | ASN1_INTEGER *version; /* version 1 */ | ||
| 152 | STACK_OF(X509_ALGOR) *md_algs; /* md used */ | ||
| 153 | STACK_OF(X509) *cert; /* [ 0 ] */ | ||
| 154 | STACK_OF(X509_CRL) *crl; /* [ 1 ] */ | ||
| 155 | STACK_OF(PKCS7_SIGNER_INFO) *signer_info; | ||
| 156 | |||
| 157 | PKCS7_ENC_CONTENT *enc_data; | ||
| 158 | STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; | ||
| 159 | } PKCS7_SIGN_ENVELOPE; | ||
| 160 | |||
| 161 | typedef struct pkcs7_digest_st | ||
| 162 | { | ||
| 163 | ASN1_INTEGER *version; /* version 0 */ | ||
| 164 | X509_ALGOR *md; /* md used */ | ||
| 165 | struct pkcs7_st *contents; | ||
| 166 | ASN1_OCTET_STRING *digest; | ||
| 167 | } PKCS7_DIGEST; | ||
| 168 | |||
| 169 | typedef struct pkcs7_encrypted_st | ||
| 170 | { | ||
| 171 | ASN1_INTEGER *version; /* version 0 */ | ||
| 172 | PKCS7_ENC_CONTENT *enc_data; | ||
| 173 | } PKCS7_ENCRYPT; | ||
| 174 | |||
| 175 | typedef struct pkcs7_st | ||
| 176 | { | ||
| 177 | /* The following is non NULL if it contains ASN1 encoding of | ||
| 178 | * this structure */ | ||
| 179 | unsigned char *asn1; | ||
| 180 | long length; | ||
| 181 | |||
| 182 | #define PKCS7_S_HEADER 0 | ||
| 183 | #define PKCS7_S_BODY 1 | ||
| 184 | #define PKCS7_S_TAIL 2 | ||
| 185 | int state; /* used during processing */ | ||
| 186 | |||
| 187 | int detached; | ||
| 188 | |||
| 189 | ASN1_OBJECT *type; | ||
| 190 | /* content as defined by the type */ | ||
| 191 | /* all encryption/message digests are applied to the 'contents', | ||
| 192 | * leaving out the 'type' field. */ | ||
| 193 | union { | ||
| 194 | char *ptr; | ||
| 195 | |||
| 196 | /* NID_pkcs7_data */ | ||
| 197 | ASN1_OCTET_STRING *data; | ||
| 198 | |||
| 199 | /* NID_pkcs7_signed */ | ||
| 200 | PKCS7_SIGNED *sign; | ||
| 201 | |||
| 202 | /* NID_pkcs7_enveloped */ | ||
| 203 | PKCS7_ENVELOPE *enveloped; | ||
| 204 | |||
| 205 | /* NID_pkcs7_signedAndEnveloped */ | ||
| 206 | PKCS7_SIGN_ENVELOPE *signed_and_enveloped; | ||
| 207 | |||
| 208 | /* NID_pkcs7_digest */ | ||
| 209 | PKCS7_DIGEST *digest; | ||
| 210 | |||
| 211 | /* NID_pkcs7_encrypted */ | ||
| 212 | PKCS7_ENCRYPT *encrypted; | ||
| 213 | |||
| 214 | /* Anything else */ | ||
| 215 | ASN1_TYPE *other; | ||
| 216 | } d; | ||
| 217 | } PKCS7; | ||
| 218 | |||
| 219 | DECLARE_STACK_OF(PKCS7) | ||
| 220 | DECLARE_ASN1_SET_OF(PKCS7) | ||
| 221 | DECLARE_PKCS12_STACK_OF(PKCS7) | ||
| 222 | |||
| 223 | #define PKCS7_OP_SET_DETACHED_SIGNATURE 1 | ||
| 224 | #define PKCS7_OP_GET_DETACHED_SIGNATURE 2 | ||
| 225 | |||
| 226 | #define PKCS7_get_signed_attributes(si) ((si)->auth_attr) | ||
| 227 | #define PKCS7_get_attributes(si) ((si)->unauth_attr) | ||
| 228 | |||
| 229 | #define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed) | ||
| 230 | #define PKCS7_type_is_encrypted(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_encrypted) | ||
| 231 | #define PKCS7_type_is_enveloped(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped) | ||
| 232 | #define PKCS7_type_is_signedAndEnveloped(a) \ | ||
| 233 | (OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped) | ||
| 234 | #define PKCS7_type_is_data(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_data) | ||
| 235 | |||
| 236 | #define PKCS7_set_detached(p,v) \ | ||
| 237 | PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL) | ||
| 238 | #define PKCS7_get_detached(p) \ | ||
| 239 | PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL) | ||
| 240 | |||
| 241 | #define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7)) | ||
| 242 | |||
| 243 | #ifdef SSLEAY_MACROS | ||
| 244 | #ifndef PKCS7_ISSUER_AND_SERIAL_digest | ||
| 245 | #define PKCS7_ISSUER_AND_SERIAL_digest(data,type,md,len) \ | ||
| 246 | ASN1_digest((int (*)())i2d_PKCS7_ISSUER_AND_SERIAL,type,\ | ||
| 247 | (char *)data,md,len) | ||
| 248 | #endif | ||
| 249 | #endif | ||
| 250 | |||
| 251 | /* S/MIME related flags */ | ||
| 252 | |||
| 253 | #define PKCS7_TEXT 0x1 | ||
| 254 | #define PKCS7_NOCERTS 0x2 | ||
| 255 | #define PKCS7_NOSIGS 0x4 | ||
| 256 | #define PKCS7_NOCHAIN 0x8 | ||
| 257 | #define PKCS7_NOINTERN 0x10 | ||
| 258 | #define PKCS7_NOVERIFY 0x20 | ||
| 259 | #define PKCS7_DETACHED 0x40 | ||
| 260 | #define PKCS7_BINARY 0x80 | ||
| 261 | #define PKCS7_NOATTR 0x100 | ||
| 262 | #define PKCS7_NOSMIMECAP 0x200 | ||
| 263 | |||
| 264 | /* Flags: for compatibility with older code */ | ||
| 265 | |||
| 266 | #define SMIME_TEXT PKCS7_TEXT | ||
| 267 | #define SMIME_NOCERTS PKCS7_NOCERTS | ||
| 268 | #define SMIME_NOSIGS PKCS7_NOSIGS | ||
| 269 | #define SMIME_NOCHAIN PKCS7_NOCHAIN | ||
| 270 | #define SMIME_NOINTERN PKCS7_NOINTERN | ||
| 271 | #define SMIME_NOVERIFY PKCS7_NOVERIFY | ||
| 272 | #define SMIME_DETACHED PKCS7_DETACHED | ||
| 273 | #define SMIME_BINARY PKCS7_BINARY | ||
| 274 | #define SMIME_NOATTR PKCS7_NOATTR | ||
| 275 | |||
| 276 | DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) | ||
| 277 | |||
| 278 | #ifndef SSLEAY_MACROS | ||
| 279 | int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,const EVP_MD *type, | ||
| 280 | unsigned char *md,unsigned int *len); | ||
| 281 | #ifndef OPENSSL_NO_FP_API | ||
| 282 | PKCS7 *d2i_PKCS7_fp(FILE *fp,PKCS7 **p7); | ||
| 283 | int i2d_PKCS7_fp(FILE *fp,PKCS7 *p7); | ||
| 284 | #endif | ||
| 285 | PKCS7 *PKCS7_dup(PKCS7 *p7); | ||
| 286 | PKCS7 *d2i_PKCS7_bio(BIO *bp,PKCS7 **p7); | ||
| 287 | int i2d_PKCS7_bio(BIO *bp,PKCS7 *p7); | ||
| 288 | #endif | ||
| 289 | |||
| 290 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) | ||
| 291 | DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) | ||
| 292 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED) | ||
| 293 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) | ||
| 294 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE) | ||
| 295 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) | ||
| 296 | DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST) | ||
| 297 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT) | ||
| 298 | DECLARE_ASN1_FUNCTIONS(PKCS7) | ||
| 299 | |||
| 300 | DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN) | ||
| 301 | DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY) | ||
| 302 | |||
| 303 | |||
| 304 | long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg); | ||
| 305 | |||
| 306 | int PKCS7_set_type(PKCS7 *p7, int type); | ||
| 307 | int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data); | ||
| 308 | int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, | ||
| 309 | const EVP_MD *dgst); | ||
| 310 | int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i); | ||
| 311 | int PKCS7_add_certificate(PKCS7 *p7, X509 *x509); | ||
| 312 | int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509); | ||
| 313 | int PKCS7_content_new(PKCS7 *p7, int nid); | ||
| 314 | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, | ||
| 315 | BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si); | ||
| 316 | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, | ||
| 317 | X509 *x509); | ||
| 318 | |||
| 319 | BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio); | ||
| 320 | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio); | ||
| 321 | BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert); | ||
| 322 | |||
| 323 | |||
| 324 | PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, | ||
| 325 | EVP_PKEY *pkey, const EVP_MD *dgst); | ||
| 326 | X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si); | ||
| 327 | STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); | ||
| 328 | |||
| 329 | PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509); | ||
| 330 | int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri); | ||
| 331 | int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509); | ||
| 332 | int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher); | ||
| 333 | |||
| 334 | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx); | ||
| 335 | ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 336 | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si,int nid,int type, | ||
| 337 | void *data); | ||
| 338 | int PKCS7_add_attribute (PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 339 | void *value); | ||
| 340 | ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid); | ||
| 341 | ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid); | ||
| 342 | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, | ||
| 343 | STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 344 | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 345 | |||
| 346 | |||
| 347 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | ||
| 348 | BIO *data, int flags); | ||
| 349 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | ||
| 350 | BIO *indata, BIO *out, int flags); | ||
| 351 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags); | ||
| 352 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | ||
| 353 | int flags); | ||
| 354 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags); | ||
| 355 | |||
| 356 | int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, | ||
| 357 | STACK_OF(X509_ALGOR) *cap); | ||
| 358 | STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si); | ||
| 359 | int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg); | ||
| 360 | |||
| 361 | int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags); | ||
| 362 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont); | ||
| 363 | int SMIME_crlf_copy(BIO *in, BIO *out, int flags); | ||
| 364 | int SMIME_text(BIO *in, BIO *out); | ||
| 365 | |||
| 366 | /* BEGIN ERROR CODES */ | ||
| 367 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 368 | * made after this point may be overwritten when the script is next run. | ||
| 369 | */ | ||
| 370 | void ERR_load_PKCS7_strings(void); | ||
| 371 | |||
| 372 | /* Error codes for the PKCS7 functions. */ | ||
| 373 | |||
| 374 | /* Function codes. */ | ||
| 375 | #define PKCS7_F_B64_READ_PKCS7 120 | ||
| 376 | #define PKCS7_F_B64_WRITE_PKCS7 121 | ||
| 377 | #define PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP 118 | ||
| 378 | #define PKCS7_F_PKCS7_ADD_CERTIFICATE 100 | ||
| 379 | #define PKCS7_F_PKCS7_ADD_CRL 101 | ||
| 380 | #define PKCS7_F_PKCS7_ADD_RECIPIENT_INFO 102 | ||
| 381 | #define PKCS7_F_PKCS7_ADD_SIGNER 103 | ||
| 382 | #define PKCS7_F_PKCS7_CTRL 104 | ||
| 383 | #define PKCS7_F_PKCS7_DATADECODE 112 | ||
| 384 | #define PKCS7_F_PKCS7_DATAINIT 105 | ||
| 385 | #define PKCS7_F_PKCS7_DATASIGN 106 | ||
| 386 | #define PKCS7_F_PKCS7_DATAVERIFY 107 | ||
| 387 | #define PKCS7_F_PKCS7_DECRYPT 114 | ||
| 388 | #define PKCS7_F_PKCS7_ENCRYPT 115 | ||
| 389 | #define PKCS7_F_PKCS7_GET0_SIGNERS 124 | ||
| 390 | #define PKCS7_F_PKCS7_SET_CIPHER 108 | ||
| 391 | #define PKCS7_F_PKCS7_SET_CONTENT 109 | ||
| 392 | #define PKCS7_F_PKCS7_SET_TYPE 110 | ||
| 393 | #define PKCS7_F_PKCS7_SIGN 116 | ||
| 394 | #define PKCS7_F_PKCS7_SIGNATUREVERIFY 113 | ||
| 395 | #define PKCS7_F_PKCS7_SIMPLE_SMIMECAP 119 | ||
| 396 | #define PKCS7_F_PKCS7_VERIFY 117 | ||
| 397 | #define PKCS7_F_SMIME_READ_PKCS7 122 | ||
| 398 | #define PKCS7_F_SMIME_TEXT 123 | ||
| 399 | |||
| 400 | /* Reason codes. */ | ||
| 401 | #define PKCS7_R_CERTIFICATE_VERIFY_ERROR 117 | ||
| 402 | #define PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 144 | ||
| 403 | #define PKCS7_R_CIPHER_NOT_INITIALIZED 116 | ||
| 404 | #define PKCS7_R_CONTENT_AND_DATA_PRESENT 118 | ||
| 405 | #define PKCS7_R_DECODE_ERROR 130 | ||
| 406 | #define PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH 100 | ||
| 407 | #define PKCS7_R_DECRYPT_ERROR 119 | ||
| 408 | #define PKCS7_R_DIGEST_FAILURE 101 | ||
| 409 | #define PKCS7_R_ERROR_ADDING_RECIPIENT 120 | ||
| 410 | #define PKCS7_R_ERROR_SETTING_CIPHER 121 | ||
| 411 | #define PKCS7_R_INVALID_MIME_TYPE 131 | ||
| 412 | #define PKCS7_R_INVALID_NULL_POINTER 143 | ||
| 413 | #define PKCS7_R_MIME_NO_CONTENT_TYPE 132 | ||
| 414 | #define PKCS7_R_MIME_PARSE_ERROR 133 | ||
| 415 | #define PKCS7_R_MIME_SIG_PARSE_ERROR 134 | ||
| 416 | #define PKCS7_R_MISSING_CERIPEND_INFO 103 | ||
| 417 | #define PKCS7_R_NO_CONTENT 122 | ||
| 418 | #define PKCS7_R_NO_CONTENT_TYPE 135 | ||
| 419 | #define PKCS7_R_NO_MULTIPART_BODY_FAILURE 136 | ||
| 420 | #define PKCS7_R_NO_MULTIPART_BOUNDARY 137 | ||
| 421 | #define PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE 115 | ||
| 422 | #define PKCS7_R_NO_SIGNATURES_ON_DATA 123 | ||
| 423 | #define PKCS7_R_NO_SIGNERS 142 | ||
| 424 | #define PKCS7_R_NO_SIG_CONTENT_TYPE 138 | ||
| 425 | #define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE 104 | ||
| 426 | #define PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR 124 | ||
| 427 | #define PKCS7_R_PKCS7_DATAFINAL_ERROR 125 | ||
| 428 | #define PKCS7_R_PKCS7_DATASIGN 126 | ||
| 429 | #define PKCS7_R_PKCS7_PARSE_ERROR 139 | ||
| 430 | #define PKCS7_R_PKCS7_SIG_PARSE_ERROR 140 | ||
| 431 | #define PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 127 | ||
| 432 | #define PKCS7_R_SIGNATURE_FAILURE 105 | ||
| 433 | #define PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND 128 | ||
| 434 | #define PKCS7_R_SIG_INVALID_MIME_TYPE 141 | ||
| 435 | #define PKCS7_R_SMIME_TEXT_ERROR 129 | ||
| 436 | #define PKCS7_R_UNABLE_TO_FIND_CERTIFICATE 106 | ||
| 437 | #define PKCS7_R_UNABLE_TO_FIND_MEM_BIO 107 | ||
| 438 | #define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST 108 | ||
| 439 | #define PKCS7_R_UNKNOWN_DIGEST_TYPE 109 | ||
| 440 | #define PKCS7_R_UNKNOWN_OPERATION 110 | ||
| 441 | #define PKCS7_R_UNSUPPORTED_CIPHER_TYPE 111 | ||
| 442 | #define PKCS7_R_UNSUPPORTED_CONTENT_TYPE 112 | ||
| 443 | #define PKCS7_R_WRONG_CONTENT_TYPE 113 | ||
| 444 | #define PKCS7_R_WRONG_PKCS7_TYPE 114 | ||
| 445 | |||
| 446 | #ifdef __cplusplus | ||
| 447 | } | ||
| 448 | #endif | ||
| 449 | #endif | ||
diff --git a/src/lib/libcrypto/pkcs7/pkcs7err.c b/src/lib/libcrypto/pkcs7/pkcs7err.c new file mode 100644 index 0000000000..5e51527a40 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pkcs7err.c | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | /* crypto/pkcs7/pkcs7err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@OpenSSL.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* NOTE: this file was auto generated by the mkerr.pl script: any changes | ||
| 57 | * made to it will be overwritten when the script next updates this file, | ||
| 58 | * only reason strings will be preserved. | ||
| 59 | */ | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <openssl/err.h> | ||
| 63 | #include <openssl/pkcs7.h> | ||
| 64 | |||
| 65 | /* BEGIN ERROR CODES */ | ||
| 66 | #ifndef OPENSSL_NO_ERR | ||
| 67 | static ERR_STRING_DATA PKCS7_str_functs[]= | ||
| 68 | { | ||
| 69 | {ERR_PACK(0,PKCS7_F_B64_READ_PKCS7,0), "B64_READ_PKCS7"}, | ||
| 70 | {ERR_PACK(0,PKCS7_F_B64_WRITE_PKCS7,0), "B64_WRITE_PKCS7"}, | ||
| 71 | {ERR_PACK(0,PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,0), "PKCS7_add_attrib_smimecap"}, | ||
| 72 | {ERR_PACK(0,PKCS7_F_PKCS7_ADD_CERTIFICATE,0), "PKCS7_add_certificate"}, | ||
| 73 | {ERR_PACK(0,PKCS7_F_PKCS7_ADD_CRL,0), "PKCS7_add_crl"}, | ||
| 74 | {ERR_PACK(0,PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,0), "PKCS7_add_recipient_info"}, | ||
| 75 | {ERR_PACK(0,PKCS7_F_PKCS7_ADD_SIGNER,0), "PKCS7_add_signer"}, | ||
| 76 | {ERR_PACK(0,PKCS7_F_PKCS7_CTRL,0), "PKCS7_ctrl"}, | ||
| 77 | {ERR_PACK(0,PKCS7_F_PKCS7_DATADECODE,0), "PKCS7_dataDecode"}, | ||
| 78 | {ERR_PACK(0,PKCS7_F_PKCS7_DATAINIT,0), "PKCS7_dataInit"}, | ||
| 79 | {ERR_PACK(0,PKCS7_F_PKCS7_DATASIGN,0), "PKCS7_DATASIGN"}, | ||
| 80 | {ERR_PACK(0,PKCS7_F_PKCS7_DATAVERIFY,0), "PKCS7_dataVerify"}, | ||
| 81 | {ERR_PACK(0,PKCS7_F_PKCS7_DECRYPT,0), "PKCS7_decrypt"}, | ||
| 82 | {ERR_PACK(0,PKCS7_F_PKCS7_ENCRYPT,0), "PKCS7_encrypt"}, | ||
| 83 | {ERR_PACK(0,PKCS7_F_PKCS7_GET0_SIGNERS,0), "PKCS7_get0_signers"}, | ||
| 84 | {ERR_PACK(0,PKCS7_F_PKCS7_SET_CIPHER,0), "PKCS7_set_cipher"}, | ||
| 85 | {ERR_PACK(0,PKCS7_F_PKCS7_SET_CONTENT,0), "PKCS7_set_content"}, | ||
| 86 | {ERR_PACK(0,PKCS7_F_PKCS7_SET_TYPE,0), "PKCS7_set_type"}, | ||
| 87 | {ERR_PACK(0,PKCS7_F_PKCS7_SIGN,0), "PKCS7_sign"}, | ||
| 88 | {ERR_PACK(0,PKCS7_F_PKCS7_SIGNATUREVERIFY,0), "PKCS7_signatureVerify"}, | ||
| 89 | {ERR_PACK(0,PKCS7_F_PKCS7_SIMPLE_SMIMECAP,0), "PKCS7_simple_smimecap"}, | ||
| 90 | {ERR_PACK(0,PKCS7_F_PKCS7_VERIFY,0), "PKCS7_verify"}, | ||
| 91 | {ERR_PACK(0,PKCS7_F_SMIME_READ_PKCS7,0), "SMIME_read_PKCS7"}, | ||
| 92 | {ERR_PACK(0,PKCS7_F_SMIME_TEXT,0), "SMIME_text"}, | ||
| 93 | {0,NULL} | ||
| 94 | }; | ||
| 95 | |||
| 96 | static ERR_STRING_DATA PKCS7_str_reasons[]= | ||
| 97 | { | ||
| 98 | {PKCS7_R_CERTIFICATE_VERIFY_ERROR ,"certificate verify error"}, | ||
| 99 | {PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER ,"cipher has no object identifier"}, | ||
| 100 | {PKCS7_R_CIPHER_NOT_INITIALIZED ,"cipher not initialized"}, | ||
| 101 | {PKCS7_R_CONTENT_AND_DATA_PRESENT ,"content and data present"}, | ||
| 102 | {PKCS7_R_DECODE_ERROR ,"decode error"}, | ||
| 103 | {PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH ,"decrypted key is wrong length"}, | ||
| 104 | {PKCS7_R_DECRYPT_ERROR ,"decrypt error"}, | ||
| 105 | {PKCS7_R_DIGEST_FAILURE ,"digest failure"}, | ||
| 106 | {PKCS7_R_ERROR_ADDING_RECIPIENT ,"error adding recipient"}, | ||
| 107 | {PKCS7_R_ERROR_SETTING_CIPHER ,"error setting cipher"}, | ||
| 108 | {PKCS7_R_INVALID_MIME_TYPE ,"invalid mime type"}, | ||
| 109 | {PKCS7_R_INVALID_NULL_POINTER ,"invalid null pointer"}, | ||
| 110 | {PKCS7_R_MIME_NO_CONTENT_TYPE ,"mime no content type"}, | ||
| 111 | {PKCS7_R_MIME_PARSE_ERROR ,"mime parse error"}, | ||
| 112 | {PKCS7_R_MIME_SIG_PARSE_ERROR ,"mime sig parse error"}, | ||
| 113 | {PKCS7_R_MISSING_CERIPEND_INFO ,"missing ceripend info"}, | ||
| 114 | {PKCS7_R_NO_CONTENT ,"no content"}, | ||
| 115 | {PKCS7_R_NO_CONTENT_TYPE ,"no content type"}, | ||
| 116 | {PKCS7_R_NO_MULTIPART_BODY_FAILURE ,"no multipart body failure"}, | ||
| 117 | {PKCS7_R_NO_MULTIPART_BOUNDARY ,"no multipart boundary"}, | ||
| 118 | {PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE,"no recipient matches certificate"}, | ||
| 119 | {PKCS7_R_NO_SIGNATURES_ON_DATA ,"no signatures on data"}, | ||
| 120 | {PKCS7_R_NO_SIGNERS ,"no signers"}, | ||
| 121 | {PKCS7_R_NO_SIG_CONTENT_TYPE ,"no sig content type"}, | ||
| 122 | {PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE,"operation not supported on this type"}, | ||
| 123 | {PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR ,"pkcs7 add signature error"}, | ||
| 124 | {PKCS7_R_PKCS7_DATAFINAL_ERROR ,"pkcs7 datafinal error"}, | ||
| 125 | {PKCS7_R_PKCS7_DATASIGN ,"pkcs7 datasign"}, | ||
| 126 | {PKCS7_R_PKCS7_PARSE_ERROR ,"pkcs7 parse error"}, | ||
| 127 | {PKCS7_R_PKCS7_SIG_PARSE_ERROR ,"pkcs7 sig parse error"}, | ||
| 128 | {PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE,"private key does not match certificate"}, | ||
| 129 | {PKCS7_R_SIGNATURE_FAILURE ,"signature failure"}, | ||
| 130 | {PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND ,"signer certificate not found"}, | ||
| 131 | {PKCS7_R_SIG_INVALID_MIME_TYPE ,"sig invalid mime type"}, | ||
| 132 | {PKCS7_R_SMIME_TEXT_ERROR ,"smime text error"}, | ||
| 133 | {PKCS7_R_UNABLE_TO_FIND_CERTIFICATE ,"unable to find certificate"}, | ||
| 134 | {PKCS7_R_UNABLE_TO_FIND_MEM_BIO ,"unable to find mem bio"}, | ||
| 135 | {PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST ,"unable to find message digest"}, | ||
| 136 | {PKCS7_R_UNKNOWN_DIGEST_TYPE ,"unknown digest type"}, | ||
| 137 | {PKCS7_R_UNKNOWN_OPERATION ,"unknown operation"}, | ||
| 138 | {PKCS7_R_UNSUPPORTED_CIPHER_TYPE ,"unsupported cipher type"}, | ||
| 139 | {PKCS7_R_UNSUPPORTED_CONTENT_TYPE ,"unsupported content type"}, | ||
| 140 | {PKCS7_R_WRONG_CONTENT_TYPE ,"wrong content type"}, | ||
| 141 | {PKCS7_R_WRONG_PKCS7_TYPE ,"wrong pkcs7 type"}, | ||
| 142 | {0,NULL} | ||
| 143 | }; | ||
| 144 | |||
| 145 | #endif | ||
| 146 | |||
| 147 | void ERR_load_PKCS7_strings(void) | ||
| 148 | { | ||
| 149 | static int init=1; | ||
| 150 | |||
| 151 | if (init) | ||
| 152 | { | ||
| 153 | init=0; | ||
| 154 | #ifndef OPENSSL_NO_ERR | ||
| 155 | ERR_load_strings(ERR_LIB_PKCS7,PKCS7_str_functs); | ||
| 156 | ERR_load_strings(ERR_LIB_PKCS7,PKCS7_str_reasons); | ||
| 157 | #endif | ||
| 158 | |||
| 159 | } | ||
| 160 | } | ||
