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