diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs7')
| -rw-r--r-- | src/lib/libcrypto/pkcs7/bio_pk7.c | 69 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_asn1.c | 247 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_attr.c | 165 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_doit.c | 1248 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_lib.c | 665 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_mime.c | 97 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_smime.c | 587 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pkcs7.h | 499 | ||||
| -rw-r--r-- | src/lib/libcrypto/pkcs7/pkcs7err.c | 187 |
9 files changed, 3764 insertions, 0 deletions
diff --git a/src/lib/libcrypto/pkcs7/bio_pk7.c b/src/lib/libcrypto/pkcs7/bio_pk7.c new file mode 100644 index 0000000000..c8d06d6cdc --- /dev/null +++ b/src/lib/libcrypto/pkcs7/bio_pk7.c | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | /* bio_pk7.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | ||
| 3 | * project. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2008 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 | */ | ||
| 54 | |||
| 55 | #include <openssl/asn1.h> | ||
| 56 | #include <openssl/pkcs7.h> | ||
| 57 | #include <openssl/bio.h> | ||
| 58 | |||
| 59 | #ifndef OPENSSL_SYSNAME_NETWARE | ||
| 60 | #include <memory.h> | ||
| 61 | #endif | ||
| 62 | #include <stdio.h> | ||
| 63 | |||
| 64 | /* Streaming encode support for PKCS#7 */ | ||
| 65 | |||
| 66 | BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7) | ||
| 67 | { | ||
| 68 | return BIO_new_NDEF(out, (ASN1_VALUE *)p7, ASN1_ITEM_rptr(PKCS7)); | ||
| 69 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_asn1.c b/src/lib/libcrypto/pkcs7/pk7_asn1.c new file mode 100644 index 0000000000..b7ec2883cb --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_asn1.c | |||
| @@ -0,0 +1,247 @@ | |||
| 1 | /* pk7_asn.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) 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 | /* PKCS#7 streaming support */ | ||
| 81 | static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | ||
| 82 | void *exarg) | ||
| 83 | { | ||
| 84 | ASN1_STREAM_ARG *sarg = exarg; | ||
| 85 | PKCS7 **pp7 = (PKCS7 **)pval; | ||
| 86 | |||
| 87 | switch(operation) | ||
| 88 | { | ||
| 89 | |||
| 90 | case ASN1_OP_STREAM_PRE: | ||
| 91 | if (PKCS7_stream(&sarg->boundary, *pp7) <= 0) | ||
| 92 | return 0; | ||
| 93 | case ASN1_OP_DETACHED_PRE: | ||
| 94 | sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out); | ||
| 95 | if (!sarg->ndef_bio) | ||
| 96 | return 0; | ||
| 97 | break; | ||
| 98 | |||
| 99 | case ASN1_OP_STREAM_POST: | ||
| 100 | case ASN1_OP_DETACHED_POST: | ||
| 101 | if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0) | ||
| 102 | return 0; | ||
| 103 | break; | ||
| 104 | |||
| 105 | } | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | ASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = { | ||
| 110 | ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT), | ||
| 111 | ASN1_ADB_OBJECT(PKCS7) | ||
| 112 | }ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7) | ||
| 113 | |||
| 114 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7) | ||
| 115 | IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7) | ||
| 116 | IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7) | ||
| 117 | |||
| 118 | ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = { | ||
| 119 | ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER), | ||
| 120 | ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR), | ||
| 121 | ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7), | ||
| 122 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0), | ||
| 123 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1), | ||
| 124 | ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO) | ||
| 125 | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED) | ||
| 126 | |||
| 127 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED) | ||
| 128 | |||
| 129 | /* Minor tweak to operation: free up EVP_PKEY */ | ||
| 130 | static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | ||
| 131 | void *exarg) | ||
| 132 | { | ||
| 133 | if(operation == ASN1_OP_FREE_POST) { | ||
| 134 | PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval; | ||
| 135 | EVP_PKEY_free(si->pkey); | ||
| 136 | } | ||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = { | ||
| 141 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER), | ||
| 142 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | ||
| 143 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR), | ||
| 144 | /* NB this should be a SET OF but we use a SEQUENCE OF so the | ||
| 145 | * original order * is retained when the structure is reencoded. | ||
| 146 | * Since the attributes are implicitly tagged this will not affect | ||
| 147 | * the encoding. | ||
| 148 | */ | ||
| 149 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0), | ||
| 150 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR), | ||
| 151 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING), | ||
| 152 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1) | ||
| 153 | } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO) | ||
| 154 | |||
| 155 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) | ||
| 156 | |||
| 157 | ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = { | ||
| 158 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME), | ||
| 159 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER) | ||
| 160 | } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL) | ||
| 161 | |||
| 162 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) | ||
| 163 | |||
| 164 | ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = { | ||
| 165 | ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER), | ||
| 166 | ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | ||
| 167 | ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT) | ||
| 168 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE) | ||
| 169 | |||
| 170 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE) | ||
| 171 | |||
| 172 | /* Minor tweak to operation: free up X509 */ | ||
| 173 | static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | ||
| 174 | void *exarg) | ||
| 175 | { | ||
| 176 | if(operation == ASN1_OP_FREE_POST) { | ||
| 177 | PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval; | ||
| 178 | X509_free(ri->cert); | ||
| 179 | } | ||
| 180 | return 1; | ||
| 181 | } | ||
| 182 | |||
| 183 | ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = { | ||
| 184 | ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER), | ||
| 185 | ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | ||
| 186 | ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR), | ||
| 187 | ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING) | ||
| 188 | } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO) | ||
| 189 | |||
| 190 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) | ||
| 191 | |||
| 192 | ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = { | ||
| 193 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT), | ||
| 194 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR), | ||
| 195 | ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0) | ||
| 196 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT) | ||
| 197 | |||
| 198 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) | ||
| 199 | |||
| 200 | ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = { | ||
| 201 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER), | ||
| 202 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | ||
| 203 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR), | ||
| 204 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT), | ||
| 205 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0), | ||
| 206 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1), | ||
| 207 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO) | ||
| 208 | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE) | ||
| 209 | |||
| 210 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) | ||
| 211 | |||
| 212 | ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = { | ||
| 213 | ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER), | ||
| 214 | ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT) | ||
| 215 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT) | ||
| 216 | |||
| 217 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT) | ||
| 218 | |||
| 219 | ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = { | ||
| 220 | ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER), | ||
| 221 | ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR), | ||
| 222 | ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7), | ||
| 223 | ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING) | ||
| 224 | } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST) | ||
| 225 | |||
| 226 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST) | ||
| 227 | |||
| 228 | /* Specials for authenticated attributes */ | ||
| 229 | |||
| 230 | /* When signing attributes we want to reorder them to match the sorted | ||
| 231 | * encoding. | ||
| 232 | */ | ||
| 233 | |||
| 234 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) = | ||
| 235 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | ||
| 236 | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN) | ||
| 237 | |||
| 238 | /* When verifying attributes we need to use the received order. So | ||
| 239 | * we use SEQUENCE OF and tag it to SET OF | ||
| 240 | */ | ||
| 241 | |||
| 242 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) = | ||
| 243 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, | ||
| 244 | V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | ||
| 245 | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY) | ||
| 246 | |||
| 247 | IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7) | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_attr.c b/src/lib/libcrypto/pkcs7/pk7_attr.c new file mode 100644 index 0000000000..a97db51210 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_attr.c | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | /* pk7_attr.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) 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/asn1t.h> | ||
| 64 | #include <openssl/pem.h> | ||
| 65 | #include <openssl/pkcs7.h> | ||
| 66 | #include <openssl/x509.h> | ||
| 67 | #include <openssl/err.h> | ||
| 68 | |||
| 69 | int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, STACK_OF(X509_ALGOR) *cap) | ||
| 70 | { | ||
| 71 | ASN1_STRING *seq; | ||
| 72 | if(!(seq = ASN1_STRING_new())) { | ||
| 73 | PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | seq->length = ASN1_item_i2d((ASN1_VALUE *)cap,&seq->data, | ||
| 77 | ASN1_ITEM_rptr(X509_ALGORS)); | ||
| 78 | return PKCS7_add_signed_attribute(si, NID_SMIMECapabilities, | ||
| 79 | V_ASN1_SEQUENCE, seq); | ||
| 80 | } | ||
| 81 | |||
| 82 | STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si) | ||
| 83 | { | ||
| 84 | ASN1_TYPE *cap; | ||
| 85 | const unsigned char *p; | ||
| 86 | |||
| 87 | cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities); | ||
| 88 | if (!cap || (cap->type != V_ASN1_SEQUENCE)) | ||
| 89 | return NULL; | ||
| 90 | p = cap->value.sequence->data; | ||
| 91 | return (STACK_OF(X509_ALGOR) *) | ||
| 92 | ASN1_item_d2i(NULL, &p, cap->value.sequence->length, | ||
| 93 | ASN1_ITEM_rptr(X509_ALGORS)); | ||
| 94 | } | ||
| 95 | |||
| 96 | /* Basic smime-capabilities OID and optional integer arg */ | ||
| 97 | int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) | ||
| 98 | { | ||
| 99 | X509_ALGOR *alg; | ||
| 100 | |||
| 101 | if(!(alg = X509_ALGOR_new())) { | ||
| 102 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 103 | return 0; | ||
| 104 | } | ||
| 105 | ASN1_OBJECT_free(alg->algorithm); | ||
| 106 | alg->algorithm = OBJ_nid2obj (nid); | ||
| 107 | if (arg > 0) { | ||
| 108 | ASN1_INTEGER *nbit; | ||
| 109 | if(!(alg->parameter = ASN1_TYPE_new())) { | ||
| 110 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | if(!(nbit = ASN1_INTEGER_new())) { | ||
| 114 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | if(!ASN1_INTEGER_set (nbit, arg)) { | ||
| 118 | PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE); | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | alg->parameter->value.integer = nbit; | ||
| 122 | alg->parameter->type = V_ASN1_INTEGER; | ||
| 123 | } | ||
| 124 | sk_X509_ALGOR_push (sk, alg); | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid) | ||
| 129 | { | ||
| 130 | if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType)) | ||
| 131 | return 0; | ||
| 132 | if (!coid) | ||
| 133 | coid = OBJ_nid2obj(NID_pkcs7_data); | ||
| 134 | return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, | ||
| 135 | V_ASN1_OBJECT, coid); | ||
| 136 | } | ||
| 137 | |||
| 138 | int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t) | ||
| 139 | { | ||
| 140 | if (!t && !(t=X509_gmtime_adj(NULL,0))) | ||
| 141 | { | ||
| 142 | PKCS7err(PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME, | ||
| 143 | ERR_R_MALLOC_FAILURE); | ||
| 144 | return 0; | ||
| 145 | } | ||
| 146 | return PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime, | ||
| 147 | V_ASN1_UTCTIME, t); | ||
| 148 | } | ||
| 149 | |||
| 150 | int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si, | ||
| 151 | const unsigned char *md, int mdlen) | ||
| 152 | { | ||
| 153 | ASN1_OCTET_STRING *os; | ||
| 154 | os = ASN1_OCTET_STRING_new(); | ||
| 155 | if (!os) | ||
| 156 | return 0; | ||
| 157 | if (!ASN1_STRING_set(os, md, mdlen) | ||
| 158 | || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest, | ||
| 159 | V_ASN1_OCTET_STRING, os)) | ||
| 160 | { | ||
| 161 | ASN1_OCTET_STRING_free(os); | ||
| 162 | return 0; | ||
| 163 | } | ||
| 164 | return 1; | ||
| 165 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_doit.c b/src/lib/libcrypto/pkcs7/pk7_doit.c new file mode 100644 index 0000000000..3bf1a367bb --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_doit.c | |||
| @@ -0,0 +1,1248 @@ | |||
| 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 | static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri, | ||
| 142 | unsigned char *key, int keylen) | ||
| 143 | { | ||
| 144 | EVP_PKEY_CTX *pctx = NULL; | ||
| 145 | EVP_PKEY *pkey = NULL; | ||
| 146 | unsigned char *ek = NULL; | ||
| 147 | int ret = 0; | ||
| 148 | size_t eklen; | ||
| 149 | |||
| 150 | pkey = X509_get_pubkey(ri->cert); | ||
| 151 | |||
| 152 | if (!pkey) | ||
| 153 | return 0; | ||
| 154 | |||
| 155 | pctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
| 156 | if (!pctx) | ||
| 157 | return 0; | ||
| 158 | |||
| 159 | if (EVP_PKEY_encrypt_init(pctx) <= 0) | ||
| 160 | goto err; | ||
| 161 | |||
| 162 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT, | ||
| 163 | EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) | ||
| 164 | { | ||
| 165 | PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR); | ||
| 166 | goto err; | ||
| 167 | } | ||
| 168 | |||
| 169 | if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0) | ||
| 170 | goto err; | ||
| 171 | |||
| 172 | ek = OPENSSL_malloc(eklen); | ||
| 173 | |||
| 174 | if (ek == NULL) | ||
| 175 | { | ||
| 176 | PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE); | ||
| 177 | goto err; | ||
| 178 | } | ||
| 179 | |||
| 180 | if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0) | ||
| 181 | goto err; | ||
| 182 | |||
| 183 | ASN1_STRING_set0(ri->enc_key, ek, eklen); | ||
| 184 | ek = NULL; | ||
| 185 | |||
| 186 | ret = 1; | ||
| 187 | |||
| 188 | err: | ||
| 189 | if (pkey) | ||
| 190 | EVP_PKEY_free(pkey); | ||
| 191 | if (pctx) | ||
| 192 | EVP_PKEY_CTX_free(pctx); | ||
| 193 | if (ek) | ||
| 194 | OPENSSL_free(ek); | ||
| 195 | return ret; | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen, | ||
| 201 | PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey) | ||
| 202 | { | ||
| 203 | EVP_PKEY_CTX *pctx = NULL; | ||
| 204 | unsigned char *ek = NULL; | ||
| 205 | size_t eklen; | ||
| 206 | |||
| 207 | int ret = 0; | ||
| 208 | |||
| 209 | pctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
| 210 | if (!pctx) | ||
| 211 | return 0; | ||
| 212 | |||
| 213 | if (EVP_PKEY_decrypt_init(pctx) <= 0) | ||
| 214 | goto err; | ||
| 215 | |||
| 216 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT, | ||
| 217 | EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) | ||
| 218 | { | ||
| 219 | PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR); | ||
| 220 | goto err; | ||
| 221 | } | ||
| 222 | |||
| 223 | if (EVP_PKEY_decrypt(pctx, NULL, &eklen, | ||
| 224 | ri->enc_key->data, ri->enc_key->length) <= 0) | ||
| 225 | goto err; | ||
| 226 | |||
| 227 | ek = OPENSSL_malloc(eklen); | ||
| 228 | |||
| 229 | if (ek == NULL) | ||
| 230 | { | ||
| 231 | PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE); | ||
| 232 | goto err; | ||
| 233 | } | ||
| 234 | |||
| 235 | if (EVP_PKEY_decrypt(pctx, ek, &eklen, | ||
| 236 | ri->enc_key->data, ri->enc_key->length) <= 0) | ||
| 237 | { | ||
| 238 | PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB); | ||
| 239 | goto err; | ||
| 240 | } | ||
| 241 | |||
| 242 | ret = 1; | ||
| 243 | |||
| 244 | *pek = ek; | ||
| 245 | *peklen = eklen; | ||
| 246 | |||
| 247 | err: | ||
| 248 | if (pctx) | ||
| 249 | EVP_PKEY_CTX_free(pctx); | ||
| 250 | if (!ret && ek) | ||
| 251 | OPENSSL_free(ek); | ||
| 252 | |||
| 253 | return ret; | ||
| 254 | } | ||
| 255 | |||
| 256 | BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) | ||
| 257 | { | ||
| 258 | int i; | ||
| 259 | BIO *out=NULL,*btmp=NULL; | ||
| 260 | X509_ALGOR *xa = NULL; | ||
| 261 | const EVP_CIPHER *evp_cipher=NULL; | ||
| 262 | STACK_OF(X509_ALGOR) *md_sk=NULL; | ||
| 263 | STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; | ||
| 264 | X509_ALGOR *xalg=NULL; | ||
| 265 | PKCS7_RECIP_INFO *ri=NULL; | ||
| 266 | ASN1_OCTET_STRING *os=NULL; | ||
| 267 | |||
| 268 | i=OBJ_obj2nid(p7->type); | ||
| 269 | p7->state=PKCS7_S_HEADER; | ||
| 270 | |||
| 271 | switch (i) | ||
| 272 | { | ||
| 273 | case NID_pkcs7_signed: | ||
| 274 | md_sk=p7->d.sign->md_algs; | ||
| 275 | os = PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 276 | break; | ||
| 277 | case NID_pkcs7_signedAndEnveloped: | ||
| 278 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 279 | md_sk=p7->d.signed_and_enveloped->md_algs; | ||
| 280 | xalg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 281 | evp_cipher=p7->d.signed_and_enveloped->enc_data->cipher; | ||
| 282 | if (evp_cipher == NULL) | ||
| 283 | { | ||
| 284 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 285 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 286 | goto err; | ||
| 287 | } | ||
| 288 | break; | ||
| 289 | case NID_pkcs7_enveloped: | ||
| 290 | rsk=p7->d.enveloped->recipientinfo; | ||
| 291 | xalg=p7->d.enveloped->enc_data->algorithm; | ||
| 292 | evp_cipher=p7->d.enveloped->enc_data->cipher; | ||
| 293 | if (evp_cipher == NULL) | ||
| 294 | { | ||
| 295 | PKCS7err(PKCS7_F_PKCS7_DATAINIT, | ||
| 296 | PKCS7_R_CIPHER_NOT_INITIALIZED); | ||
| 297 | goto err; | ||
| 298 | } | ||
| 299 | break; | ||
| 300 | case NID_pkcs7_digest: | ||
| 301 | xa = p7->d.digest->md; | ||
| 302 | os = PKCS7_get_octet_string(p7->d.digest->contents); | ||
| 303 | break; | ||
| 304 | case NID_pkcs7_data: | ||
| 305 | break; | ||
| 306 | default: | ||
| 307 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 308 | goto err; | ||
| 309 | } | ||
| 310 | |||
| 311 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 312 | if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i))) | ||
| 313 | goto err; | ||
| 314 | |||
| 315 | if (xa && !PKCS7_bio_add_digest(&out, xa)) | ||
| 316 | goto err; | ||
| 317 | |||
| 318 | if (evp_cipher != NULL) | ||
| 319 | { | ||
| 320 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| 321 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 322 | int keylen,ivlen; | ||
| 323 | EVP_CIPHER_CTX *ctx; | ||
| 324 | |||
| 325 | if ((btmp=BIO_new(BIO_f_cipher())) == NULL) | ||
| 326 | { | ||
| 327 | PKCS7err(PKCS7_F_PKCS7_DATAINIT,ERR_R_BIO_LIB); | ||
| 328 | goto err; | ||
| 329 | } | ||
| 330 | BIO_get_cipher_ctx(btmp, &ctx); | ||
| 331 | keylen=EVP_CIPHER_key_length(evp_cipher); | ||
| 332 | ivlen=EVP_CIPHER_iv_length(evp_cipher); | ||
| 333 | xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher)); | ||
| 334 | if (ivlen > 0) | ||
| 335 | if (RAND_pseudo_bytes(iv,ivlen) <= 0) | ||
| 336 | goto err; | ||
| 337 | if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1)<=0) | ||
| 338 | goto err; | ||
| 339 | if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) | ||
| 340 | goto err; | ||
| 341 | if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0) | ||
| 342 | goto err; | ||
| 343 | |||
| 344 | if (ivlen > 0) { | ||
| 345 | if (xalg->parameter == NULL) { | ||
| 346 | xalg->parameter = ASN1_TYPE_new(); | ||
| 347 | if (xalg->parameter == NULL) | ||
| 348 | goto err; | ||
| 349 | } | ||
| 350 | if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0) | ||
| 351 | goto err; | ||
| 352 | } | ||
| 353 | |||
| 354 | /* Lets do the pub key stuff :-) */ | ||
| 355 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 356 | { | ||
| 357 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 358 | if (pkcs7_encode_rinfo(ri, key, keylen) <= 0) | ||
| 359 | goto err; | ||
| 360 | } | ||
| 361 | OPENSSL_cleanse(key, keylen); | ||
| 362 | |||
| 363 | if (out == NULL) | ||
| 364 | out=btmp; | ||
| 365 | else | ||
| 366 | BIO_push(out,btmp); | ||
| 367 | btmp=NULL; | ||
| 368 | } | ||
| 369 | |||
| 370 | if (bio == NULL) | ||
| 371 | { | ||
| 372 | if (PKCS7_is_detached(p7)) | ||
| 373 | bio=BIO_new(BIO_s_null()); | ||
| 374 | else if (os && os->length > 0) | ||
| 375 | bio = BIO_new_mem_buf(os->data, os->length); | ||
| 376 | if(bio == NULL) | ||
| 377 | { | ||
| 378 | bio=BIO_new(BIO_s_mem()); | ||
| 379 | if (bio == NULL) | ||
| 380 | goto err; | ||
| 381 | BIO_set_mem_eof_return(bio,0); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | if (out) | ||
| 385 | BIO_push(out,bio); | ||
| 386 | else | ||
| 387 | out = bio; | ||
| 388 | bio=NULL; | ||
| 389 | if (0) | ||
| 390 | { | ||
| 391 | err: | ||
| 392 | if (out != NULL) | ||
| 393 | BIO_free_all(out); | ||
| 394 | if (btmp != NULL) | ||
| 395 | BIO_free_all(btmp); | ||
| 396 | out=NULL; | ||
| 397 | } | ||
| 398 | return(out); | ||
| 399 | } | ||
| 400 | |||
| 401 | static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert) | ||
| 402 | { | ||
| 403 | int ret; | ||
| 404 | ret = X509_NAME_cmp(ri->issuer_and_serial->issuer, | ||
| 405 | pcert->cert_info->issuer); | ||
| 406 | if (ret) | ||
| 407 | return ret; | ||
| 408 | return M_ASN1_INTEGER_cmp(pcert->cert_info->serialNumber, | ||
| 409 | ri->issuer_and_serial->serial); | ||
| 410 | } | ||
| 411 | |||
| 412 | /* int */ | ||
| 413 | BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) | ||
| 414 | { | ||
| 415 | int i,j; | ||
| 416 | BIO *out=NULL,*btmp=NULL,*etmp=NULL,*bio=NULL; | ||
| 417 | X509_ALGOR *xa; | ||
| 418 | ASN1_OCTET_STRING *data_body=NULL; | ||
| 419 | const EVP_MD *evp_md; | ||
| 420 | const EVP_CIPHER *evp_cipher=NULL; | ||
| 421 | EVP_CIPHER_CTX *evp_ctx=NULL; | ||
| 422 | X509_ALGOR *enc_alg=NULL; | ||
| 423 | STACK_OF(X509_ALGOR) *md_sk=NULL; | ||
| 424 | STACK_OF(PKCS7_RECIP_INFO) *rsk=NULL; | ||
| 425 | PKCS7_RECIP_INFO *ri=NULL; | ||
| 426 | |||
| 427 | i=OBJ_obj2nid(p7->type); | ||
| 428 | p7->state=PKCS7_S_HEADER; | ||
| 429 | |||
| 430 | switch (i) | ||
| 431 | { | ||
| 432 | case NID_pkcs7_signed: | ||
| 433 | data_body=PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 434 | md_sk=p7->d.sign->md_algs; | ||
| 435 | break; | ||
| 436 | case NID_pkcs7_signedAndEnveloped: | ||
| 437 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 438 | md_sk=p7->d.signed_and_enveloped->md_algs; | ||
| 439 | data_body=p7->d.signed_and_enveloped->enc_data->enc_data; | ||
| 440 | enc_alg=p7->d.signed_and_enveloped->enc_data->algorithm; | ||
| 441 | evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm); | ||
| 442 | if (evp_cipher == NULL) | ||
| 443 | { | ||
| 444 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 445 | goto err; | ||
| 446 | } | ||
| 447 | break; | ||
| 448 | case NID_pkcs7_enveloped: | ||
| 449 | rsk=p7->d.enveloped->recipientinfo; | ||
| 450 | enc_alg=p7->d.enveloped->enc_data->algorithm; | ||
| 451 | data_body=p7->d.enveloped->enc_data->enc_data; | ||
| 452 | evp_cipher=EVP_get_cipherbyobj(enc_alg->algorithm); | ||
| 453 | if (evp_cipher == NULL) | ||
| 454 | { | ||
| 455 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CIPHER_TYPE); | ||
| 456 | goto err; | ||
| 457 | } | ||
| 458 | break; | ||
| 459 | default: | ||
| 460 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 461 | goto err; | ||
| 462 | } | ||
| 463 | |||
| 464 | /* We will be checking the signature */ | ||
| 465 | if (md_sk != NULL) | ||
| 466 | { | ||
| 467 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 468 | { | ||
| 469 | xa=sk_X509_ALGOR_value(md_sk,i); | ||
| 470 | if ((btmp=BIO_new(BIO_f_md())) == NULL) | ||
| 471 | { | ||
| 472 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); | ||
| 473 | goto err; | ||
| 474 | } | ||
| 475 | |||
| 476 | j=OBJ_obj2nid(xa->algorithm); | ||
| 477 | evp_md=EVP_get_digestbynid(j); | ||
| 478 | if (evp_md == NULL) | ||
| 479 | { | ||
| 480 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,PKCS7_R_UNKNOWN_DIGEST_TYPE); | ||
| 481 | goto err; | ||
| 482 | } | ||
| 483 | |||
| 484 | BIO_set_md(btmp,evp_md); | ||
| 485 | if (out == NULL) | ||
| 486 | out=btmp; | ||
| 487 | else | ||
| 488 | BIO_push(out,btmp); | ||
| 489 | btmp=NULL; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | if (evp_cipher != NULL) | ||
| 494 | { | ||
| 495 | #if 0 | ||
| 496 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
| 497 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
| 498 | unsigned char *p; | ||
| 499 | int keylen,ivlen; | ||
| 500 | int max; | ||
| 501 | X509_OBJECT ret; | ||
| 502 | #endif | ||
| 503 | unsigned char *ek = NULL; | ||
| 504 | int eklen; | ||
| 505 | |||
| 506 | if ((etmp=BIO_new(BIO_f_cipher())) == NULL) | ||
| 507 | { | ||
| 508 | PKCS7err(PKCS7_F_PKCS7_DATADECODE,ERR_R_BIO_LIB); | ||
| 509 | goto err; | ||
| 510 | } | ||
| 511 | |||
| 512 | /* It was encrypted, we need to decrypt the secret key | ||
| 513 | * with the private key */ | ||
| 514 | |||
| 515 | /* Find the recipientInfo which matches the passed certificate | ||
| 516 | * (if any) | ||
| 517 | */ | ||
| 518 | |||
| 519 | if (pcert) | ||
| 520 | { | ||
| 521 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 522 | { | ||
| 523 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 524 | if (!pkcs7_cmp_ri(ri, pcert)) | ||
| 525 | break; | ||
| 526 | ri=NULL; | ||
| 527 | } | ||
| 528 | if (ri == NULL) | ||
| 529 | { | ||
| 530 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 531 | PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); | ||
| 532 | goto err; | ||
| 533 | } | ||
| 534 | } | ||
| 535 | |||
| 536 | /* If we haven't got a certificate try each ri in turn */ | ||
| 537 | |||
| 538 | if (pcert == NULL) | ||
| 539 | { | ||
| 540 | for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++) | ||
| 541 | { | ||
| 542 | ri=sk_PKCS7_RECIP_INFO_value(rsk,i); | ||
| 543 | if (pkcs7_decrypt_rinfo(&ek, &eklen, | ||
| 544 | ri, pkey) > 0) | ||
| 545 | break; | ||
| 546 | ERR_clear_error(); | ||
| 547 | ri = NULL; | ||
| 548 | } | ||
| 549 | if (ri == NULL) | ||
| 550 | { | ||
| 551 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 552 | PKCS7_R_NO_RECIPIENT_MATCHES_KEY); | ||
| 553 | goto err; | ||
| 554 | } | ||
| 555 | } | ||
| 556 | else | ||
| 557 | { | ||
| 558 | if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) <= 0) | ||
| 559 | goto err; | ||
| 560 | } | ||
| 561 | |||
| 562 | evp_ctx=NULL; | ||
| 563 | BIO_get_cipher_ctx(etmp,&evp_ctx); | ||
| 564 | if (EVP_CipherInit_ex(evp_ctx,evp_cipher,NULL,NULL,NULL,0) <= 0) | ||
| 565 | goto err; | ||
| 566 | if (EVP_CIPHER_asn1_to_param(evp_ctx,enc_alg->parameter) < 0) | ||
| 567 | goto err; | ||
| 568 | |||
| 569 | if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) { | ||
| 570 | /* Some S/MIME clients don't use the same key | ||
| 571 | * and effective key length. The key length is | ||
| 572 | * determined by the size of the decrypted RSA key. | ||
| 573 | */ | ||
| 574 | if(!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) | ||
| 575 | { | ||
| 576 | PKCS7err(PKCS7_F_PKCS7_DATADECODE, | ||
| 577 | PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH); | ||
| 578 | goto err; | ||
| 579 | } | ||
| 580 | } | ||
| 581 | if (EVP_CipherInit_ex(evp_ctx,NULL,NULL,ek,NULL,0) <= 0) | ||
| 582 | goto err; | ||
| 583 | |||
| 584 | if (ek) | ||
| 585 | { | ||
| 586 | OPENSSL_cleanse(ek,eklen); | ||
| 587 | OPENSSL_free(ek); | ||
| 588 | } | ||
| 589 | |||
| 590 | if (out == NULL) | ||
| 591 | out=etmp; | ||
| 592 | else | ||
| 593 | BIO_push(out,etmp); | ||
| 594 | etmp=NULL; | ||
| 595 | } | ||
| 596 | |||
| 597 | #if 1 | ||
| 598 | if (PKCS7_is_detached(p7) || (in_bio != NULL)) | ||
| 599 | { | ||
| 600 | bio=in_bio; | ||
| 601 | } | ||
| 602 | else | ||
| 603 | { | ||
| 604 | #if 0 | ||
| 605 | bio=BIO_new(BIO_s_mem()); | ||
| 606 | /* We need to set this so that when we have read all | ||
| 607 | * the data, the encrypt BIO, if present, will read | ||
| 608 | * EOF and encode the last few bytes */ | ||
| 609 | BIO_set_mem_eof_return(bio,0); | ||
| 610 | |||
| 611 | if (data_body->length > 0) | ||
| 612 | BIO_write(bio,(char *)data_body->data,data_body->length); | ||
| 613 | #else | ||
| 614 | if (data_body->length > 0) | ||
| 615 | bio = BIO_new_mem_buf(data_body->data,data_body->length); | ||
| 616 | else { | ||
| 617 | bio=BIO_new(BIO_s_mem()); | ||
| 618 | BIO_set_mem_eof_return(bio,0); | ||
| 619 | } | ||
| 620 | if (bio == NULL) | ||
| 621 | goto err; | ||
| 622 | #endif | ||
| 623 | } | ||
| 624 | BIO_push(out,bio); | ||
| 625 | bio=NULL; | ||
| 626 | #endif | ||
| 627 | if (0) | ||
| 628 | { | ||
| 629 | err: | ||
| 630 | if (out != NULL) BIO_free_all(out); | ||
| 631 | if (btmp != NULL) BIO_free_all(btmp); | ||
| 632 | if (etmp != NULL) BIO_free_all(etmp); | ||
| 633 | if (bio != NULL) BIO_free_all(bio); | ||
| 634 | out=NULL; | ||
| 635 | } | ||
| 636 | return(out); | ||
| 637 | } | ||
| 638 | |||
| 639 | static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid) | ||
| 640 | { | ||
| 641 | for (;;) | ||
| 642 | { | ||
| 643 | bio=BIO_find_type(bio,BIO_TYPE_MD); | ||
| 644 | if (bio == NULL) | ||
| 645 | { | ||
| 646 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 647 | return NULL; | ||
| 648 | } | ||
| 649 | BIO_get_md_ctx(bio,pmd); | ||
| 650 | if (*pmd == NULL) | ||
| 651 | { | ||
| 652 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,ERR_R_INTERNAL_ERROR); | ||
| 653 | return NULL; | ||
| 654 | } | ||
| 655 | if (EVP_MD_CTX_type(*pmd) == nid) | ||
| 656 | return bio; | ||
| 657 | bio=BIO_next(bio); | ||
| 658 | } | ||
| 659 | return NULL; | ||
| 660 | } | ||
| 661 | |||
| 662 | static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx) | ||
| 663 | { | ||
| 664 | unsigned char md_data[EVP_MAX_MD_SIZE]; | ||
| 665 | unsigned int md_len; | ||
| 666 | |||
| 667 | /* Add signing time if not already present */ | ||
| 668 | if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) | ||
| 669 | { | ||
| 670 | if (!PKCS7_add0_attrib_signing_time(si, NULL)) | ||
| 671 | { | ||
| 672 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, | ||
| 673 | ERR_R_MALLOC_FAILURE); | ||
| 674 | return 0; | ||
| 675 | } | ||
| 676 | } | ||
| 677 | |||
| 678 | /* Add digest */ | ||
| 679 | EVP_DigestFinal_ex(mctx, md_data,&md_len); | ||
| 680 | if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) | ||
| 681 | { | ||
| 682 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE); | ||
| 683 | return 0; | ||
| 684 | } | ||
| 685 | |||
| 686 | /* Now sign the attributes */ | ||
| 687 | if (!PKCS7_SIGNER_INFO_sign(si)) | ||
| 688 | return 0; | ||
| 689 | |||
| 690 | return 1; | ||
| 691 | } | ||
| 692 | |||
| 693 | |||
| 694 | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio) | ||
| 695 | { | ||
| 696 | int ret=0; | ||
| 697 | int i,j; | ||
| 698 | BIO *btmp; | ||
| 699 | PKCS7_SIGNER_INFO *si; | ||
| 700 | EVP_MD_CTX *mdc,ctx_tmp; | ||
| 701 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 702 | STACK_OF(PKCS7_SIGNER_INFO) *si_sk=NULL; | ||
| 703 | ASN1_OCTET_STRING *os=NULL; | ||
| 704 | |||
| 705 | EVP_MD_CTX_init(&ctx_tmp); | ||
| 706 | i=OBJ_obj2nid(p7->type); | ||
| 707 | p7->state=PKCS7_S_HEADER; | ||
| 708 | |||
| 709 | switch (i) | ||
| 710 | { | ||
| 711 | case NID_pkcs7_data: | ||
| 712 | os = p7->d.data; | ||
| 713 | break; | ||
| 714 | case NID_pkcs7_signedAndEnveloped: | ||
| 715 | /* XXXXXXXXXXXXXXXX */ | ||
| 716 | si_sk=p7->d.signed_and_enveloped->signer_info; | ||
| 717 | os = p7->d.signed_and_enveloped->enc_data->enc_data; | ||
| 718 | if (!os) | ||
| 719 | { | ||
| 720 | os=M_ASN1_OCTET_STRING_new(); | ||
| 721 | if (!os) | ||
| 722 | { | ||
| 723 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_MALLOC_FAILURE); | ||
| 724 | goto err; | ||
| 725 | } | ||
| 726 | p7->d.signed_and_enveloped->enc_data->enc_data=os; | ||
| 727 | } | ||
| 728 | break; | ||
| 729 | case NID_pkcs7_enveloped: | ||
| 730 | /* XXXXXXXXXXXXXXXX */ | ||
| 731 | os = p7->d.enveloped->enc_data->enc_data; | ||
| 732 | if (!os) | ||
| 733 | { | ||
| 734 | os=M_ASN1_OCTET_STRING_new(); | ||
| 735 | if (!os) | ||
| 736 | { | ||
| 737 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,ERR_R_MALLOC_FAILURE); | ||
| 738 | goto err; | ||
| 739 | } | ||
| 740 | p7->d.enveloped->enc_data->enc_data=os; | ||
| 741 | } | ||
| 742 | break; | ||
| 743 | case NID_pkcs7_signed: | ||
| 744 | si_sk=p7->d.sign->signer_info; | ||
| 745 | os=PKCS7_get_octet_string(p7->d.sign->contents); | ||
| 746 | /* If detached data then the content is excluded */ | ||
| 747 | if(PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { | ||
| 748 | M_ASN1_OCTET_STRING_free(os); | ||
| 749 | p7->d.sign->contents->d.data = NULL; | ||
| 750 | } | ||
| 751 | break; | ||
| 752 | |||
| 753 | case NID_pkcs7_digest: | ||
| 754 | os=PKCS7_get_octet_string(p7->d.digest->contents); | ||
| 755 | /* If detached data then the content is excluded */ | ||
| 756 | if(PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) | ||
| 757 | { | ||
| 758 | M_ASN1_OCTET_STRING_free(os); | ||
| 759 | p7->d.digest->contents->d.data = NULL; | ||
| 760 | } | ||
| 761 | break; | ||
| 762 | |||
| 763 | default: | ||
| 764 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 765 | goto err; | ||
| 766 | } | ||
| 767 | |||
| 768 | if (si_sk != NULL) | ||
| 769 | { | ||
| 770 | for (i=0; i<sk_PKCS7_SIGNER_INFO_num(si_sk); i++) | ||
| 771 | { | ||
| 772 | si=sk_PKCS7_SIGNER_INFO_value(si_sk,i); | ||
| 773 | if (si->pkey == NULL) | ||
| 774 | continue; | ||
| 775 | |||
| 776 | j = OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 777 | |||
| 778 | btmp=bio; | ||
| 779 | |||
| 780 | btmp = PKCS7_find_digest(&mdc, btmp, j); | ||
| 781 | |||
| 782 | if (btmp == NULL) | ||
| 783 | goto err; | ||
| 784 | |||
| 785 | /* We now have the EVP_MD_CTX, lets do the | ||
| 786 | * signing. */ | ||
| 787 | EVP_MD_CTX_copy_ex(&ctx_tmp,mdc); | ||
| 788 | |||
| 789 | sk=si->auth_attr; | ||
| 790 | |||
| 791 | /* If there are attributes, we add the digest | ||
| 792 | * attribute and only sign the attributes */ | ||
| 793 | if (sk_X509_ATTRIBUTE_num(sk) > 0) | ||
| 794 | { | ||
| 795 | if (!do_pkcs7_signed_attrib(si, &ctx_tmp)) | ||
| 796 | goto err; | ||
| 797 | } | ||
| 798 | else | ||
| 799 | { | ||
| 800 | unsigned char *abuf = NULL; | ||
| 801 | unsigned int abuflen; | ||
| 802 | abuflen = EVP_PKEY_size(si->pkey); | ||
| 803 | abuf = OPENSSL_malloc(abuflen); | ||
| 804 | if (!abuf) | ||
| 805 | goto err; | ||
| 806 | |||
| 807 | if (!EVP_SignFinal(&ctx_tmp, abuf, &abuflen, | ||
| 808 | si->pkey)) | ||
| 809 | { | ||
| 810 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, | ||
| 811 | ERR_R_EVP_LIB); | ||
| 812 | goto err; | ||
| 813 | } | ||
| 814 | ASN1_STRING_set0(si->enc_digest, abuf, abuflen); | ||
| 815 | } | ||
| 816 | } | ||
| 817 | } | ||
| 818 | else if (i == NID_pkcs7_digest) | ||
| 819 | { | ||
| 820 | unsigned char md_data[EVP_MAX_MD_SIZE]; | ||
| 821 | unsigned int md_len; | ||
| 822 | if (!PKCS7_find_digest(&mdc, bio, | ||
| 823 | OBJ_obj2nid(p7->d.digest->md->algorithm))) | ||
| 824 | goto err; | ||
| 825 | EVP_DigestFinal_ex(mdc,md_data,&md_len); | ||
| 826 | M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len); | ||
| 827 | } | ||
| 828 | |||
| 829 | if (!PKCS7_is_detached(p7) && !(os->flags & ASN1_STRING_FLAG_NDEF)) | ||
| 830 | { | ||
| 831 | char *cont; | ||
| 832 | long contlen; | ||
| 833 | btmp=BIO_find_type(bio,BIO_TYPE_MEM); | ||
| 834 | if (btmp == NULL) | ||
| 835 | { | ||
| 836 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL,PKCS7_R_UNABLE_TO_FIND_MEM_BIO); | ||
| 837 | goto err; | ||
| 838 | } | ||
| 839 | contlen = BIO_get_mem_data(btmp, &cont); | ||
| 840 | /* Mark the BIO read only then we can use its copy of the data | ||
| 841 | * instead of making an extra copy. | ||
| 842 | */ | ||
| 843 | BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY); | ||
| 844 | BIO_set_mem_eof_return(btmp, 0); | ||
| 845 | ASN1_STRING_set0(os, (unsigned char *)cont, contlen); | ||
| 846 | } | ||
| 847 | ret=1; | ||
| 848 | err: | ||
| 849 | EVP_MD_CTX_cleanup(&ctx_tmp); | ||
| 850 | return(ret); | ||
| 851 | } | ||
| 852 | |||
| 853 | int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si) | ||
| 854 | { | ||
| 855 | EVP_MD_CTX mctx; | ||
| 856 | EVP_PKEY_CTX *pctx; | ||
| 857 | unsigned char *abuf = NULL; | ||
| 858 | int alen; | ||
| 859 | size_t siglen; | ||
| 860 | const EVP_MD *md = NULL; | ||
| 861 | |||
| 862 | md = EVP_get_digestbyobj(si->digest_alg->algorithm); | ||
| 863 | if (md == NULL) | ||
| 864 | return 0; | ||
| 865 | |||
| 866 | EVP_MD_CTX_init(&mctx); | ||
| 867 | if (EVP_DigestSignInit(&mctx, &pctx, md,NULL, si->pkey) <= 0) | ||
| 868 | goto err; | ||
| 869 | |||
| 870 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, | ||
| 871 | EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) | ||
| 872 | { | ||
| 873 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); | ||
| 874 | goto err; | ||
| 875 | } | ||
| 876 | |||
| 877 | alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr,&abuf, | ||
| 878 | ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); | ||
| 879 | if(!abuf) | ||
| 880 | goto err; | ||
| 881 | if (EVP_DigestSignUpdate(&mctx,abuf,alen) <= 0) | ||
| 882 | goto err; | ||
| 883 | OPENSSL_free(abuf); | ||
| 884 | if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0) | ||
| 885 | goto err; | ||
| 886 | abuf = OPENSSL_malloc(siglen); | ||
| 887 | if(!abuf) | ||
| 888 | goto err; | ||
| 889 | if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0) | ||
| 890 | goto err; | ||
| 891 | |||
| 892 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, | ||
| 893 | EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) | ||
| 894 | { | ||
| 895 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); | ||
| 896 | goto err; | ||
| 897 | } | ||
| 898 | |||
| 899 | EVP_MD_CTX_cleanup(&mctx); | ||
| 900 | |||
| 901 | ASN1_STRING_set0(si->enc_digest, abuf, siglen); | ||
| 902 | |||
| 903 | return 1; | ||
| 904 | |||
| 905 | err: | ||
| 906 | if (abuf) | ||
| 907 | OPENSSL_free(abuf); | ||
| 908 | EVP_MD_CTX_cleanup(&mctx); | ||
| 909 | return 0; | ||
| 910 | |||
| 911 | } | ||
| 912 | |||
| 913 | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio, | ||
| 914 | PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 915 | { | ||
| 916 | PKCS7_ISSUER_AND_SERIAL *ias; | ||
| 917 | int ret=0,i; | ||
| 918 | STACK_OF(X509) *cert; | ||
| 919 | X509 *x509; | ||
| 920 | |||
| 921 | if (PKCS7_type_is_signed(p7)) | ||
| 922 | { | ||
| 923 | cert=p7->d.sign->cert; | ||
| 924 | } | ||
| 925 | else if (PKCS7_type_is_signedAndEnveloped(p7)) | ||
| 926 | { | ||
| 927 | cert=p7->d.signed_and_enveloped->cert; | ||
| 928 | } | ||
| 929 | else | ||
| 930 | { | ||
| 931 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 932 | goto err; | ||
| 933 | } | ||
| 934 | /* XXXXXXXXXXXXXXXXXXXXXXX */ | ||
| 935 | ias=si->issuer_and_serial; | ||
| 936 | |||
| 937 | x509=X509_find_by_issuer_and_serial(cert,ias->issuer,ias->serial); | ||
| 938 | |||
| 939 | /* were we able to find the cert in passed to us */ | ||
| 940 | if (x509 == NULL) | ||
| 941 | { | ||
| 942 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,PKCS7_R_UNABLE_TO_FIND_CERTIFICATE); | ||
| 943 | goto err; | ||
| 944 | } | ||
| 945 | |||
| 946 | /* Lets verify */ | ||
| 947 | if(!X509_STORE_CTX_init(ctx,cert_store,x509,cert)) | ||
| 948 | { | ||
| 949 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); | ||
| 950 | goto err; | ||
| 951 | } | ||
| 952 | X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN); | ||
| 953 | i=X509_verify_cert(ctx); | ||
| 954 | if (i <= 0) | ||
| 955 | { | ||
| 956 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,ERR_R_X509_LIB); | ||
| 957 | X509_STORE_CTX_cleanup(ctx); | ||
| 958 | goto err; | ||
| 959 | } | ||
| 960 | X509_STORE_CTX_cleanup(ctx); | ||
| 961 | |||
| 962 | return PKCS7_signatureVerify(bio, p7, si, x509); | ||
| 963 | err: | ||
| 964 | return ret; | ||
| 965 | } | ||
| 966 | |||
| 967 | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, | ||
| 968 | X509 *x509) | ||
| 969 | { | ||
| 970 | ASN1_OCTET_STRING *os; | ||
| 971 | EVP_MD_CTX mdc_tmp,*mdc; | ||
| 972 | int ret=0,i; | ||
| 973 | int md_type; | ||
| 974 | STACK_OF(X509_ATTRIBUTE) *sk; | ||
| 975 | BIO *btmp; | ||
| 976 | EVP_PKEY *pkey; | ||
| 977 | |||
| 978 | EVP_MD_CTX_init(&mdc_tmp); | ||
| 979 | |||
| 980 | if (!PKCS7_type_is_signed(p7) && | ||
| 981 | !PKCS7_type_is_signedAndEnveloped(p7)) { | ||
| 982 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 983 | PKCS7_R_WRONG_PKCS7_TYPE); | ||
| 984 | goto err; | ||
| 985 | } | ||
| 986 | |||
| 987 | md_type=OBJ_obj2nid(si->digest_alg->algorithm); | ||
| 988 | |||
| 989 | btmp=bio; | ||
| 990 | for (;;) | ||
| 991 | { | ||
| 992 | if ((btmp == NULL) || | ||
| 993 | ((btmp=BIO_find_type(btmp,BIO_TYPE_MD)) == NULL)) | ||
| 994 | { | ||
| 995 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 996 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 997 | goto err; | ||
| 998 | } | ||
| 999 | BIO_get_md_ctx(btmp,&mdc); | ||
| 1000 | if (mdc == NULL) | ||
| 1001 | { | ||
| 1002 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 1003 | ERR_R_INTERNAL_ERROR); | ||
| 1004 | goto err; | ||
| 1005 | } | ||
| 1006 | if (EVP_MD_CTX_type(mdc) == md_type) | ||
| 1007 | break; | ||
| 1008 | /* Workaround for some broken clients that put the signature | ||
| 1009 | * OID instead of the digest OID in digest_alg->algorithm | ||
| 1010 | */ | ||
| 1011 | if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type) | ||
| 1012 | break; | ||
| 1013 | btmp=BIO_next(btmp); | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | /* mdc is the digest ctx that we want, unless there are attributes, | ||
| 1017 | * in which case the digest is the signed attributes */ | ||
| 1018 | EVP_MD_CTX_copy_ex(&mdc_tmp,mdc); | ||
| 1019 | |||
| 1020 | sk=si->auth_attr; | ||
| 1021 | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) | ||
| 1022 | { | ||
| 1023 | unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL; | ||
| 1024 | unsigned int md_len; | ||
| 1025 | int alen; | ||
| 1026 | ASN1_OCTET_STRING *message_digest; | ||
| 1027 | |||
| 1028 | EVP_DigestFinal_ex(&mdc_tmp,md_dat,&md_len); | ||
| 1029 | message_digest=PKCS7_digest_from_attributes(sk); | ||
| 1030 | if (!message_digest) | ||
| 1031 | { | ||
| 1032 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 1033 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | ||
| 1034 | goto err; | ||
| 1035 | } | ||
| 1036 | if ((message_digest->length != (int)md_len) || | ||
| 1037 | (memcmp(message_digest->data,md_dat,md_len))) | ||
| 1038 | { | ||
| 1039 | #if 0 | ||
| 1040 | { | ||
| 1041 | int ii; | ||
| 1042 | for (ii=0; ii<message_digest->length; ii++) | ||
| 1043 | printf("%02X",message_digest->data[ii]); printf(" sent\n"); | ||
| 1044 | for (ii=0; ii<md_len; ii++) printf("%02X",md_dat[ii]); printf(" calc\n"); | ||
| 1045 | } | ||
| 1046 | #endif | ||
| 1047 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 1048 | PKCS7_R_DIGEST_FAILURE); | ||
| 1049 | ret= -1; | ||
| 1050 | goto err; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | EVP_VerifyInit_ex(&mdc_tmp,EVP_get_digestbynid(md_type), NULL); | ||
| 1054 | |||
| 1055 | alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, | ||
| 1056 | ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); | ||
| 1057 | if (alen <= 0) | ||
| 1058 | { | ||
| 1059 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,ERR_R_ASN1_LIB); | ||
| 1060 | ret = -1; | ||
| 1061 | goto err; | ||
| 1062 | } | ||
| 1063 | EVP_VerifyUpdate(&mdc_tmp, abuf, alen); | ||
| 1064 | |||
| 1065 | OPENSSL_free(abuf); | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | os=si->enc_digest; | ||
| 1069 | pkey = X509_get_pubkey(x509); | ||
| 1070 | if (!pkey) | ||
| 1071 | { | ||
| 1072 | ret = -1; | ||
| 1073 | goto err; | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | i=EVP_VerifyFinal(&mdc_tmp,os->data,os->length, pkey); | ||
| 1077 | EVP_PKEY_free(pkey); | ||
| 1078 | if (i <= 0) | ||
| 1079 | { | ||
| 1080 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | ||
| 1081 | PKCS7_R_SIGNATURE_FAILURE); | ||
| 1082 | ret= -1; | ||
| 1083 | goto err; | ||
| 1084 | } | ||
| 1085 | else | ||
| 1086 | ret=1; | ||
| 1087 | err: | ||
| 1088 | EVP_MD_CTX_cleanup(&mdc_tmp); | ||
| 1089 | return(ret); | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) | ||
| 1093 | { | ||
| 1094 | STACK_OF(PKCS7_RECIP_INFO) *rsk; | ||
| 1095 | PKCS7_RECIP_INFO *ri; | ||
| 1096 | int i; | ||
| 1097 | |||
| 1098 | i=OBJ_obj2nid(p7->type); | ||
| 1099 | if (i != NID_pkcs7_signedAndEnveloped) | ||
| 1100 | return NULL; | ||
| 1101 | if (p7->d.signed_and_enveloped == NULL) | ||
| 1102 | return NULL; | ||
| 1103 | rsk=p7->d.signed_and_enveloped->recipientinfo; | ||
| 1104 | if (rsk == NULL) | ||
| 1105 | return NULL; | ||
| 1106 | ri=sk_PKCS7_RECIP_INFO_value(rsk,0); | ||
| 1107 | if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL); | ||
| 1108 | ri=sk_PKCS7_RECIP_INFO_value(rsk,idx); | ||
| 1109 | return(ri->issuer_and_serial); | ||
| 1110 | } | ||
| 1111 | |||
| 1112 | ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 1113 | { | ||
| 1114 | return(get_attribute(si->auth_attr,nid)); | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid) | ||
| 1118 | { | ||
| 1119 | return(get_attribute(si->unauth_attr,nid)); | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid) | ||
| 1123 | { | ||
| 1124 | int i; | ||
| 1125 | X509_ATTRIBUTE *xa; | ||
| 1126 | ASN1_OBJECT *o; | ||
| 1127 | |||
| 1128 | o=OBJ_nid2obj(nid); | ||
| 1129 | if (!o || !sk) return(NULL); | ||
| 1130 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 1131 | { | ||
| 1132 | xa=sk_X509_ATTRIBUTE_value(sk,i); | ||
| 1133 | if (OBJ_cmp(xa->object,o) == 0) | ||
| 1134 | { | ||
| 1135 | if (!xa->single && sk_ASN1_TYPE_num(xa->value.set)) | ||
| 1136 | return(sk_ASN1_TYPE_value(xa->value.set,0)); | ||
| 1137 | else | ||
| 1138 | return(NULL); | ||
| 1139 | } | ||
| 1140 | } | ||
| 1141 | return(NULL); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1145 | { | ||
| 1146 | ASN1_TYPE *astype; | ||
| 1147 | if(!(astype = get_attribute(sk, NID_pkcs9_messageDigest))) return NULL; | ||
| 1148 | return astype->value.octet_string; | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, | ||
| 1152 | STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1153 | { | ||
| 1154 | int i; | ||
| 1155 | |||
| 1156 | if (p7si->auth_attr != NULL) | ||
| 1157 | sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr,X509_ATTRIBUTE_free); | ||
| 1158 | p7si->auth_attr=sk_X509_ATTRIBUTE_dup(sk); | ||
| 1159 | if (p7si->auth_attr == NULL) | ||
| 1160 | return 0; | ||
| 1161 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 1162 | { | ||
| 1163 | if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr,i, | ||
| 1164 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) | ||
| 1165 | == NULL) | ||
| 1166 | return(0); | ||
| 1167 | } | ||
| 1168 | return(1); | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk) | ||
| 1172 | { | ||
| 1173 | int i; | ||
| 1174 | |||
| 1175 | if (p7si->unauth_attr != NULL) | ||
| 1176 | sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, | ||
| 1177 | X509_ATTRIBUTE_free); | ||
| 1178 | p7si->unauth_attr=sk_X509_ATTRIBUTE_dup(sk); | ||
| 1179 | if (p7si->unauth_attr == NULL) | ||
| 1180 | return 0; | ||
| 1181 | for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) | ||
| 1182 | { | ||
| 1183 | if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr,i, | ||
| 1184 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk,i)))) | ||
| 1185 | == NULL) | ||
| 1186 | return(0); | ||
| 1187 | } | ||
| 1188 | return(1); | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 1192 | void *value) | ||
| 1193 | { | ||
| 1194 | return(add_attribute(&(p7si->auth_attr),nid,atrtype,value)); | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 1198 | void *value) | ||
| 1199 | { | ||
| 1200 | return(add_attribute(&(p7si->unauth_attr),nid,atrtype,value)); | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, | ||
| 1204 | void *value) | ||
| 1205 | { | ||
| 1206 | X509_ATTRIBUTE *attr=NULL; | ||
| 1207 | |||
| 1208 | if (*sk == NULL) | ||
| 1209 | { | ||
| 1210 | *sk = sk_X509_ATTRIBUTE_new_null(); | ||
| 1211 | if (*sk == NULL) | ||
| 1212 | return 0; | ||
| 1213 | new_attrib: | ||
| 1214 | if (!(attr=X509_ATTRIBUTE_create(nid,atrtype,value))) | ||
| 1215 | return 0; | ||
| 1216 | if (!sk_X509_ATTRIBUTE_push(*sk,attr)) | ||
| 1217 | { | ||
| 1218 | X509_ATTRIBUTE_free(attr); | ||
| 1219 | return 0; | ||
| 1220 | } | ||
| 1221 | } | ||
| 1222 | else | ||
| 1223 | { | ||
| 1224 | int i; | ||
| 1225 | |||
| 1226 | for (i=0; i<sk_X509_ATTRIBUTE_num(*sk); i++) | ||
| 1227 | { | ||
| 1228 | attr=sk_X509_ATTRIBUTE_value(*sk,i); | ||
| 1229 | if (OBJ_obj2nid(attr->object) == nid) | ||
| 1230 | { | ||
| 1231 | X509_ATTRIBUTE_free(attr); | ||
| 1232 | attr=X509_ATTRIBUTE_create(nid,atrtype,value); | ||
| 1233 | if (attr == NULL) | ||
| 1234 | return 0; | ||
| 1235 | if (!sk_X509_ATTRIBUTE_set(*sk,i,attr)) | ||
| 1236 | { | ||
| 1237 | X509_ATTRIBUTE_free(attr); | ||
| 1238 | return 0; | ||
| 1239 | } | ||
| 1240 | goto end; | ||
| 1241 | } | ||
| 1242 | } | ||
| 1243 | goto new_attrib; | ||
| 1244 | } | ||
| 1245 | end: | ||
| 1246 | return(1); | ||
| 1247 | } | ||
| 1248 | |||
diff --git a/src/lib/libcrypto/pkcs7/pk7_lib.c b/src/lib/libcrypto/pkcs7/pk7_lib.c new file mode 100644 index 0000000000..d411269b50 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_lib.c | |||
| @@ -0,0 +1,665 @@ | |||
| 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 | #include "asn1_locl.h" | ||
| 64 | |||
| 65 | long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg) | ||
| 66 | { | ||
| 67 | int nid; | ||
| 68 | long ret; | ||
| 69 | |||
| 70 | nid=OBJ_obj2nid(p7->type); | ||
| 71 | |||
| 72 | switch (cmd) | ||
| 73 | { | ||
| 74 | case PKCS7_OP_SET_DETACHED_SIGNATURE: | ||
| 75 | if (nid == NID_pkcs7_signed) | ||
| 76 | { | ||
| 77 | ret=p7->detached=(int)larg; | ||
| 78 | if (ret && PKCS7_type_is_data(p7->d.sign->contents)) | ||
| 79 | { | ||
| 80 | ASN1_OCTET_STRING *os; | ||
| 81 | os=p7->d.sign->contents->d.data; | ||
| 82 | ASN1_OCTET_STRING_free(os); | ||
| 83 | p7->d.sign->contents->d.data = NULL; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | else | ||
| 87 | { | ||
| 88 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); | ||
| 89 | ret=0; | ||
| 90 | } | ||
| 91 | break; | ||
| 92 | case PKCS7_OP_GET_DETACHED_SIGNATURE: | ||
| 93 | if (nid == NID_pkcs7_signed) | ||
| 94 | { | ||
| 95 | if(!p7->d.sign || !p7->d.sign->contents->d.ptr) | ||
| 96 | ret = 1; | ||
| 97 | else ret = 0; | ||
| 98 | |||
| 99 | p7->detached = ret; | ||
| 100 | } | ||
| 101 | else | ||
| 102 | { | ||
| 103 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); | ||
| 104 | ret=0; | ||
| 105 | } | ||
| 106 | |||
| 107 | break; | ||
| 108 | default: | ||
| 109 | PKCS7err(PKCS7_F_PKCS7_CTRL,PKCS7_R_UNKNOWN_OPERATION); | ||
| 110 | ret=0; | ||
| 111 | } | ||
| 112 | return(ret); | ||
| 113 | } | ||
| 114 | |||
| 115 | int PKCS7_content_new(PKCS7 *p7, int type) | ||
| 116 | { | ||
| 117 | PKCS7 *ret=NULL; | ||
| 118 | |||
| 119 | if ((ret=PKCS7_new()) == NULL) goto err; | ||
| 120 | if (!PKCS7_set_type(ret,type)) goto err; | ||
| 121 | if (!PKCS7_set_content(p7,ret)) goto err; | ||
| 122 | |||
| 123 | return(1); | ||
| 124 | err: | ||
| 125 | if (ret != NULL) PKCS7_free(ret); | ||
| 126 | return(0); | ||
| 127 | } | ||
| 128 | |||
| 129 | int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data) | ||
| 130 | { | ||
| 131 | int i; | ||
| 132 | |||
| 133 | i=OBJ_obj2nid(p7->type); | ||
| 134 | switch (i) | ||
| 135 | { | ||
| 136 | case NID_pkcs7_signed: | ||
| 137 | if (p7->d.sign->contents != NULL) | ||
| 138 | PKCS7_free(p7->d.sign->contents); | ||
| 139 | p7->d.sign->contents=p7_data; | ||
| 140 | break; | ||
| 141 | case NID_pkcs7_digest: | ||
| 142 | if (p7->d.digest->contents != NULL) | ||
| 143 | PKCS7_free(p7->d.digest->contents); | ||
| 144 | p7->d.digest->contents=p7_data; | ||
| 145 | break; | ||
| 146 | case NID_pkcs7_data: | ||
| 147 | case NID_pkcs7_enveloped: | ||
| 148 | case NID_pkcs7_signedAndEnveloped: | ||
| 149 | case NID_pkcs7_encrypted: | ||
| 150 | default: | ||
| 151 | PKCS7err(PKCS7_F_PKCS7_SET_CONTENT,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 152 | goto err; | ||
| 153 | } | ||
| 154 | return(1); | ||
| 155 | err: | ||
| 156 | return(0); | ||
| 157 | } | ||
| 158 | |||
| 159 | int PKCS7_set_type(PKCS7 *p7, int type) | ||
| 160 | { | ||
| 161 | ASN1_OBJECT *obj; | ||
| 162 | |||
| 163 | /*PKCS7_content_free(p7);*/ | ||
| 164 | obj=OBJ_nid2obj(type); /* will not fail */ | ||
| 165 | |||
| 166 | switch (type) | ||
| 167 | { | ||
| 168 | case NID_pkcs7_signed: | ||
| 169 | p7->type=obj; | ||
| 170 | if ((p7->d.sign=PKCS7_SIGNED_new()) == NULL) | ||
| 171 | goto err; | ||
| 172 | if (!ASN1_INTEGER_set(p7->d.sign->version,1)) | ||
| 173 | { | ||
| 174 | PKCS7_SIGNED_free(p7->d.sign); | ||
| 175 | p7->d.sign=NULL; | ||
| 176 | goto err; | ||
| 177 | } | ||
| 178 | break; | ||
| 179 | case NID_pkcs7_data: | ||
| 180 | p7->type=obj; | ||
| 181 | if ((p7->d.data=M_ASN1_OCTET_STRING_new()) == NULL) | ||
| 182 | goto err; | ||
| 183 | break; | ||
| 184 | case NID_pkcs7_signedAndEnveloped: | ||
| 185 | p7->type=obj; | ||
| 186 | if ((p7->d.signed_and_enveloped=PKCS7_SIGN_ENVELOPE_new()) | ||
| 187 | == NULL) goto err; | ||
| 188 | ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1); | ||
| 189 | if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version,1)) | ||
| 190 | goto err; | ||
| 191 | p7->d.signed_and_enveloped->enc_data->content_type | ||
| 192 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 193 | break; | ||
| 194 | case NID_pkcs7_enveloped: | ||
| 195 | p7->type=obj; | ||
| 196 | if ((p7->d.enveloped=PKCS7_ENVELOPE_new()) | ||
| 197 | == NULL) goto err; | ||
| 198 | if (!ASN1_INTEGER_set(p7->d.enveloped->version,0)) | ||
| 199 | goto err; | ||
| 200 | p7->d.enveloped->enc_data->content_type | ||
| 201 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 202 | break; | ||
| 203 | case NID_pkcs7_encrypted: | ||
| 204 | p7->type=obj; | ||
| 205 | if ((p7->d.encrypted=PKCS7_ENCRYPT_new()) | ||
| 206 | == NULL) goto err; | ||
| 207 | if (!ASN1_INTEGER_set(p7->d.encrypted->version,0)) | ||
| 208 | goto err; | ||
| 209 | p7->d.encrypted->enc_data->content_type | ||
| 210 | = OBJ_nid2obj(NID_pkcs7_data); | ||
| 211 | break; | ||
| 212 | |||
| 213 | case NID_pkcs7_digest: | ||
| 214 | p7->type=obj; | ||
| 215 | if ((p7->d.digest=PKCS7_DIGEST_new()) | ||
| 216 | == NULL) goto err; | ||
| 217 | if (!ASN1_INTEGER_set(p7->d.digest->version,0)) | ||
| 218 | goto err; | ||
| 219 | break; | ||
| 220 | default: | ||
| 221 | PKCS7err(PKCS7_F_PKCS7_SET_TYPE,PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | ||
| 222 | goto err; | ||
| 223 | } | ||
| 224 | return(1); | ||
| 225 | err: | ||
| 226 | return(0); | ||
| 227 | } | ||
| 228 | |||
| 229 | int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other) | ||
| 230 | { | ||
| 231 | p7->type = OBJ_nid2obj(type); | ||
| 232 | p7->d.other = other; | ||
| 233 | return 1; | ||
| 234 | } | ||
| 235 | |||
| 236 | int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi) | ||
| 237 | { | ||
| 238 | int i,j,nid; | ||
| 239 | X509_ALGOR *alg; | ||
| 240 | STACK_OF(PKCS7_SIGNER_INFO) *signer_sk; | ||
| 241 | STACK_OF(X509_ALGOR) *md_sk; | ||
| 242 | |||
| 243 | i=OBJ_obj2nid(p7->type); | ||
| 244 | switch (i) | ||
| 245 | { | ||
| 246 | case NID_pkcs7_signed: | ||
| 247 | signer_sk= p7->d.sign->signer_info; | ||
| 248 | md_sk= p7->d.sign->md_algs; | ||
| 249 | break; | ||
| 250 | case NID_pkcs7_signedAndEnveloped: | ||
| 251 | signer_sk= p7->d.signed_and_enveloped->signer_info; | ||
| 252 | md_sk= p7->d.signed_and_enveloped->md_algs; | ||
| 253 | break; | ||
| 254 | default: | ||
| 255 | PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 256 | return(0); | ||
| 257 | } | ||
| 258 | |||
| 259 | nid=OBJ_obj2nid(psi->digest_alg->algorithm); | ||
| 260 | |||
| 261 | /* If the digest is not currently listed, add it */ | ||
| 262 | j=0; | ||
| 263 | for (i=0; i<sk_X509_ALGOR_num(md_sk); i++) | ||
| 264 | { | ||
| 265 | alg=sk_X509_ALGOR_value(md_sk,i); | ||
| 266 | if (OBJ_obj2nid(alg->algorithm) == nid) | ||
| 267 | { | ||
| 268 | j=1; | ||
| 269 | break; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | if (!j) /* we need to add another algorithm */ | ||
| 273 | { | ||
| 274 | if(!(alg=X509_ALGOR_new()) | ||
| 275 | || !(alg->parameter = ASN1_TYPE_new())) | ||
| 276 | { | ||
| 277 | X509_ALGOR_free(alg); | ||
| 278 | PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE); | ||
| 279 | return(0); | ||
| 280 | } | ||
| 281 | alg->algorithm=OBJ_nid2obj(nid); | ||
| 282 | alg->parameter->type = V_ASN1_NULL; | ||
| 283 | if (!sk_X509_ALGOR_push(md_sk,alg)) | ||
| 284 | { | ||
| 285 | X509_ALGOR_free(alg); | ||
| 286 | return 0; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | if (!sk_PKCS7_SIGNER_INFO_push(signer_sk,psi)) | ||
| 291 | return 0; | ||
| 292 | return(1); | ||
| 293 | } | ||
| 294 | |||
| 295 | int PKCS7_add_certificate(PKCS7 *p7, X509 *x509) | ||
| 296 | { | ||
| 297 | int i; | ||
| 298 | STACK_OF(X509) **sk; | ||
| 299 | |||
| 300 | i=OBJ_obj2nid(p7->type); | ||
| 301 | switch (i) | ||
| 302 | { | ||
| 303 | case NID_pkcs7_signed: | ||
| 304 | sk= &(p7->d.sign->cert); | ||
| 305 | break; | ||
| 306 | case NID_pkcs7_signedAndEnveloped: | ||
| 307 | sk= &(p7->d.signed_and_enveloped->cert); | ||
| 308 | break; | ||
| 309 | default: | ||
| 310 | PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 311 | return(0); | ||
| 312 | } | ||
| 313 | |||
| 314 | if (*sk == NULL) | ||
| 315 | *sk=sk_X509_new_null(); | ||
| 316 | if (*sk == NULL) | ||
| 317 | { | ||
| 318 | PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE); | ||
| 319 | return 0; | ||
| 320 | } | ||
| 321 | CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); | ||
| 322 | if (!sk_X509_push(*sk,x509)) | ||
| 323 | { | ||
| 324 | X509_free(x509); | ||
| 325 | return 0; | ||
| 326 | } | ||
| 327 | return(1); | ||
| 328 | } | ||
| 329 | |||
| 330 | int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) | ||
| 331 | { | ||
| 332 | int i; | ||
| 333 | STACK_OF(X509_CRL) **sk; | ||
| 334 | |||
| 335 | i=OBJ_obj2nid(p7->type); | ||
| 336 | switch (i) | ||
| 337 | { | ||
| 338 | case NID_pkcs7_signed: | ||
| 339 | sk= &(p7->d.sign->crl); | ||
| 340 | break; | ||
| 341 | case NID_pkcs7_signedAndEnveloped: | ||
| 342 | sk= &(p7->d.signed_and_enveloped->crl); | ||
| 343 | break; | ||
| 344 | default: | ||
| 345 | PKCS7err(PKCS7_F_PKCS7_ADD_CRL,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 346 | return(0); | ||
| 347 | } | ||
| 348 | |||
| 349 | if (*sk == NULL) | ||
| 350 | *sk=sk_X509_CRL_new_null(); | ||
| 351 | if (*sk == NULL) | ||
| 352 | { | ||
| 353 | PKCS7err(PKCS7_F_PKCS7_ADD_CRL,ERR_R_MALLOC_FAILURE); | ||
| 354 | return 0; | ||
| 355 | } | ||
| 356 | |||
| 357 | CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL); | ||
| 358 | if (!sk_X509_CRL_push(*sk,crl)) | ||
| 359 | { | ||
| 360 | X509_CRL_free(crl); | ||
| 361 | return 0; | ||
| 362 | } | ||
| 363 | return(1); | ||
| 364 | } | ||
| 365 | |||
| 366 | int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, | ||
| 367 | const EVP_MD *dgst) | ||
| 368 | { | ||
| 369 | int ret; | ||
| 370 | |||
| 371 | /* We now need to add another PKCS7_SIGNER_INFO entry */ | ||
| 372 | if (!ASN1_INTEGER_set(p7i->version,1)) | ||
| 373 | goto err; | ||
| 374 | if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, | ||
| 375 | X509_get_issuer_name(x509))) | ||
| 376 | goto err; | ||
| 377 | |||
| 378 | /* because ASN1_INTEGER_set is used to set a 'long' we will do | ||
| 379 | * things the ugly way. */ | ||
| 380 | M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); | ||
| 381 | if (!(p7i->issuer_and_serial->serial= | ||
| 382 | M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) | ||
| 383 | goto err; | ||
| 384 | |||
| 385 | /* lets keep the pkey around for a while */ | ||
| 386 | CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); | ||
| 387 | p7i->pkey=pkey; | ||
| 388 | |||
| 389 | /* Set the algorithms */ | ||
| 390 | |||
| 391 | X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)), | ||
| 392 | V_ASN1_NULL, NULL); | ||
| 393 | |||
| 394 | if (pkey->ameth && pkey->ameth->pkey_ctrl) | ||
| 395 | { | ||
| 396 | ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, | ||
| 397 | 0, p7i); | ||
| 398 | if (ret > 0) | ||
| 399 | return 1; | ||
| 400 | if (ret != -2) | ||
| 401 | { | ||
| 402 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET, | ||
| 403 | PKCS7_R_SIGNING_CTRL_FAILURE); | ||
| 404 | return 0; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET, | ||
| 408 | PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); | ||
| 409 | err: | ||
| 410 | return 0; | ||
| 411 | } | ||
| 412 | |||
| 413 | PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey, | ||
| 414 | const EVP_MD *dgst) | ||
| 415 | { | ||
| 416 | PKCS7_SIGNER_INFO *si = NULL; | ||
| 417 | |||
| 418 | if (dgst == NULL) | ||
| 419 | { | ||
| 420 | int def_nid; | ||
| 421 | if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) | ||
| 422 | goto err; | ||
| 423 | dgst = EVP_get_digestbynid(def_nid); | ||
| 424 | if (dgst == NULL) | ||
| 425 | { | ||
| 426 | PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, | ||
| 427 | PKCS7_R_NO_DEFAULT_DIGEST); | ||
| 428 | goto err; | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | if ((si=PKCS7_SIGNER_INFO_new()) == NULL) goto err; | ||
| 433 | if (!PKCS7_SIGNER_INFO_set(si,x509,pkey,dgst)) goto err; | ||
| 434 | if (!PKCS7_add_signer(p7,si)) goto err; | ||
| 435 | return(si); | ||
| 436 | err: | ||
| 437 | if (si) | ||
| 438 | PKCS7_SIGNER_INFO_free(si); | ||
| 439 | return(NULL); | ||
| 440 | } | ||
| 441 | |||
| 442 | int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md) | ||
| 443 | { | ||
| 444 | if (PKCS7_type_is_digest(p7)) | ||
| 445 | { | ||
| 446 | if(!(p7->d.digest->md->parameter = ASN1_TYPE_new())) | ||
| 447 | { | ||
| 448 | PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,ERR_R_MALLOC_FAILURE); | ||
| 449 | return 0; | ||
| 450 | } | ||
| 451 | p7->d.digest->md->parameter->type = V_ASN1_NULL; | ||
| 452 | p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md)); | ||
| 453 | return 1; | ||
| 454 | } | ||
| 455 | |||
| 456 | PKCS7err(PKCS7_F_PKCS7_SET_DIGEST,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 457 | return 1; | ||
| 458 | } | ||
| 459 | |||
| 460 | STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7) | ||
| 461 | { | ||
| 462 | if (PKCS7_type_is_signed(p7)) | ||
| 463 | { | ||
| 464 | return(p7->d.sign->signer_info); | ||
| 465 | } | ||
| 466 | else if (PKCS7_type_is_signedAndEnveloped(p7)) | ||
| 467 | { | ||
| 468 | return(p7->d.signed_and_enveloped->signer_info); | ||
| 469 | } | ||
| 470 | else | ||
| 471 | return(NULL); | ||
| 472 | } | ||
| 473 | |||
| 474 | void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, | ||
| 475 | X509_ALGOR **pdig, X509_ALGOR **psig) | ||
| 476 | { | ||
| 477 | if (pk) | ||
| 478 | *pk = si->pkey; | ||
| 479 | if (pdig) | ||
| 480 | *pdig = si->digest_alg; | ||
| 481 | if (psig) | ||
| 482 | *psig = si->digest_enc_alg; | ||
| 483 | } | ||
| 484 | |||
| 485 | void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc) | ||
| 486 | { | ||
| 487 | if (penc) | ||
| 488 | *penc = ri->key_enc_algor; | ||
| 489 | } | ||
| 490 | |||
| 491 | PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509) | ||
| 492 | { | ||
| 493 | PKCS7_RECIP_INFO *ri; | ||
| 494 | |||
| 495 | if ((ri=PKCS7_RECIP_INFO_new()) == NULL) goto err; | ||
| 496 | if (!PKCS7_RECIP_INFO_set(ri,x509)) goto err; | ||
| 497 | if (!PKCS7_add_recipient_info(p7,ri)) goto err; | ||
| 498 | return ri; | ||
| 499 | err: | ||
| 500 | if (ri) | ||
| 501 | PKCS7_RECIP_INFO_free(ri); | ||
| 502 | return NULL; | ||
| 503 | } | ||
| 504 | |||
| 505 | int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) | ||
| 506 | { | ||
| 507 | int i; | ||
| 508 | STACK_OF(PKCS7_RECIP_INFO) *sk; | ||
| 509 | |||
| 510 | i=OBJ_obj2nid(p7->type); | ||
| 511 | switch (i) | ||
| 512 | { | ||
| 513 | case NID_pkcs7_signedAndEnveloped: | ||
| 514 | sk= p7->d.signed_and_enveloped->recipientinfo; | ||
| 515 | break; | ||
| 516 | case NID_pkcs7_enveloped: | ||
| 517 | sk= p7->d.enveloped->recipientinfo; | ||
| 518 | break; | ||
| 519 | default: | ||
| 520 | PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 521 | return(0); | ||
| 522 | } | ||
| 523 | |||
| 524 | if (!sk_PKCS7_RECIP_INFO_push(sk,ri)) | ||
| 525 | return 0; | ||
| 526 | return(1); | ||
| 527 | } | ||
| 528 | |||
| 529 | int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509) | ||
| 530 | { | ||
| 531 | int ret; | ||
| 532 | EVP_PKEY *pkey = NULL; | ||
| 533 | if (!ASN1_INTEGER_set(p7i->version,0)) | ||
| 534 | return 0; | ||
| 535 | if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, | ||
| 536 | X509_get_issuer_name(x509))) | ||
| 537 | return 0; | ||
| 538 | |||
| 539 | M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); | ||
| 540 | if (!(p7i->issuer_and_serial->serial= | ||
| 541 | M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) | ||
| 542 | return 0; | ||
| 543 | |||
| 544 | pkey = X509_get_pubkey(x509); | ||
| 545 | |||
| 546 | if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) | ||
| 547 | { | ||
| 548 | PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, | ||
| 549 | PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); | ||
| 550 | goto err; | ||
| 551 | } | ||
| 552 | |||
| 553 | ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, | ||
| 554 | 0, p7i); | ||
| 555 | if (ret == -2) | ||
| 556 | { | ||
| 557 | PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, | ||
| 558 | PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); | ||
| 559 | goto err; | ||
| 560 | } | ||
| 561 | if (ret <= 0) | ||
| 562 | { | ||
| 563 | PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, | ||
| 564 | PKCS7_R_ENCRYPTION_CTRL_FAILURE); | ||
| 565 | goto err; | ||
| 566 | } | ||
| 567 | |||
| 568 | EVP_PKEY_free(pkey); | ||
| 569 | |||
| 570 | CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); | ||
| 571 | p7i->cert=x509; | ||
| 572 | |||
| 573 | return 1; | ||
| 574 | |||
| 575 | err: | ||
| 576 | if (pkey) | ||
| 577 | EVP_PKEY_free(pkey); | ||
| 578 | return 0; | ||
| 579 | } | ||
| 580 | |||
| 581 | X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 582 | { | ||
| 583 | if (PKCS7_type_is_signed(p7)) | ||
| 584 | return(X509_find_by_issuer_and_serial(p7->d.sign->cert, | ||
| 585 | si->issuer_and_serial->issuer, | ||
| 586 | si->issuer_and_serial->serial)); | ||
| 587 | else | ||
| 588 | return(NULL); | ||
| 589 | } | ||
| 590 | |||
| 591 | int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher) | ||
| 592 | { | ||
| 593 | int i; | ||
| 594 | PKCS7_ENC_CONTENT *ec; | ||
| 595 | |||
| 596 | i=OBJ_obj2nid(p7->type); | ||
| 597 | switch (i) | ||
| 598 | { | ||
| 599 | case NID_pkcs7_signedAndEnveloped: | ||
| 600 | ec=p7->d.signed_and_enveloped->enc_data; | ||
| 601 | break; | ||
| 602 | case NID_pkcs7_enveloped: | ||
| 603 | ec=p7->d.enveloped->enc_data; | ||
| 604 | break; | ||
| 605 | default: | ||
| 606 | PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 607 | return(0); | ||
| 608 | } | ||
| 609 | |||
| 610 | /* Check cipher OID exists and has data in it*/ | ||
| 611 | i = EVP_CIPHER_type(cipher); | ||
| 612 | if(i == NID_undef) { | ||
| 613 | PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); | ||
| 614 | return(0); | ||
| 615 | } | ||
| 616 | |||
| 617 | ec->cipher = cipher; | ||
| 618 | return 1; | ||
| 619 | } | ||
| 620 | |||
| 621 | int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7) | ||
| 622 | { | ||
| 623 | ASN1_OCTET_STRING *os = NULL; | ||
| 624 | |||
| 625 | switch (OBJ_obj2nid(p7->type)) | ||
| 626 | { | ||
| 627 | case NID_pkcs7_data: | ||
| 628 | os = p7->d.data; | ||
| 629 | break; | ||
| 630 | |||
| 631 | case NID_pkcs7_signedAndEnveloped: | ||
| 632 | os = p7->d.signed_and_enveloped->enc_data->enc_data; | ||
| 633 | if (os == NULL) | ||
| 634 | { | ||
| 635 | os=M_ASN1_OCTET_STRING_new(); | ||
| 636 | p7->d.signed_and_enveloped->enc_data->enc_data=os; | ||
| 637 | } | ||
| 638 | break; | ||
| 639 | |||
| 640 | case NID_pkcs7_enveloped: | ||
| 641 | os = p7->d.enveloped->enc_data->enc_data; | ||
| 642 | if (os == NULL) | ||
| 643 | { | ||
| 644 | os=M_ASN1_OCTET_STRING_new(); | ||
| 645 | p7->d.enveloped->enc_data->enc_data=os; | ||
| 646 | } | ||
| 647 | break; | ||
| 648 | |||
| 649 | case NID_pkcs7_signed: | ||
| 650 | os=p7->d.sign->contents->d.data; | ||
| 651 | break; | ||
| 652 | |||
| 653 | default: | ||
| 654 | os = NULL; | ||
| 655 | break; | ||
| 656 | } | ||
| 657 | |||
| 658 | if (os == NULL) | ||
| 659 | return 0; | ||
| 660 | |||
| 661 | os->flags |= ASN1_STRING_FLAG_NDEF; | ||
| 662 | *boundary = &os->data; | ||
| 663 | |||
| 664 | return 1; | ||
| 665 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_mime.c b/src/lib/libcrypto/pkcs7/pk7_mime.c new file mode 100644 index 0000000000..938f79a646 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_mime.c | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | /* pk7_mime.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) 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 | */ | ||
| 54 | |||
| 55 | #include <stdio.h> | ||
| 56 | #include <ctype.h> | ||
| 57 | #include "cryptlib.h" | ||
| 58 | #include <openssl/rand.h> | ||
| 59 | #include <openssl/x509.h> | ||
| 60 | #include <openssl/asn1.h> | ||
| 61 | |||
| 62 | /* PKCS#7 wrappers round generalised stream and MIME routines */ | ||
| 63 | |||
| 64 | int i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags) | ||
| 65 | { | ||
| 66 | return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)p7, in, flags, | ||
| 67 | ASN1_ITEM_rptr(PKCS7)); | ||
| 68 | } | ||
| 69 | |||
| 70 | int PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags) | ||
| 71 | { | ||
| 72 | return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *) p7, in, flags, | ||
| 73 | "PKCS7", | ||
| 74 | ASN1_ITEM_rptr(PKCS7)); | ||
| 75 | } | ||
| 76 | |||
| 77 | int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags) | ||
| 78 | { | ||
| 79 | STACK_OF(X509_ALGOR) *mdalgs; | ||
| 80 | int ctype_nid = OBJ_obj2nid(p7->type); | ||
| 81 | if (ctype_nid == NID_pkcs7_signed) | ||
| 82 | mdalgs = p7->d.sign->md_algs; | ||
| 83 | else | ||
| 84 | mdalgs = NULL; | ||
| 85 | |||
| 86 | flags ^= SMIME_OLDMIME; | ||
| 87 | |||
| 88 | |||
| 89 | return SMIME_write_ASN1(bio, (ASN1_VALUE *)p7, data, flags, | ||
| 90 | ctype_nid, NID_undef, mdalgs, | ||
| 91 | ASN1_ITEM_rptr(PKCS7)); | ||
| 92 | } | ||
| 93 | |||
| 94 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont) | ||
| 95 | { | ||
| 96 | return (PKCS7 *)SMIME_read_ASN1(bio, bcont, ASN1_ITEM_rptr(PKCS7)); | ||
| 97 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pk7_smime.c b/src/lib/libcrypto/pkcs7/pk7_smime.c new file mode 100644 index 0000000000..86742d0dcd --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pk7_smime.c | |||
| @@ -0,0 +1,587 @@ | |||
| 1 | /* pk7_smime.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) 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 | static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si); | ||
| 67 | |||
| 68 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | ||
| 69 | BIO *data, int flags) | ||
| 70 | { | ||
| 71 | PKCS7 *p7; | ||
| 72 | int i; | ||
| 73 | |||
| 74 | if(!(p7 = PKCS7_new())) | ||
| 75 | { | ||
| 76 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 77 | return NULL; | ||
| 78 | } | ||
| 79 | |||
| 80 | if (!PKCS7_set_type(p7, NID_pkcs7_signed)) | ||
| 81 | goto err; | ||
| 82 | |||
| 83 | if (!PKCS7_content_new(p7, NID_pkcs7_data)) | ||
| 84 | goto err; | ||
| 85 | |||
| 86 | if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) | ||
| 87 | { | ||
| 88 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_ADD_SIGNER_ERROR); | ||
| 89 | goto err; | ||
| 90 | } | ||
| 91 | |||
| 92 | if(!(flags & PKCS7_NOCERTS)) | ||
| 93 | { | ||
| 94 | for(i = 0; i < sk_X509_num(certs); i++) | ||
| 95 | { | ||
| 96 | if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i))) | ||
| 97 | goto err; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | if(flags & PKCS7_DETACHED) | ||
| 102 | PKCS7_set_detached(p7, 1); | ||
| 103 | |||
| 104 | if (flags & (PKCS7_STREAM|PKCS7_PARTIAL)) | ||
| 105 | return p7; | ||
| 106 | |||
| 107 | if (PKCS7_final(p7, data, flags)) | ||
| 108 | return p7; | ||
| 109 | |||
| 110 | err: | ||
| 111 | PKCS7_free(p7); | ||
| 112 | return NULL; | ||
| 113 | } | ||
| 114 | |||
| 115 | int PKCS7_final(PKCS7 *p7, BIO *data, int flags) | ||
| 116 | { | ||
| 117 | BIO *p7bio; | ||
| 118 | int ret = 0; | ||
| 119 | if (!(p7bio = PKCS7_dataInit(p7, NULL))) | ||
| 120 | { | ||
| 121 | PKCS7err(PKCS7_F_PKCS7_FINAL,ERR_R_MALLOC_FAILURE); | ||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | |||
| 125 | SMIME_crlf_copy(data, p7bio, flags); | ||
| 126 | |||
| 127 | (void)BIO_flush(p7bio); | ||
| 128 | |||
| 129 | |||
| 130 | if (!PKCS7_dataFinal(p7,p7bio)) | ||
| 131 | { | ||
| 132 | PKCS7err(PKCS7_F_PKCS7_FINAL,PKCS7_R_PKCS7_DATASIGN); | ||
| 133 | goto err; | ||
| 134 | } | ||
| 135 | |||
| 136 | ret = 1; | ||
| 137 | |||
| 138 | err: | ||
| 139 | BIO_free_all(p7bio); | ||
| 140 | |||
| 141 | return ret; | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | /* Check to see if a cipher exists and if so add S/MIME capabilities */ | ||
| 146 | |||
| 147 | static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) | ||
| 148 | { | ||
| 149 | if (EVP_get_cipherbynid(nid)) | ||
| 150 | return PKCS7_simple_smimecap(sk, nid, arg); | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) | ||
| 155 | { | ||
| 156 | if (EVP_get_digestbynid(nid)) | ||
| 157 | return PKCS7_simple_smimecap(sk, nid, arg); | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert, | ||
| 162 | EVP_PKEY *pkey, const EVP_MD *md, | ||
| 163 | int flags) | ||
| 164 | { | ||
| 165 | PKCS7_SIGNER_INFO *si = NULL; | ||
| 166 | STACK_OF(X509_ALGOR) *smcap = NULL; | ||
| 167 | if(!X509_check_private_key(signcert, pkey)) | ||
| 168 | { | ||
| 169 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, | ||
| 170 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | ||
| 171 | return NULL; | ||
| 172 | } | ||
| 173 | |||
| 174 | if (!(si = PKCS7_add_signature(p7,signcert,pkey, md))) | ||
| 175 | { | ||
| 176 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, | ||
| 177 | PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); | ||
| 178 | return NULL; | ||
| 179 | } | ||
| 180 | |||
| 181 | if(!(flags & PKCS7_NOCERTS)) | ||
| 182 | { | ||
| 183 | if (!PKCS7_add_certificate(p7, signcert)) | ||
| 184 | goto err; | ||
| 185 | } | ||
| 186 | |||
| 187 | if(!(flags & PKCS7_NOATTR)) | ||
| 188 | { | ||
| 189 | if (!PKCS7_add_attrib_content_type(si, NULL)) | ||
| 190 | goto err; | ||
| 191 | /* Add SMIMECapabilities */ | ||
| 192 | if(!(flags & PKCS7_NOSMIMECAP)) | ||
| 193 | { | ||
| 194 | if(!(smcap = sk_X509_ALGOR_new_null())) | ||
| 195 | { | ||
| 196 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, | ||
| 197 | ERR_R_MALLOC_FAILURE); | ||
| 198 | goto err; | ||
| 199 | } | ||
| 200 | if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1) | ||
| 201 | || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1) | ||
| 202 | || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1) | ||
| 203 | || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1) | ||
| 204 | || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1) | ||
| 205 | || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1) | ||
| 206 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 128) | ||
| 207 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 64) | ||
| 208 | || !add_cipher_smcap(smcap, NID_des_cbc, -1) | ||
| 209 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 40) | ||
| 210 | || !PKCS7_add_attrib_smimecap (si, smcap)) | ||
| 211 | goto err; | ||
| 212 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | ||
| 213 | smcap = NULL; | ||
| 214 | } | ||
| 215 | if (flags & PKCS7_REUSE_DIGEST) | ||
| 216 | { | ||
| 217 | if (!pkcs7_copy_existing_digest(p7, si)) | ||
| 218 | goto err; | ||
| 219 | if (!(flags & PKCS7_PARTIAL) && | ||
| 220 | !PKCS7_SIGNER_INFO_sign(si)) | ||
| 221 | goto err; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | return si; | ||
| 225 | err: | ||
| 226 | if (smcap) | ||
| 227 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | ||
| 228 | return NULL; | ||
| 229 | } | ||
| 230 | |||
| 231 | /* Search for a digest matching SignerInfo digest type and if found | ||
| 232 | * copy across. | ||
| 233 | */ | ||
| 234 | |||
| 235 | static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si) | ||
| 236 | { | ||
| 237 | int i; | ||
| 238 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 239 | PKCS7_SIGNER_INFO *sitmp; | ||
| 240 | ASN1_OCTET_STRING *osdig = NULL; | ||
| 241 | sinfos = PKCS7_get_signer_info(p7); | ||
| 242 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) | ||
| 243 | { | ||
| 244 | sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | ||
| 245 | if (si == sitmp) | ||
| 246 | break; | ||
| 247 | if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0) | ||
| 248 | continue; | ||
| 249 | if (!OBJ_cmp(si->digest_alg->algorithm, | ||
| 250 | sitmp->digest_alg->algorithm)) | ||
| 251 | { | ||
| 252 | osdig = PKCS7_digest_from_attributes(sitmp->auth_attr); | ||
| 253 | break; | ||
| 254 | } | ||
| 255 | |||
| 256 | } | ||
| 257 | |||
| 258 | if (osdig) | ||
| 259 | return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length); | ||
| 260 | |||
| 261 | PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST, | ||
| 262 | PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND); | ||
| 263 | return 0; | ||
| 264 | } | ||
| 265 | |||
| 266 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | ||
| 267 | BIO *indata, BIO *out, int flags) | ||
| 268 | { | ||
| 269 | STACK_OF(X509) *signers; | ||
| 270 | X509 *signer; | ||
| 271 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 272 | PKCS7_SIGNER_INFO *si; | ||
| 273 | X509_STORE_CTX cert_ctx; | ||
| 274 | char buf[4096]; | ||
| 275 | int i, j=0, k, ret = 0; | ||
| 276 | BIO *p7bio; | ||
| 277 | BIO *tmpin, *tmpout; | ||
| 278 | |||
| 279 | if(!p7) { | ||
| 280 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_INVALID_NULL_POINTER); | ||
| 281 | return 0; | ||
| 282 | } | ||
| 283 | |||
| 284 | if(!PKCS7_type_is_signed(p7)) { | ||
| 285 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 286 | return 0; | ||
| 287 | } | ||
| 288 | |||
| 289 | /* Check for no data and no content: no data to verify signature */ | ||
| 290 | if(PKCS7_get_detached(p7) && !indata) { | ||
| 291 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_CONTENT); | ||
| 292 | return 0; | ||
| 293 | } | ||
| 294 | #if 0 | ||
| 295 | /* NB: this test commented out because some versions of Netscape | ||
| 296 | * illegally include zero length content when signing data. | ||
| 297 | */ | ||
| 298 | |||
| 299 | /* Check for data and content: two sets of data */ | ||
| 300 | if(!PKCS7_get_detached(p7) && indata) { | ||
| 301 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CONTENT_AND_DATA_PRESENT); | ||
| 302 | return 0; | ||
| 303 | } | ||
| 304 | #endif | ||
| 305 | |||
| 306 | sinfos = PKCS7_get_signer_info(p7); | ||
| 307 | |||
| 308 | if(!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) { | ||
| 309 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_NO_SIGNATURES_ON_DATA); | ||
| 310 | return 0; | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | signers = PKCS7_get0_signers(p7, certs, flags); | ||
| 315 | |||
| 316 | if(!signers) return 0; | ||
| 317 | |||
| 318 | /* Now verify the certificates */ | ||
| 319 | |||
| 320 | if (!(flags & PKCS7_NOVERIFY)) for (k = 0; k < sk_X509_num(signers); k++) { | ||
| 321 | signer = sk_X509_value (signers, k); | ||
| 322 | if (!(flags & PKCS7_NOCHAIN)) { | ||
| 323 | if(!X509_STORE_CTX_init(&cert_ctx, store, signer, | ||
| 324 | p7->d.sign->cert)) | ||
| 325 | { | ||
| 326 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB); | ||
| 327 | sk_X509_free(signers); | ||
| 328 | return 0; | ||
| 329 | } | ||
| 330 | X509_STORE_CTX_set_default(&cert_ctx, "smime_sign"); | ||
| 331 | } else if(!X509_STORE_CTX_init (&cert_ctx, store, signer, NULL)) { | ||
| 332 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_X509_LIB); | ||
| 333 | sk_X509_free(signers); | ||
| 334 | return 0; | ||
| 335 | } | ||
| 336 | if (!(flags & PKCS7_NOCRL)) | ||
| 337 | X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl); | ||
| 338 | i = X509_verify_cert(&cert_ctx); | ||
| 339 | if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx); | ||
| 340 | X509_STORE_CTX_cleanup(&cert_ctx); | ||
| 341 | if (i <= 0) { | ||
| 342 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_CERTIFICATE_VERIFY_ERROR); | ||
| 343 | ERR_add_error_data(2, "Verify error:", | ||
| 344 | X509_verify_cert_error_string(j)); | ||
| 345 | sk_X509_free(signers); | ||
| 346 | return 0; | ||
| 347 | } | ||
| 348 | /* Check for revocation status here */ | ||
| 349 | } | ||
| 350 | |||
| 351 | /* Performance optimization: if the content is a memory BIO then | ||
| 352 | * store its contents in a temporary read only memory BIO. This | ||
| 353 | * avoids potentially large numbers of slow copies of data which will | ||
| 354 | * occur when reading from a read write memory BIO when signatures | ||
| 355 | * are calculated. | ||
| 356 | */ | ||
| 357 | |||
| 358 | if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) | ||
| 359 | { | ||
| 360 | char *ptr; | ||
| 361 | long len; | ||
| 362 | len = BIO_get_mem_data(indata, &ptr); | ||
| 363 | tmpin = BIO_new_mem_buf(ptr, len); | ||
| 364 | if (tmpin == NULL) | ||
| 365 | { | ||
| 366 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE); | ||
| 367 | return 0; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | else | ||
| 371 | tmpin = indata; | ||
| 372 | |||
| 373 | |||
| 374 | if (!(p7bio=PKCS7_dataInit(p7,tmpin))) | ||
| 375 | goto err; | ||
| 376 | |||
| 377 | if(flags & PKCS7_TEXT) { | ||
| 378 | if(!(tmpout = BIO_new(BIO_s_mem()))) { | ||
| 379 | PKCS7err(PKCS7_F_PKCS7_VERIFY,ERR_R_MALLOC_FAILURE); | ||
| 380 | goto err; | ||
| 381 | } | ||
| 382 | BIO_set_mem_eof_return(tmpout, 0); | ||
| 383 | } else tmpout = out; | ||
| 384 | |||
| 385 | /* We now have to 'read' from p7bio to calculate digests etc. */ | ||
| 386 | for (;;) | ||
| 387 | { | ||
| 388 | i=BIO_read(p7bio,buf,sizeof(buf)); | ||
| 389 | if (i <= 0) break; | ||
| 390 | if (tmpout) BIO_write(tmpout, buf, i); | ||
| 391 | } | ||
| 392 | |||
| 393 | if(flags & PKCS7_TEXT) { | ||
| 394 | if(!SMIME_text(tmpout, out)) { | ||
| 395 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SMIME_TEXT_ERROR); | ||
| 396 | BIO_free(tmpout); | ||
| 397 | goto err; | ||
| 398 | } | ||
| 399 | BIO_free(tmpout); | ||
| 400 | } | ||
| 401 | |||
| 402 | /* Now Verify All Signatures */ | ||
| 403 | if (!(flags & PKCS7_NOSIGS)) | ||
| 404 | for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sinfos); i++) | ||
| 405 | { | ||
| 406 | si=sk_PKCS7_SIGNER_INFO_value(sinfos,i); | ||
| 407 | signer = sk_X509_value (signers, i); | ||
| 408 | j=PKCS7_signatureVerify(p7bio,p7,si, signer); | ||
| 409 | if (j <= 0) { | ||
| 410 | PKCS7err(PKCS7_F_PKCS7_VERIFY,PKCS7_R_SIGNATURE_FAILURE); | ||
| 411 | goto err; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | ret = 1; | ||
| 416 | |||
| 417 | err: | ||
| 418 | |||
| 419 | if (tmpin == indata) | ||
| 420 | { | ||
| 421 | if (indata) BIO_pop(p7bio); | ||
| 422 | } | ||
| 423 | BIO_free_all(p7bio); | ||
| 424 | |||
| 425 | sk_X509_free(signers); | ||
| 426 | |||
| 427 | return ret; | ||
| 428 | } | ||
| 429 | |||
| 430 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags) | ||
| 431 | { | ||
| 432 | STACK_OF(X509) *signers; | ||
| 433 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | ||
| 434 | PKCS7_SIGNER_INFO *si; | ||
| 435 | PKCS7_ISSUER_AND_SERIAL *ias; | ||
| 436 | X509 *signer; | ||
| 437 | int i; | ||
| 438 | |||
| 439 | if(!p7) { | ||
| 440 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_INVALID_NULL_POINTER); | ||
| 441 | return NULL; | ||
| 442 | } | ||
| 443 | |||
| 444 | if(!PKCS7_type_is_signed(p7)) { | ||
| 445 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 446 | return NULL; | ||
| 447 | } | ||
| 448 | |||
| 449 | /* Collect all the signers together */ | ||
| 450 | |||
| 451 | sinfos = PKCS7_get_signer_info(p7); | ||
| 452 | |||
| 453 | if(sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) { | ||
| 454 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_NO_SIGNERS); | ||
| 455 | return 0; | ||
| 456 | } | ||
| 457 | |||
| 458 | if(!(signers = sk_X509_new_null())) { | ||
| 459 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,ERR_R_MALLOC_FAILURE); | ||
| 460 | return NULL; | ||
| 461 | } | ||
| 462 | |||
| 463 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) | ||
| 464 | { | ||
| 465 | si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | ||
| 466 | ias = si->issuer_and_serial; | ||
| 467 | signer = NULL; | ||
| 468 | /* If any certificates passed they take priority */ | ||
| 469 | if (certs) signer = X509_find_by_issuer_and_serial (certs, | ||
| 470 | ias->issuer, ias->serial); | ||
| 471 | if (!signer && !(flags & PKCS7_NOINTERN) | ||
| 472 | && p7->d.sign->cert) signer = | ||
| 473 | X509_find_by_issuer_and_serial (p7->d.sign->cert, | ||
| 474 | ias->issuer, ias->serial); | ||
| 475 | if (!signer) { | ||
| 476 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); | ||
| 477 | sk_X509_free(signers); | ||
| 478 | return 0; | ||
| 479 | } | ||
| 480 | |||
| 481 | if (!sk_X509_push(signers, signer)) { | ||
| 482 | sk_X509_free(signers); | ||
| 483 | return NULL; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | return signers; | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | /* Build a complete PKCS#7 enveloped data */ | ||
| 491 | |||
| 492 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | ||
| 493 | int flags) | ||
| 494 | { | ||
| 495 | PKCS7 *p7; | ||
| 496 | BIO *p7bio = NULL; | ||
| 497 | int i; | ||
| 498 | X509 *x509; | ||
| 499 | if(!(p7 = PKCS7_new())) { | ||
| 500 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 501 | return NULL; | ||
| 502 | } | ||
| 503 | |||
| 504 | if (!PKCS7_set_type(p7, NID_pkcs7_enveloped)) | ||
| 505 | goto err; | ||
| 506 | if (!PKCS7_set_cipher(p7, cipher)) { | ||
| 507 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER); | ||
| 508 | goto err; | ||
| 509 | } | ||
| 510 | |||
| 511 | for(i = 0; i < sk_X509_num(certs); i++) { | ||
| 512 | x509 = sk_X509_value(certs, i); | ||
| 513 | if(!PKCS7_add_recipient(p7, x509)) { | ||
| 514 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, | ||
| 515 | PKCS7_R_ERROR_ADDING_RECIPIENT); | ||
| 516 | goto err; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | if (flags & PKCS7_STREAM) | ||
| 521 | return p7; | ||
| 522 | |||
| 523 | if (PKCS7_final(p7, in, flags)) | ||
| 524 | return p7; | ||
| 525 | |||
| 526 | err: | ||
| 527 | |||
| 528 | BIO_free_all(p7bio); | ||
| 529 | PKCS7_free(p7); | ||
| 530 | return NULL; | ||
| 531 | |||
| 532 | } | ||
| 533 | |||
| 534 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) | ||
| 535 | { | ||
| 536 | BIO *tmpmem; | ||
| 537 | int ret, i; | ||
| 538 | char buf[4096]; | ||
| 539 | |||
| 540 | if(!p7) { | ||
| 541 | PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_INVALID_NULL_POINTER); | ||
| 542 | return 0; | ||
| 543 | } | ||
| 544 | |||
| 545 | if(!PKCS7_type_is_enveloped(p7)) { | ||
| 546 | PKCS7err(PKCS7_F_PKCS7_DECRYPT,PKCS7_R_WRONG_CONTENT_TYPE); | ||
| 547 | return 0; | ||
| 548 | } | ||
| 549 | |||
| 550 | if(cert && !X509_check_private_key(cert, pkey)) { | ||
| 551 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, | ||
| 552 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | ||
| 553 | return 0; | ||
| 554 | } | ||
| 555 | |||
| 556 | if(!(tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert))) { | ||
| 557 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR); | ||
| 558 | return 0; | ||
| 559 | } | ||
| 560 | |||
| 561 | if (flags & PKCS7_TEXT) { | ||
| 562 | BIO *tmpbuf, *bread; | ||
| 563 | /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ | ||
| 564 | if(!(tmpbuf = BIO_new(BIO_f_buffer()))) { | ||
| 565 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | ||
| 566 | BIO_free_all(tmpmem); | ||
| 567 | return 0; | ||
| 568 | } | ||
| 569 | if(!(bread = BIO_push(tmpbuf, tmpmem))) { | ||
| 570 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | ||
| 571 | BIO_free_all(tmpbuf); | ||
| 572 | BIO_free_all(tmpmem); | ||
| 573 | return 0; | ||
| 574 | } | ||
| 575 | ret = SMIME_text(bread, data); | ||
| 576 | BIO_free_all(bread); | ||
| 577 | return ret; | ||
| 578 | } else { | ||
| 579 | for(;;) { | ||
| 580 | i = BIO_read(tmpmem, buf, sizeof(buf)); | ||
| 581 | if(i <= 0) break; | ||
| 582 | BIO_write(data, buf, i); | ||
| 583 | } | ||
| 584 | BIO_free_all(tmpmem); | ||
| 585 | return 1; | ||
| 586 | } | ||
| 587 | } | ||
diff --git a/src/lib/libcrypto/pkcs7/pkcs7.h b/src/lib/libcrypto/pkcs7/pkcs7.h new file mode 100644 index 0000000000..e4d443193c --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pkcs7.h | |||
| @@ -0,0 +1,499 @@ | |||
| 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 | #define PKCS7_type_is_digest(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_digest) | ||
| 236 | #define PKCS7_type_is_encrypted(a) \ | ||
| 237 | (OBJ_obj2nid((a)->type) == NID_pkcs7_encrypted) | ||
| 238 | |||
| 239 | #define PKCS7_type_is_digest(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_digest) | ||
| 240 | |||
| 241 | #define PKCS7_set_detached(p,v) \ | ||
| 242 | PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL) | ||
| 243 | #define PKCS7_get_detached(p) \ | ||
| 244 | PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL) | ||
| 245 | |||
| 246 | #define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7)) | ||
| 247 | |||
| 248 | /* S/MIME related flags */ | ||
| 249 | |||
| 250 | #define PKCS7_TEXT 0x1 | ||
| 251 | #define PKCS7_NOCERTS 0x2 | ||
| 252 | #define PKCS7_NOSIGS 0x4 | ||
| 253 | #define PKCS7_NOCHAIN 0x8 | ||
| 254 | #define PKCS7_NOINTERN 0x10 | ||
| 255 | #define PKCS7_NOVERIFY 0x20 | ||
| 256 | #define PKCS7_DETACHED 0x40 | ||
| 257 | #define PKCS7_BINARY 0x80 | ||
| 258 | #define PKCS7_NOATTR 0x100 | ||
| 259 | #define PKCS7_NOSMIMECAP 0x200 | ||
| 260 | #define PKCS7_NOOLDMIMETYPE 0x400 | ||
| 261 | #define PKCS7_CRLFEOL 0x800 | ||
| 262 | #define PKCS7_STREAM 0x1000 | ||
| 263 | #define PKCS7_NOCRL 0x2000 | ||
| 264 | #define PKCS7_PARTIAL 0x4000 | ||
| 265 | #define PKCS7_REUSE_DIGEST 0x8000 | ||
| 266 | |||
| 267 | /* Flags: for compatibility with older code */ | ||
| 268 | |||
| 269 | #define SMIME_TEXT PKCS7_TEXT | ||
| 270 | #define SMIME_NOCERTS PKCS7_NOCERTS | ||
| 271 | #define SMIME_NOSIGS PKCS7_NOSIGS | ||
| 272 | #define SMIME_NOCHAIN PKCS7_NOCHAIN | ||
| 273 | #define SMIME_NOINTERN PKCS7_NOINTERN | ||
| 274 | #define SMIME_NOVERIFY PKCS7_NOVERIFY | ||
| 275 | #define SMIME_DETACHED PKCS7_DETACHED | ||
| 276 | #define SMIME_BINARY PKCS7_BINARY | ||
| 277 | #define SMIME_NOATTR PKCS7_NOATTR | ||
| 278 | |||
| 279 | DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) | ||
| 280 | |||
| 281 | int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,const EVP_MD *type, | ||
| 282 | unsigned char *md,unsigned int *len); | ||
| 283 | #ifndef OPENSSL_NO_FP_API | ||
| 284 | PKCS7 *d2i_PKCS7_fp(FILE *fp,PKCS7 **p7); | ||
| 285 | int i2d_PKCS7_fp(FILE *fp,PKCS7 *p7); | ||
| 286 | #endif | ||
| 287 | PKCS7 *PKCS7_dup(PKCS7 *p7); | ||
| 288 | PKCS7 *d2i_PKCS7_bio(BIO *bp,PKCS7 **p7); | ||
| 289 | int i2d_PKCS7_bio(BIO *bp,PKCS7 *p7); | ||
| 290 | int i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags); | ||
| 291 | int PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags); | ||
| 292 | |||
| 293 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) | ||
| 294 | DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) | ||
| 295 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED) | ||
| 296 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) | ||
| 297 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE) | ||
| 298 | DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) | ||
| 299 | DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST) | ||
| 300 | DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT) | ||
| 301 | DECLARE_ASN1_FUNCTIONS(PKCS7) | ||
| 302 | |||
| 303 | DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN) | ||
| 304 | DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY) | ||
| 305 | |||
| 306 | DECLARE_ASN1_NDEF_FUNCTION(PKCS7) | ||
| 307 | DECLARE_ASN1_PRINT_FUNCTION(PKCS7) | ||
| 308 | |||
| 309 | long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg); | ||
| 310 | |||
| 311 | int PKCS7_set_type(PKCS7 *p7, int type); | ||
| 312 | int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other); | ||
| 313 | int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data); | ||
| 314 | int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, | ||
| 315 | const EVP_MD *dgst); | ||
| 316 | int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si); | ||
| 317 | int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i); | ||
| 318 | int PKCS7_add_certificate(PKCS7 *p7, X509 *x509); | ||
| 319 | int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509); | ||
| 320 | int PKCS7_content_new(PKCS7 *p7, int nid); | ||
| 321 | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, | ||
| 322 | BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si); | ||
| 323 | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, | ||
| 324 | X509 *x509); | ||
| 325 | |||
| 326 | BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio); | ||
| 327 | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio); | ||
| 328 | BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert); | ||
| 329 | |||
| 330 | |||
| 331 | PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, | ||
| 332 | EVP_PKEY *pkey, const EVP_MD *dgst); | ||
| 333 | X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si); | ||
| 334 | int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md); | ||
| 335 | STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); | ||
| 336 | |||
| 337 | PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509); | ||
| 338 | void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, | ||
| 339 | X509_ALGOR **pdig, X509_ALGOR **psig); | ||
| 340 | void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc); | ||
| 341 | int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri); | ||
| 342 | int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509); | ||
| 343 | int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher); | ||
| 344 | int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7); | ||
| 345 | |||
| 346 | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx); | ||
| 347 | ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 348 | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si,int nid,int type, | ||
| 349 | void *data); | ||
| 350 | int PKCS7_add_attribute (PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, | ||
| 351 | void *value); | ||
| 352 | ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid); | ||
| 353 | ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid); | ||
| 354 | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, | ||
| 355 | STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 356 | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,STACK_OF(X509_ATTRIBUTE) *sk); | ||
| 357 | |||
| 358 | |||
| 359 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | ||
| 360 | BIO *data, int flags); | ||
| 361 | |||
| 362 | PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, | ||
| 363 | X509 *signcert, EVP_PKEY *pkey, const EVP_MD *md, | ||
| 364 | int flags); | ||
| 365 | |||
| 366 | int PKCS7_final(PKCS7 *p7, BIO *data, int flags); | ||
| 367 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | ||
| 368 | BIO *indata, BIO *out, int flags); | ||
| 369 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags); | ||
| 370 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | ||
| 371 | int flags); | ||
| 372 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags); | ||
| 373 | |||
| 374 | int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, | ||
| 375 | STACK_OF(X509_ALGOR) *cap); | ||
| 376 | STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si); | ||
| 377 | int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg); | ||
| 378 | |||
| 379 | int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid); | ||
| 380 | int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t); | ||
| 381 | int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si, | ||
| 382 | const unsigned char *md, int mdlen); | ||
| 383 | |||
| 384 | int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags); | ||
| 385 | PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont); | ||
| 386 | |||
| 387 | BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7); | ||
| 388 | |||
| 389 | |||
| 390 | /* BEGIN ERROR CODES */ | ||
| 391 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 392 | * made after this point may be overwritten when the script is next run. | ||
| 393 | */ | ||
| 394 | void ERR_load_PKCS7_strings(void); | ||
| 395 | |||
| 396 | /* Error codes for the PKCS7 functions. */ | ||
| 397 | |||
| 398 | /* Function codes. */ | ||
| 399 | #define PKCS7_F_B64_READ_PKCS7 120 | ||
| 400 | #define PKCS7_F_B64_WRITE_PKCS7 121 | ||
| 401 | #define PKCS7_F_DO_PKCS7_SIGNED_ATTRIB 136 | ||
| 402 | #define PKCS7_F_I2D_PKCS7_BIO_STREAM 140 | ||
| 403 | #define PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME 135 | ||
| 404 | #define PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP 118 | ||
| 405 | #define PKCS7_F_PKCS7_ADD_CERTIFICATE 100 | ||
| 406 | #define PKCS7_F_PKCS7_ADD_CRL 101 | ||
| 407 | #define PKCS7_F_PKCS7_ADD_RECIPIENT_INFO 102 | ||
| 408 | #define PKCS7_F_PKCS7_ADD_SIGNATURE 131 | ||
| 409 | #define PKCS7_F_PKCS7_ADD_SIGNER 103 | ||
| 410 | #define PKCS7_F_PKCS7_BIO_ADD_DIGEST 125 | ||
| 411 | #define PKCS7_F_PKCS7_COPY_EXISTING_DIGEST 138 | ||
| 412 | #define PKCS7_F_PKCS7_CTRL 104 | ||
| 413 | #define PKCS7_F_PKCS7_DATADECODE 112 | ||
| 414 | #define PKCS7_F_PKCS7_DATAFINAL 128 | ||
| 415 | #define PKCS7_F_PKCS7_DATAINIT 105 | ||
| 416 | #define PKCS7_F_PKCS7_DATASIGN 106 | ||
| 417 | #define PKCS7_F_PKCS7_DATAVERIFY 107 | ||
| 418 | #define PKCS7_F_PKCS7_DECRYPT 114 | ||
| 419 | #define PKCS7_F_PKCS7_DECRYPT_RINFO 133 | ||
| 420 | #define PKCS7_F_PKCS7_ENCODE_RINFO 132 | ||
| 421 | #define PKCS7_F_PKCS7_ENCRYPT 115 | ||
| 422 | #define PKCS7_F_PKCS7_FINAL 134 | ||
| 423 | #define PKCS7_F_PKCS7_FIND_DIGEST 127 | ||
| 424 | #define PKCS7_F_PKCS7_GET0_SIGNERS 124 | ||
| 425 | #define PKCS7_F_PKCS7_RECIP_INFO_SET 130 | ||
| 426 | #define PKCS7_F_PKCS7_SET_CIPHER 108 | ||
| 427 | #define PKCS7_F_PKCS7_SET_CONTENT 109 | ||
| 428 | #define PKCS7_F_PKCS7_SET_DIGEST 126 | ||
| 429 | #define PKCS7_F_PKCS7_SET_TYPE 110 | ||
| 430 | #define PKCS7_F_PKCS7_SIGN 116 | ||
| 431 | #define PKCS7_F_PKCS7_SIGNATUREVERIFY 113 | ||
| 432 | #define PKCS7_F_PKCS7_SIGNER_INFO_SET 129 | ||
| 433 | #define PKCS7_F_PKCS7_SIGNER_INFO_SIGN 139 | ||
| 434 | #define PKCS7_F_PKCS7_SIGN_ADD_SIGNER 137 | ||
| 435 | #define PKCS7_F_PKCS7_SIMPLE_SMIMECAP 119 | ||
| 436 | #define PKCS7_F_PKCS7_VERIFY 117 | ||
| 437 | #define PKCS7_F_SMIME_READ_PKCS7 122 | ||
| 438 | #define PKCS7_F_SMIME_TEXT 123 | ||
| 439 | |||
| 440 | /* Reason codes. */ | ||
| 441 | #define PKCS7_R_CERTIFICATE_VERIFY_ERROR 117 | ||
| 442 | #define PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 144 | ||
| 443 | #define PKCS7_R_CIPHER_NOT_INITIALIZED 116 | ||
| 444 | #define PKCS7_R_CONTENT_AND_DATA_PRESENT 118 | ||
| 445 | #define PKCS7_R_CTRL_ERROR 152 | ||
| 446 | #define PKCS7_R_DECODE_ERROR 130 | ||
| 447 | #define PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH 100 | ||
| 448 | #define PKCS7_R_DECRYPT_ERROR 119 | ||
| 449 | #define PKCS7_R_DIGEST_FAILURE 101 | ||
| 450 | #define PKCS7_R_ENCRYPTION_CTRL_FAILURE 149 | ||
| 451 | #define PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 150 | ||
| 452 | #define PKCS7_R_ERROR_ADDING_RECIPIENT 120 | ||
| 453 | #define PKCS7_R_ERROR_SETTING_CIPHER 121 | ||
| 454 | #define PKCS7_R_INVALID_MIME_TYPE 131 | ||
| 455 | #define PKCS7_R_INVALID_NULL_POINTER 143 | ||
| 456 | #define PKCS7_R_MIME_NO_CONTENT_TYPE 132 | ||
| 457 | #define PKCS7_R_MIME_PARSE_ERROR 133 | ||
| 458 | #define PKCS7_R_MIME_SIG_PARSE_ERROR 134 | ||
| 459 | #define PKCS7_R_MISSING_CERIPEND_INFO 103 | ||
| 460 | #define PKCS7_R_NO_CONTENT 122 | ||
| 461 | #define PKCS7_R_NO_CONTENT_TYPE 135 | ||
| 462 | #define PKCS7_R_NO_DEFAULT_DIGEST 151 | ||
| 463 | #define PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND 154 | ||
| 464 | #define PKCS7_R_NO_MULTIPART_BODY_FAILURE 136 | ||
| 465 | #define PKCS7_R_NO_MULTIPART_BOUNDARY 137 | ||
| 466 | #define PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE 115 | ||
| 467 | #define PKCS7_R_NO_RECIPIENT_MATCHES_KEY 146 | ||
| 468 | #define PKCS7_R_NO_SIGNATURES_ON_DATA 123 | ||
| 469 | #define PKCS7_R_NO_SIGNERS 142 | ||
| 470 | #define PKCS7_R_NO_SIG_CONTENT_TYPE 138 | ||
| 471 | #define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE 104 | ||
| 472 | #define PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR 124 | ||
| 473 | #define PKCS7_R_PKCS7_ADD_SIGNER_ERROR 153 | ||
| 474 | #define PKCS7_R_PKCS7_DATAFINAL 126 | ||
| 475 | #define PKCS7_R_PKCS7_DATAFINAL_ERROR 125 | ||
| 476 | #define PKCS7_R_PKCS7_DATASIGN 145 | ||
| 477 | #define PKCS7_R_PKCS7_PARSE_ERROR 139 | ||
| 478 | #define PKCS7_R_PKCS7_SIG_PARSE_ERROR 140 | ||
| 479 | #define PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 127 | ||
| 480 | #define PKCS7_R_SIGNATURE_FAILURE 105 | ||
| 481 | #define PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND 128 | ||
| 482 | #define PKCS7_R_SIGNING_CTRL_FAILURE 147 | ||
| 483 | #define PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 148 | ||
| 484 | #define PKCS7_R_SIG_INVALID_MIME_TYPE 141 | ||
| 485 | #define PKCS7_R_SMIME_TEXT_ERROR 129 | ||
| 486 | #define PKCS7_R_UNABLE_TO_FIND_CERTIFICATE 106 | ||
| 487 | #define PKCS7_R_UNABLE_TO_FIND_MEM_BIO 107 | ||
| 488 | #define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST 108 | ||
| 489 | #define PKCS7_R_UNKNOWN_DIGEST_TYPE 109 | ||
| 490 | #define PKCS7_R_UNKNOWN_OPERATION 110 | ||
| 491 | #define PKCS7_R_UNSUPPORTED_CIPHER_TYPE 111 | ||
| 492 | #define PKCS7_R_UNSUPPORTED_CONTENT_TYPE 112 | ||
| 493 | #define PKCS7_R_WRONG_CONTENT_TYPE 113 | ||
| 494 | #define PKCS7_R_WRONG_PKCS7_TYPE 114 | ||
| 495 | |||
| 496 | #ifdef __cplusplus | ||
| 497 | } | ||
| 498 | #endif | ||
| 499 | #endif | ||
diff --git a/src/lib/libcrypto/pkcs7/pkcs7err.c b/src/lib/libcrypto/pkcs7/pkcs7err.c new file mode 100644 index 0000000000..d0af32a265 --- /dev/null +++ b/src/lib/libcrypto/pkcs7/pkcs7err.c | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | /* crypto/pkcs7/pkcs7err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999-2007 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_DO_PKCS7_SIGNED_ATTRIB), "DO_PKCS7_SIGNED_ATTRIB"}, | ||
| 76 | {ERR_FUNC(PKCS7_F_I2D_PKCS7_BIO_STREAM), "i2d_PKCS7_bio_stream"}, | ||
| 77 | {ERR_FUNC(PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME), "PKCS7_add0_attrib_signing_time"}, | ||
| 78 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP), "PKCS7_add_attrib_smimecap"}, | ||
| 79 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_CERTIFICATE), "PKCS7_add_certificate"}, | ||
| 80 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_CRL), "PKCS7_add_crl"}, | ||
| 81 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO), "PKCS7_add_recipient_info"}, | ||
| 82 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_SIGNATURE), "PKCS7_add_signature"}, | ||
| 83 | {ERR_FUNC(PKCS7_F_PKCS7_ADD_SIGNER), "PKCS7_add_signer"}, | ||
| 84 | {ERR_FUNC(PKCS7_F_PKCS7_BIO_ADD_DIGEST), "PKCS7_BIO_ADD_DIGEST"}, | ||
| 85 | {ERR_FUNC(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST), "PKCS7_COPY_EXISTING_DIGEST"}, | ||
| 86 | {ERR_FUNC(PKCS7_F_PKCS7_CTRL), "PKCS7_ctrl"}, | ||
| 87 | {ERR_FUNC(PKCS7_F_PKCS7_DATADECODE), "PKCS7_dataDecode"}, | ||
| 88 | {ERR_FUNC(PKCS7_F_PKCS7_DATAFINAL), "PKCS7_dataFinal"}, | ||
| 89 | {ERR_FUNC(PKCS7_F_PKCS7_DATAINIT), "PKCS7_dataInit"}, | ||
| 90 | {ERR_FUNC(PKCS7_F_PKCS7_DATASIGN), "PKCS7_DATASIGN"}, | ||
| 91 | {ERR_FUNC(PKCS7_F_PKCS7_DATAVERIFY), "PKCS7_dataVerify"}, | ||
| 92 | {ERR_FUNC(PKCS7_F_PKCS7_DECRYPT), "PKCS7_decrypt"}, | ||
| 93 | {ERR_FUNC(PKCS7_F_PKCS7_DECRYPT_RINFO), "PKCS7_DECRYPT_RINFO"}, | ||
| 94 | {ERR_FUNC(PKCS7_F_PKCS7_ENCODE_RINFO), "PKCS7_ENCODE_RINFO"}, | ||
| 95 | {ERR_FUNC(PKCS7_F_PKCS7_ENCRYPT), "PKCS7_encrypt"}, | ||
| 96 | {ERR_FUNC(PKCS7_F_PKCS7_FINAL), "PKCS7_final"}, | ||
| 97 | {ERR_FUNC(PKCS7_F_PKCS7_FIND_DIGEST), "PKCS7_FIND_DIGEST"}, | ||
| 98 | {ERR_FUNC(PKCS7_F_PKCS7_GET0_SIGNERS), "PKCS7_get0_signers"}, | ||
| 99 | {ERR_FUNC(PKCS7_F_PKCS7_RECIP_INFO_SET), "PKCS7_RECIP_INFO_set"}, | ||
| 100 | {ERR_FUNC(PKCS7_F_PKCS7_SET_CIPHER), "PKCS7_set_cipher"}, | ||
| 101 | {ERR_FUNC(PKCS7_F_PKCS7_SET_CONTENT), "PKCS7_set_content"}, | ||
| 102 | {ERR_FUNC(PKCS7_F_PKCS7_SET_DIGEST), "PKCS7_set_digest"}, | ||
| 103 | {ERR_FUNC(PKCS7_F_PKCS7_SET_TYPE), "PKCS7_set_type"}, | ||
| 104 | {ERR_FUNC(PKCS7_F_PKCS7_SIGN), "PKCS7_sign"}, | ||
| 105 | {ERR_FUNC(PKCS7_F_PKCS7_SIGNATUREVERIFY), "PKCS7_signatureVerify"}, | ||
| 106 | {ERR_FUNC(PKCS7_F_PKCS7_SIGNER_INFO_SET), "PKCS7_SIGNER_INFO_set"}, | ||
| 107 | {ERR_FUNC(PKCS7_F_PKCS7_SIGNER_INFO_SIGN), "PKCS7_SIGNER_INFO_sign"}, | ||
| 108 | {ERR_FUNC(PKCS7_F_PKCS7_SIGN_ADD_SIGNER), "PKCS7_sign_add_signer"}, | ||
| 109 | {ERR_FUNC(PKCS7_F_PKCS7_SIMPLE_SMIMECAP), "PKCS7_simple_smimecap"}, | ||
| 110 | {ERR_FUNC(PKCS7_F_PKCS7_VERIFY), "PKCS7_verify"}, | ||
| 111 | {ERR_FUNC(PKCS7_F_SMIME_READ_PKCS7), "SMIME_read_PKCS7"}, | ||
| 112 | {ERR_FUNC(PKCS7_F_SMIME_TEXT), "SMIME_text"}, | ||
| 113 | {0,NULL} | ||
| 114 | }; | ||
| 115 | |||
| 116 | static ERR_STRING_DATA PKCS7_str_reasons[]= | ||
| 117 | { | ||
| 118 | {ERR_REASON(PKCS7_R_CERTIFICATE_VERIFY_ERROR),"certificate verify error"}, | ||
| 119 | {ERR_REASON(PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER),"cipher has no object identifier"}, | ||
| 120 | {ERR_REASON(PKCS7_R_CIPHER_NOT_INITIALIZED),"cipher not initialized"}, | ||
| 121 | {ERR_REASON(PKCS7_R_CONTENT_AND_DATA_PRESENT),"content and data present"}, | ||
| 122 | {ERR_REASON(PKCS7_R_CTRL_ERROR) ,"ctrl error"}, | ||
| 123 | {ERR_REASON(PKCS7_R_DECODE_ERROR) ,"decode error"}, | ||
| 124 | {ERR_REASON(PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH),"decrypted key is wrong length"}, | ||
| 125 | {ERR_REASON(PKCS7_R_DECRYPT_ERROR) ,"decrypt error"}, | ||
| 126 | {ERR_REASON(PKCS7_R_DIGEST_FAILURE) ,"digest failure"}, | ||
| 127 | {ERR_REASON(PKCS7_R_ENCRYPTION_CTRL_FAILURE),"encryption ctrl failure"}, | ||
| 128 | {ERR_REASON(PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE),"encryption not supported for this key type"}, | ||
| 129 | {ERR_REASON(PKCS7_R_ERROR_ADDING_RECIPIENT),"error adding recipient"}, | ||
| 130 | {ERR_REASON(PKCS7_R_ERROR_SETTING_CIPHER),"error setting cipher"}, | ||
| 131 | {ERR_REASON(PKCS7_R_INVALID_MIME_TYPE) ,"invalid mime type"}, | ||
| 132 | {ERR_REASON(PKCS7_R_INVALID_NULL_POINTER),"invalid null pointer"}, | ||
| 133 | {ERR_REASON(PKCS7_R_MIME_NO_CONTENT_TYPE),"mime no content type"}, | ||
| 134 | {ERR_REASON(PKCS7_R_MIME_PARSE_ERROR) ,"mime parse error"}, | ||
| 135 | {ERR_REASON(PKCS7_R_MIME_SIG_PARSE_ERROR),"mime sig parse error"}, | ||
| 136 | {ERR_REASON(PKCS7_R_MISSING_CERIPEND_INFO),"missing ceripend info"}, | ||
| 137 | {ERR_REASON(PKCS7_R_NO_CONTENT) ,"no content"}, | ||
| 138 | {ERR_REASON(PKCS7_R_NO_CONTENT_TYPE) ,"no content type"}, | ||
| 139 | {ERR_REASON(PKCS7_R_NO_DEFAULT_DIGEST) ,"no default digest"}, | ||
| 140 | {ERR_REASON(PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND),"no matching digest type found"}, | ||
| 141 | {ERR_REASON(PKCS7_R_NO_MULTIPART_BODY_FAILURE),"no multipart body failure"}, | ||
| 142 | {ERR_REASON(PKCS7_R_NO_MULTIPART_BOUNDARY),"no multipart boundary"}, | ||
| 143 | {ERR_REASON(PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE),"no recipient matches certificate"}, | ||
| 144 | {ERR_REASON(PKCS7_R_NO_RECIPIENT_MATCHES_KEY),"no recipient matches key"}, | ||
| 145 | {ERR_REASON(PKCS7_R_NO_SIGNATURES_ON_DATA),"no signatures on data"}, | ||
| 146 | {ERR_REASON(PKCS7_R_NO_SIGNERS) ,"no signers"}, | ||
| 147 | {ERR_REASON(PKCS7_R_NO_SIG_CONTENT_TYPE) ,"no sig content type"}, | ||
| 148 | {ERR_REASON(PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE),"operation not supported on this type"}, | ||
| 149 | {ERR_REASON(PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR),"pkcs7 add signature error"}, | ||
| 150 | {ERR_REASON(PKCS7_R_PKCS7_ADD_SIGNER_ERROR),"pkcs7 add signer error"}, | ||
| 151 | {ERR_REASON(PKCS7_R_PKCS7_DATAFINAL) ,"pkcs7 datafinal"}, | ||
| 152 | {ERR_REASON(PKCS7_R_PKCS7_DATAFINAL_ERROR),"pkcs7 datafinal error"}, | ||
| 153 | {ERR_REASON(PKCS7_R_PKCS7_DATASIGN) ,"pkcs7 datasign"}, | ||
| 154 | {ERR_REASON(PKCS7_R_PKCS7_PARSE_ERROR) ,"pkcs7 parse error"}, | ||
| 155 | {ERR_REASON(PKCS7_R_PKCS7_SIG_PARSE_ERROR),"pkcs7 sig parse error"}, | ||
| 156 | {ERR_REASON(PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE),"private key does not match certificate"}, | ||
| 157 | {ERR_REASON(PKCS7_R_SIGNATURE_FAILURE) ,"signature failure"}, | ||
| 158 | {ERR_REASON(PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND),"signer certificate not found"}, | ||
| 159 | {ERR_REASON(PKCS7_R_SIGNING_CTRL_FAILURE),"signing ctrl failure"}, | ||
| 160 | {ERR_REASON(PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE),"signing not supported for this key type"}, | ||
| 161 | {ERR_REASON(PKCS7_R_SIG_INVALID_MIME_TYPE),"sig invalid mime type"}, | ||
| 162 | {ERR_REASON(PKCS7_R_SMIME_TEXT_ERROR) ,"smime text error"}, | ||
| 163 | {ERR_REASON(PKCS7_R_UNABLE_TO_FIND_CERTIFICATE),"unable to find certificate"}, | ||
| 164 | {ERR_REASON(PKCS7_R_UNABLE_TO_FIND_MEM_BIO),"unable to find mem bio"}, | ||
| 165 | {ERR_REASON(PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST),"unable to find message digest"}, | ||
| 166 | {ERR_REASON(PKCS7_R_UNKNOWN_DIGEST_TYPE) ,"unknown digest type"}, | ||
| 167 | {ERR_REASON(PKCS7_R_UNKNOWN_OPERATION) ,"unknown operation"}, | ||
| 168 | {ERR_REASON(PKCS7_R_UNSUPPORTED_CIPHER_TYPE),"unsupported cipher type"}, | ||
| 169 | {ERR_REASON(PKCS7_R_UNSUPPORTED_CONTENT_TYPE),"unsupported content type"}, | ||
| 170 | {ERR_REASON(PKCS7_R_WRONG_CONTENT_TYPE) ,"wrong content type"}, | ||
| 171 | {ERR_REASON(PKCS7_R_WRONG_PKCS7_TYPE) ,"wrong pkcs7 type"}, | ||
| 172 | {0,NULL} | ||
| 173 | }; | ||
| 174 | |||
| 175 | #endif | ||
| 176 | |||
| 177 | void ERR_load_PKCS7_strings(void) | ||
| 178 | { | ||
| 179 | #ifndef OPENSSL_NO_ERR | ||
| 180 | |||
| 181 | if (ERR_func_error_string(PKCS7_str_functs[0].error) == NULL) | ||
| 182 | { | ||
| 183 | ERR_load_strings(0,PKCS7_str_functs); | ||
| 184 | ERR_load_strings(0,PKCS7_str_reasons); | ||
| 185 | } | ||
| 186 | #endif | ||
| 187 | } | ||
