2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2000-12-08 19:09:35 +00:00
|
|
|
*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2000-12-08 19:09:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2000-12-08 19:09:35 +00:00
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/pkcs7.h>
|
2001-08-05 18:02:16 +00:00
|
|
|
#include <openssl/x509.h>
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* PKCS#7 ASN1 module */
|
|
|
|
|
|
|
|
/* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
|
|
|
|
|
|
|
|
ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0);
|
|
|
|
|
|
|
|
ASN1_ADB(PKCS7) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)),
|
|
|
|
ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)),
|
|
|
|
ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)),
|
|
|
|
ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)),
|
|
|
|
ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)),
|
|
|
|
ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0))
|
2000-12-08 19:09:35 +00:00
|
|
|
} ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL);
|
|
|
|
|
2008-03-12 21:14:28 +00:00
|
|
|
/* PKCS#7 streaming support */
|
|
|
|
static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
|
2015-01-22 03:40:55 +00:00
|
|
|
void *exarg)
|
2008-03-12 21:14:28 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_STREAM_ARG *sarg = exarg;
|
|
|
|
PKCS7 **pp7 = (PKCS7 **)pval;
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
|
|
|
|
case ASN1_OP_STREAM_PRE:
|
|
|
|
if (PKCS7_stream(&sarg->boundary, *pp7) <= 0)
|
|
|
|
return 0;
|
2017-05-11 14:21:37 +00:00
|
|
|
/* fall thru */
|
2015-01-22 03:40:55 +00:00
|
|
|
case ASN1_OP_DETACHED_PRE:
|
|
|
|
sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out);
|
|
|
|
if (!sarg->ndef_bio)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASN1_OP_STREAM_POST:
|
|
|
|
case ASN1_OP_DETACHED_POST:
|
|
|
|
if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return 1;
|
2008-03-12 21:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT),
|
|
|
|
ASN1_ADB_OBJECT(PKCS7)
|
2008-03-12 21:14:28 +00:00
|
|
|
}ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7)
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2001-07-27 02:22:42 +00:00
|
|
|
IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
|
|
|
|
ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR),
|
|
|
|
ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7),
|
|
|
|
ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0),
|
|
|
|
ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1),
|
|
|
|
ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO)
|
2002-10-03 12:38:52 +00:00
|
|
|
} ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED)
|
|
|
|
|
|
|
|
/* Minor tweak to operation: free up EVP_PKEY */
|
2005-09-01 20:42:52 +00:00
|
|
|
static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
|
2015-01-22 03:40:55 +00:00
|
|
|
void *exarg)
|
2000-12-08 19:09:35 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
if (operation == ASN1_OP_FREE_POST) {
|
|
|
|
PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval;
|
|
|
|
EVP_PKEY_free(si->pkey);
|
|
|
|
}
|
|
|
|
return 1;
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER),
|
|
|
|
ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
|
|
|
|
ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR),
|
|
|
|
/* NB this should be a SET OF but we use a SEQUENCE OF so the
|
|
|
|
* original order * is retained when the structure is reencoded.
|
|
|
|
* Since the attributes are implicitly tagged this will not affect
|
|
|
|
* the encoding.
|
|
|
|
*/
|
|
|
|
ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0),
|
|
|
|
ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR),
|
|
|
|
ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING),
|
|
|
|
ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1)
|
2001-02-23 12:47:06 +00:00
|
|
|
} ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
|
|
|
|
|
|
|
|
ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME),
|
|
|
|
ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER)
|
2001-02-23 12:47:06 +00:00
|
|
|
} ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER),
|
|
|
|
ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
|
|
|
|
ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT)
|
2002-10-03 12:38:52 +00:00
|
|
|
} ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
|
|
|
|
|
2001-01-24 18:39:54 +00:00
|
|
|
/* Minor tweak to operation: free up X509 */
|
2005-09-01 20:42:52 +00:00
|
|
|
static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
|
2015-01-22 03:40:55 +00:00
|
|
|
void *exarg)
|
2001-01-24 18:39:54 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
if (operation == ASN1_OP_FREE_POST) {
|
|
|
|
PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval;
|
|
|
|
X509_free(ri->cert);
|
|
|
|
}
|
|
|
|
return 1;
|
2001-01-24 18:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER),
|
|
|
|
ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
|
|
|
|
ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR),
|
|
|
|
ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING)
|
2001-02-23 12:47:06 +00:00
|
|
|
} ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT),
|
|
|
|
ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR),
|
|
|
|
ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0)
|
2002-10-03 12:38:52 +00:00
|
|
|
} ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER),
|
|
|
|
ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
|
|
|
|
ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR),
|
|
|
|
ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT),
|
|
|
|
ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0),
|
|
|
|
ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1),
|
|
|
|
ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO)
|
2002-10-03 12:38:52 +00:00
|
|
|
} ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER),
|
|
|
|
ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT)
|
2002-10-03 12:38:52 +00:00
|
|
|
} ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT)
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER),
|
|
|
|
ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR),
|
|
|
|
ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7),
|
|
|
|
ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING)
|
2002-10-03 12:38:52 +00:00
|
|
|
} ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST)
|
2000-12-21 01:38:55 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST)
|
2000-12-31 17:31:57 +00:00
|
|
|
|
|
|
|
/* Specials for authenticated attributes */
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* When signing attributes we want to reorder them to match the sorted
|
2000-12-31 17:31:57 +00:00
|
|
|
* encoding.
|
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) =
|
|
|
|
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
|
2001-02-23 12:47:06 +00:00
|
|
|
ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN)
|
2000-12-31 17:31:57 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* When verifying attributes we need to use the received order. So we use
|
|
|
|
* SEQUENCE OF and tag it to SET OF
|
2000-12-31 17:31:57 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) =
|
|
|
|
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
|
|
|
|
V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
|
2001-02-23 12:47:06 +00:00
|
|
|
ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY)
|
2005-09-01 18:00:56 +00:00
|
|
|
|
|
|
|
IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7)
|