diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs7')
| -rw-r--r-- | src/lib/libcrypto/pkcs7/Makefile.ssl | 243 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_asn1.c | 214 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_attr.c | 141 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_doit.c | 1147 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_lib.c | 589 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_mime.c | 722 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_smime.c | 500 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pkcs7.h | 464 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pkcs7err.c | 167 |
9 files changed, 4187 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..c3bfc7d560 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/Makefile.ssl | |||
| @@ -0,0 +1,243 @@ | |||
| 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) -- $(CFLAG) $(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/aes.h | ||
| 104 | pk7_asn1.o: ../../include/openssl/asn1.h ../../include/openssl/asn1t.h | ||
| 105 | pk7_asn1.o: ../../include/openssl/bio.h ../../include/openssl/blowfish.h | ||
| 106 | pk7_asn1.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 107 | pk7_asn1.o: ../../include/openssl/cast.h ../../include/openssl/crypto.h | ||
| 108 | pk7_asn1.o: ../../include/openssl/des.h ../../include/openssl/des_old.h | ||
| 109 | pk7_asn1.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
| 110 | pk7_asn1.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 111 | pk7_asn1.o: ../../include/openssl/evp.h ../../include/openssl/idea.h | ||
| 112 | pk7_asn1.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h | ||
| 113 | pk7_asn1.o: ../../include/openssl/md4.h ../../include/openssl/md5.h | ||
| 114 | pk7_asn1.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h | ||
| 115 | pk7_asn1.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
| 116 | pk7_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 117 | pk7_asn1.o: ../../include/openssl/pkcs7.h ../../include/openssl/rc2.h | ||
| 118 | pk7_asn1.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h | ||
| 119 | pk7_asn1.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h | ||
| 120 | pk7_asn1.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 121 | pk7_asn1.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 122 | pk7_asn1.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h | ||
| 123 | pk7_asn1.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 124 | pk7_asn1.o: ../cryptlib.h pk7_asn1.c | ||
| 125 | pk7_attr.o: ../../include/openssl/aes.h ../../include/openssl/asn1.h | ||
| 126 | pk7_attr.o: ../../include/openssl/bio.h ../../include/openssl/blowfish.h | ||
| 127 | pk7_attr.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 128 | pk7_attr.o: ../../include/openssl/cast.h ../../include/openssl/crypto.h | ||
| 129 | pk7_attr.o: ../../include/openssl/des.h ../../include/openssl/des_old.h | ||
| 130 | pk7_attr.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
| 131 | pk7_attr.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 132 | pk7_attr.o: ../../include/openssl/evp.h ../../include/openssl/idea.h | ||
| 133 | pk7_attr.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h | ||
| 134 | pk7_attr.o: ../../include/openssl/md4.h ../../include/openssl/md5.h | ||
| 135 | pk7_attr.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h | ||
| 136 | pk7_attr.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
| 137 | pk7_attr.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 138 | pk7_attr.o: ../../include/openssl/pem.h ../../include/openssl/pem2.h | ||
| 139 | pk7_attr.o: ../../include/openssl/pkcs7.h ../../include/openssl/rc2.h | ||
| 140 | pk7_attr.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h | ||
| 141 | pk7_attr.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h | ||
| 142 | pk7_attr.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 143 | pk7_attr.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 144 | pk7_attr.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h | ||
| 145 | pk7_attr.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 146 | pk7_attr.o: pk7_attr.c | ||
| 147 | pk7_doit.o: ../../e_os.h ../../include/openssl/aes.h | ||
| 148 | pk7_doit.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 149 | pk7_doit.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
| 150 | pk7_doit.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
| 151 | pk7_doit.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h | ||
| 152 | pk7_doit.o: ../../include/openssl/des.h ../../include/openssl/des_old.h | ||
| 153 | pk7_doit.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
| 154 | pk7_doit.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 155 | pk7_doit.o: ../../include/openssl/evp.h ../../include/openssl/idea.h | ||
| 156 | pk7_doit.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h | ||
| 157 | pk7_doit.o: ../../include/openssl/md4.h ../../include/openssl/md5.h | ||
| 158 | pk7_doit.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h | ||
| 159 | pk7_doit.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
| 160 | pk7_doit.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 161 | pk7_doit.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h | ||
| 162 | pk7_doit.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h | ||
| 163 | pk7_doit.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h | ||
| 164 | pk7_doit.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 165 | pk7_doit.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
| 166 | pk7_doit.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h | ||
| 167 | pk7_doit.o: ../../include/openssl/ui_compat.h ../../include/openssl/x509.h | ||
| 168 | pk7_doit.o: ../../include/openssl/x509_vfy.h ../../include/openssl/x509v3.h | ||
| 169 | pk7_doit.o: ../cryptlib.h pk7_doit.c | ||
| 170 | pk7_lib.o: ../../e_os.h ../../include/openssl/aes.h | ||
| 171 | pk7_lib.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 172 | pk7_lib.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
| 173 | pk7_lib.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
| 174 | pk7_lib.o: ../../include/openssl/crypto.h ../../include/openssl/des.h | ||
| 175 | pk7_lib.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h | ||
| 176 | pk7_lib.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 177 | pk7_lib.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 178 | pk7_lib.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h | ||
| 179 | pk7_lib.o: ../../include/openssl/md2.h ../../include/openssl/md4.h | ||
| 180 | pk7_lib.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h | ||
| 181 | pk7_lib.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
| 182 | pk7_lib.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
| 183 | pk7_lib.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
| 184 | pk7_lib.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h | ||
| 185 | pk7_lib.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h | ||
| 186 | pk7_lib.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 187 | pk7_lib.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
| 188 | pk7_lib.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h | ||
| 189 | pk7_lib.o: ../../include/openssl/ui_compat.h ../../include/openssl/x509.h | ||
| 190 | pk7_lib.o: ../../include/openssl/x509_vfy.h ../cryptlib.h pk7_lib.c | ||
| 191 | pk7_mime.o: ../../e_os.h ../../include/openssl/aes.h | ||
| 192 | pk7_mime.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 193 | pk7_mime.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
| 194 | pk7_mime.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
| 195 | pk7_mime.o: ../../include/openssl/crypto.h ../../include/openssl/des.h | ||
| 196 | pk7_mime.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h | ||
| 197 | pk7_mime.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 198 | pk7_mime.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 199 | pk7_mime.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h | ||
| 200 | pk7_mime.o: ../../include/openssl/md2.h ../../include/openssl/md4.h | ||
| 201 | pk7_mime.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h | ||
| 202 | pk7_mime.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
| 203 | pk7_mime.o: ../../include/openssl/opensslconf.h | ||
| 204 | pk7_mime.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 205 | pk7_mime.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h | ||
| 206 | pk7_mime.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h | ||
| 207 | pk7_mime.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h | ||
| 208 | pk7_mime.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 209 | pk7_mime.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
| 210 | pk7_mime.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h | ||
| 211 | pk7_mime.o: ../../include/openssl/ui_compat.h ../../include/openssl/x509.h | ||
| 212 | pk7_mime.o: ../../include/openssl/x509_vfy.h ../cryptlib.h pk7_mime.c | ||
| 213 | pk7_smime.o: ../../e_os.h ../../include/openssl/aes.h | ||
| 214 | pk7_smime.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 215 | pk7_smime.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
| 216 | pk7_smime.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
| 217 | pk7_smime.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h | ||
| 218 | pk7_smime.o: ../../include/openssl/des.h ../../include/openssl/des_old.h | ||
| 219 | pk7_smime.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
| 220 | pk7_smime.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 221 | pk7_smime.o: ../../include/openssl/evp.h ../../include/openssl/idea.h | ||
| 222 | pk7_smime.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h | ||
| 223 | pk7_smime.o: ../../include/openssl/md4.h ../../include/openssl/md5.h | ||
| 224 | pk7_smime.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h | ||
| 225 | pk7_smime.o: ../../include/openssl/objects.h | ||
| 226 | pk7_smime.o: ../../include/openssl/opensslconf.h | ||
| 227 | pk7_smime.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 228 | pk7_smime.o: ../../include/openssl/pkcs7.h ../../include/openssl/rc2.h | ||
| 229 | pk7_smime.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h | ||
| 230 | pk7_smime.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h | ||
| 231 | pk7_smime.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 232 | pk7_smime.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 233 | pk7_smime.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h | ||
| 234 | pk7_smime.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 235 | pk7_smime.o: ../../include/openssl/x509v3.h ../cryptlib.h pk7_smime.c | ||
| 236 | pkcs7err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 237 | pkcs7err.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h | ||
| 238 | pkcs7err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 239 | pkcs7err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 240 | pkcs7err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 241 | pkcs7err.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
| 242 | pkcs7err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 243 | 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..77931feeb4 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_asn1.c | |||
| @@ -0,0 +1,214 @@ | |||
| 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_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)), | ||
| 73 | ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)), | ||
| 74 | ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)), | ||
| 75 | ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)), | ||
| 76 | ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)), | ||
| 77 | ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0)) | ||
| 78 | } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL); | ||
| 79 | |||
| 80 | ASN1_NDEF_SEQUENCE(PKCS7) = { | ||
| 81 | ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT), | ||
| 82 | ASN1_ADB_OBJECT(PKCS7) | ||
| 83 | }ASN1_NDEF_SEQUENCE_END(PKCS7) | ||
| 84 | |||
| 85 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7) | ||
| 86 | IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7) | ||
| 87 | IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7) | ||
| 88 | |||
| 89 | ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = { | ||
| 90 | ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER), | ||
| 91 | ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR), | ||
| 92 | ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7), | ||
| 93 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0), | ||
| 94 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1), | ||
| 95 | ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO) | ||
| 96 | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED) | ||
| 97 | |||
| 98 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED) | ||
| 99 | |||
| 100 | /* Minor tweak to operation: free up EVP_PKEY */ | ||
| 101 | static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 102 | { | ||
| 103 | if(operation == ASN1_OP_FREE_POST) { | ||
| 104 | PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval; | ||
| 105 | EVP_PKEY_free(si->pkey); | ||
| 106 | } | ||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = { | ||
| 111 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER), | ||
| 112 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | ||
| 113 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR), | ||
| 114 | /* NB this should be a SET OF but we use a SEQUENCE OF so the | ||
| 115 | * original order * is retained when the structure is reencoded. | ||
| 116 | * Since the attributes are implicitly tagged this will not affect | ||
| 117 | * the encoding. | ||
| 118 | */ | ||
| 119 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0), | ||
| 120 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR), | ||
| 121 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING), | ||
| 122 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1) | ||
| 123 | } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO) | ||
| 124 | |||
| 125 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) | ||
| 126 | |||
| 127 | ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = { | ||
| 128 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME), | ||
| 129 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER) | ||
| 130 | } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL) | ||
| 131 | |||
| 132 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) | ||
| 133 | |||
| 134 | ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = { | ||
| 135 | ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER), | ||
| 136 | ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | ||
| 137 | ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT) | ||
| 138 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE) | ||
| 139 | |||
| 140 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE) | ||
| 141 | |||
| 142 | /* Minor tweak to operation: free up X509 */ | ||
| 143 | static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 144 | { | ||
| 145 | if(operation == ASN1_OP_FREE_POST) { | ||
| 146 | PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval; | ||
| 147 | X509_free(ri->cert); | ||
| 148 | } | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = { | ||
| 153 | ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER), | ||
| 154 | ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | ||
| 155 | ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR), | ||
| 156 | ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING) | ||
| 157 | } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO) | ||
| 158 | |||
| 159 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) | ||
| 160 | |||
| 161 | ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = { | ||
| 162 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT), | ||
| 163 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR), | ||
| 164 | ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING, 0) | ||
| 165 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT) | ||
| 166 | |||
| 167 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) | ||
| 168 | |||
| 169 | ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = { | ||
| 170 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER), | ||
| 171 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | ||
| 172 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR), | ||
| 173 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT), | ||
| 174 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0), | ||
| 175 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1), | ||
| 176 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO) | ||
| 177 | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE) | ||
| 178 | |||
| 179 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) | ||
| 180 | |||
| 181 | ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = { | ||
| 182 | ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER), | ||
| 183 | ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT) | ||
| 184 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT) | ||
| 185 | |||
| 186 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT) | ||
| 187 | |||
| 188 | ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = { | ||
| 189 | ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER), | ||
| 190 | ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR), | ||
| 191 | ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7), | ||
| 192 | ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING) | ||
| 193 | } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST) | ||
| 194 | |||
| 195 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST) | ||
| 196 | |||
| 197 | /* Specials for authenticated attributes */ | ||
| 198 | |||
| 199 | /* When signing attributes we want to reorder them to match the sorted | ||
| 200 | * encoding. | ||
| 201 | */ | ||
| 202 | |||
| 203 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) = | ||
| 204 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | ||
| 205 | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN) | ||
| 206 | |||
| 207 | /* When verifying attributes we need to use the received order. So | ||
| 208 | * we use SEQUENCE OF and tag it to SET OF | ||
| 209 | */ | ||
| 210 | |||
| 211 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) = | ||
| 212 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, | ||
| 213 | V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | ||
| 214 | 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..735c8800e1 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_attr.c | |||
| @@ -0,0 +1,141 @@ | |||
| 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-2004 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 | const unsigned char *p; | ||
| 100 | |||
| 101 | cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities); | ||
| 102 | if (!cap || (cap->type != V_ASN1_SEQUENCE)) | ||
| 103 | return NULL; | ||
| 104 | p = cap->value.sequence->data; | ||
| 105 | return d2i_ASN1_SET_OF_X509_ALGOR(NULL, &p, | ||
| 106 | cap->value.sequence->length, | ||
| 107 | d2i_X509_ALGOR, X509_ALGOR_free, | ||
| 108 | V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); | ||
| 109 | } | ||
| 110 | |||
| 111 | /* Basic smime-capabilities OID and optional integer arg */ | ||
| 112 | int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) | ||
| 113 | { | ||
| 114 | X509_ALGOR *alg; | ||
| 115 | |||
| 116 | if(!(alg = X509_ALGOR_new())) { | ||
| 117 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | ASN1_OBJECT_free(alg->algorithm); | ||
| 121 | alg->algorithm = OBJ_nid2obj (nid); | ||
| 122 | if (arg > 0) { | ||
| 123 | ASN1_INTEGER *nbit; | ||
| 124 | if(!(alg->parameter = ASN1_TYPE_new())) { | ||
| 125 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | if(!(nbit = ASN1_INTEGER_new())) { | ||
| 129 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 130 | return 0; | ||
| 131 | } | ||
| 132 | if(!ASN1_INTEGER_set (nbit, arg)) { | ||
| 133 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 134 | return 0; | ||
| 135 | } | ||
| 136 | alg->parameter->value.integer = nbit; | ||
| 137 | alg->parameter->type = V_ASN1_INTEGER; | ||
| 138 | } | ||
| 139 | sk_X509_ALGOR_push (sk, alg); | ||
| 140 | return 1; | ||
| 141 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_doit.c b/src/lib/libcrypto/pkcs7/pk7_doit.c new file mode 100644 index 0000000000..a03d7ebedf --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_doit.c | |||
| @@ -0,0 +1,1147 @@ | |||
| 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 | #include <openssl/err.h> | ||
| 66 | |||
| 67 | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, | ||
| 68 | void *value); | ||
| 69 | static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid); | ||
| 70 | |||
| 71 | static int PKCS7_type_is_other(PKCS7* p7) | ||
| 72 | { | ||
| 73 | int isOther=1; | ||
| 74 | |||
| 75 | int nid=OBJ_obj2nid(p7->type); | ||
| 76 | |||
| 77 | switch( nid ) | ||
| 78 | { | ||
| 79 | case NID_pkcs7_data: | ||
| 80 | case NID_pkcs7_signed: | ||
| 81 | case NID_pkcs7_enveloped: | ||
| 82 | case NID_pkcs7_signedAndEnveloped: | ||
| 83 | case NID_pkcs7_digest: | ||
| 84 | case NID_pkcs7_encrypted: | ||
| 85 | isOther=0; | ||
| 86 | break; | ||
| 87 | default: | ||
| 88 | isOther=1; | ||
| 89 | } | ||
| 90 | |||
| 91 | return isOther; | ||
| 92 | |||
| 93 | } | ||
| 94 | |||
| 95 | static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7) | ||
| 96 | { | ||
| 97 | if ( PKCS7_type_is_data(p7)) | ||
| 98 | return p7->d.data; | ||
| 99 | if ( PKCS7_type_is_other(p7) && p7->d.other | ||
| 100 | && (p7->d.other->type == V_ASN1_OCTET_STRING)) | ||
| 101 | return p7->d.other->value.octet_string; | ||
| 102 | return NULL; | ||
| 103 | } | ||
| 104 | |||
| 105 | static int PKCS7_bio_add_digest(BIO **pbio, X509_ALGOR *alg) | ||
| 106 | { | ||
| 107 | BIO *btmp; | ||
| 108 | const EVP_MD *md; | ||
| 109 | if ((btmp=BIO_new(BIO_f_md())) == NULL) | ||
| 110 | { | ||
| 111 | PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST,ERR_R_BIO_LIB); | ||
| 112 | goto err; | ||
| 113 | } | ||
| 114 | |||
| 115 | md=EVP_get_digestbyobj(alg->algorithm); | ||
| 116 | if (md == NULL) | ||
| 117 | { | ||
| 118 | PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST,PKCS7_R_UNKNOWN_DIGEST_TYPE); | ||
| 119 | goto err; | ||
| 120 | } | ||
| 121 | |||
| 122 | BIO_set_md(btmp,md); | ||
| 123 | if (*pbio == NULL) | ||
| 124 | *pbio=btmp; | ||
| 125 | else if (!BIO_push(*pbio,btmp)) | ||
| 126 | { | ||
| 127 | PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST,ERR_R_BIO_LIB); | ||
| 128 | goto err; | ||
| 129 | } | ||
| 130 | btmp=NULL; | ||
| 131 | |||
| 132 | return 1; | ||
| 133 | |||
| 134 | err: | ||
| 135 | if (btmp) | ||
| 136 | BIO_free(btmp); | ||
| 137 | return 0; | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) | ||
| 142 | { | ||
| 143 | int i; | ||
| 144 | BIO *out=NULL,*btmp=NULL; | ||
| 145 | X509_ALGOR *xa = NULL; | ||
| 146 | const EVP_CIPHER *evp_cipher=NULL; | ||
| 147 | STACK_OF(X509_ALGOR) *md_sk=NULL; | ||
| 148 | STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; | ||
| 149 | X509_ALGOR *xalg=NULL; | ||
| 150 | PKCS7_RECIP_INFO *ri=NULL; | ||
| 151 | EVP_PKEY *pkey; | ||
| 152 | ASN1_OCTET_STRING *os=NULL; | ||
| 153 | |||
| 154 | i=OBJ_obj2nid(p7->type); | ||
| 155 | p7->state=PKCS7_S_HEADER; | ||
| 156 | |||
| 157 | switch (i) | ||
| 158 | { | ||
| 159 | case NID_pkcs7_signed: | ||
| 160 | md_sk=p7->d.sign->md_algs; | ||
| 161 | os = PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 162 | break; | ||
| 163 | case NID_pkcs7_signedAndEnveloped: | ||
| 164 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 165 | md_sk=p7->d.signed_and_enveloped->md_algs; | ||
| 166 | xalg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 167 | evp_cipher=p7->d.signed_and_enveloped->enc_data->cipher; | ||
| 168 | if (evp_cipher == NULL) | ||
| 169 | { | ||
| 170 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 171 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 172 | goto err; | ||
| 173 | } | ||
| 174 | break; | ||
| 175 | case NID_pkcs7_enveloped: | ||
| 176 | rsk=p7->d.enveloped->recipientinfo; | ||
| 177 | xalg=p7->d.enveloped->enc_data->algorithm; | ||
| 178 | evp_cipher=p7->d.enveloped->enc_data->cipher; | ||
| 179 | if (evp_cipher == NULL) | ||
| 180 | { | ||
| 181 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 182 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 183 | goto err; | ||
| 184 | } | ||
| 185 | break; | ||
| 186 | case NID_pkcs7_digest: | ||
| 187 | xa = p7->d.digest->md; | ||
| 188 | os = PKCS7_get_octet_string(p7->d.digest->contents); | ||
| 189 | break; | ||
| 190 | default: | ||
| 191 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 192 | goto err; | ||
| 193 | } | ||
| 194 | |||
| 195 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 196 | if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i))) | ||
| 197 | goto err; | ||
| 198 | |||
| 199 | if (xa && !PKCS7_bio_add_digest(&out, xa)) | ||
| 200 | goto err; | ||
| 201 | |||
| 202 | if (evp_cipher != NULL) | ||
| 203 | { | ||
| 204 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| 205 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 206 | int keylen,ivlen; | ||
| 207 | int jj,max; | ||
| 208 | unsigned char *tmp; | ||
| 209 | EVP_CIPHER_CTX *ctx; | ||
| 210 | |||
| 211 | if ((btmp=BIO_new(BIO_f_cipher())) == NULL) | ||
| 212 | { | ||
| 213 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB); | ||
| 214 | goto err; | ||
| 215 | } | ||
| 216 | BIO_get_cipher_ctx(btmp, &ctx); | ||
| 217 | keylen=EVP_CIPHER_key_length(evp_cipher); | ||
| 218 | ivlen=EVP_CIPHER_iv_length(evp_cipher); | ||
| 219 | xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher)); | ||
| 220 | if (ivlen > 0) | ||
| 221 | if (RAND_pseudo_bytes(iv,ivlen) <= 0) | ||
| 222 | goto err; | ||
| 223 | if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1)<=0) | ||
| 224 | goto err; | ||
| 225 | if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) | ||
| 226 | goto err; | ||
| 227 | if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0) | ||
| 228 | goto err; | ||
| 229 | |||
| 230 | if (ivlen > 0) { | ||
| 231 | if (xalg->parameter == NULL) { | ||
| 232 | xalg->parameter = ASN1_TYPE_new(); | ||
| 233 | if (xalg->parameter == NULL) | ||
| 234 | goto err; | ||
| 235 | } | ||
| 236 | if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0) | ||
| 237 | goto err; | ||
| 238 | } | ||
| 239 | |||
| 240 | /* Lets do the pub key stuff :-) */ | ||
| 241 | max=0; | ||
| 242 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 243 | { | ||
| 244 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 245 | if (ri->cert == NULL) | ||
| 246 | { | ||
| 247 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_MISSING_CERIPEND_INFO); | ||
| 248 | goto err; | ||
| 249 | } | ||
| 250 | if ((pkey=X509_get_pubkey(ri->cert)) == NULL) | ||
| 251 | goto err; | ||
| 252 | jj=EVP_PKEY_size(pkey); | ||
| 253 | EVP_PKEY_free(pkey); | ||
| 254 | if (max < jj) max=jj; | ||
| 255 | } | ||
| 256 | if ((tmp=(unsigned char *)OPENSSL_malloc(max)) == NULL) | ||
| 257 | { | ||
| 258 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_MALLOC_FAILURE); | ||
| 259 | goto err; | ||
| 260 | } | ||
| 261 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 262 | { | ||
| 263 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 264 | if ((pkey=X509_get_pubkey(ri->cert)) == NULL) | ||
| 265 | goto err; | ||
| 266 | jj=EVP_PKEY_encrypt(tmp,key,keylen,pkey); | ||
| 267 | EVP_PKEY_free(pkey); | ||
| 268 | if (jj <= 0) | ||
| 269 | { | ||
| 270 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_EVP_LIB); | ||
| 271 | OPENSSL_free(tmp); | ||
| 272 | goto err; | ||
| 273 | } | ||
| 274 | if (!M_ASN1_OCTET_STRING_set(ri->enc_key,tmp,jj)) | ||
| 275 | { | ||
| 276 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 277 | ERR_R_MALLOC_FAILURE); | ||
| 278 | OPENSSL_free(tmp); | ||
| 279 | goto err; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | OPENSSL_free(tmp); | ||
| 283 | OPENSSL_cleanse(key, keylen); | ||
| 284 | |||
| 285 | if (out == NULL) | ||
| 286 | out=btmp; | ||
| 287 | else | ||
| 288 | BIO_push(out,btmp); | ||
| 289 | btmp=NULL; | ||
| 290 | } | ||
| 291 | |||
| 292 | if (bio == NULL) | ||
| 293 | { | ||
| 294 | if (PKCS7_is_detached(p7)) | ||
| 295 | bio=BIO_new(BIO_s_null()); | ||
| 296 | else if (os && os->length > 0) | ||
| 297 | bio = BIO_new_mem_buf(os->data, os->length); | ||
| 298 | if(bio == NULL) | ||
| 299 | { | ||
| 300 | bio=BIO_new(BIO_s_mem()); | ||
| 301 | if (bio == NULL) | ||
| 302 | goto err; | ||
| 303 | BIO_set_mem_eof_return(bio,0); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | BIO_push(out,bio); | ||
| 307 | bio=NULL; | ||
| 308 | if (0) | ||
| 309 | { | ||
| 310 | err: | ||
| 311 | if (out != NULL) | ||
| 312 | BIO_free_all(out); | ||
| 313 | if (btmp != NULL) | ||
| 314 | BIO_free_all(btmp); | ||
| 315 | out=NULL; | ||
| 316 | } | ||
| 317 | return(out); | ||
| 318 | } | ||
| 319 | |||
| 320 | static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert) | ||
| 321 | { | ||
| 322 | int ret; | ||
| 323 | ret = X509_NAME_cmp(ri->issuer_and_serial->issuer, | ||
| 324 | pcert->cert_info->issuer); | ||
| 325 | if (ret) | ||
| 326 | return ret; | ||
| 327 | return M_ASN1_INTEGER_cmp(pcert->cert_info->serialNumber, | ||
| 328 | ri->issuer_and_serial->serial); | ||
| 329 | } | ||
| 330 | |||
| 331 | /* int */ | ||
| 332 | BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) | ||
| 333 | { | ||
| 334 | int i,j; | ||
| 335 | BIO *out=NULL,*btmp=NULL,*etmp=NULL,*bio=NULL; | ||
| 336 | unsigned char *tmp=NULL; | ||
| 337 | X509_ALGOR *xa; | ||
| 338 | ASN1_OCTET_STRING *data_body=NULL; | ||
| 339 | const EVP_MD *evp_md; | ||
| 340 | const EVP_CIPHER *evp_cipher=NULL; | ||
| 341 | EVP_CIPHER_CTX *evp_ctx=NULL; | ||
| 342 | X509_ALGOR *enc_alg=NULL; | ||
| 343 | STACK_OF(X509_ALGOR) *md_sk=NULL; | ||
| 344 | STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; | ||
| 345 | X509_ALGOR *xalg=NULL; | ||
| 346 | PKCS7_RECIP_INFO *ri=NULL; | ||
| 347 | |||
| 348 | i=OBJ_obj2nid(p7->type); | ||
| 349 | p7->state=PKCS7_S_HEADER; | ||
| 350 | |||
| 351 | switch (i) | ||
| 352 | { | ||
| 353 | case NID_pkcs7_signed: | ||
| 354 | data_body=PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 355 | md_sk=p7->d.sign->md_algs; | ||
| 356 | break; | ||
| 357 | case NID_pkcs7_signedAndEnveloped: | ||
| 358 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 359 | md_sk=p7->d.signed_and_enveloped->md_algs; | ||
| 360 | data_body=p7->d.signed_and_enveloped->enc_data->enc_data; | ||
| 361 | enc_alg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 362 | evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm); | ||
| 363 | if (evp_cipher == NULL) | ||
| 364 | { | ||
| 365 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 366 | goto err; | ||
| 367 | } | ||
| 368 | xalg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 369 | break; | ||
| 370 | case NID_pkcs7_enveloped: | ||
| 371 | rsk=p7->d.enveloped->recipientinfo; | ||
| 372 | enc_alg=p7->d.enveloped->enc_data->algorithm; | ||
| 373 | data_body=p7->d.enveloped->enc_data->enc_data; | ||
| 374 | evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm); | ||
| 375 | if (evp_cipher == NULL) | ||
| 376 | { | ||
| 377 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 378 | goto err; | ||
| 379 | } | ||
| 380 | xalg=p7->d.enveloped->enc_data->algorithm; | ||
| 381 | break; | ||
| 382 | default: | ||
| 383 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 384 | goto err; | ||
| 385 | } | ||
| 386 | |||
| 387 | /* We will be checking the signature */ | ||
| 388 | if (md_sk != NULL) | ||
| 389 | { | ||
| 390 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 391 | { | ||
| 392 | xa=sk_X509_ALGOR_value(md_sk,i); | ||
| 393 | if ((btmp=BIO_new(BIO_f_md())) == NULL) | ||
| 394 | { | ||
| 395 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); | ||
| 396 | goto err; | ||
| 397 | } | ||
| 398 | |||
| 399 | j=OBJ_obj2nid(xa->algorithm); | ||
| 400 | evp_md=EVP_get_digestbynid(j); | ||
| 401 | if (evp_md == NULL) | ||
| 402 | { | ||
| 403 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNKNOWN_DIGEST_TYPE); | ||
| 404 | goto err; | ||
| 405 | } | ||
| 406 | |||
| 407 | BIO_set_md(btmp,evp_md); | ||
| 408 | if (out == NULL) | ||
| 409 | out=btmp; | ||
| 410 | else | ||
| 411 | BIO_push(out,btmp); | ||
| 412 | btmp=NULL; | ||
| 413 | } | ||
| 414 | } | ||
| 415 | |||
| 416 | if (evp_cipher != NULL) | ||
| 417 | { | ||
| 418 | #if 0 | ||
| 419 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| 420 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 421 | unsigned char *p; | ||
| 422 | int keylen,ivlen; | ||
| 423 | int max; | ||
| 424 | X509_OBJECT ret; | ||
| 425 | #endif | ||
| 426 | int jj; | ||
| 427 | |||
| 428 | if ((etmp=BIO_new(BIO_f_cipher())) == NULL) | ||
| 429 | { | ||
| 430 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); | ||
| 431 | goto err; | ||
| 432 | } | ||
| 433 | |||
| 434 | /* It was encrypted, we need to decrypt the secret key | ||
| 435 | * with the private key */ | ||
| 436 | |||
| 437 | /* Find the recipientInfo which matches the passed certificate | ||
| 438 | * (if any) | ||
| 439 | */ | ||
| 440 | |||
| 441 | if (pcert) { | ||
| 442 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) { | ||
| 443 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 444 | if (!pkcs7_cmp_ri(ri, pcert)) | ||
| 445 | break; | ||
| 446 | ri=NULL; | ||
| 447 | } | ||
| 448 | if (ri == NULL) { | ||
| 449 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 450 | PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); | ||
| 451 | goto err; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | jj=EVP_PKEY_size(pkey); | ||
| 456 | tmp=(unsigned char *)OPENSSL_malloc(jj+10); | ||
| 457 | if (tmp == NULL) | ||
| 458 | { | ||
| 459 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_MALLOC_FAILURE); | ||
| 460 | goto err; | ||
| 461 | } | ||
| 462 | |||
| 463 | /* If we haven't got a certificate try each ri in turn */ | ||
| 464 | |||
| 465 | if (pcert == NULL) | ||
| 466 | { | ||
| 467 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 468 | { | ||
| 469 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 470 | jj=EVP_PKEY_decrypt(tmp, | ||
| 471 | M_ASN1_STRING_data(ri->enc_key), | ||
| 472 | M_ASN1_STRING_length(ri->enc_key), | ||
| 473 | pkey); | ||
| 474 | if (jj > 0) | ||
| 475 | break; | ||
| 476 | ERR_clear_error(); | ||
| 477 | ri = NULL; | ||
| 478 | } | ||
| 479 | if (ri == NULL) | ||
| 480 | { | ||
| 481 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 482 | PKCS7_R_NO_RECIPIENT_MATCHES_KEY); | ||
| 483 | goto err; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | else | ||
| 487 | { | ||
| 488 | jj=EVP_PKEY_decrypt(tmp, | ||
| 489 | M_ASN1_STRING_data(ri->enc_key), | ||
| 490 | M_ASN1_STRING_length(ri->enc_key), pkey); | ||
| 491 | if (jj <= 0) | ||
| 492 | { | ||
| 493 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 494 | ERR_R_EVP_LIB); | ||
| 495 | goto err; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | evp_ctx=NULL; | ||
| 500 | BIO_get_cipher_ctx(etmp,&evp_ctx); | ||
| 501 | if (EVP_CipherInit_ex(evp_ctx,evp_cipher,NULL,NULL,NULL,0) <= 0) | ||
| 502 | goto err; | ||
| 503 | if (EVP_CIPHER_asn1_to_param(evp_ctx,enc_alg->parameter) < 0) | ||
| 504 | goto err; | ||
| 505 | |||
| 506 | if (jj != EVP_CIPHER_CTX_key_length(evp_ctx)) { | ||
| 507 | /* Some S/MIME clients don't use the same key | ||
| 508 | * and effective key length. The key length is | ||
| 509 | * determined by the size of the decrypted RSA key. | ||
| 510 | */ | ||
| 511 | if(!EVP_CIPHER_CTX_set_key_length(evp_ctx, jj)) | ||
| 512 | { | ||
| 513 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 514 | PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH); | ||
| 515 | goto err; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | if (EVP_CipherInit_ex(evp_ctx,NULL,NULL,tmp,NULL,0) <= 0) | ||
| 519 | goto err; | ||
| 520 | |||
| 521 | OPENSSL_cleanse(tmp,jj); | ||
| 522 | |||
| 523 | if (out == NULL) | ||
| 524 | out=etmp; | ||
| 525 | else | ||
| 526 | BIO_push(out,etmp); | ||
| 527 | etmp=NULL; | ||
| 528 | } | ||
| 529 | |||
| 530 | #if 1 | ||
| 531 | if (PKCS7_is_detached(p7) || (in_bio != NULL)) | ||
| 532 | { | ||
| 533 | bio=in_bio; | ||
| 534 | } | ||
| 535 | else | ||
| 536 | { | ||
| 537 | #if 0 | ||
| 538 | bio=BIO_new(BIO_s_mem()); | ||
| 539 | /* We need to set this so that when we have read all | ||
| 540 | * the data, the encrypt BIO, if present, will read | ||
| 541 | * EOF and encode the last few bytes */ | ||
| 542 | BIO_set_mem_eof_return(bio,0); | ||
| 543 | |||
| 544 | if (data_body->length > 0) | ||
| 545 | BIO_write(bio,(char *)data_body->data,data_body->length); | ||
| 546 | #else | ||
| 547 | if (data_body->length > 0) | ||
| 548 | bio = BIO_new_mem_buf(data_body->data,data_body->length); | ||
| 549 | else { | ||
| 550 | bio=BIO_new(BIO_s_mem()); | ||
| 551 | BIO_set_mem_eof_return(bio,0); | ||
| 552 | } | ||
| 553 | if (bio == NULL) | ||
| 554 | goto err; | ||
| 555 | #endif | ||
| 556 | } | ||
| 557 | BIO_push(out,bio); | ||
| 558 | bio=NULL; | ||
| 559 | #endif | ||
| 560 | if (0) | ||
| 561 | { | ||
| 562 | err: | ||
| 563 | if (out != NULL) BIO_free_all(out); | ||
| 564 | if (btmp != NULL) BIO_free_all(btmp); | ||
| 565 | if (etmp != NULL) BIO_free_all(etmp); | ||
| 566 | if (bio != NULL) BIO_free_all(bio); | ||
| 567 | out=NULL; | ||
| 568 | } | ||
| 569 | if (tmp != NULL) | ||
| 570 | OPENSSL_free(tmp); | ||
| 571 | return(out); | ||
| 572 | } | ||
| 573 | |||
| 574 | static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid) | ||
| 575 | { | ||
| 576 | for (;;) | ||
| 577 | { | ||
| 578 | bio=BIO_find_type(bio,BIO_TYPE_MD); | ||
| 579 | if (bio == NULL) | ||
| 580 | { | ||
| 581 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 582 | return NULL; | ||
| 583 | } | ||
| 584 | BIO_get_md_ctx(bio,pmd); | ||
| 585 | if (*pmd == NULL) | ||
| 586 | { | ||
| 587 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,ERR_R_INTERNAL_ERROR); | ||
| 588 | return NULL; | ||
| 589 | } | ||
| 590 | if (EVP_MD_CTX_type(*pmd) == nid) | ||
| 591 | return bio; | ||
| 592 | bio=BIO_next(bio); | ||
| 593 | } | ||
| 594 | return NULL; | ||
| 595 | } | ||
| 596 | |||
| 597 | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio) | ||
| 598 | { | ||
| 599 | int ret=0; | ||
| 600 | int i,j; | ||
| 601 | BIO *btmp; | ||
| 602 | BUF_MEM *buf_mem=NULL; | ||
| 603 | BUF_MEM *buf=NULL; | ||
| 604 | PKCS7_SIGNER_INFO *si; | ||
| 605 | EVP_MD_CTX *mdc,ctx_tmp; | ||
| 606 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 607 | STACK_OF(PKCS7_SIGNER_INFO) *si_sk=NULL; | ||
| 608 | ASN1_OCTET_STRING *os=NULL; | ||
| 609 | |||
| 610 | EVP_MD_CTX_init(&ctx_tmp); | ||
| 611 | i=OBJ_obj2nid(p7->type); | ||
| 612 | p7->state=PKCS7_S_HEADER; | ||
| 613 | |||
| 614 | switch (i) | ||
| 615 | { | ||
| 616 | case NID_pkcs7_signedAndEnveloped: | ||
| 617 | /* XXXXXXXXXXXXXXXX */ | ||
| 618 | si_sk=p7->d.signed_and_enveloped->signer_info; | ||
| 619 | if (!(os=M_ASN1_OCTET_STRING_new())) | ||
| 620 | { | ||
| 621 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_MALLOC_FAILURE); | ||
| 622 | goto err; | ||
| 623 | } | ||
| 624 | p7->d.signed_and_enveloped->enc_data->enc_data=os; | ||
| 625 | break; | ||
| 626 | case NID_pkcs7_enveloped: | ||
| 627 | /* XXXXXXXXXXXXXXXX */ | ||
| 628 | if (!(os=M_ASN1_OCTET_STRING_new())) | ||
| 629 | { | ||
| 630 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_MALLOC_FAILURE); | ||
| 631 | goto err; | ||
| 632 | } | ||
| 633 | p7->d.enveloped->enc_data->enc_data=os; | ||
| 634 | break; | ||
| 635 | case NID_pkcs7_signed: | ||
| 636 | si_sk=p7->d.sign->signer_info; | ||
| 637 | os=PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 638 | /* If detached data then the content is excluded */ | ||
| 639 | if(PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { | ||
| 640 | M_ASN1_OCTET_STRING_free(os); | ||
| 641 | p7->d.sign->contents->d.data = NULL; | ||
| 642 | } | ||
| 643 | break; | ||
| 644 | |||
| 645 | case NID_pkcs7_digest: | ||
| 646 | os=PKCS7_get_octet_string(p7->d.digest->contents); | ||
| 647 | /* If detached data then the content is excluded */ | ||
| 648 | if(PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) | ||
| 649 | { | ||
| 650 | M_ASN1_OCTET_STRING_free(os); | ||
| 651 | p7->d.digest->contents->d.data = NULL; | ||
| 652 | } | ||
| 653 | break; | ||
| 654 | |||
| 655 | } | ||
| 656 | |||
| 657 | if (si_sk != NULL) | ||
| 658 | { | ||
| 659 | if ((buf=BUF_MEM_new()) == NULL) | ||
| 660 | { | ||
| 661 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_BIO_LIB); | ||
| 662 | goto err; | ||
| 663 | } | ||
| 664 | for (i=0; i<sk_PKCS7_SIGNER_INFO_num(si_sk); i++) | ||
| 665 | { | ||
| 666 | si=sk_PKCS7_SIGNER_INFO_value(si_sk,i); | ||
| 667 | if (si->pkey == NULL) continue; | ||
| 668 | |||
| 669 | j=OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 670 | |||
| 671 | btmp=bio; | ||
| 672 | |||
| 673 | btmp = PKCS7_find_digest(&mdc, btmp, j); | ||
| 674 | |||
| 675 | if (btmp == NULL) | ||
| 676 | goto err; | ||
| 677 | |||
| 678 | /* We now have the EVP_MD_CTX, lets do the | ||
| 679 | * signing. */ | ||
| 680 | EVP_MD_CTX_copy_ex(&ctx_tmp,mdc); | ||
| 681 | if (!BUF_MEM_grow_clean(buf,EVP_PKEY_size(si->pkey))) | ||
| 682 | { | ||
| 683 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_BIO_LIB); | ||
| 684 | goto err; | ||
| 685 | } | ||
| 686 | |||
| 687 | sk=si->auth_attr; | ||
| 688 | |||
| 689 | /* If there are attributes, we add the digest | ||
| 690 | * attribute and only sign the attributes */ | ||
| 691 | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) | ||
| 692 | { | ||
| 693 | unsigned char md_data[EVP_MAX_MD_SIZE], *abuf=NULL; | ||
| 694 | unsigned int md_len, alen; | ||
| 695 | ASN1_OCTET_STRING *digest; | ||
| 696 | ASN1_UTCTIME *sign_time; | ||
| 697 | const EVP_MD *md_tmp; | ||
| 698 | |||
| 699 | /* Add signing time if not already present */ | ||
| 700 | if (!PKCS7_get_signed_attribute(si, | ||
| 701 | NID_pkcs9_signingTime)) | ||
| 702 | { | ||
| 703 | if (!(sign_time=X509_gmtime_adj(NULL,0))) | ||
| 704 | { | ||
| 705 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 706 | ERR_R_MALLOC_FAILURE); | ||
| 707 | goto err; | ||
| 708 | } | ||
| 709 | if (!PKCS7_add_signed_attribute(si, | ||
| 710 | NID_pkcs9_signingTime, | ||
| 711 | V_ASN1_UTCTIME,sign_time)) | ||
| 712 | { | ||
| 713 | M_ASN1_UTCTIME_free(sign_time); | ||
| 714 | goto err; | ||
| 715 | } | ||
| 716 | } | ||
| 717 | |||
| 718 | /* Add digest */ | ||
| 719 | md_tmp=EVP_MD_CTX_md(&ctx_tmp); | ||
| 720 | EVP_DigestFinal_ex(&ctx_tmp,md_data,&md_len); | ||
| 721 | if (!(digest=M_ASN1_OCTET_STRING_new())) | ||
| 722 | { | ||
| 723 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 724 | ERR_R_MALLOC_FAILURE); | ||
| 725 | goto err; | ||
| 726 | } | ||
| 727 | if (!M_ASN1_OCTET_STRING_set(digest,md_data, | ||
| 728 | md_len)) | ||
| 729 | { | ||
| 730 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 731 | ERR_R_MALLOC_FAILURE); | ||
| 732 | M_ASN1_OCTET_STRING_free(digest); | ||
| 733 | goto err; | ||
| 734 | } | ||
| 735 | if (!PKCS7_add_signed_attribute(si, | ||
| 736 | NID_pkcs9_messageDigest, | ||
| 737 | V_ASN1_OCTET_STRING,digest)) | ||
| 738 | { | ||
| 739 | M_ASN1_OCTET_STRING_free(digest); | ||
| 740 | goto err; | ||
| 741 | } | ||
| 742 | |||
| 743 | /* Now sign the attributes */ | ||
| 744 | EVP_SignInit_ex(&ctx_tmp,md_tmp,NULL); | ||
| 745 | alen = ASN1_item_i2d((ASN1_VALUE *)sk,&abuf, | ||
| 746 | ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); | ||
| 747 | if(!abuf) goto err; | ||
| 748 | EVP_SignUpdate(&ctx_tmp,abuf,alen); | ||
| 749 | OPENSSL_free(abuf); | ||
| 750 | } | ||
| 751 | |||
| 752 | #ifndef OPENSSL_NO_DSA | ||
| 753 | if (si->pkey->type == EVP_PKEY_DSA) | ||
| 754 | ctx_tmp.digest=EVP_dss1(); | ||
| 755 | #endif | ||
| 756 | #ifndef OPENSSL_NO_ECDSA | ||
| 757 | if (si->pkey->type == EVP_PKEY_EC) | ||
| 758 | ctx_tmp.digest=EVP_ecdsa(); | ||
| 759 | #endif | ||
| 760 | |||
| 761 | if (!EVP_SignFinal(&ctx_tmp,(unsigned char *)buf->data, | ||
| 762 | (unsigned int *)&buf->length,si->pkey)) | ||
| 763 | { | ||
| 764 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_EVP_LIB); | ||
| 765 | goto err; | ||
| 766 | } | ||
| 767 | if (!ASN1_STRING_set(si->enc_digest, | ||
| 768 | (unsigned char *)buf->data,buf->length)) | ||
| 769 | { | ||
| 770 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_ASN1_LIB); | ||
| 771 | goto err; | ||
| 772 | } | ||
| 773 | } | ||
| 774 | } | ||
| 775 | else if (i == NID_pkcs7_digest) | ||
| 776 | { | ||
| 777 | unsigned char md_data[EVP_MAX_MD_SIZE]; | ||
| 778 | unsigned int md_len; | ||
| 779 | if (!PKCS7_find_digest(&mdc, bio, | ||
| 780 | OBJ_obj2nid(p7->d.digest->md->algorithm))) | ||
| 781 | goto err; | ||
| 782 | EVP_DigestFinal_ex(mdc,md_data,&md_len); | ||
| 783 | M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len); | ||
| 784 | } | ||
| 785 | |||
| 786 | if (!PKCS7_is_detached(p7)) | ||
| 787 | { | ||
| 788 | btmp=BIO_find_type(bio,BIO_TYPE_MEM); | ||
| 789 | if (btmp == NULL) | ||
| 790 | { | ||
| 791 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MEM_BIO); | ||
| 792 | goto err; | ||
| 793 | } | ||
| 794 | BIO_get_mem_ptr(btmp,&buf_mem); | ||
| 795 | /* Mark the BIO read only then we can use its copy of the data | ||
| 796 | * instead of making an extra copy. | ||
| 797 | */ | ||
| 798 | BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY); | ||
| 799 | BIO_set_mem_eof_return(btmp, 0); | ||
| 800 | os->data = (unsigned char *)buf_mem->data; | ||
| 801 | os->length = buf_mem->length; | ||
| 802 | #if 0 | ||
| 803 | M_ASN1_OCTET_STRING_set(os, | ||
| 804 | (unsigned char *)buf_mem->data,buf_mem->length); | ||
| 805 | #endif | ||
| 806 | } | ||
| 807 | ret=1; | ||
| 808 | err: | ||
| 809 | EVP_MD_CTX_cleanup(&ctx_tmp); | ||
| 810 | if (buf != NULL) BUF_MEM_free(buf); | ||
| 811 | return(ret); | ||
| 812 | } | ||
| 813 | |||
| 814 | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio, | ||
| 815 | PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 816 | { | ||
| 817 | PKCS7_ISSUER_AND_SERIAL *ias; | ||
| 818 | int ret=0,i; | ||
| 819 | STACK_OF(X509) *cert; | ||
| 820 | X509 *x509; | ||
| 821 | |||
| 822 | if (PKCS7_type_is_signed(p7)) | ||
| 823 | { | ||
| 824 | cert=p7->d.sign->cert; | ||
| 825 | } | ||
| 826 | else if (PKCS7_type_is_signedAndEnveloped(p7)) | ||
| 827 | { | ||
| 828 | cert=p7->d.signed_and_enveloped->cert; | ||
| 829 | } | ||
| 830 | else | ||
| 831 | { | ||
| 832 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 833 | goto err; | ||
| 834 | } | ||
| 835 | /* XXXXXXXXXXXXXXXXXXXXXXX */ | ||
| 836 | ias=si->issuer_and_serial; | ||
| 837 | |||
| 838 | x509=X509_find_by_issuer_and_serial(cert,ias->issuer,ias->serial); | ||
| 839 | |||
| 840 | /* were we able to find the cert in passed to us */ | ||
| 841 | if (x509 == NULL) | ||
| 842 | { | ||
| 843 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE); | ||
| 844 | goto err; | ||
| 845 | } | ||
| 846 | |||
| 847 | /* Lets verify */ | ||
| 848 | if(!X509_STORE_CTX_init(ctx,cert_store,x509,cert)) | ||
| 849 | { | ||
| 850 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); | ||
| 851 | goto err; | ||
| 852 | } | ||
| 853 | X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN); | ||
| 854 | i=X509_verify_cert(ctx); | ||
| 855 | if (i <= 0) | ||
| 856 | { | ||
| 857 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); | ||
| 858 | X509_STORE_CTX_cleanup(ctx); | ||
| 859 | goto err; | ||
| 860 | } | ||
| 861 | X509_STORE_CTX_cleanup(ctx); | ||
| 862 | |||
| 863 | return PKCS7_signatureVerify(bio, p7, si, x509); | ||
| 864 | err: | ||
| 865 | return ret; | ||
| 866 | } | ||
| 867 | |||
| 868 | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, | ||
| 869 | X509 *x509) | ||
| 870 | { | ||
| 871 | ASN1_OCTET_STRING *os; | ||
| 872 | EVP_MD_CTX mdc_tmp,*mdc; | ||
| 873 | int ret=0,i; | ||
| 874 | int md_type; | ||
| 875 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 876 | BIO *btmp; | ||
| 877 | EVP_PKEY *pkey; | ||
| 878 | |||
| 879 | EVP_MD_CTX_init(&mdc_tmp); | ||
| 880 | |||
| 881 | if (!PKCS7_type_is_signed(p7) && | ||
| 882 | !PKCS7_type_is_signedAndEnveloped(p7)) { | ||
| 883 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 884 | PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 885 | goto err; | ||
| 886 | } | ||
| 887 | |||
| 888 | md_type=OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 889 | |||
| 890 | btmp=bio; | ||
| 891 | for (;;) | ||
| 892 | { | ||
| 893 | if ((btmp == NULL) || | ||
| 894 | ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL)) | ||
| 895 | { | ||
| 896 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 897 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 898 | goto err; | ||
| 899 | } | ||
| 900 | BIO_get_md_ctx(btmp,&mdc); | ||
| 901 | if (mdc == NULL) | ||
| 902 | { | ||
| 903 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 904 | ERR_R_INTERNAL_ERROR); | ||
| 905 | goto err; | ||
| 906 | } | ||
| 907 | if (EVP_MD_CTX_type(mdc) == md_type) | ||
| 908 | break; | ||
| 909 | /* Workaround for some broken clients that put the signature | ||
| 910 | * OID instead of the digest OID in digest_alg->algorithm | ||
| 911 | */ | ||
| 912 | if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type) | ||
| 913 | break; | ||
| 914 | btmp=BIO_next(btmp); | ||
| 915 | } | ||
| 916 | |||
| 917 | /* mdc is the digest ctx that we want, unless there are attributes, | ||
| 918 | * in which case the digest is the signed attributes */ | ||
| 919 | EVP_MD_CTX_copy_ex(&mdc_tmp,mdc); | ||
| 920 | |||
| 921 | sk=si->auth_attr; | ||
| 922 | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) | ||
| 923 | { | ||
| 924 | unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL; | ||
| 925 | unsigned int md_len, alen; | ||
| 926 | ASN1_OCTET_STRING *message_digest; | ||
| 927 | |||
| 928 | EVP_DigestFinal_ex(&mdc_tmp,md_dat,&md_len); | ||
| 929 | message_digest=PKCS7_digest_from_attributes(sk); | ||
| 930 | if (!message_digest) | ||
| 931 | { | ||
| 932 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 933 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 934 | goto err; | ||
| 935 | } | ||
| 936 | if ((message_digest->length != (int)md_len) || | ||
| 937 | (memcmp(message_digest->data,md_dat,md_len))) | ||
| 938 | { | ||
| 939 | #if 0 | ||
| 940 | { | ||
| 941 | int ii; | ||
| 942 | for (ii=0; ii<message_digest->length; ii++) | ||
| 943 | printf("%02X",message_digest->data[ii]); printf(" sent\n"); | ||
| 944 | for (ii=0; ii<md_len; ii++) printf("%02X",md_dat[ii]); printf(" calc\n"); | ||
| 945 | } | ||
| 946 | #endif | ||
| 947 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 948 | PKCS7_R_DIGEST_FAILURE); | ||
| 949 | ret= -1; | ||
| 950 | goto err; | ||
| 951 | } | ||
| 952 | |||
| 953 | EVP_VerifyInit_ex(&mdc_tmp,EVP_get_digestbynid(md_type), NULL); | ||
| 954 | |||
| 955 | alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, | ||
| 956 | ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); | ||
| 957 | EVP_VerifyUpdate(&mdc_tmp, abuf, alen); | ||
| 958 | |||
| 959 | OPENSSL_free(abuf); | ||
| 960 | } | ||
| 961 | |||
| 962 | os=si->enc_digest; | ||
| 963 | pkey = X509_get_pubkey(x509); | ||
| 964 | if (!pkey) | ||
| 965 | { | ||
| 966 | ret = -1; | ||
| 967 | goto err; | ||
| 968 | } | ||
| 969 | #ifndef OPENSSL_NO_DSA | ||
| 970 | if(pkey->type == EVP_PKEY_DSA) mdc_tmp.digest=EVP_dss1(); | ||
| 971 | #endif | ||
| 972 | #ifndef OPENSSL_NO_ECDSA | ||
| 973 | if (pkey->type == EVP_PKEY_EC) mdc_tmp.digest=EVP_ecdsa(); | ||
| 974 | #endif | ||
| 975 | |||
| 976 | i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length, pkey); | ||
| 977 | EVP_PKEY_free(pkey); | ||
| 978 | if (i <= 0) | ||
| 979 | { | ||
| 980 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 981 | PKCS7_R_SIGNATURE_FAILURE); | ||
| 982 | ret= -1; | ||
| 983 | goto err; | ||
| 984 | } | ||
| 985 | else | ||
| 986 | ret=1; | ||
| 987 | err: | ||
| 988 | EVP_MD_CTX_cleanup(&mdc_tmp); | ||
| 989 | return(ret); | ||
| 990 | } | ||
| 991 | |||
| 992 | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) | ||
| 993 | { | ||
| 994 | STACK_OF(PKCS7_RECIP_INFO) *rsk; | ||
| 995 | PKCS7_RECIP_INFO *ri; | ||
| 996 | int i; | ||
| 997 | |||
| 998 | i=OBJ_obj2nid(p7->type); | ||
| 999 | if (i != NID_pkcs7_signedAndEnveloped) | ||
| 1000 | return NULL; | ||
| 1001 | if (p7->d.signed_and_enveloped == NULL) | ||
| 1002 | return NULL; | ||
| 1003 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 1004 | if (rsk == NULL) | ||
| 1005 | return NULL; | ||
| 1006 | ri=sk_PKCS7_RECIP_INFO_value(rsk,0); | ||
| 1007 | if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL); | ||
| 1008 | ri=sk_PKCS7_RECIP_INFO_value(rsk,idx); | ||
| 1009 | return(ri->issuer_and_serial); | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 1013 | { | ||
| 1014 | return(get_attribute(si->auth_attr,nid)); | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 1018 | { | ||
| 1019 | return(get_attribute(si->unauth_attr,nid)); | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid) | ||
| 1023 | { | ||
| 1024 | int i; | ||
| 1025 | X509_ATTRIBUTE *xa; | ||
| 1026 | ASN1_OBJECT *o; | ||
| 1027 | |||
| 1028 | o=OBJ_nid2obj(nid); | ||
| 1029 | if (!o || !sk) return(NULL); | ||
| 1030 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 1031 | { | ||
| 1032 | xa=sk_X509_ATTRIBUTE_value(sk,i); | ||
| 1033 | if (OBJ_cmp(xa->object,o) == 0) | ||
| 1034 | { | ||
| 1035 | if (!xa->single && sk_ASN1_TYPE_num(xa->value.set)) | ||
| 1036 | return(sk_ASN1_TYPE_value(xa->value.set,0)); | ||
| 1037 | else | ||
| 1038 | return(NULL); | ||
| 1039 | } | ||
| 1040 | } | ||
| 1041 | return(NULL); | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1045 | { | ||
| 1046 | ASN1_TYPE *astype; | ||
| 1047 | if(!(astype = get_attribute(sk, NID_pkcs9_messageDigest))) return NULL; | ||
| 1048 | return astype->value.octet_string; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, | ||
| 1052 | STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1053 | { | ||
| 1054 | int i; | ||
| 1055 | |||
| 1056 | if (p7si->auth_attr != NULL) | ||
| 1057 | sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr,X509_ATTRIBUTE_free); | ||
| 1058 | p7si->auth_attr=sk_X509_ATTRIBUTE_dup(sk); | ||
| 1059 | if (p7si->auth_attr == NULL) | ||
| 1060 | return 0; | ||
| 1061 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 1062 | { | ||
| 1063 | if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr,i, | ||
| 1064 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) | ||
| 1065 | == NULL) | ||
| 1066 | return(0); | ||
| 1067 | } | ||
| 1068 | return(1); | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1072 | { | ||
| 1073 | int i; | ||
| 1074 | |||
| 1075 | if (p7si->unauth_attr != NULL) | ||
| 1076 | sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, | ||
| 1077 | X509_ATTRIBUTE_free); | ||
| 1078 | p7si->unauth_attr=sk_X509_ATTRIBUTE_dup(sk); | ||
| 1079 | if (p7si->unauth_attr == NULL) | ||
| 1080 | return 0; | ||
| 1081 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 1082 | { | ||
| 1083 | if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr,i, | ||
| 1084 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) | ||
| 1085 | == NULL) | ||
| 1086 | return(0); | ||
| 1087 | } | ||
| 1088 | return(1); | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 1092 | void *value) | ||
| 1093 | { | ||
| 1094 | return(add_attribute(&(p7si->auth_attr),nid,atrtype,value)); | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 1098 | void *value) | ||
| 1099 | { | ||
| 1100 | return(add_attribute(&(p7si->unauth_attr),nid,atrtype,value)); | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, | ||
| 1104 | void *value) | ||
| 1105 | { | ||
| 1106 | X509_ATTRIBUTE *attr=NULL; | ||
| 1107 | |||
| 1108 | if (*sk == NULL) | ||
| 1109 | { | ||
| 1110 | if (!(*sk = sk_X509_ATTRIBUTE_new_null())) | ||
| 1111 | return 0; | ||
| 1112 | new_attrib: | ||
| 1113 | if (!(attr=X509_ATTRIBUTE_create(nid,atrtype,value))) | ||
| 1114 | return 0; | ||
| 1115 | if (!sk_X509_ATTRIBUTE_push(*sk,attr)) | ||
| 1116 | { | ||
| 1117 | X509_ATTRIBUTE_free(attr); | ||
| 1118 | return 0; | ||
| 1119 | } | ||
| 1120 | } | ||
| 1121 | else | ||
| 1122 | { | ||
| 1123 | int i; | ||
| 1124 | |||
| 1125 | for (i=0; i<sk_X509_ATTRIBUTE_num(*sk); i++) | ||
| 1126 | { | ||
| 1127 | attr=sk_X509_ATTRIBUTE_value(*sk,i); | ||
| 1128 | if (OBJ_obj2nid(attr->object) == nid) | ||
| 1129 | { | ||
| 1130 | X509_ATTRIBUTE_free(attr); | ||
| 1131 | attr=X509_ATTRIBUTE_create(nid,atrtype,value); | ||
| 1132 | if (attr == NULL) | ||
| 1133 | return 0; | ||
| 1134 | if (!sk_X509_ATTRIBUTE_set(*sk,i,attr)) | ||
| 1135 | { | ||
| 1136 | X509_ATTRIBUTE_free(attr); | ||
| 1137 | return 0; | ||
| 1138 | } | ||
| 1139 | goto end; | ||
| 1140 | } | ||
| 1141 | } | ||
| 1142 | goto new_attrib; | ||
| 1143 | } | ||
| 1144 | end: | ||
| 1145 | return(1); | ||
| 1146 | } | ||
| 1147 | |||
diff --git a/src/lib/libcrypto/pkcs7/pk7_lib.c b/src/lib/libcrypto/pkcs7/pk7_lib.c new file mode 100644 index 0000000000..f2490941a3 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_lib.c | |||
| @@ -0,0 +1,589 @@ | |||
| 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 | if (ret && PKCS7_type_is_data(p7->d.sign->contents)) | ||
| 78 | { | ||
| 79 | ASN1_OCTET_STRING *os; | ||
| 80 | os=p7->d.sign->contents->d.data; | ||
| 81 | ASN1_OCTET_STRING_free(os); | ||
| 82 | p7->d.sign->contents->d.data = NULL; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); | ||
| 88 | ret=0; | ||
| 89 | } | ||
| 90 | break; | ||
| 91 | case PKCS7_OP_GET_DETACHED_SIGNATURE: | ||
| 92 | if (nid == NID_pkcs7_signed) | ||
| 93 | { | ||
| 94 | if(!p7->d.sign || !p7->d.sign->contents->d.ptr) | ||
| 95 | ret = 1; | ||
| 96 | else ret = 0; | ||
| 97 | |||
| 98 | p7->detached = ret; | ||
| 99 | } | ||
| 100 | else | ||
| 101 | { | ||
| 102 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); | ||
| 103 | ret=0; | ||
| 104 | } | ||
| 105 | |||
| 106 | break; | ||
| 107 | default: | ||
| 108 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_UNKNOWN_OPERATION); | ||
| 109 | ret=0; | ||
| 110 | } | ||
| 111 | return(ret); | ||
| 112 | } | ||
| 113 | |||
| 114 | int PKCS7_content_new(PKCS7 *p7, int type) | ||
| 115 | { | ||
| 116 | PKCS7 *ret=NULL; | ||
| 117 | |||
| 118 | if ((ret=PKCS7_new()) == NULL) goto err; | ||
| 119 | if (!PKCS7_set_type(ret,type)) goto err; | ||
| 120 | if (!PKCS7_set_content(p7,ret)) goto err; | ||
| 121 | |||
| 122 | return(1); | ||
| 123 | err: | ||
| 124 | if (ret != NULL) PKCS7_free(ret); | ||
| 125 | return(0); | ||
| 126 | } | ||
| 127 | |||
| 128 | int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data) | ||
| 129 | { | ||
| 130 | int i; | ||
| 131 | |||
| 132 | i=OBJ_obj2nid(p7->type); | ||
| 133 | switch (i) | ||
| 134 | { | ||
| 135 | case NID_pkcs7_signed: | ||
| 136 | if (p7->d.sign->contents != NULL) | ||
| 137 | PKCS7_free(p7->d.sign->contents); | ||
| 138 | p7->d.sign->contents=p7_data; | ||
| 139 | break; | ||
| 140 | case NID_pkcs7_digest: | ||
| 141 | if (p7->d.digest->contents != NULL) | ||
| 142 | PKCS7_free(p7->d.digest->contents); | ||
| 143 | p7->d.digest->contents=p7_data; | ||
| 144 | break; | ||
| 145 | case NID_pkcs7_data: | ||
| 146 | case NID_pkcs7_enveloped: | ||
| 147 | case NID_pkcs7_signedAndEnveloped: | ||
| 148 | case NID_pkcs7_encrypted: | ||
| 149 | default: | ||
| 150 | PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 151 | goto err; | ||
| 152 | } | ||
| 153 | return(1); | ||
| 154 | err: | ||
| 155 | return(0); | ||
| 156 | } | ||
| 157 | |||
| 158 | int PKCS7_set_type(PKCS7 *p7, int type) | ||
| 159 | { | ||
| 160 | ASN1_OBJECT *obj; | ||
| 161 | |||
| 162 | /*PKCS7_content_free(p7);*/ | ||
| 163 | obj=OBJ_nid2obj(type); /* will not fail */ | ||
| 164 | |||
| 165 | switch (type) | ||
| 166 | { | ||
| 167 | case NID_pkcs7_signed: | ||
| 168 | p7->type=obj; | ||
| 169 | if ((p7->d.sign=PKCS7_SIGNED_new()) == NULL) | ||
| 170 | goto err; | ||
| 171 | if (!ASN1_INTEGER_set(p7->d.sign->version,1)) | ||
| 172 | { | ||
| 173 | PKCS7_SIGNED_free(p7->d.sign); | ||
| 174 | p7->d.sign=NULL; | ||
| 175 | goto err; | ||
| 176 | } | ||
| 177 | break; | ||
| 178 | case NID_pkcs7_data: | ||
| 179 | p7->type=obj; | ||
| 180 | if ((p7->d.data=M_ASN1_OCTET_STRING_new()) == NULL) | ||
| 181 | goto err; | ||
| 182 | break; | ||
| 183 | case NID_pkcs7_signedAndEnveloped: | ||
| 184 | p7->type=obj; | ||
| 185 | if ((p7->d.signed_and_enveloped=PKCS7_SIGN_ENVELOPE_new()) | ||
| 186 | == NULL) goto err; | ||
| 187 | ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1); | ||
| 188 | if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1)) | ||
| 189 | goto err; | ||
| 190 | p7->d.signed_and_enveloped->enc_data->content_type | ||
| 191 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 192 | break; | ||
| 193 | case NID_pkcs7_enveloped: | ||
| 194 | p7->type=obj; | ||
| 195 | if ((p7->d.enveloped=PKCS7_ENVELOPE_new()) | ||
| 196 | == NULL) goto err; | ||
| 197 | if (!ASN1_INTEGER_set(p7->d.enveloped->version,0)) | ||
| 198 | goto err; | ||
| 199 | p7->d.enveloped->enc_data->content_type | ||
| 200 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 201 | break; | ||
| 202 | case NID_pkcs7_encrypted: | ||
| 203 | p7->type=obj; | ||
| 204 | if ((p7->d.encrypted=PKCS7_ENCRYPT_new()) | ||
| 205 | == NULL) goto err; | ||
| 206 | if (!ASN1_INTEGER_set(p7->d.encrypted->version,0)) | ||
| 207 | goto err; | ||
| 208 | p7->d.encrypted->enc_data->content_type | ||
| 209 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 210 | break; | ||
| 211 | |||
| 212 | case NID_pkcs7_digest: | ||
| 213 | p7->type=obj; | ||
| 214 | if ((p7->d.digest=PKCS7_DIGEST_new()) | ||
| 215 | == NULL) goto err; | ||
| 216 | if (!ASN1_INTEGER_set(p7->d.digest->version,0)) | ||
| 217 | goto err; | ||
| 218 | break; | ||
| 219 | default: | ||
| 220 | PKCS7err(PKCS7_F_PKCS7_SET_TYPE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 221 | goto err; | ||
| 222 | } | ||
| 223 | return(1); | ||
| 224 | err: | ||
| 225 | return(0); | ||
| 226 | } | ||
| 227 | |||
| 228 | int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other) | ||
| 229 | { | ||
| 230 | p7->type = OBJ_nid2obj(type); | ||
| 231 | p7->d.other = other; | ||
| 232 | return 1; | ||
| 233 | } | ||
| 234 | |||
| 235 | int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi) | ||
| 236 | { | ||
| 237 | int i,j,nid; | ||
| 238 | X509_ALGOR *alg; | ||
| 239 | STACK_OF(PKCS7_SIGNER_INFO) *signer_sk; | ||
| 240 | STACK_OF(X509_ALGOR) *md_sk; | ||
| 241 | |||
| 242 | i=OBJ_obj2nid(p7->type); | ||
| 243 | switch (i) | ||
| 244 | { | ||
| 245 | case NID_pkcs7_signed: | ||
| 246 | signer_sk= p7->d.sign->signer_info; | ||
| 247 | md_sk= p7->d.sign->md_algs; | ||
| 248 | break; | ||
| 249 | case NID_pkcs7_signedAndEnveloped: | ||
| 250 | signer_sk= p7->d.signed_and_enveloped->signer_info; | ||
| 251 | md_sk= p7->d.signed_and_enveloped->md_algs; | ||
| 252 | break; | ||
| 253 | default: | ||
| 254 | PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 255 | return(0); | ||
| 256 | } | ||
| 257 | |||
| 258 | nid=OBJ_obj2nid(psi->digest_alg->algorithm); | ||
| 259 | |||
| 260 | /* If the digest is not currently listed, add it */ | ||
| 261 | j=0; | ||
| 262 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 263 | { | ||
| 264 | alg=sk_X509_ALGOR_value(md_sk,i); | ||
| 265 | if (OBJ_obj2nid(alg->algorithm) == nid) | ||
| 266 | { | ||
| 267 | j=1; | ||
| 268 | break; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | if (!j) /* we need to add another algorithm */ | ||
| 272 | { | ||
| 273 | if(!(alg=X509_ALGOR_new()) | ||
| 274 | || !(alg->parameter = ASN1_TYPE_new())) | ||
| 275 | { | ||
| 276 | X509_ALGOR_free(alg); | ||
| 277 | PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE); | ||
| 278 | return(0); | ||
| 279 | } | ||
| 280 | alg->algorithm=OBJ_nid2obj(nid); | ||
| 281 | alg->parameter->type = V_ASN1_NULL; | ||
| 282 | if (!sk_X509_ALGOR_push(md_sk,alg)) | ||
| 283 | { | ||
| 284 | X509_ALGOR_free(alg); | ||
| 285 | return 0; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | if (!sk_PKCS7_SIGNER_INFO_push(signer_sk,psi)) | ||
| 290 | return 0; | ||
| 291 | return(1); | ||
| 292 | } | ||
| 293 | |||
| 294 | int PKCS7_add_certificate(PKCS7 *p7, X509 *x509) | ||
| 295 | { | ||
| 296 | int i; | ||
| 297 | STACK_OF(X509) **sk; | ||
| 298 | |||
| 299 | i=OBJ_obj2nid(p7->type); | ||
| 300 | switch (i) | ||
| 301 | { | ||
| 302 | case NID_pkcs7_signed: | ||
| 303 | sk= &(p7->d.sign->cert); | ||
| 304 | break; | ||
| 305 | case NID_pkcs7_signedAndEnveloped: | ||
| 306 | sk= &(p7->d.signed_and_enveloped->cert); | ||
| 307 | break; | ||
| 308 | default: | ||
| 309 | PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 310 | return(0); | ||
| 311 | } | ||
| 312 | |||
| 313 | if (*sk == NULL) | ||
| 314 | *sk=sk_X509_new_null(); | ||
| 315 | if (*sk == NULL) | ||
| 316 | { | ||
| 317 | PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,ERR_R_MALLOC_FAILURE); | ||
| 318 | return 0; | ||
| 319 | } | ||
| 320 | CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); | ||
| 321 | if (!sk_X509_push(*sk,x509)) | ||
| 322 | { | ||
| 323 | X509_free(x509); | ||
| 324 | return 0; | ||
| 325 | } | ||
| 326 | return(1); | ||
| 327 | } | ||
| 328 | |||
| 329 | int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) | ||
| 330 | { | ||
| 331 | int i; | ||
| 332 | STACK_OF(X509_CRL) **sk; | ||
| 333 | |||
| 334 | i=OBJ_obj2nid(p7->type); | ||
| 335 | switch (i) | ||
| 336 | { | ||
| 337 | case NID_pkcs7_signed: | ||
| 338 | sk= &(p7->d.sign->crl); | ||
| 339 | break; | ||
| 340 | case NID_pkcs7_signedAndEnveloped: | ||
| 341 | sk= &(p7->d.signed_and_enveloped->crl); | ||
| 342 | break; | ||
| 343 | default: | ||
| 344 | PKCS7err(PKCS7_F_PKCS7_ADD_CRL,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 345 | return(0); | ||
| 346 | } | ||
| 347 | |||
| 348 | if (*sk == NULL) | ||
| 349 | *sk=sk_X509_CRL_new_null(); | ||
| 350 | if (*sk == NULL) | ||
| 351 | { | ||
| 352 | PKCS7err(PKCS7_F_PKCS7_ADD_CRL,ERR_R_MALLOC_FAILURE); | ||
| 353 | return 0; | ||
| 354 | } | ||
| 355 | |||
| 356 | CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL); | ||
| 357 | if (!sk_X509_CRL_push(*sk,crl)) | ||
| 358 | { | ||
| 359 | X509_CRL_free(crl); | ||
| 360 | return 0; | ||
| 361 | } | ||
| 362 | return(1); | ||
| 363 | } | ||
| 364 | |||
| 365 | int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, | ||
| 366 | const EVP_MD *dgst) | ||
| 367 | { | ||
| 368 | int nid; | ||
| 369 | char is_dsa; | ||
| 370 | |||
| 371 | if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC) | ||
| 372 | is_dsa = 1; | ||
| 373 | else | ||
| 374 | is_dsa = 0; | ||
| 375 | /* We now need to add another PKCS7_SIGNER_INFO entry */ | ||
| 376 | if (!ASN1_INTEGER_set(p7i->version,1)) | ||
| 377 | goto err; | ||
| 378 | if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, | ||
| 379 | X509_get_issuer_name(x509))) | ||
| 380 | goto err; | ||
| 381 | |||
| 382 | /* because ASN1_INTEGER_set is used to set a 'long' we will do | ||
| 383 | * things the ugly way. */ | ||
| 384 | M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); | ||
| 385 | if (!(p7i->issuer_and_serial->serial= | ||
| 386 | M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) | ||
| 387 | goto err; | ||
| 388 | |||
| 389 | /* lets keep the pkey around for a while */ | ||
| 390 | CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); | ||
| 391 | p7i->pkey=pkey; | ||
| 392 | |||
| 393 | /* Set the algorithms */ | ||
| 394 | if (is_dsa) p7i->digest_alg->algorithm=OBJ_nid2obj(NID_sha1); | ||
| 395 | else | ||
| 396 | p7i->digest_alg->algorithm=OBJ_nid2obj(EVP_MD_type(dgst)); | ||
| 397 | |||
| 398 | if (p7i->digest_alg->parameter != NULL) | ||
| 399 | ASN1_TYPE_free(p7i->digest_alg->parameter); | ||
| 400 | if ((p7i->digest_alg->parameter=ASN1_TYPE_new()) == NULL) | ||
| 401 | goto err; | ||
| 402 | p7i->digest_alg->parameter->type=V_ASN1_NULL; | ||
| 403 | |||
| 404 | if (p7i->digest_enc_alg->parameter != NULL) | ||
| 405 | ASN1_TYPE_free(p7i->digest_enc_alg->parameter); | ||
| 406 | nid = EVP_PKEY_type(pkey->type); | ||
| 407 | if (nid == EVP_PKEY_RSA) | ||
| 408 | { | ||
| 409 | p7i->digest_enc_alg->algorithm=OBJ_nid2obj(NID_rsaEncryption); | ||
| 410 | if (!(p7i->digest_enc_alg->parameter=ASN1_TYPE_new())) | ||
| 411 | goto err; | ||
| 412 | p7i->digest_enc_alg->parameter->type=V_ASN1_NULL; | ||
| 413 | } | ||
| 414 | else if (nid == EVP_PKEY_DSA) | ||
| 415 | { | ||
| 416 | #if 1 | ||
| 417 | /* use 'dsaEncryption' OID for compatibility with other software | ||
| 418 | * (PKCS #7 v1.5 does specify how to handle DSA) ... */ | ||
| 419 | p7i->digest_enc_alg->algorithm=OBJ_nid2obj(NID_dsa); | ||
| 420 | #else | ||
| 421 | /* ... although the 'dsaWithSHA1' OID (as required by RFC 2630 for CMS) | ||
| 422 | * would make more sense. */ | ||
| 423 | p7i->digest_enc_alg->algorithm=OBJ_nid2obj(NID_dsaWithSHA1); | ||
| 424 | #endif | ||
| 425 | p7i->digest_enc_alg->parameter = NULL; /* special case for DSA: omit 'parameter'! */ | ||
| 426 | } | ||
| 427 | else if (nid == EVP_PKEY_EC) | ||
| 428 | { | ||
| 429 | p7i->digest_enc_alg->algorithm=OBJ_nid2obj(NID_ecdsa_with_SHA1); | ||
| 430 | if (!(p7i->digest_enc_alg->parameter=ASN1_TYPE_new())) | ||
| 431 | goto err; | ||
| 432 | p7i->digest_enc_alg->parameter->type=V_ASN1_NULL; | ||
| 433 | } | ||
| 434 | else | ||
| 435 | return(0); | ||
| 436 | |||
| 437 | return(1); | ||
| 438 | err: | ||
| 439 | return(0); | ||
| 440 | } | ||
| 441 | |||
| 442 | PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey, | ||
| 443 | const EVP_MD *dgst) | ||
| 444 | { | ||
| 445 | PKCS7_SIGNER_INFO *si; | ||
| 446 | |||
| 447 | if ((si=PKCS7_SIGNER_INFO_new()) == NULL) goto err; | ||
| 448 | if (!PKCS7_SIGNER_INFO_set(si,x509,pkey,dgst)) goto err; | ||
| 449 | if (!PKCS7_add_signer(p7,si)) goto err; | ||
| 450 | return(si); | ||
| 451 | err: | ||
| 452 | PKCS7_SIGNER_INFO_free(si); | ||
| 453 | return(NULL); | ||
| 454 | } | ||
| 455 | |||
| 456 | int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md) | ||
| 457 | { | ||
| 458 | if (PKCS7_type_is_digest(p7)) | ||
| 459 | { | ||
| 460 | if(!(p7->d.digest->md->parameter = ASN1_TYPE_new())) | ||
| 461 | { | ||
| 462 | PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,ERR_R_MALLOC_FAILURE); | ||
| 463 | return 0; | ||
| 464 | } | ||
| 465 | p7->d.digest->md->parameter->type = V_ASN1_NULL; | ||
| 466 | p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md)); | ||
| 467 | return 1; | ||
| 468 | } | ||
| 469 | |||
| 470 | PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 471 | return 1; | ||
| 472 | } | ||
| 473 | |||
| 474 | STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7) | ||
| 475 | { | ||
| 476 | if (PKCS7_type_is_signed(p7)) | ||
| 477 | { | ||
| 478 | return(p7->d.sign->signer_info); | ||
| 479 | } | ||
| 480 | else if (PKCS7_type_is_signedAndEnveloped(p7)) | ||
| 481 | { | ||
| 482 | return(p7->d.signed_and_enveloped->signer_info); | ||
| 483 | } | ||
| 484 | else | ||
| 485 | return(NULL); | ||
| 486 | } | ||
| 487 | |||
| 488 | PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509) | ||
| 489 | { | ||
| 490 | PKCS7_RECIP_INFO *ri; | ||
| 491 | |||
| 492 | if ((ri=PKCS7_RECIP_INFO_new()) == NULL) goto err; | ||
| 493 | if (!PKCS7_RECIP_INFO_set(ri,x509)) goto err; | ||
| 494 | if (!PKCS7_add_recipient_info(p7,ri)) goto err; | ||
| 495 | return(ri); | ||
| 496 | err: | ||
| 497 | PKCS7_RECIP_INFO_free(ri); | ||
| 498 | return(NULL); | ||
| 499 | } | ||
| 500 | |||
| 501 | int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) | ||
| 502 | { | ||
| 503 | int i; | ||
| 504 | STACK_OF(PKCS7_RECIP_INFO) *sk; | ||
| 505 | |||
| 506 | i=OBJ_obj2nid(p7->type); | ||
| 507 | switch (i) | ||
| 508 | { | ||
| 509 | case NID_pkcs7_signedAndEnveloped: | ||
| 510 | sk= p7->d.signed_and_enveloped->recipientinfo; | ||
| 511 | break; | ||
| 512 | case NID_pkcs7_enveloped: | ||
| 513 | sk= p7->d.enveloped->recipientinfo; | ||
| 514 | break; | ||
| 515 | default: | ||
| 516 | PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 517 | return(0); | ||
| 518 | } | ||
| 519 | |||
| 520 | if (!sk_PKCS7_RECIP_INFO_push(sk,ri)) | ||
| 521 | return 0; | ||
| 522 | return(1); | ||
| 523 | } | ||
| 524 | |||
| 525 | int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509) | ||
| 526 | { | ||
| 527 | if (!ASN1_INTEGER_set(p7i->version,0)) | ||
| 528 | return 0; | ||
| 529 | if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, | ||
| 530 | X509_get_issuer_name(x509))) | ||
| 531 | return 0; | ||
| 532 | |||
| 533 | M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); | ||
| 534 | if (!(p7i->issuer_and_serial->serial= | ||
| 535 | M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) | ||
| 536 | return 0; | ||
| 537 | |||
| 538 | X509_ALGOR_free(p7i->key_enc_algor); | ||
| 539 | if (!(p7i->key_enc_algor= X509_ALGOR_dup(x509->cert_info->key->algor))) | ||
| 540 | return 0; | ||
| 541 | |||
| 542 | CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); | ||
| 543 | p7i->cert=x509; | ||
| 544 | |||
| 545 | return(1); | ||
| 546 | } | ||
| 547 | |||
| 548 | X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 549 | { | ||
| 550 | if (PKCS7_type_is_signed(p7)) | ||
| 551 | return(X509_find_by_issuer_and_serial(p7->d.sign->cert, | ||
| 552 | si->issuer_and_serial->issuer, | ||
| 553 | si->issuer_and_serial->serial)); | ||
| 554 | else | ||
| 555 | return(NULL); | ||
| 556 | } | ||
| 557 | |||
| 558 | int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher) | ||
| 559 | { | ||
| 560 | int i; | ||
| 561 | ASN1_OBJECT *objtmp; | ||
| 562 | PKCS7_ENC_CONTENT *ec; | ||
| 563 | |||
| 564 | i=OBJ_obj2nid(p7->type); | ||
| 565 | switch (i) | ||
| 566 | { | ||
| 567 | case NID_pkcs7_signedAndEnveloped: | ||
| 568 | ec=p7->d.signed_and_enveloped->enc_data; | ||
| 569 | break; | ||
| 570 | case NID_pkcs7_enveloped: | ||
| 571 | ec=p7->d.enveloped->enc_data; | ||
| 572 | break; | ||
| 573 | default: | ||
| 574 | PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 575 | return(0); | ||
| 576 | } | ||
| 577 | |||
| 578 | /* Check cipher OID exists and has data in it*/ | ||
| 579 | i = EVP_CIPHER_type(cipher); | ||
| 580 | if(i == NID_undef) { | ||
| 581 | PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); | ||
| 582 | return(0); | ||
| 583 | } | ||
| 584 | objtmp = OBJ_nid2obj(i); | ||
| 585 | |||
| 586 | ec->cipher = cipher; | ||
| 587 | return 1; | ||
| 588 | } | ||
| 589 | |||
diff --git a/src/lib/libcrypto/pkcs7/pk7_mime.c b/src/lib/libcrypto/pkcs7/pk7_mime.c new file mode 100644 index 0000000000..17b68992f7 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_mime.c | |||
| @@ -0,0 +1,722 @@ | |||
| 1 | /* pk7_mime.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 1999-2005 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 pkcs7_output_data(BIO *bio, BIO *data, PKCS7 *p7, int flags); | ||
| 90 | static int B64_write_PKCS7(BIO *bio, PKCS7 *p7); | ||
| 91 | static PKCS7 *B64_read_PKCS7(BIO *bio); | ||
| 92 | static char * strip_ends(char *name); | ||
| 93 | static char * strip_start(char *name); | ||
| 94 | static char * strip_end(char *name); | ||
| 95 | static MIME_HEADER *mime_hdr_new(char *name, char *value); | ||
| 96 | static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value); | ||
| 97 | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio); | ||
| 98 | static int mime_hdr_cmp(const MIME_HEADER * const *a, | ||
| 99 | const MIME_HEADER * const *b); | ||
| 100 | static int mime_param_cmp(const MIME_PARAM * const *a, | ||
| 101 | const MIME_PARAM * const *b); | ||
| 102 | static void mime_param_free(MIME_PARAM *param); | ||
| 103 | static int mime_bound_check(char *line, int linelen, char *bound, int blen); | ||
| 104 | static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret); | ||
| 105 | static int strip_eol(char *linebuf, int *plen); | ||
| 106 | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name); | ||
| 107 | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name); | ||
| 108 | static void mime_hdr_free(MIME_HEADER *hdr); | ||
| 109 | |||
| 110 | #define MAX_SMLEN 1024 | ||
| 111 | #define mime_debug(x) /* x */ | ||
| 112 | |||
| 113 | /* Base 64 read and write of PKCS#7 structure */ | ||
| 114 | |||
| 115 | static int B64_write_PKCS7(BIO *bio, PKCS7 *p7) | ||
| 116 | { | ||
| 117 | BIO *b64; | ||
| 118 | if(!(b64 = BIO_new(BIO_f_base64()))) { | ||
| 119 | PKCS7err(PKCS7_F_B64_WRITE_PKCS7,ERR_R_MALLOC_FAILURE); | ||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | bio = BIO_push(b64, bio); | ||
| 123 | i2d_PKCS7_bio(bio, p7); | ||
| 124 | (void)BIO_flush(bio); | ||
| 125 | bio = BIO_pop(bio); | ||
| 126 | BIO_free(b64); | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | static PKCS7 *B64_read_PKCS7(BIO *bio) | ||
| 131 | { | ||
| 132 | BIO *b64; | ||
| 133 | PKCS7 *p7; | ||
| 134 | if(!(b64 = BIO_new(BIO_f_base64()))) { | ||
| 135 | PKCS7err(PKCS7_F_B64_READ_PKCS7,ERR_R_MALLOC_FAILURE); | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | bio = BIO_push(b64, bio); | ||
| 139 | if(!(p7 = d2i_PKCS7_bio(bio, NULL))) | ||
| 140 | PKCS7err(PKCS7_F_B64_READ_PKCS7,PKCS7_R_DECODE_ERROR); | ||
| 141 | (void)BIO_flush(bio); | ||
| 142 | bio = BIO_pop(bio); | ||
| 143 | BIO_free(b64); | ||
| 144 | return p7; | ||
| 145 | } | ||
| 146 | |||
| 147 | /* SMIME sender */ | ||
| 148 | |||
| 149 | int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags) | ||
| 150 | { | ||
| 151 | char bound[33], c; | ||
| 152 | int i; | ||
| 153 | char *mime_prefix, *mime_eol, *msg_type=NULL; | ||
| 154 | if (flags & PKCS7_NOOLDMIMETYPE) | ||
| 155 | mime_prefix = "application/pkcs7-"; | ||
| 156 | else | ||
| 157 | mime_prefix = "application/x-pkcs7-"; | ||
| 158 | |||
| 159 | if (flags & PKCS7_CRLFEOL) | ||
| 160 | mime_eol = "\r\n"; | ||
| 161 | else | ||
| 162 | mime_eol = "\n"; | ||
| 163 | if((flags & PKCS7_DETACHED) && data) { | ||
| 164 | /* We want multipart/signed */ | ||
| 165 | /* Generate a random boundary */ | ||
| 166 | RAND_pseudo_bytes((unsigned char *)bound, 32); | ||
| 167 | for(i = 0; i < 32; i++) { | ||
| 168 | c = bound[i] & 0xf; | ||
| 169 | if(c < 10) c += '0'; | ||
| 170 | else c += 'A' - 10; | ||
| 171 | bound[i] = c; | ||
| 172 | } | ||
| 173 | bound[32] = 0; | ||
| 174 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); | ||
| 175 | BIO_printf(bio, "Content-Type: multipart/signed;"); | ||
| 176 | BIO_printf(bio, " protocol=\"%ssignature\";", mime_prefix); | ||
| 177 | BIO_printf(bio, " micalg=sha1; boundary=\"----%s\"%s%s", | ||
| 178 | bound, mime_eol, mime_eol); | ||
| 179 | BIO_printf(bio, "This is an S/MIME signed message%s%s", | ||
| 180 | mime_eol, mime_eol); | ||
| 181 | /* Now write out the first part */ | ||
| 182 | BIO_printf(bio, "------%s%s", bound, mime_eol); | ||
| 183 | pkcs7_output_data(bio, data, p7, flags); | ||
| 184 | BIO_printf(bio, "%s------%s%s", mime_eol, bound, mime_eol); | ||
| 185 | |||
| 186 | /* Headers for signature */ | ||
| 187 | |||
| 188 | BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix); | ||
| 189 | BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol); | ||
| 190 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s", | ||
| 191 | mime_eol); | ||
| 192 | BIO_printf(bio, "Content-Disposition: attachment;"); | ||
| 193 | BIO_printf(bio, " filename=\"smime.p7s\"%s%s", | ||
| 194 | mime_eol, mime_eol); | ||
| 195 | B64_write_PKCS7(bio, p7); | ||
| 196 | BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound, | ||
| 197 | mime_eol, mime_eol); | ||
| 198 | return 1; | ||
| 199 | } | ||
| 200 | |||
| 201 | /* Determine smime-type header */ | ||
| 202 | |||
| 203 | if (PKCS7_type_is_enveloped(p7)) | ||
| 204 | msg_type = "enveloped-data"; | ||
| 205 | else if (PKCS7_type_is_signed(p7)) | ||
| 206 | { | ||
| 207 | /* If we have any signers it is signed-data othewise | ||
| 208 | * certs-only. | ||
| 209 | */ | ||
| 210 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 211 | sinfos = PKCS7_get_signer_info(p7); | ||
| 212 | if (sk_PKCS7_SIGNER_INFO_num(sinfos) > 0) | ||
| 213 | msg_type = "signed-data"; | ||
| 214 | else | ||
| 215 | msg_type = "certs-only"; | ||
| 216 | } | ||
| 217 | /* MIME headers */ | ||
| 218 | BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); | ||
| 219 | BIO_printf(bio, "Content-Disposition: attachment;"); | ||
| 220 | BIO_printf(bio, " filename=\"smime.p7m\"%s", mime_eol); | ||
| 221 | BIO_printf(bio, "Content-Type: %smime;", mime_prefix); | ||
| 222 | if (msg_type) | ||
| 223 | BIO_printf(bio, " smime-type=%s;", msg_type); | ||
| 224 | BIO_printf(bio, " name=\"smime.p7m\"%s", mime_eol); | ||
| 225 | BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s", | ||
| 226 | mime_eol, mime_eol); | ||
| 227 | B64_write_PKCS7(bio, p7); | ||
| 228 | BIO_printf(bio, "%s", mime_eol); | ||
| 229 | return 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | /* Handle output of PKCS#7 data */ | ||
| 233 | |||
| 234 | |||
| 235 | static int pkcs7_output_data(BIO *out, BIO *data, PKCS7 *p7, int flags) | ||
| 236 | { | ||
| 237 | BIO *tmpbio, *p7bio; | ||
| 238 | |||
| 239 | if (!(flags & PKCS7_STREAM)) | ||
| 240 | { | ||
| 241 | SMIME_crlf_copy(data, out, flags); | ||
| 242 | return 1; | ||
| 243 | } | ||
| 244 | |||
| 245 | /* Partial sign operation */ | ||
| 246 | |||
| 247 | /* Initialize sign operation */ | ||
| 248 | p7bio = PKCS7_dataInit(p7, out); | ||
| 249 | |||
| 250 | /* Copy data across, computing digests etc */ | ||
| 251 | SMIME_crlf_copy(data, p7bio, flags); | ||
| 252 | |||
| 253 | /* Must be detached */ | ||
| 254 | PKCS7_set_detached(p7, 1); | ||
| 255 | |||
| 256 | /* Finalize signatures */ | ||
| 257 | PKCS7_dataFinal(p7, p7bio); | ||
| 258 | |||
| 259 | /* Now remove any digests prepended to the BIO */ | ||
| 260 | |||
| 261 | while (p7bio != out) | ||
| 262 | { | ||
| 263 | tmpbio = BIO_pop(p7bio); | ||
| 264 | BIO_free(p7bio); | ||
| 265 | p7bio = tmpbio; | ||
| 266 | } | ||
| 267 | |||
| 268 | return 1; | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | /* SMIME reader: handle multipart/signed and opaque signing. | ||
| 273 | * in multipart case the content is placed in a memory BIO | ||
| 274 | * pointed to by "bcont". In opaque this is set to NULL | ||
| 275 | */ | ||
| 276 | |||
| 277 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont) | ||
| 278 | { | ||
| 279 | BIO *p7in; | ||
| 280 | STACK_OF(MIME_HEADER) *headers = NULL; | ||
| 281 | STACK_OF(BIO) *parts = NULL; | ||
| 282 | MIME_HEADER *hdr; | ||
| 283 | MIME_PARAM *prm; | ||
| 284 | PKCS7 *p7; | ||
| 285 | int ret; | ||
| 286 | |||
| 287 | if(bcont) *bcont = NULL; | ||
| 288 | |||
| 289 | if (!(headers = mime_parse_hdr(bio))) { | ||
| 290 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_PARSE_ERROR); | ||
| 291 | return NULL; | ||
| 292 | } | ||
| 293 | |||
| 294 | if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { | ||
| 295 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 296 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_CONTENT_TYPE); | ||
| 297 | return NULL; | ||
| 298 | } | ||
| 299 | |||
| 300 | /* Handle multipart/signed */ | ||
| 301 | |||
| 302 | if(!strcmp(hdr->value, "multipart/signed")) { | ||
| 303 | /* Split into two parts */ | ||
| 304 | prm = mime_param_find(hdr, "boundary"); | ||
| 305 | if(!prm || !prm->param_value) { | ||
| 306 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 307 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BOUNDARY); | ||
| 308 | return NULL; | ||
| 309 | } | ||
| 310 | ret = multi_split(bio, prm->param_value, &parts); | ||
| 311 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 312 | if(!ret || (sk_BIO_num(parts) != 2) ) { | ||
| 313 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_MULTIPART_BODY_FAILURE); | ||
| 314 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 315 | return NULL; | ||
| 316 | } | ||
| 317 | |||
| 318 | /* Parse the signature piece */ | ||
| 319 | p7in = sk_BIO_value(parts, 1); | ||
| 320 | |||
| 321 | if (!(headers = mime_parse_hdr(p7in))) { | ||
| 322 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_MIME_SIG_PARSE_ERROR); | ||
| 323 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 324 | return NULL; | ||
| 325 | } | ||
| 326 | |||
| 327 | /* Get content type */ | ||
| 328 | |||
| 329 | if(!(hdr = mime_hdr_find(headers, "content-type")) || | ||
| 330 | !hdr->value) { | ||
| 331 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 332 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_NO_SIG_CONTENT_TYPE); | ||
| 333 | return NULL; | ||
| 334 | } | ||
| 335 | |||
| 336 | if(strcmp(hdr->value, "application/x-pkcs7-signature") && | ||
| 337 | strcmp(hdr->value, "application/pkcs7-signature")) { | ||
| 338 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 339 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_SIG_INVALID_MIME_TYPE); | ||
| 340 | ERR_add_error_data(2, "type: ", hdr->value); | ||
| 341 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 342 | return NULL; | ||
| 343 | } | ||
| 344 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 345 | /* Read in PKCS#7 */ | ||
| 346 | if(!(p7 = B64_read_PKCS7(p7in))) { | ||
| 347 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_PKCS7_SIG_PARSE_ERROR); | ||
| 348 | sk_BIO_pop_free(parts, BIO_vfree); | ||
| 349 | return NULL; | ||
| 350 | } | ||
| 351 | |||
| 352 | if(bcont) { | ||
| 353 | *bcont = sk_BIO_value(parts, 0); | ||
| 354 | BIO_free(p7in); | ||
| 355 | sk_BIO_free(parts); | ||
| 356 | } else sk_BIO_pop_free(parts, BIO_vfree); | ||
| 357 | return p7; | ||
| 358 | } | ||
| 359 | |||
| 360 | /* OK, if not multipart/signed try opaque signature */ | ||
| 361 | |||
| 362 | if (strcmp (hdr->value, "application/x-pkcs7-mime") && | ||
| 363 | strcmp (hdr->value, "application/pkcs7-mime")) { | ||
| 364 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7,PKCS7_R_INVALID_MIME_TYPE); | ||
| 365 | ERR_add_error_data(2, "type: ", hdr->value); | ||
| 366 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 367 | return NULL; | ||
| 368 | } | ||
| 369 | |||
| 370 | sk_MIME_HEADER_pop_free(headers, mime_hdr_free); | ||
| 371 | |||
| 372 | if(!(p7 = B64_read_PKCS7(bio))) { | ||
| 373 | PKCS7err(PKCS7_F_SMIME_READ_PKCS7, PKCS7_R_PKCS7_PARSE_ERROR); | ||
| 374 | return NULL; | ||
| 375 | } | ||
| 376 | return p7; | ||
| 377 | |||
| 378 | } | ||
| 379 | |||
| 380 | /* Split a multipart/XXX message body into component parts: result is | ||
| 381 | * canonical parts in a STACK of bios | ||
| 382 | */ | ||
| 383 | |||
| 384 | static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret) | ||
| 385 | { | ||
| 386 | char linebuf[MAX_SMLEN]; | ||
| 387 | int len, blen; | ||
| 388 | int eol = 0, next_eol = 0; | ||
| 389 | BIO *bpart = NULL; | ||
| 390 | STACK_OF(BIO) *parts; | ||
| 391 | char state, part, first; | ||
| 392 | |||
| 393 | blen = strlen(bound); | ||
| 394 | part = 0; | ||
| 395 | state = 0; | ||
| 396 | first = 1; | ||
| 397 | parts = sk_BIO_new_null(); | ||
| 398 | *ret = parts; | ||
| 399 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { | ||
| 400 | state = mime_bound_check(linebuf, len, bound, blen); | ||
| 401 | if(state == 1) { | ||
| 402 | first = 1; | ||
| 403 | part++; | ||
| 404 | } else if(state == 2) { | ||
| 405 | sk_BIO_push(parts, bpart); | ||
| 406 | return 1; | ||
| 407 | } else if(part) { | ||
| 408 | /* Strip CR+LF from linebuf */ | ||
| 409 | next_eol = strip_eol(linebuf, &len); | ||
| 410 | if(first) { | ||
| 411 | first = 0; | ||
| 412 | if(bpart) sk_BIO_push(parts, bpart); | ||
| 413 | bpart = BIO_new(BIO_s_mem()); | ||
| 414 | BIO_set_mem_eof_return(bpart, 0); | ||
| 415 | } else if (eol) | ||
| 416 | BIO_write(bpart, "\r\n", 2); | ||
| 417 | eol = next_eol; | ||
| 418 | if (len) | ||
| 419 | BIO_write(bpart, linebuf, len); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | return 0; | ||
| 423 | } | ||
| 424 | |||
| 425 | /* This is the big one: parse MIME header lines up to message body */ | ||
| 426 | |||
| 427 | #define MIME_INVALID 0 | ||
| 428 | #define MIME_START 1 | ||
| 429 | #define MIME_TYPE 2 | ||
| 430 | #define MIME_NAME 3 | ||
| 431 | #define MIME_VALUE 4 | ||
| 432 | #define MIME_QUOTE 5 | ||
| 433 | #define MIME_COMMENT 6 | ||
| 434 | |||
| 435 | |||
| 436 | static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) | ||
| 437 | { | ||
| 438 | char *p, *q, c; | ||
| 439 | char *ntmp; | ||
| 440 | char linebuf[MAX_SMLEN]; | ||
| 441 | MIME_HEADER *mhdr = NULL; | ||
| 442 | STACK_OF(MIME_HEADER) *headers; | ||
| 443 | int len, state, save_state = 0; | ||
| 444 | |||
| 445 | headers = sk_MIME_HEADER_new(mime_hdr_cmp); | ||
| 446 | while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { | ||
| 447 | /* If whitespace at line start then continuation line */ | ||
| 448 | if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME; | ||
| 449 | else state = MIME_START; | ||
| 450 | ntmp = NULL; | ||
| 451 | /* Go through all characters */ | ||
| 452 | for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) { | ||
| 453 | |||
| 454 | /* State machine to handle MIME headers | ||
| 455 | * if this looks horrible that's because it *is* | ||
| 456 | */ | ||
| 457 | |||
| 458 | switch(state) { | ||
| 459 | case MIME_START: | ||
| 460 | if(c == ':') { | ||
| 461 | state = MIME_TYPE; | ||
| 462 | *p = 0; | ||
| 463 | ntmp = strip_ends(q); | ||
| 464 | q = p + 1; | ||
| 465 | } | ||
| 466 | break; | ||
| 467 | |||
| 468 | case MIME_TYPE: | ||
| 469 | if(c == ';') { | ||
| 470 | mime_debug("Found End Value\n"); | ||
| 471 | *p = 0; | ||
| 472 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | ||
| 473 | sk_MIME_HEADER_push(headers, mhdr); | ||
| 474 | ntmp = NULL; | ||
| 475 | q = p + 1; | ||
| 476 | state = MIME_NAME; | ||
| 477 | } else if(c == '(') { | ||
| 478 | save_state = state; | ||
| 479 | state = MIME_COMMENT; | ||
| 480 | } | ||
| 481 | break; | ||
| 482 | |||
| 483 | case MIME_COMMENT: | ||
| 484 | if(c == ')') { | ||
| 485 | state = save_state; | ||
| 486 | } | ||
| 487 | break; | ||
| 488 | |||
| 489 | case MIME_NAME: | ||
| 490 | if(c == '=') { | ||
| 491 | state = MIME_VALUE; | ||
| 492 | *p = 0; | ||
| 493 | ntmp = strip_ends(q); | ||
| 494 | q = p + 1; | ||
| 495 | } | ||
| 496 | break ; | ||
| 497 | |||
| 498 | case MIME_VALUE: | ||
| 499 | if(c == ';') { | ||
| 500 | state = MIME_NAME; | ||
| 501 | *p = 0; | ||
| 502 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | ||
| 503 | ntmp = NULL; | ||
| 504 | q = p + 1; | ||
| 505 | } else if (c == '"') { | ||
| 506 | mime_debug("Found Quote\n"); | ||
| 507 | state = MIME_QUOTE; | ||
| 508 | } else if(c == '(') { | ||
| 509 | save_state = state; | ||
| 510 | state = MIME_COMMENT; | ||
| 511 | } | ||
| 512 | break; | ||
| 513 | |||
| 514 | case MIME_QUOTE: | ||
| 515 | if(c == '"') { | ||
| 516 | mime_debug("Found Match Quote\n"); | ||
| 517 | state = MIME_VALUE; | ||
| 518 | } | ||
| 519 | break; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | |||
| 523 | if(state == MIME_TYPE) { | ||
| 524 | mhdr = mime_hdr_new(ntmp, strip_ends(q)); | ||
| 525 | sk_MIME_HEADER_push(headers, mhdr); | ||
| 526 | } else if(state == MIME_VALUE) | ||
| 527 | mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); | ||
| 528 | if(p == linebuf) break; /* Blank line means end of headers */ | ||
| 529 | } | ||
| 530 | |||
| 531 | return headers; | ||
| 532 | |||
| 533 | } | ||
| 534 | |||
| 535 | static char *strip_ends(char *name) | ||
| 536 | { | ||
| 537 | return strip_end(strip_start(name)); | ||
| 538 | } | ||
| 539 | |||
| 540 | /* Strip a parameter of whitespace from start of param */ | ||
| 541 | static char *strip_start(char *name) | ||
| 542 | { | ||
| 543 | char *p, c; | ||
| 544 | /* Look for first non white space or quote */ | ||
| 545 | for(p = name; (c = *p) ;p++) { | ||
| 546 | if(c == '"') { | ||
| 547 | /* Next char is start of string if non null */ | ||
| 548 | if(p[1]) return p + 1; | ||
| 549 | /* Else null string */ | ||
| 550 | return NULL; | ||
| 551 | } | ||
| 552 | if(!isspace((unsigned char)c)) return p; | ||
| 553 | } | ||
| 554 | return NULL; | ||
| 555 | } | ||
| 556 | |||
| 557 | /* As above but strip from end of string : maybe should handle brackets? */ | ||
| 558 | static char *strip_end(char *name) | ||
| 559 | { | ||
| 560 | char *p, c; | ||
| 561 | if(!name) return NULL; | ||
| 562 | /* Look for first non white space or quote */ | ||
| 563 | for(p = name + strlen(name) - 1; p >= name ;p--) { | ||
| 564 | c = *p; | ||
| 565 | if(c == '"') { | ||
| 566 | if(p - 1 == name) return NULL; | ||
| 567 | *p = 0; | ||
| 568 | return name; | ||
| 569 | } | ||
| 570 | if(isspace((unsigned char)c)) *p = 0; | ||
| 571 | else return name; | ||
| 572 | } | ||
| 573 | return NULL; | ||
| 574 | } | ||
| 575 | |||
| 576 | static MIME_HEADER *mime_hdr_new(char *name, char *value) | ||
| 577 | { | ||
| 578 | MIME_HEADER *mhdr; | ||
| 579 | char *tmpname, *tmpval, *p; | ||
| 580 | int c; | ||
| 581 | if(name) { | ||
| 582 | if(!(tmpname = BUF_strdup(name))) return NULL; | ||
| 583 | for(p = tmpname ; *p; p++) { | ||
| 584 | c = *p; | ||
| 585 | if(isupper(c)) { | ||
| 586 | c = tolower(c); | ||
| 587 | *p = c; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | } else tmpname = NULL; | ||
| 591 | if(value) { | ||
| 592 | if(!(tmpval = BUF_strdup(value))) return NULL; | ||
| 593 | for(p = tmpval ; *p; p++) { | ||
| 594 | c = *p; | ||
| 595 | if(isupper(c)) { | ||
| 596 | c = tolower(c); | ||
| 597 | *p = c; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | } else tmpval = NULL; | ||
| 601 | mhdr = (MIME_HEADER *) OPENSSL_malloc(sizeof(MIME_HEADER)); | ||
| 602 | if(!mhdr) return NULL; | ||
| 603 | mhdr->name = tmpname; | ||
| 604 | mhdr->value = tmpval; | ||
| 605 | if(!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) return NULL; | ||
| 606 | return mhdr; | ||
| 607 | } | ||
| 608 | |||
| 609 | static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value) | ||
| 610 | { | ||
| 611 | char *tmpname, *tmpval, *p; | ||
| 612 | int c; | ||
| 613 | MIME_PARAM *mparam; | ||
| 614 | if(name) { | ||
| 615 | tmpname = BUF_strdup(name); | ||
| 616 | if(!tmpname) return 0; | ||
| 617 | for(p = tmpname ; *p; p++) { | ||
| 618 | c = *p; | ||
| 619 | if(isupper(c)) { | ||
| 620 | c = tolower(c); | ||
| 621 | *p = c; | ||
| 622 | } | ||
| 623 | } | ||
| 624 | } else tmpname = NULL; | ||
| 625 | if(value) { | ||
| 626 | tmpval = BUF_strdup(value); | ||
| 627 | if(!tmpval) return 0; | ||
| 628 | } else tmpval = NULL; | ||
| 629 | /* Parameter values are case sensitive so leave as is */ | ||
| 630 | mparam = (MIME_PARAM *) OPENSSL_malloc(sizeof(MIME_PARAM)); | ||
| 631 | if(!mparam) return 0; | ||
| 632 | mparam->param_name = tmpname; | ||
| 633 | mparam->param_value = tmpval; | ||
| 634 | sk_MIME_PARAM_push(mhdr->params, mparam); | ||
| 635 | return 1; | ||
| 636 | } | ||
| 637 | |||
| 638 | static int mime_hdr_cmp(const MIME_HEADER * const *a, | ||
| 639 | const MIME_HEADER * const *b) | ||
| 640 | { | ||
| 641 | return(strcmp((*a)->name, (*b)->name)); | ||
| 642 | } | ||
| 643 | |||
| 644 | static int mime_param_cmp(const MIME_PARAM * const *a, | ||
| 645 | const MIME_PARAM * const *b) | ||
| 646 | { | ||
| 647 | return(strcmp((*a)->param_name, (*b)->param_name)); | ||
| 648 | } | ||
| 649 | |||
| 650 | /* Find a header with a given name (if possible) */ | ||
| 651 | |||
| 652 | static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name) | ||
| 653 | { | ||
| 654 | MIME_HEADER htmp; | ||
| 655 | int idx; | ||
| 656 | htmp.name = name; | ||
| 657 | idx = sk_MIME_HEADER_find(hdrs, &htmp); | ||
| 658 | if(idx < 0) return NULL; | ||
| 659 | return sk_MIME_HEADER_value(hdrs, idx); | ||
| 660 | } | ||
| 661 | |||
| 662 | static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name) | ||
| 663 | { | ||
| 664 | MIME_PARAM param; | ||
| 665 | int idx; | ||
| 666 | param.param_name = name; | ||
| 667 | idx = sk_MIME_PARAM_find(hdr->params, ¶m); | ||
| 668 | if(idx < 0) return NULL; | ||
| 669 | return sk_MIME_PARAM_value(hdr->params, idx); | ||
| 670 | } | ||
| 671 | |||
| 672 | static void mime_hdr_free(MIME_HEADER *hdr) | ||
| 673 | { | ||
| 674 | if(hdr->name) OPENSSL_free(hdr->name); | ||
| 675 | if(hdr->value) OPENSSL_free(hdr->value); | ||
| 676 | if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); | ||
| 677 | OPENSSL_free(hdr); | ||
| 678 | } | ||
| 679 | |||
| 680 | static void mime_param_free(MIME_PARAM *param) | ||
| 681 | { | ||
| 682 | if(param->param_name) OPENSSL_free(param->param_name); | ||
| 683 | if(param->param_value) OPENSSL_free(param->param_value); | ||
| 684 | OPENSSL_free(param); | ||
| 685 | } | ||
| 686 | |||
| 687 | /* Check for a multipart boundary. Returns: | ||
| 688 | * 0 : no boundary | ||
| 689 | * 1 : part boundary | ||
| 690 | * 2 : final boundary | ||
| 691 | */ | ||
| 692 | static int mime_bound_check(char *line, int linelen, char *bound, int blen) | ||
| 693 | { | ||
| 694 | if(linelen == -1) linelen = strlen(line); | ||
| 695 | if(blen == -1) blen = strlen(bound); | ||
| 696 | /* Quickly eliminate if line length too short */ | ||
| 697 | if(blen + 2 > linelen) return 0; | ||
| 698 | /* Check for part boundary */ | ||
| 699 | if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) { | ||
| 700 | if(!strncmp(line + blen + 2, "--", 2)) return 2; | ||
| 701 | else return 1; | ||
| 702 | } | ||
| 703 | return 0; | ||
| 704 | } | ||
| 705 | |||
| 706 | static int strip_eol(char *linebuf, int *plen) | ||
| 707 | { | ||
| 708 | int len = *plen; | ||
| 709 | char *p, c; | ||
| 710 | int is_eol = 0; | ||
| 711 | p = linebuf + len - 1; | ||
| 712 | for (p = linebuf + len - 1; len > 0; len--, p--) | ||
| 713 | { | ||
| 714 | c = *p; | ||
| 715 | if (c == '\n') | ||
| 716 | is_eol = 1; | ||
| 717 | else if (c != '\r') | ||
| 718 | break; | ||
| 719 | } | ||
| 720 | *plen = len; | ||
| 721 | return is_eol; | ||
| 722 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_smime.c b/src/lib/libcrypto/pkcs7/pk7_smime.c new file mode 100644 index 0000000000..5c6b0fe24b --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_smime.c | |||
| @@ -0,0 +1,500 @@ | |||
| 1 | /* pk7_smime.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 1999-2004 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 = NULL; | ||
| 70 | PKCS7_SIGNER_INFO *si; | ||
| 71 | BIO *p7bio = NULL; | ||
| 72 | STACK_OF(X509_ALGOR) *smcap = NULL; | ||
| 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 | if (!PKCS7_set_type(p7, NID_pkcs7_signed)) | ||
| 86 | goto err; | ||
| 87 | |||
| 88 | if (!PKCS7_content_new(p7, NID_pkcs7_data)) | ||
| 89 | goto err; | ||
| 90 | |||
| 91 | if (!(si = PKCS7_add_signature(p7,signcert,pkey,EVP_sha1()))) { | ||
| 92 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); | ||
| 93 | goto err; | ||
| 94 | } | ||
| 95 | |||
| 96 | if(!(flags & PKCS7_NOCERTS)) { | ||
| 97 | if (!PKCS7_add_certificate(p7, signcert)) | ||
| 98 | goto err; | ||
| 99 | if(certs) for(i = 0; i < sk_X509_num(certs); i++) | ||
| 100 | if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i))) | ||
| 101 | goto err; | ||
| 102 | } | ||
| 103 | |||
| 104 | if(!(flags & PKCS7_NOATTR)) { | ||
| 105 | if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, | ||
| 106 | V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data))) | ||
| 107 | goto err; | ||
| 108 | /* Add SMIMECapabilities */ | ||
| 109 | if(!(flags & PKCS7_NOSMIMECAP)) | ||
| 110 | { | ||
| 111 | if(!(smcap = sk_X509_ALGOR_new_null())) { | ||
| 112 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 113 | goto err; | ||
| 114 | } | ||
| 115 | #ifndef OPENSSL_NO_DES | ||
| 116 | if (!PKCS7_simple_smimecap (smcap, NID_des_ede3_cbc, -1)) | ||
| 117 | goto err; | ||
| 118 | #endif | ||
| 119 | #ifndef OPENSSL_NO_RC2 | ||
| 120 | if (!PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 128)) | ||
| 121 | goto err; | ||
| 122 | if (!PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 64)) | ||
| 123 | goto err; | ||
| 124 | #endif | ||
| 125 | #ifndef OPENSSL_NO_DES | ||
| 126 | if (!PKCS7_simple_smimecap (smcap, NID_des_cbc, -1)) | ||
| 127 | goto err; | ||
| 128 | #endif | ||
| 129 | #ifndef OPENSSL_NO_RC2 | ||
| 130 | if (!PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 40)) | ||
| 131 | goto err; | ||
| 132 | #endif | ||
| 133 | if (!PKCS7_add_attrib_smimecap (si, smcap)) | ||
| 134 | goto err; | ||
| 135 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | ||
| 136 | smcap = NULL; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | if(flags & PKCS7_DETACHED)PKCS7_set_detached(p7, 1); | ||
| 141 | |||
| 142 | if (flags & PKCS7_STREAM) | ||
| 143 | return p7; | ||
| 144 | |||
| 145 | |||
| 146 | if (!(p7bio = PKCS7_dataInit(p7, NULL))) { | ||
| 147 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 148 | goto err; | ||
| 149 | } | ||
| 150 | |||
| 151 | SMIME_crlf_copy(data, p7bio, flags); | ||
| 152 | |||
| 153 | |||
| 154 | if (!PKCS7_dataFinal(p7,p7bio)) { | ||
| 155 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_DATASIGN); | ||
| 156 | goto err; | ||
| 157 | } | ||
| 158 | |||
| 159 | BIO_free_all(p7bio); | ||
| 160 | return p7; | ||
| 161 | err: | ||
| 162 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | ||
| 163 | BIO_free_all(p7bio); | ||
| 164 | PKCS7_free(p7); | ||
| 165 | return NULL; | ||
| 166 | } | ||
| 167 | |||
| 168 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | ||
| 169 | BIO *indata, BIO *out, int flags) | ||
| 170 | { | ||
| 171 | STACK_OF(X509) *signers; | ||
| 172 | X509 *signer; | ||
| 173 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 174 | PKCS7_SIGNER_INFO *si; | ||
| 175 | X509_STORE_CTX cert_ctx; | ||
| 176 | char buf[4096]; | ||
| 177 | int i, j=0, k, ret = 0; | ||
| 178 | BIO *p7bio; | ||
| 179 | BIO *tmpin, *tmpout; | ||
| 180 | |||
| 181 | if(!p7) { | ||
| 182 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_INVALID_NULL_POINTER); | ||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | |||
| 186 | if(!PKCS7_type_is_signed(p7)) { | ||
| 187 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 188 | return 0; | ||
| 189 | } | ||
| 190 | |||
| 191 | /* Check for no data and no content: no data to verify signature */ | ||
| 192 | if(PKCS7_get_detached(p7) && !indata) { | ||
| 193 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_CONTENT); | ||
| 194 | return 0; | ||
| 195 | } | ||
| 196 | #if 0 | ||
| 197 | /* NB: this test commented out because some versions of Netscape | ||
| 198 | * illegally include zero length content when signing data. | ||
| 199 | */ | ||
| 200 | |||
| 201 | /* Check for data and content: two sets of data */ | ||
| 202 | if(!PKCS7_get_detached(p7) && indata) { | ||
| 203 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CONTENT_AND_DATA_PRESENT); | ||
| 204 | return 0; | ||
| 205 | } | ||
| 206 | #endif | ||
| 207 | |||
| 208 | sinfos = PKCS7_get_signer_info(p7); | ||
| 209 | |||
| 210 | if(!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) { | ||
| 211 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_SIGNATURES_ON_DATA); | ||
| 212 | return 0; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | signers = PKCS7_get0_signers(p7, certs, flags); | ||
| 217 | |||
| 218 | if(!signers) return 0; | ||
| 219 | |||
| 220 | /* Now verify the certificates */ | ||
| 221 | |||
| 222 | if (!(flags & PKCS7_NOVERIFY)) for (k = 0; k < sk_X509_num(signers); k++) { | ||
| 223 | signer = sk_X509_value (signers, k); | ||
| 224 | if (!(flags & PKCS7_NOCHAIN)) { | ||
| 225 | if(!X509_STORE_CTX_init(&cert_ctx, store, signer, | ||
| 226 | p7->d.sign->cert)) | ||
| 227 | { | ||
| 228 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB); | ||
| 229 | sk_X509_free(signers); | ||
| 230 | return 0; | ||
| 231 | } | ||
| 232 | X509_STORE_CTX_set_purpose(&cert_ctx, | ||
| 233 | X509_PURPOSE_SMIME_SIGN); | ||
| 234 | } else if(!X509_STORE_CTX_init (&cert_ctx, store, signer, NULL)) { | ||
| 235 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB); | ||
| 236 | sk_X509_free(signers); | ||
| 237 | return 0; | ||
| 238 | } | ||
| 239 | if (!(flags & PKCS7_NOCRL)) | ||
| 240 | X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl); | ||
| 241 | i = X509_verify_cert(&cert_ctx); | ||
| 242 | if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx); | ||
| 243 | X509_STORE_CTX_cleanup(&cert_ctx); | ||
| 244 | if (i <= 0) { | ||
| 245 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CERTIFICATE_VERIFY_ERROR); | ||
| 246 | ERR_add_error_data(2, "Verify error:", | ||
| 247 | X509_verify_cert_error_string(j)); | ||
| 248 | sk_X509_free(signers); | ||
| 249 | return 0; | ||
| 250 | } | ||
| 251 | /* Check for revocation status here */ | ||
| 252 | } | ||
| 253 | |||
| 254 | /* Performance optimization: if the content is a memory BIO then | ||
| 255 | * store its contents in a temporary read only memory BIO. This | ||
| 256 | * avoids potentially large numbers of slow copies of data which will | ||
| 257 | * occur when reading from a read write memory BIO when signatures | ||
| 258 | * are calculated. | ||
| 259 | */ | ||
| 260 | |||
| 261 | if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) | ||
| 262 | { | ||
| 263 | char *ptr; | ||
| 264 | long len; | ||
| 265 | len = BIO_get_mem_data(indata, &ptr); | ||
| 266 | tmpin = BIO_new_mem_buf(ptr, len); | ||
| 267 | if (tmpin == NULL) | ||
| 268 | { | ||
| 269 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE); | ||
| 270 | return 0; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | else | ||
| 274 | tmpin = indata; | ||
| 275 | |||
| 276 | |||
| 277 | if (!(p7bio=PKCS7_dataInit(p7,tmpin))) | ||
| 278 | goto err; | ||
| 279 | |||
| 280 | if(flags & PKCS7_TEXT) { | ||
| 281 | if(!(tmpout = BIO_new(BIO_s_mem()))) { | ||
| 282 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE); | ||
| 283 | goto err; | ||
| 284 | } | ||
| 285 | } else tmpout = out; | ||
| 286 | |||
| 287 | /* We now have to 'read' from p7bio to calculate digests etc. */ | ||
| 288 | for (;;) | ||
| 289 | { | ||
| 290 | i=BIO_read(p7bio,buf,sizeof(buf)); | ||
| 291 | if (i <= 0) break; | ||
| 292 | if (tmpout) BIO_write(tmpout, buf, i); | ||
| 293 | } | ||
| 294 | |||
| 295 | if(flags & PKCS7_TEXT) { | ||
| 296 | if(!SMIME_text(tmpout, out)) { | ||
| 297 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SMIME_TEXT_ERROR); | ||
| 298 | BIO_free(tmpout); | ||
| 299 | goto err; | ||
| 300 | } | ||
| 301 | BIO_free(tmpout); | ||
| 302 | } | ||
| 303 | |||
| 304 | /* Now Verify All Signatures */ | ||
| 305 | if (!(flags & PKCS7_NOSIGS)) | ||
| 306 | for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sinfos); i++) | ||
| 307 | { | ||
| 308 | si=sk_PKCS7_SIGNER_INFO_value(sinfos,i); | ||
| 309 | signer = sk_X509_value (signers, i); | ||
| 310 | j=PKCS7_signatureVerify(p7bio,p7,si, signer); | ||
| 311 | if (j <= 0) { | ||
| 312 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SIGNATURE_FAILURE); | ||
| 313 | goto err; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | ret = 1; | ||
| 318 | |||
| 319 | err: | ||
| 320 | |||
| 321 | if (tmpin == indata) | ||
| 322 | { | ||
| 323 | if (indata) BIO_pop(p7bio); | ||
| 324 | } | ||
| 325 | BIO_free_all(p7bio); | ||
| 326 | |||
| 327 | sk_X509_free(signers); | ||
| 328 | |||
| 329 | return ret; | ||
| 330 | } | ||
| 331 | |||
| 332 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags) | ||
| 333 | { | ||
| 334 | STACK_OF(X509) *signers; | ||
| 335 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 336 | PKCS7_SIGNER_INFO *si; | ||
| 337 | PKCS7_ISSUER_AND_SERIAL *ias; | ||
| 338 | X509 *signer; | ||
| 339 | int i; | ||
| 340 | |||
| 341 | if(!p7) { | ||
| 342 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_INVALID_NULL_POINTER); | ||
| 343 | return NULL; | ||
| 344 | } | ||
| 345 | |||
| 346 | if(!PKCS7_type_is_signed(p7)) { | ||
| 347 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 348 | return NULL; | ||
| 349 | } | ||
| 350 | |||
| 351 | /* Collect all the signers together */ | ||
| 352 | |||
| 353 | sinfos = PKCS7_get_signer_info(p7); | ||
| 354 | |||
| 355 | if(sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) { | ||
| 356 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_NO_SIGNERS); | ||
| 357 | return NULL; | ||
| 358 | } | ||
| 359 | |||
| 360 | if(!(signers = sk_X509_new_null())) { | ||
| 361 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,ERR_R_MALLOC_FAILURE); | ||
| 362 | return NULL; | ||
| 363 | } | ||
| 364 | |||
| 365 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) | ||
| 366 | { | ||
| 367 | si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | ||
| 368 | ias = si->issuer_and_serial; | ||
| 369 | signer = NULL; | ||
| 370 | /* If any certificates passed they take priority */ | ||
| 371 | if (certs) signer = X509_find_by_issuer_and_serial (certs, | ||
| 372 | ias->issuer, ias->serial); | ||
| 373 | if (!signer && !(flags & PKCS7_NOINTERN) | ||
| 374 | && p7->d.sign->cert) signer = | ||
| 375 | X509_find_by_issuer_and_serial (p7->d.sign->cert, | ||
| 376 | ias->issuer, ias->serial); | ||
| 377 | if (!signer) { | ||
| 378 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); | ||
| 379 | sk_X509_free(signers); | ||
| 380 | return NULL; | ||
| 381 | } | ||
| 382 | |||
| 383 | if (!sk_X509_push(signers, signer)) { | ||
| 384 | sk_X509_free(signers); | ||
| 385 | return NULL; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | return signers; | ||
| 389 | } | ||
| 390 | |||
| 391 | |||
| 392 | /* Build a complete PKCS#7 enveloped data */ | ||
| 393 | |||
| 394 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | ||
| 395 | int flags) | ||
| 396 | { | ||
| 397 | PKCS7 *p7; | ||
| 398 | BIO *p7bio = NULL; | ||
| 399 | int i; | ||
| 400 | X509 *x509; | ||
| 401 | if(!(p7 = PKCS7_new())) { | ||
| 402 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 403 | return NULL; | ||
| 404 | } | ||
| 405 | |||
| 406 | if (!PKCS7_set_type(p7, NID_pkcs7_enveloped)) | ||
| 407 | goto err; | ||
| 408 | if(!PKCS7_set_cipher(p7, cipher)) { | ||
| 409 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER); | ||
| 410 | goto err; | ||
| 411 | } | ||
| 412 | |||
| 413 | for(i = 0; i < sk_X509_num(certs); i++) { | ||
| 414 | x509 = sk_X509_value(certs, i); | ||
| 415 | if(!PKCS7_add_recipient(p7, x509)) { | ||
| 416 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, | ||
| 417 | PKCS7_R_ERROR_ADDING_RECIPIENT); | ||
| 418 | goto err; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | if(!(p7bio = PKCS7_dataInit(p7, NULL))) { | ||
| 423 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 424 | goto err; | ||
| 425 | } | ||
| 426 | |||
| 427 | SMIME_crlf_copy(in, p7bio, flags); | ||
| 428 | |||
| 429 | (void)BIO_flush(p7bio); | ||
| 430 | |||
| 431 | if (!PKCS7_dataFinal(p7,p7bio)) { | ||
| 432 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_PKCS7_DATAFINAL_ERROR); | ||
| 433 | goto err; | ||
| 434 | } | ||
| 435 | BIO_free_all(p7bio); | ||
| 436 | |||
| 437 | return p7; | ||
| 438 | |||
| 439 | err: | ||
| 440 | |||
| 441 | BIO_free_all(p7bio); | ||
| 442 | PKCS7_free(p7); | ||
| 443 | return NULL; | ||
| 444 | |||
| 445 | } | ||
| 446 | |||
| 447 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) | ||
| 448 | { | ||
| 449 | BIO *tmpmem; | ||
| 450 | int ret, i; | ||
| 451 | char buf[4096]; | ||
| 452 | |||
| 453 | if(!p7) { | ||
| 454 | PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_INVALID_NULL_POINTER); | ||
| 455 | return 0; | ||
| 456 | } | ||
| 457 | |||
| 458 | if(!PKCS7_type_is_enveloped(p7)) { | ||
| 459 | PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 460 | return 0; | ||
| 461 | } | ||
| 462 | |||
| 463 | if(cert && !X509_check_private_key(cert, pkey)) { | ||
| 464 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, | ||
| 465 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | ||
| 466 | return 0; | ||
| 467 | } | ||
| 468 | |||
| 469 | if(!(tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert))) { | ||
| 470 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR); | ||
| 471 | return 0; | ||
| 472 | } | ||
| 473 | |||
| 474 | if (flags & PKCS7_TEXT) { | ||
| 475 | BIO *tmpbuf, *bread; | ||
| 476 | /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ | ||
| 477 | if(!(tmpbuf = BIO_new(BIO_f_buffer()))) { | ||
| 478 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | ||
| 479 | BIO_free_all(tmpmem); | ||
| 480 | return 0; | ||
| 481 | } | ||
| 482 | if(!(bread = BIO_push(tmpbuf, tmpmem))) { | ||
| 483 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | ||
| 484 | BIO_free_all(tmpbuf); | ||
| 485 | BIO_free_all(tmpmem); | ||
| 486 | return 0; | ||
| 487 | } | ||
| 488 | ret = SMIME_text(bread, data); | ||
| 489 | BIO_free_all(bread); | ||
| 490 | return ret; | ||
| 491 | } else { | ||
| 492 | for(;;) { | ||
| 493 | i = BIO_read(tmpmem, buf, sizeof(buf)); | ||
| 494 | if(i <= 0) break; | ||
| 495 | BIO_write(data, buf, i); | ||
| 496 | } | ||
| 497 | BIO_free_all(tmpmem); | ||
| 498 | return 1; | ||
| 499 | } | ||
| 500 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pkcs7.h b/src/lib/libcrypto/pkcs7/pkcs7.h new file mode 100644 index 0000000000..cc092d262d --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pkcs7.h | |||
| @@ -0,0 +1,464 @@ | |||
| 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_type_is_digest(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_digest) | ||
| 237 | |||
| 238 | #define PKCS7_set_detached(p,v) \ | ||
| 239 | PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL) | ||
| 240 | #define PKCS7_get_detached(p) \ | ||
| 241 | PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL) | ||
| 242 | |||
| 243 | #define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7)) | ||
| 244 | |||
| 245 | #ifdef SSLEAY_MACROS | ||
| 246 | #ifndef PKCS7_ISSUER_AND_SERIAL_digest | ||
| 247 | #define PKCS7_ISSUER_AND_SERIAL_digest(data,type,md,len) \ | ||
| 248 | ASN1_digest((int (*)())i2d_PKCS7_ISSUER_AND_SERIAL,type,\ | ||
| 249 | (char *)data,md,len) | ||
| 250 | #endif | ||
| 251 | #endif | ||
| 252 | |||
| 253 | /* S/MIME related flags */ | ||
| 254 | |||
| 255 | #define PKCS7_TEXT 0x1 | ||
| 256 | #define PKCS7_NOCERTS 0x2 | ||
| 257 | #define PKCS7_NOSIGS 0x4 | ||
| 258 | #define PKCS7_NOCHAIN 0x8 | ||
| 259 | #define PKCS7_NOINTERN 0x10 | ||
| 260 | #define PKCS7_NOVERIFY 0x20 | ||
| 261 | #define PKCS7_DETACHED 0x40 | ||
| 262 | #define PKCS7_BINARY 0x80 | ||
| 263 | #define PKCS7_NOATTR 0x100 | ||
| 264 | #define PKCS7_NOSMIMECAP 0x200 | ||
| 265 | #define PKCS7_NOOLDMIMETYPE 0x400 | ||
| 266 | #define PKCS7_CRLFEOL 0x800 | ||
| 267 | #define PKCS7_STREAM 0x1000 | ||
| 268 | #define PKCS7_NOCRL 0x2000 | ||
| 269 | |||
| 270 | /* Flags: for compatibility with older code */ | ||
| 271 | |||
| 272 | #define SMIME_TEXT PKCS7_TEXT | ||
| 273 | #define SMIME_NOCERTS PKCS7_NOCERTS | ||
| 274 | #define SMIME_NOSIGS PKCS7_NOSIGS | ||
| 275 | #define SMIME_NOCHAIN PKCS7_NOCHAIN | ||
| 276 | #define SMIME_NOINTERN PKCS7_NOINTERN | ||
| 277 | #define SMIME_NOVERIFY PKCS7_NOVERIFY | ||
| 278 | #define SMIME_DETACHED PKCS7_DETACHED | ||
| 279 | #define SMIME_BINARY PKCS7_BINARY | ||
| 280 | #define SMIME_NOATTR PKCS7_NOATTR | ||
| 281 | |||
| 282 | DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) | ||
| 283 | |||
| 284 | #ifndef SSLEAY_MACROS | ||
| 285 | int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,const EVP_MD *type, | ||
| 286 | unsigned char *md,unsigned int *len); | ||
| 287 | #ifndef OPENSSL_NO_FP_API | ||
| 288 | PKCS7 *d2i_PKCS7_fp(FILE *fp,PKCS7 **p7); | ||
| 289 | int i2d_PKCS7_fp(FILE *fp,PKCS7 *p7); | ||
| 290 | #endif | ||
| 291 | PKCS7 *PKCS7_dup(PKCS7 *p7); | ||
| 292 | PKCS7 *d2i_PKCS7_bio(BIO *bp,PKCS7 **p7); | ||
| 293 | int i2d_PKCS7_bio(BIO *bp,PKCS7 *p7); | ||
| 294 | #endif | ||
| 295 | |||
| 296 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) | ||
| 297 | DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) | ||
| 298 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED) | ||
| 299 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) | ||
| 300 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE) | ||
| 301 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) | ||
| 302 | DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST) | ||
| 303 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT) | ||
| 304 | DECLARE_ASN1_FUNCTIONS(PKCS7) | ||
| 305 | |||
| 306 | DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN) | ||
| 307 | DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY) | ||
| 308 | |||
| 309 | DECLARE_ASN1_NDEF_FUNCTION(PKCS7) | ||
| 310 | |||
| 311 | long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg); | ||
| 312 | |||
| 313 | int PKCS7_set_type(PKCS7 *p7, int type); | ||
| 314 | int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other); | ||
| 315 | int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data); | ||
| 316 | int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, | ||
| 317 | const EVP_MD *dgst); | ||
| 318 | int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i); | ||
| 319 | int PKCS7_add_certificate(PKCS7 *p7, X509 *x509); | ||
| 320 | int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509); | ||
| 321 | int PKCS7_content_new(PKCS7 *p7, int nid); | ||
| 322 | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, | ||
| 323 | BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si); | ||
| 324 | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, | ||
| 325 | X509 *x509); | ||
| 326 | |||
| 327 | BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio); | ||
| 328 | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio); | ||
| 329 | BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert); | ||
| 330 | |||
| 331 | |||
| 332 | PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, | ||
| 333 | EVP_PKEY *pkey, const EVP_MD *dgst); | ||
| 334 | X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si); | ||
| 335 | int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md); | ||
| 336 | STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); | ||
| 337 | |||
| 338 | PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509); | ||
| 339 | int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri); | ||
| 340 | int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509); | ||
| 341 | int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher); | ||
| 342 | |||
| 343 | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx); | ||
| 344 | ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 345 | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si,int nid,int type, | ||
| 346 | void *data); | ||
| 347 | int PKCS7_add_attribute (PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 348 | void *value); | ||
| 349 | ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid); | ||
| 350 | ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid); | ||
| 351 | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, | ||
| 352 | STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 353 | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 354 | |||
| 355 | |||
| 356 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | ||
| 357 | BIO *data, int flags); | ||
| 358 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | ||
| 359 | BIO *indata, BIO *out, int flags); | ||
| 360 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags); | ||
| 361 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | ||
| 362 | int flags); | ||
| 363 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags); | ||
| 364 | |||
| 365 | int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, | ||
| 366 | STACK_OF(X509_ALGOR) *cap); | ||
| 367 | STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si); | ||
| 368 | int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg); | ||
| 369 | |||
| 370 | int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags); | ||
| 371 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont); | ||
| 372 | int SMIME_crlf_copy(BIO *in, BIO *out, int flags); | ||
| 373 | int SMIME_text(BIO *in, BIO *out); | ||
| 374 | |||
| 375 | /* BEGIN ERROR CODES */ | ||
| 376 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 377 | * made after this point may be overwritten when the script is next run. | ||
| 378 | */ | ||
| 379 | void ERR_load_PKCS7_strings(void); | ||
| 380 | |||
| 381 | /* Error codes for the PKCS7 functions. */ | ||
| 382 | |||
| 383 | /* Function codes. */ | ||
| 384 | #define PKCS7_F_B64_READ_PKCS7 120 | ||
| 385 | #define PKCS7_F_B64_WRITE_PKCS7 121 | ||
| 386 | #define PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP 118 | ||
| 387 | #define PKCS7_F_PKCS7_ADD_CERTIFICATE 100 | ||
| 388 | #define PKCS7_F_PKCS7_ADD_CRL 101 | ||
| 389 | #define PKCS7_F_PKCS7_ADD_RECIPIENT_INFO 102 | ||
| 390 | #define PKCS7_F_PKCS7_ADD_SIGNER 103 | ||
| 391 | #define PKCS7_F_PKCS7_BIO_ADD_DIGEST 125 | ||
| 392 | #define PKCS7_F_PKCS7_CTRL 104 | ||
| 393 | #define PKCS7_F_PKCS7_DATADECODE 112 | ||
| 394 | #define PKCS7_F_PKCS7_DATAFINAL 128 | ||
| 395 | #define PKCS7_F_PKCS7_DATAINIT 105 | ||
| 396 | #define PKCS7_F_PKCS7_DATASIGN 106 | ||
| 397 | #define PKCS7_F_PKCS7_DATAVERIFY 107 | ||
| 398 | #define PKCS7_F_PKCS7_DECRYPT 114 | ||
| 399 | #define PKCS7_F_PKCS7_ENCRYPT 115 | ||
| 400 | #define PKCS7_F_PKCS7_FIND_DIGEST 127 | ||
| 401 | #define PKCS7_F_PKCS7_GET0_SIGNERS 124 | ||
| 402 | #define PKCS7_F_PKCS7_SET_CIPHER 108 | ||
| 403 | #define PKCS7_F_PKCS7_SET_CONTENT 109 | ||
| 404 | #define PKCS7_F_PKCS7_SET_DIGEST 126 | ||
| 405 | #define PKCS7_F_PKCS7_SET_TYPE 110 | ||
| 406 | #define PKCS7_F_PKCS7_SIGN 116 | ||
| 407 | #define PKCS7_F_PKCS7_SIGNATUREVERIFY 113 | ||
| 408 | #define PKCS7_F_PKCS7_SIMPLE_SMIMECAP 119 | ||
| 409 | #define PKCS7_F_PKCS7_VERIFY 117 | ||
| 410 | #define PKCS7_F_SMIME_READ_PKCS7 122 | ||
| 411 | #define PKCS7_F_SMIME_TEXT 123 | ||
| 412 | |||
| 413 | /* Reason codes. */ | ||
| 414 | #define PKCS7_R_CERTIFICATE_VERIFY_ERROR 117 | ||
| 415 | #define PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 144 | ||
| 416 | #define PKCS7_R_CIPHER_NOT_INITIALIZED 116 | ||
| 417 | #define PKCS7_R_CONTENT_AND_DATA_PRESENT 118 | ||
| 418 | #define PKCS7_R_DECODE_ERROR 130 | ||
| 419 | #define PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH 100 | ||
| 420 | #define PKCS7_R_DECRYPT_ERROR 119 | ||
| 421 | #define PKCS7_R_DIGEST_FAILURE 101 | ||
| 422 | #define PKCS7_R_ERROR_ADDING_RECIPIENT 120 | ||
| 423 | #define PKCS7_R_ERROR_SETTING_CIPHER 121 | ||
| 424 | #define PKCS7_R_INVALID_MIME_TYPE 131 | ||
| 425 | #define PKCS7_R_INVALID_NULL_POINTER 143 | ||
| 426 | #define PKCS7_R_MIME_NO_CONTENT_TYPE 132 | ||
| 427 | #define PKCS7_R_MIME_PARSE_ERROR 133 | ||
| 428 | #define PKCS7_R_MIME_SIG_PARSE_ERROR 134 | ||
| 429 | #define PKCS7_R_MISSING_CERIPEND_INFO 103 | ||
| 430 | #define PKCS7_R_NO_CONTENT 122 | ||
| 431 | #define PKCS7_R_NO_CONTENT_TYPE 135 | ||
| 432 | #define PKCS7_R_NO_MULTIPART_BODY_FAILURE 136 | ||
| 433 | #define PKCS7_R_NO_MULTIPART_BOUNDARY 137 | ||
| 434 | #define PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE 115 | ||
| 435 | #define PKCS7_R_NO_RECIPIENT_MATCHES_KEY 146 | ||
| 436 | #define PKCS7_R_NO_SIGNATURES_ON_DATA 123 | ||
| 437 | #define PKCS7_R_NO_SIGNERS 142 | ||
| 438 | #define PKCS7_R_NO_SIG_CONTENT_TYPE 138 | ||
| 439 | #define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE 104 | ||
| 440 | #define PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR 124 | ||
| 441 | #define PKCS7_R_PKCS7_DATAFINAL 126 | ||
| 442 | #define PKCS7_R_PKCS7_DATAFINAL_ERROR 125 | ||
| 443 | #define PKCS7_R_PKCS7_DATASIGN 145 | ||
| 444 | #define PKCS7_R_PKCS7_PARSE_ERROR 139 | ||
| 445 | #define PKCS7_R_PKCS7_SIG_PARSE_ERROR 140 | ||
| 446 | #define PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 127 | ||
| 447 | #define PKCS7_R_SIGNATURE_FAILURE 105 | ||
| 448 | #define PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND 128 | ||
| 449 | #define PKCS7_R_SIG_INVALID_MIME_TYPE 141 | ||
| 450 | #define PKCS7_R_SMIME_TEXT_ERROR 129 | ||
| 451 | #define PKCS7_R_UNABLE_TO_FIND_CERTIFICATE 106 | ||
| 452 | #define PKCS7_R_UNABLE_TO_FIND_MEM_BIO 107 | ||
| 453 | #define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST 108 | ||
| 454 | #define PKCS7_R_UNKNOWN_DIGEST_TYPE 109 | ||
| 455 | #define PKCS7_R_UNKNOWN_OPERATION 110 | ||
| 456 | #define PKCS7_R_UNSUPPORTED_CIPHER_TYPE 111 | ||
| 457 | #define PKCS7_R_UNSUPPORTED_CONTENT_TYPE 112 | ||
| 458 | #define PKCS7_R_WRONG_CONTENT_TYPE 113 | ||
| 459 | #define PKCS7_R_WRONG_PKCS7_TYPE 114 | ||
| 460 | |||
| 461 | #ifdef __cplusplus | ||
| 462 | } | ||
| 463 | #endif | ||
| 464 | #endif | ||
diff --git a/src/lib/libcrypto/pkcs7/pkcs7err.c b/src/lib/libcrypto/pkcs7/pkcs7err.c new file mode 100644 index 0000000000..c0e3d4cd33 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pkcs7err.c | |||
| @@ -0,0 +1,167 @@ | |||
| 1 | /* crypto/pkcs7/pkcs7err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999-2005 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 | |||
| 68 | #define ERR_FUNC(func) ERR_PACK(ERR_LIB_PKCS7,func,0) | ||
| 69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_PKCS7,0,reason) | ||
| 70 | |||
| 71 | static ERR_STRING_DATA PKCS7_str_functs[]= | ||
| 72 | { | ||
| 73 | {ERR_FUNC(PKCS7_F_B64_READ_PKCS7), "B64_READ_PKCS7"}, | ||
| 74 | {ERR_FUNC(PKCS7_F_B64_WRITE_PKCS7), "B64_WRITE_PKCS7"}, | ||
| 75 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP), "PKCS7_add_attrib_smimecap"}, | ||
| 76 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_CERTIFICATE), "PKCS7_add_certificate"}, | ||
| 77 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_CRL), "PKCS7_add_crl"}, | ||
| 78 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO), "PKCS7_add_recipient_info"}, | ||
| 79 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_SIGNER), "PKCS7_add_signer"}, | ||
| 80 | {ERR_FUNC(PKCS7_F_PKCS7_BIO_ADD_DIGEST), "PKCS7_BIO_ADD_DIGEST"}, | ||
| 81 | {ERR_FUNC(PKCS7_F_PKCS7_CTRL), "PKCS7_ctrl"}, | ||
| 82 | {ERR_FUNC(PKCS7_F_PKCS7_DATADECODE), "PKCS7_dataDecode"}, | ||
| 83 | {ERR_FUNC(PKCS7_F_PKCS7_DATAFINAL), "PKCS7_dataFinal"}, | ||
| 84 | {ERR_FUNC(PKCS7_F_PKCS7_DATAINIT), "PKCS7_dataInit"}, | ||
| 85 | {ERR_FUNC(PKCS7_F_PKCS7_DATASIGN), "PKCS7_DATASIGN"}, | ||
| 86 | {ERR_FUNC(PKCS7_F_PKCS7_DATAVERIFY), "PKCS7_dataVerify"}, | ||
| 87 | {ERR_FUNC(PKCS7_F_PKCS7_DECRYPT), "PKCS7_decrypt"}, | ||
| 88 | {ERR_FUNC(PKCS7_F_PKCS7_ENCRYPT), "PKCS7_encrypt"}, | ||
| 89 | {ERR_FUNC(PKCS7_F_PKCS7_FIND_DIGEST), "PKCS7_FIND_DIGEST"}, | ||
| 90 | {ERR_FUNC(PKCS7_F_PKCS7_GET0_SIGNERS), "PKCS7_get0_signers"}, | ||
| 91 | {ERR_FUNC(PKCS7_F_PKCS7_SET_CIPHER), "PKCS7_set_cipher"}, | ||
| 92 | {ERR_FUNC(PKCS7_F_PKCS7_SET_CONTENT), "PKCS7_set_content"}, | ||
| 93 | {ERR_FUNC(PKCS7_F_PKCS7_SET_DIGEST), "PKCS7_set_digest"}, | ||
| 94 | {ERR_FUNC(PKCS7_F_PKCS7_SET_TYPE), "PKCS7_set_type"}, | ||
| 95 | {ERR_FUNC(PKCS7_F_PKCS7_SIGN), "PKCS7_sign"}, | ||
| 96 | {ERR_FUNC(PKCS7_F_PKCS7_SIGNATUREVERIFY), "PKCS7_signatureVerify"}, | ||
| 97 | {ERR_FUNC(PKCS7_F_PKCS7_SIMPLE_SMIMECAP), "PKCS7_simple_smimecap"}, | ||
| 98 | {ERR_FUNC(PKCS7_F_PKCS7_VERIFY), "PKCS7_verify"}, | ||
| 99 | {ERR_FUNC(PKCS7_F_SMIME_READ_PKCS7), "SMIME_read_PKCS7"}, | ||
| 100 | {ERR_FUNC(PKCS7_F_SMIME_TEXT), "SMIME_text"}, | ||
| 101 | {0,NULL} | ||
| 102 | }; | ||
| 103 | |||
| 104 | static ERR_STRING_DATA PKCS7_str_reasons[]= | ||
| 105 | { | ||
| 106 | {ERR_REASON(PKCS7_R_CERTIFICATE_VERIFY_ERROR),"certificate verify error"}, | ||
| 107 | {ERR_REASON(PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER),"cipher has no object identifier"}, | ||
| 108 | {ERR_REASON(PKCS7_R_CIPHER_NOT_INITIALIZED),"cipher not initialized"}, | ||
| 109 | {ERR_REASON(PKCS7_R_CONTENT_AND_DATA_PRESENT),"content and data present"}, | ||
| 110 | {ERR_REASON(PKCS7_R_DECODE_ERROR) ,"decode error"}, | ||
| 111 | {ERR_REASON(PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH),"decrypted key is wrong length"}, | ||
| 112 | {ERR_REASON(PKCS7_R_DECRYPT_ERROR) ,"decrypt error"}, | ||
| 113 | {ERR_REASON(PKCS7_R_DIGEST_FAILURE) ,"digest failure"}, | ||
| 114 | {ERR_REASON(PKCS7_R_ERROR_ADDING_RECIPIENT),"error adding recipient"}, | ||
| 115 | {ERR_REASON(PKCS7_R_ERROR_SETTING_CIPHER),"error setting cipher"}, | ||
| 116 | {ERR_REASON(PKCS7_R_INVALID_MIME_TYPE) ,"invalid mime type"}, | ||
| 117 | {ERR_REASON(PKCS7_R_INVALID_NULL_POINTER),"invalid null pointer"}, | ||
| 118 | {ERR_REASON(PKCS7_R_MIME_NO_CONTENT_TYPE),"mime no content type"}, | ||
| 119 | {ERR_REASON(PKCS7_R_MIME_PARSE_ERROR) ,"mime parse error"}, | ||
| 120 | {ERR_REASON(PKCS7_R_MIME_SIG_PARSE_ERROR),"mime sig parse error"}, | ||
| 121 | {ERR_REASON(PKCS7_R_MISSING_CERIPEND_INFO),"missing ceripend info"}, | ||
| 122 | {ERR_REASON(PKCS7_R_NO_CONTENT) ,"no content"}, | ||
| 123 | {ERR_REASON(PKCS7_R_NO_CONTENT_TYPE) ,"no content type"}, | ||
| 124 | {ERR_REASON(PKCS7_R_NO_MULTIPART_BODY_FAILURE),"no multipart body failure"}, | ||
| 125 | {ERR_REASON(PKCS7_R_NO_MULTIPART_BOUNDARY),"no multipart boundary"}, | ||
| 126 | {ERR_REASON(PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE),"no recipient matches certificate"}, | ||
| 127 | {ERR_REASON(PKCS7_R_NO_RECIPIENT_MATCHES_KEY),"no recipient matches key"}, | ||
| 128 | {ERR_REASON(PKCS7_R_NO_SIGNATURES_ON_DATA),"no signatures on data"}, | ||
| 129 | {ERR_REASON(PKCS7_R_NO_SIGNERS) ,"no signers"}, | ||
| 130 | {ERR_REASON(PKCS7_R_NO_SIG_CONTENT_TYPE) ,"no sig content type"}, | ||
| 131 | {ERR_REASON(PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE),"operation not supported on this type"}, | ||
| 132 | {ERR_REASON(PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR),"pkcs7 add signature error"}, | ||
| 133 | {ERR_REASON(PKCS7_R_PKCS7_DATAFINAL) ,"pkcs7 datafinal"}, | ||
| 134 | {ERR_REASON(PKCS7_R_PKCS7_DATAFINAL_ERROR),"pkcs7 datafinal error"}, | ||
| 135 | {ERR_REASON(PKCS7_R_PKCS7_DATASIGN) ,"pkcs7 datasign"}, | ||
| 136 | {ERR_REASON(PKCS7_R_PKCS7_PARSE_ERROR) ,"pkcs7 parse error"}, | ||
| 137 | {ERR_REASON(PKCS7_R_PKCS7_SIG_PARSE_ERROR),"pkcs7 sig parse error"}, | ||
| 138 | {ERR_REASON(PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE),"private key does not match certificate"}, | ||
| 139 | {ERR_REASON(PKCS7_R_SIGNATURE_FAILURE) ,"signature failure"}, | ||
| 140 | {ERR_REASON(PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND),"signer certificate not found"}, | ||
| 141 | {ERR_REASON(PKCS7_R_SIG_INVALID_MIME_TYPE),"sig invalid mime type"}, | ||
| 142 | {ERR_REASON(PKCS7_R_SMIME_TEXT_ERROR) ,"smime text error"}, | ||
| 143 | {ERR_REASON(PKCS7_R_UNABLE_TO_FIND_CERTIFICATE),"unable to find certificate"}, | ||
| 144 | {ERR_REASON(PKCS7_R_UNABLE_TO_FIND_MEM_BIO),"unable to find mem bio"}, | ||
| 145 | {ERR_REASON(PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST),"unable to find message digest"}, | ||
| 146 | {ERR_REASON(PKCS7_R_UNKNOWN_DIGEST_TYPE) ,"unknown digest type"}, | ||
| 147 | {ERR_REASON(PKCS7_R_UNKNOWN_OPERATION) ,"unknown operation"}, | ||
| 148 | {ERR_REASON(PKCS7_R_UNSUPPORTED_CIPHER_TYPE),"unsupported cipher type"}, | ||
| 149 | {ERR_REASON(PKCS7_R_UNSUPPORTED_CONTENT_TYPE),"unsupported content type"}, | ||
| 150 | {ERR_REASON(PKCS7_R_WRONG_CONTENT_TYPE) ,"wrong content type"}, | ||
| 151 | {ERR_REASON(PKCS7_R_WRONG_PKCS7_TYPE) ,"wrong pkcs7 type"}, | ||
| 152 | {0,NULL} | ||
| 153 | }; | ||
| 154 | |||
| 155 | #endif | ||
| 156 | |||
| 157 | void ERR_load_PKCS7_strings(void) | ||
| 158 | { | ||
| 159 | #ifndef OPENSSL_NO_ERR | ||
| 160 | |||
| 161 | if (ERR_func_error_string(PKCS7_str_functs[0].error) == NULL) | ||
| 162 | { | ||
| 163 | ERR_load_strings(0,PKCS7_str_functs); | ||
| 164 | ERR_load_strings(0,PKCS7_str_reasons); | ||
| 165 | } | ||
| 166 | #endif | ||
| 167 | } | ||
