10Sstevel@tonic-gate /* pk7_asn.c */ 20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 30Sstevel@tonic-gate * project 2000. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate /* ==================================================================== 60Sstevel@tonic-gate * Copyright (c) 2000 The OpenSSL Project. All rights reserved. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 90Sstevel@tonic-gate * modification, are permitted provided that the following conditions 100Sstevel@tonic-gate * are met: 110Sstevel@tonic-gate * 120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 140Sstevel@tonic-gate * 150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 170Sstevel@tonic-gate * the documentation and/or other materials provided with the 180Sstevel@tonic-gate * distribution. 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 210Sstevel@tonic-gate * software must display the following acknowledgment: 220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 240Sstevel@tonic-gate * 250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 260Sstevel@tonic-gate * endorse or promote products derived from this software without 270Sstevel@tonic-gate * prior written permission. For written permission, please contact 280Sstevel@tonic-gate * licensing@OpenSSL.org. 290Sstevel@tonic-gate * 300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 320Sstevel@tonic-gate * permission of the OpenSSL Project. 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 350Sstevel@tonic-gate * acknowledgment: 360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 510Sstevel@tonic-gate * ==================================================================== 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 560Sstevel@tonic-gate * 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include <stdio.h> 600Sstevel@tonic-gate #include "cryptlib.h" 610Sstevel@tonic-gate #include <openssl/asn1t.h> 620Sstevel@tonic-gate #include <openssl/pkcs7.h> 630Sstevel@tonic-gate #include <openssl/x509.h> 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* PKCS#7 ASN1 module */ 660Sstevel@tonic-gate 670Sstevel@tonic-gate /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */ 680Sstevel@tonic-gate 690Sstevel@tonic-gate ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0); 700Sstevel@tonic-gate 710Sstevel@tonic-gate ASN1_ADB(PKCS7) = { 72*2139Sjp161948 ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)), 73*2139Sjp161948 ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)), 74*2139Sjp161948 ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)), 75*2139Sjp161948 ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)), 76*2139Sjp161948 ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)), 77*2139Sjp161948 ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0)) 780Sstevel@tonic-gate } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL); 790Sstevel@tonic-gate 80*2139Sjp161948 ASN1_NDEF_SEQUENCE(PKCS7) = { 810Sstevel@tonic-gate ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT), 820Sstevel@tonic-gate ASN1_ADB_OBJECT(PKCS7) 83*2139Sjp161948 }ASN1_NDEF_SEQUENCE_END(PKCS7) 840Sstevel@tonic-gate 850Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7) 86*2139Sjp161948 IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7) 870Sstevel@tonic-gate IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7) 880Sstevel@tonic-gate 89*2139Sjp161948 ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = { 900Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER), 910Sstevel@tonic-gate ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR), 920Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7), 930Sstevel@tonic-gate ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0), 940Sstevel@tonic-gate ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1), 950Sstevel@tonic-gate ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO) 96*2139Sjp161948 } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED) 970Sstevel@tonic-gate 980Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED) 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* Minor tweak to operation: free up EVP_PKEY */ 1010Sstevel@tonic-gate static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate if(operation == ASN1_OP_FREE_POST) { 1040Sstevel@tonic-gate PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval; 1050Sstevel@tonic-gate EVP_PKEY_free(si->pkey); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate return 1; 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = { 1110Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER), 1120Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), 1130Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR), 1140Sstevel@tonic-gate /* NB this should be a SET OF but we use a SEQUENCE OF so the 1150Sstevel@tonic-gate * original order * is retained when the structure is reencoded. 1160Sstevel@tonic-gate * Since the attributes are implicitly tagged this will not affect 1170Sstevel@tonic-gate * the encoding. 1180Sstevel@tonic-gate */ 1190Sstevel@tonic-gate ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0), 1200Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR), 1210Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING), 1220Sstevel@tonic-gate ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1) 1230Sstevel@tonic-gate } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO) 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = { 1280Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME), 1290Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER) 1300Sstevel@tonic-gate } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL) 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) 1330Sstevel@tonic-gate 134*2139Sjp161948 ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = { 1350Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER), 1360Sstevel@tonic-gate ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), 1370Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT) 138*2139Sjp161948 } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE) 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE) 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* Minor tweak to operation: free up X509 */ 1430Sstevel@tonic-gate static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) 1440Sstevel@tonic-gate { 1450Sstevel@tonic-gate if(operation == ASN1_OP_FREE_POST) { 1460Sstevel@tonic-gate PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval; 1470Sstevel@tonic-gate X509_free(ri->cert); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate return 1; 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = { 1530Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER), 1540Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), 1550Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR), 1560Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING) 1570Sstevel@tonic-gate } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO) 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) 1600Sstevel@tonic-gate 161*2139Sjp161948 ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = { 1620Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT), 1630Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR), 1640Sstevel@tonic-gate ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING, 0) 165*2139Sjp161948 } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT) 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) 1680Sstevel@tonic-gate 169*2139Sjp161948 ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = { 1700Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER), 1710Sstevel@tonic-gate ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), 1720Sstevel@tonic-gate ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR), 1730Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT), 1740Sstevel@tonic-gate ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0), 1750Sstevel@tonic-gate ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1), 1760Sstevel@tonic-gate ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO) 177*2139Sjp161948 } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE) 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) 1800Sstevel@tonic-gate 181*2139Sjp161948 ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = { 1820Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER), 1830Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT) 184*2139Sjp161948 } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT) 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT) 1870Sstevel@tonic-gate 188*2139Sjp161948 ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = { 1890Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER), 1900Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR), 1910Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7), 1920Sstevel@tonic-gate ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING) 193*2139Sjp161948 } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST) 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST) 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* Specials for authenticated attributes */ 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* When signing attributes we want to reorder them to match the sorted 2000Sstevel@tonic-gate * encoding. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) = 2040Sstevel@tonic-gate ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) 2050Sstevel@tonic-gate ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN) 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* When verifying attributes we need to use the received order. So 2080Sstevel@tonic-gate * we use SEQUENCE OF and tag it to SET OF 2090Sstevel@tonic-gate */ 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) = 2120Sstevel@tonic-gate ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, 2130Sstevel@tonic-gate V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) 2140Sstevel@tonic-gate ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY) 215