Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/pkcs7/pk7_asn1.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||
2 | * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. | - | ||||||
3 | * | - | ||||||
4 | * Licensed under the OpenSSL license (the "License"). You may not use | - | ||||||
5 | * this file except in compliance with the License. You can obtain a copy | - | ||||||
6 | * in the file LICENSE in the source distribution or at | - | ||||||
7 | * https://www.openssl.org/source/license.html | - | ||||||
8 | */ | - | ||||||
9 | - | |||||||
10 | #include <stdio.h> | - | ||||||
11 | #include "internal/cryptlib.h" | - | ||||||
12 | #include <openssl/asn1t.h> | - | ||||||
13 | #include <openssl/pkcs7.h> | - | ||||||
14 | #include <openssl/x509.h> | - | ||||||
15 | - | |||||||
16 | /* PKCS#7 ASN1 module */ | - | ||||||
17 | - | |||||||
18 | /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */ | - | ||||||
19 | - | |||||||
20 | ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0); | - | ||||||
21 | - | |||||||
22 | ASN1_ADB(PKCS7) = { | - | ||||||
23 | ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)), | - | ||||||
24 | ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)), | - | ||||||
25 | ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)), | - | ||||||
26 | ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)), | - | ||||||
27 | ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)), | - | ||||||
28 | ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0)) | - | ||||||
29 | } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL); | - | ||||||
30 | - | |||||||
31 | /* PKCS#7 streaming support */ | - | ||||||
32 | static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | - | ||||||
33 | void *exarg) | - | ||||||
34 | { | - | ||||||
35 | ASN1_STREAM_ARG *sarg = exarg; | - | ||||||
36 | PKCS7 **pp7 = (PKCS7 **)pval; | - | ||||||
37 | - | |||||||
38 | switch (operation) { | - | ||||||
39 | - | |||||||
40 | case ASN1_OP_STREAM_PRE: executed 9 times by 1 test: case 10: Executed by:
| 9 | ||||||
41 | if (PKCS7_stream(&sarg->boundary, *pp7) <= 0)
| 0-9 | ||||||
42 | return 0; never executed: return 0; | 0 | ||||||
43 | /* fall thru */ | - | ||||||
44 | case ASN1_OP_DETACHED_PRE: code before this statement executed 9 times by 1 test: case 12: Executed by:
executed 2 times by 1 test: case 12: Executed by:
| 2-9 | ||||||
45 | sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out); | - | ||||||
46 | if (!sarg->ndef_bio)
| 0-11 | ||||||
47 | return 0; never executed: return 0; | 0 | ||||||
48 | break; executed 11 times by 1 test: break; Executed by:
| 11 | ||||||
49 | - | |||||||
50 | case ASN1_OP_STREAM_POST: executed 9 times by 1 test: case 11: Executed by:
| 9 | ||||||
51 | case ASN1_OP_DETACHED_POST: executed 2 times by 1 test: case 13: Executed by:
| 2 | ||||||
52 | if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0)
| 0-11 | ||||||
53 | return 0; never executed: return 0; | 0 | ||||||
54 | break; executed 11 times by 1 test: break; Executed by:
| 11 | ||||||
55 | - | |||||||
56 | } | - | ||||||
57 | return 1; executed 76794 times by 1 test: return 1; Executed by:
| 76794 | ||||||
58 | } | - | ||||||
59 | - | |||||||
60 | ASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = { | - | ||||||
61 | ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT), | - | ||||||
62 | ASN1_ADB_OBJECT(PKCS7) | - | ||||||
63 | }ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7) | - | ||||||
64 | - | |||||||
65 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7) executed 57 times by 1 test: end of block Executed by:
executed 8 times by 1 test: return (PKCS7 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_it))); Executed by:
executed 12 times by 1 test: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_it))); Executed by:
executed 26 times by 1 test: return (PKCS7 *)ASN1_item_new((&(PKCS7_it))); Executed by:
| 8-57 | ||||||
66 | - | |||||||
67 | IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7) never executed: return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, (&(PKCS7_it))); | 0 | ||||||
68 | - | |||||||
69 | IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7) never executed: return ASN1_item_dup((&(PKCS7_it)), x); | 0 | ||||||
70 | - | |||||||
71 | ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = { | - | ||||||
72 | ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER), | - | ||||||
73 | ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR), | - | ||||||
74 | ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7), | - | ||||||
75 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0), | - | ||||||
76 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1), | - | ||||||
77 | ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO) | - | ||||||
78 | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED) | - | ||||||
79 | - | |||||||
80 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED) never executed: end of block never executed: return (PKCS7_SIGNED *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_SIGNED_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_SIGNED_it))); executed 11 times by 1 test: return (PKCS7_SIGNED *)ASN1_item_new((&(PKCS7_SIGNED_it))); Executed by:
| 0-11 | ||||||
81 | - | |||||||
82 | /* Minor tweak to operation: free up EVP_PKEY */ | - | ||||||
83 | static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | - | ||||||
84 | void *exarg) | - | ||||||
85 | { | - | ||||||
86 | if (operation == ASN1_OP_FREE_POST) {
| 3579-17444 | ||||||
87 | PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval; | - | ||||||
88 | EVP_PKEY_free(si->pkey); | - | ||||||
89 | } executed 3579 times by 1 test: end of block Executed by:
| 3579 | ||||||
90 | return 1; executed 21023 times by 1 test: return 1; Executed by:
| 21023 | ||||||
91 | } | - | ||||||
92 | - | |||||||
93 | ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = { | - | ||||||
94 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER), | - | ||||||
95 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | - | ||||||
96 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR), | - | ||||||
97 | /* NB this should be a SET OF but we use a SEQUENCE OF so the | - | ||||||
98 | * original order * is retained when the structure is reencoded. | - | ||||||
99 | * Since the attributes are implicitly tagged this will not affect | - | ||||||
100 | * the encoding. | - | ||||||
101 | */ | - | ||||||
102 | ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0), | - | ||||||
103 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR), | - | ||||||
104 | ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING), | - | ||||||
105 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1) | - | ||||||
106 | } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO) | - | ||||||
107 | - | |||||||
108 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) never executed: end of block never executed: return (PKCS7_SIGNER_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_SIGNER_INFO_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_SIGNER_INFO_it))); executed 24 times by 1 test: return (PKCS7_SIGNER_INFO *)ASN1_item_new((&(PKCS7_SIGNER_INFO_it))); Executed by:
| 0-24 | ||||||
109 | - | |||||||
110 | ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = { | - | ||||||
111 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME), | - | ||||||
112 | ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER) | - | ||||||
113 | } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL) | - | ||||||
114 | - | |||||||
115 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) never executed: end of block never executed: return (PKCS7_ISSUER_AND_SERIAL *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_ISSUER_AND_SERIAL_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_ISSUER_AND_SERIAL_it))); never executed: return (PKCS7_ISSUER_AND_SERIAL *)ASN1_item_new((&(PKCS7_ISSUER_AND_SERIAL_it))); | 0 | ||||||
116 | - | |||||||
117 | ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = { | - | ||||||
118 | ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER), | - | ||||||
119 | ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | - | ||||||
120 | ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT) | - | ||||||
121 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE) | - | ||||||
122 | - | |||||||
123 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE) never executed: end of block never executed: return (PKCS7_ENVELOPE *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_ENVELOPE_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_ENVELOPE_it))); executed 4 times by 1 test: return (PKCS7_ENVELOPE *)ASN1_item_new((&(PKCS7_ENVELOPE_it))); Executed by:
| 0-4 | ||||||
124 | - | |||||||
125 | /* Minor tweak to operation: free up X509 */ | - | ||||||
126 | static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | - | ||||||
127 | void *exarg) | - | ||||||
128 | { | - | ||||||
129 | if (operation == ASN1_OP_FREE_POST) {
| 3341-14379 | ||||||
130 | PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval; | - | ||||||
131 | X509_free(ri->cert); | - | ||||||
132 | } executed 3341 times by 1 test: end of block Executed by:
| 3341 | ||||||
133 | return 1; executed 17720 times by 1 test: return 1; Executed by:
| 17720 | ||||||
134 | } | - | ||||||
135 | - | |||||||
136 | ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = { | - | ||||||
137 | ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER), | - | ||||||
138 | ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL), | - | ||||||
139 | ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR), | - | ||||||
140 | ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING) | - | ||||||
141 | } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO) | - | ||||||
142 | - | |||||||
143 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) never executed: end of block never executed: return (PKCS7_RECIP_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_RECIP_INFO_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_RECIP_INFO_it))); executed 12 times by 1 test: return (PKCS7_RECIP_INFO *)ASN1_item_new((&(PKCS7_RECIP_INFO_it))); Executed by:
| 0-12 | ||||||
144 | - | |||||||
145 | ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = { | - | ||||||
146 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT), | - | ||||||
147 | ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR), | - | ||||||
148 | ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0) | - | ||||||
149 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT) | - | ||||||
150 | - | |||||||
151 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) never executed: end of block never executed: return (PKCS7_ENC_CONTENT *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_ENC_CONTENT_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_ENC_CONTENT_it))); never executed: return (PKCS7_ENC_CONTENT *)ASN1_item_new((&(PKCS7_ENC_CONTENT_it))); | 0 | ||||||
152 | - | |||||||
153 | ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = { | - | ||||||
154 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER), | - | ||||||
155 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO), | - | ||||||
156 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR), | - | ||||||
157 | ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT), | - | ||||||
158 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0), | - | ||||||
159 | ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1), | - | ||||||
160 | ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO) | - | ||||||
161 | } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE) | - | ||||||
162 | - | |||||||
163 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) never executed: end of block never executed: return (PKCS7_SIGN_ENVELOPE *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_SIGN_ENVELOPE_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_SIGN_ENVELOPE_it))); never executed: return (PKCS7_SIGN_ENVELOPE *)ASN1_item_new((&(PKCS7_SIGN_ENVELOPE_it))); | 0 | ||||||
164 | - | |||||||
165 | ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = { | - | ||||||
166 | ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER), | - | ||||||
167 | ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT) | - | ||||||
168 | } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT) | - | ||||||
169 | - | |||||||
170 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT) never executed: end of block never executed: return (PKCS7_ENCRYPT *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_ENCRYPT_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_ENCRYPT_it))); never executed: return (PKCS7_ENCRYPT *)ASN1_item_new((&(PKCS7_ENCRYPT_it))); | 0 | ||||||
171 | - | |||||||
172 | ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = { | - | ||||||
173 | ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER), | - | ||||||
174 | ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR), | - | ||||||
175 | ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7), | - | ||||||
176 | ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING) | - | ||||||
177 | } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST) | - | ||||||
178 | - | |||||||
179 | IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST) never executed: end of block never executed: return (PKCS7_DIGEST *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(PKCS7_DIGEST_it))); never executed: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(PKCS7_DIGEST_it))); never executed: return (PKCS7_DIGEST *)ASN1_item_new((&(PKCS7_DIGEST_it))); | 0 | ||||||
180 | - | |||||||
181 | /* Specials for authenticated attributes */ | - | ||||||
182 | - | |||||||
183 | /* | - | ||||||
184 | * When signing attributes we want to reorder them to match the sorted | - | ||||||
185 | * encoding. | - | ||||||
186 | */ | - | ||||||
187 | - | |||||||
188 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) = | - | ||||||
189 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | - | ||||||
190 | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN) | - | ||||||
191 | - | |||||||
192 | /* | - | ||||||
193 | * When verifying attributes we need to use the received order. So we use | - | ||||||
194 | * SEQUENCE OF and tag it to SET OF | - | ||||||
195 | */ | - | ||||||
196 | - | |||||||
197 | ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) = | - | ||||||
198 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, | - | ||||||
199 | V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE) | - | ||||||
200 | ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY) | - | ||||||
201 | - | |||||||
202 | IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7) never executed: return ASN1_item_print(out, (ASN1_VALUE *)x, indent, (&(PKCS7_it)), pctx); | 0 | ||||||
Source code | Switch to Preprocessed file |