xref: /openssl/test/pkcs7_test.c (revision 8cfc26e6)
1 /*
2  * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <string.h>
11 #include <openssl/pkcs7.h>
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/pem.h>
15 #include "internal/nelem.h"
16 #include "testutil.h"
17 
18 #ifndef OPENSSL_NO_EC
19 static const unsigned char cert_der[] = {
20     0x30, 0x82, 0x01, 0x51, 0x30, 0x81, 0xf7, 0xa0, 0x03, 0x02, 0x01, 0x02,
21     0x02, 0x02, 0x03, 0x09, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
22     0x3d, 0x04, 0x03, 0x02, 0x30, 0x27, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
23     0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06,
24     0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f,
25     0x67, 0x72, 0x61, 0x70, 0x68, 0x79, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17,
26     0x0d, 0x31, 0x37, 0x30, 0x31, 0x30, 0x31, 0x31, 0x32, 0x30, 0x31, 0x30,
27     0x30, 0x5a, 0x17, 0x0d, 0x33, 0x38, 0x31, 0x32, 0x33, 0x31, 0x30, 0x38,
28     0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x27, 0x31, 0x0b, 0x30, 0x09, 0x06,
29     0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16,
30     0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x63, 0x72, 0x79, 0x70, 0x74,
31     0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x79, 0x20, 0x43, 0x41, 0x30, 0x59,
32     0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06,
33     0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00,
34     0x04, 0x18, 0xff, 0xcf, 0xbb, 0xf9, 0x39, 0xb8, 0xf5, 0xdd, 0xc3, 0xee,
35     0xc0, 0x40, 0x8b, 0x06, 0x75, 0x06, 0xab, 0x4f, 0xcd, 0xd8, 0x2c, 0x52,
36     0x24, 0x4e, 0x1f, 0xe0, 0x10, 0x46, 0x67, 0xb5, 0x5f, 0x15, 0xb9, 0x62,
37     0xbd, 0x3b, 0xcf, 0x0c, 0x6f, 0xbe, 0x1a, 0xf7, 0xb4, 0xa1, 0x0f, 0xb4,
38     0xb9, 0xcb, 0x6e, 0x86, 0xb3, 0x50, 0xf9, 0x6c, 0x51, 0xbf, 0xc1, 0x82,
39     0xd7, 0xbe, 0xc5, 0xf9, 0x05, 0xa3, 0x13, 0x30, 0x11, 0x30, 0x0f, 0x06,
40     0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01,
41     0x01, 0xff, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04,
42     0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xd1, 0x12,
43     0xef, 0x8d, 0x97, 0x5a, 0x6e, 0xb8, 0xb6, 0x41, 0xa7, 0xcf, 0xc0, 0xe7,
44     0xa4, 0x6e, 0xae, 0xda, 0x51, 0xe4, 0x64, 0x54, 0x2b, 0xde, 0x86, 0x95,
45     0xbc, 0xf7, 0x1e, 0x9a, 0xf9, 0x5b, 0x02, 0x21, 0x00, 0xd1, 0x61, 0x86,
46     0xce, 0x66, 0x31, 0xe4, 0x2f, 0x54, 0xbd, 0xf5, 0xc8, 0x2b, 0xb3, 0x44,
47     0xce, 0x24, 0xf8, 0xa5, 0x0b, 0x72, 0x11, 0x21, 0x34, 0xb9, 0x15, 0x4a,
48     0x5f, 0x0e, 0x27, 0x32, 0xa9
49 };
50 
51 static const unsigned char smroot_der[] = {
52     0x30, 0x82, 0x03, 0x7B, 0x30, 0x82, 0x02, 0x63, 0xA0, 0x03, 0x02, 0x01,
53     0x02, 0x02, 0x14, 0x07, 0x18, 0x76, 0x2F, 0x72, 0x2D, 0xB1, 0x53, 0xEE,
54     0x06, 0x88, 0x03, 0x23, 0x45, 0x9F, 0x51, 0x7D, 0x65, 0x16, 0x73, 0x30,
55     0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
56     0x05, 0x00, 0x30, 0x44, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
57     0x06, 0x13, 0x02, 0x55, 0x4B, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55,
58     0x04, 0x0A, 0x0C, 0x0D, 0x4F, 0x70, 0x65, 0x6E, 0x53, 0x53, 0x4C, 0x20,
59     0x47, 0x72, 0x6F, 0x75, 0x70, 0x31, 0x1D, 0x30, 0x1B, 0x06, 0x03, 0x55,
60     0x04, 0x03, 0x0C, 0x14, 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x2F, 0x4D,
61     0x49, 0x4D, 0x45, 0x20, 0x52, 0x53, 0x41, 0x20, 0x52, 0x6F, 0x6F, 0x74,
62     0x30, 0x20, 0x17, 0x0D, 0x32, 0x32, 0x30, 0x36, 0x30, 0x32, 0x31, 0x35,
63     0x33, 0x33, 0x31, 0x33, 0x5A, 0x18, 0x0F, 0x32, 0x31, 0x32, 0x32, 0x30,
64     0x35, 0x31, 0x30, 0x31, 0x35, 0x33, 0x33, 0x31, 0x33, 0x5A, 0x30, 0x44,
65     0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
66     0x4B, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0D,
67     0x4F, 0x70, 0x65, 0x6E, 0x53, 0x53, 0x4C, 0x20, 0x47, 0x72, 0x6F, 0x75,
68     0x70, 0x31, 0x1D, 0x30, 0x1B, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x14,
69     0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x2F, 0x4D, 0x49, 0x4D, 0x45, 0x20,
70     0x52, 0x53, 0x41, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x30, 0x82, 0x01, 0x22,
71     0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
72     0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82, 0x01, 0x0A,
73     0x02, 0x82, 0x01, 0x01, 0x00, 0xD9, 0x2D, 0x29, 0x7C, 0x2D, 0xD5, 0x39,
74     0xE0, 0xE5, 0x00, 0x4F, 0xC7, 0x2D, 0x16, 0xE2, 0x8B, 0x4A, 0x12, 0x6E,
75     0x97, 0x62, 0x0C, 0xCD, 0xA1, 0x03, 0x90, 0x52, 0xE8, 0x2F, 0x86, 0xA0,
76     0x71, 0xC6, 0xA3, 0x82, 0xFB, 0x11, 0xC5, 0x6F, 0xBC, 0xBD, 0xB7, 0x36,
77     0x45, 0xED, 0x32, 0xD6, 0x65, 0x4C, 0xBD, 0xF3, 0x73, 0xEC, 0x28, 0x39,
78     0x6B, 0xB2, 0x9E, 0x89, 0x2B, 0x85, 0x24, 0xEA, 0xD0, 0xB6, 0xF0, 0xED,
79     0xF8, 0x79, 0x14, 0xE5, 0xCE, 0xF4, 0xE9, 0x80, 0xE6, 0xC3, 0x5E, 0x66,
80     0x06, 0x90, 0xD2, 0x1D, 0xAA, 0x64, 0x5E, 0xBF, 0x34, 0x1B, 0xF0, 0x01,
81     0x15, 0xBB, 0x2D, 0x59, 0xA7, 0xCE, 0x7D, 0xA8, 0x15, 0x5B, 0x8E, 0x70,
82     0xBF, 0xAE, 0x4A, 0x1F, 0x87, 0x27, 0xA5, 0xCA, 0xB0, 0xAC, 0xB8, 0x5A,
83     0x82, 0x60, 0xBC, 0x40, 0x6D, 0x0A, 0x7D, 0x66, 0xDF, 0xF9, 0x9D, 0x9E,
84     0x7C, 0xEC, 0x5A, 0x1A, 0xEF, 0xDD, 0x4C, 0xD0, 0x37, 0x0A, 0x7A, 0xBF,
85     0x05, 0x01, 0x93, 0x76, 0x59, 0x10, 0xDB, 0xCC, 0x5B, 0xAE, 0xF7, 0x7E,
86     0x51, 0xBC, 0xE0, 0x71, 0x8E, 0x18, 0xFE, 0x12, 0x7B, 0x17, 0x0D, 0x7F,
87     0x21, 0xE1, 0x21, 0x82, 0xBF, 0xE2, 0x54, 0x98, 0x50, 0x21, 0x40, 0x24,
88     0x96, 0x9F, 0x41, 0xFB, 0x1A, 0x83, 0x7D, 0x6B, 0xB1, 0x22, 0xDE, 0x66,
89     0x37, 0x05, 0x96, 0x87, 0xF2, 0xDF, 0x93, 0xE5, 0x31, 0xA8, 0x4F, 0x7D,
90     0x12, 0x14, 0x11, 0xD1, 0x86, 0x56, 0xC8, 0x82, 0x9F, 0x2B, 0x36, 0xDE,
91     0x24, 0xB0, 0x73, 0x0E, 0xA6, 0x36, 0x85, 0x0C, 0xD5, 0x2F, 0xA9, 0x00,
92     0xBD, 0x8F, 0x5C, 0x47, 0x55, 0x9B, 0x04, 0x8B, 0x53, 0x03, 0x41, 0xB6,
93     0x17, 0x9A, 0x99, 0xF4, 0x1F, 0xF7, 0x10, 0x71, 0x05, 0x59, 0x93, 0x01,
94     0xBE, 0x86, 0xED, 0xF9, 0x44, 0xC4, 0x92, 0xE0, 0x5D, 0x02, 0x03, 0x01,
95     0x00, 0x01, 0xA3, 0x63, 0x30, 0x61, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D,
96     0x0E, 0x04, 0x16, 0x04, 0x14, 0x15, 0xC1, 0x13, 0x21, 0x6B, 0xE2, 0x55,
97     0x5F, 0xA7, 0x6A, 0x1D, 0x57, 0x20, 0xD6, 0xCF, 0xF3, 0xA9, 0x73, 0x64,
98     0x59, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16,
99     0x80, 0x14, 0x15, 0xC1, 0x13, 0x21, 0x6B, 0xE2, 0x55, 0x5F, 0xA7, 0x6A,
100     0x1D, 0x57, 0x20, 0xD6, 0xCF, 0xF3, 0xA9, 0x73, 0x64, 0x59, 0x30, 0x0F,
101     0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01, 0xFF, 0x04, 0x05, 0x30, 0x03,
102     0x01, 0x01, 0xFF, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x01, 0x01,
103     0xFF, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30, 0x0D, 0x06, 0x09, 0x2A,
104     0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
105     0x01, 0x01, 0x00, 0xBD, 0xD0, 0x26, 0xA4, 0x33, 0xE2, 0xD5, 0x6B, 0x7B,
106     0x1E, 0x4D, 0xF4, 0x75, 0x72, 0x85, 0xED, 0x48, 0x3A, 0x31, 0x42, 0x84,
107     0x4E, 0x2F, 0xAE, 0xA0, 0xED, 0x52, 0xBA, 0x8F, 0x49, 0x84, 0xD9, 0x28,
108     0xFD, 0xCA, 0x6F, 0xA2, 0x2F, 0x1F, 0x6E, 0x8F, 0xDD, 0x9D, 0x65, 0x43,
109     0x7C, 0x75, 0x68, 0x22, 0xD1, 0x27, 0xEB, 0x8E, 0x12, 0xBE, 0xF7, 0xAB,
110     0xE5, 0x42, 0x5D, 0x28, 0x96, 0x3A, 0xD8, 0x3B, 0xE1, 0x8A, 0x5A, 0x42,
111     0xB4, 0x0C, 0xB0, 0x61, 0xF0, 0xA9, 0x83, 0x19, 0x29, 0x2A, 0x82, 0x84,
112     0x76, 0x7A, 0x63, 0x9D, 0x10, 0x52, 0x31, 0xC6, 0xD5, 0x0C, 0x89, 0xAC,
113     0xAA, 0xF7, 0xF5, 0x62, 0x3C, 0xC5, 0x2A, 0x23, 0xA2, 0x0F, 0xB9, 0x1C,
114     0x56, 0xFC, 0xF8, 0x57, 0x43, 0x59, 0x63, 0xDD, 0x59, 0x5B, 0x0B, 0xB7,
115     0xBE, 0x06, 0x92, 0xBD, 0xC4, 0xC9, 0x42, 0x4A, 0x56, 0x37, 0x46, 0xBE,
116     0x2D, 0x66, 0xA7, 0xA5, 0x52, 0xAA, 0x8A, 0x09, 0x7C, 0x9E, 0xC9, 0x2D,
117     0xB6, 0x70, 0x5E, 0x88, 0x70, 0xC7, 0x19, 0xE6, 0xF1, 0x8F, 0x78, 0x5A,
118     0xC2, 0x52, 0x1D, 0xB5, 0x59, 0x64, 0x6D, 0x0C, 0x0B, 0x2F, 0x09, 0x55,
119     0x91, 0x7F, 0x7D, 0xC7, 0x51, 0x7B, 0xDE, 0xE4, 0xB7, 0xDC, 0x8C, 0xC1,
120     0x63, 0xAF, 0x99, 0x27, 0x68, 0xAC, 0xEE, 0x60, 0xFC, 0xDF, 0xD0, 0x7C,
121     0x7C, 0xE5, 0x5D, 0xF6, 0x9B, 0xCF, 0xBC, 0x10, 0x60, 0x6A, 0x12, 0x2B,
122     0xC3, 0x9B, 0x73, 0x27, 0xF4, 0x3E, 0x80, 0x84, 0xEE, 0x55, 0x07, 0x7D,
123     0x13, 0xA4, 0x55, 0x5E, 0x1D, 0x74, 0xFC, 0x0C, 0x3E, 0x8B, 0xFF, 0x12,
124     0xEE, 0xD1, 0xE9, 0x11, 0x48, 0x0F, 0xDE, 0x10, 0x64, 0xD4, 0xFF, 0xB0,
125     0x45, 0x00, 0x56, 0x0F, 0x16, 0xC7, 0x07, 0x6F, 0x25, 0x13, 0xEE, 0x35,
126     0x8E, 0xF8, 0x77, 0x24, 0xE6, 0xD0, 0x59
127 };
128 
pkcs7_verify_test(void)129 static int pkcs7_verify_test(void)
130 {
131     int ret = 0;
132     size_t i;
133     BIO *msg_bio = NULL, *x509_bio = NULL, *bio = NULL;
134     X509 *cert = NULL;
135     X509_STORE *store = NULL;
136     PKCS7 *p7 = NULL;
137     const char *sig[] = {
138         "MIME-Version: 1.0\nContent-Type: multipart/signed; protocol=\"application/x-pkcs7-signature\"; micalg=\"sha-256\"; boundary=\"----9B5319FF2E4428B17CD26B69294E7F31\"\n\n",
139         "This is an S/MIME signed message\n\n------9B5319FF2E4428B17CD26B69294E7F31\n",
140         "Content-Type: text/plain\r\n\r\nhello world\n------9B5319FF2E4428B17CD26B69294E7F31\n",
141         "Content-Type: application/x-pkcs7-signature; name=\"smime.p7s\"\n",
142         "Content-Transfer-Encoding: base64\nContent-Disposition: attachment; filename=\"smime.p7s\"\n\n",
143         "MIIDEgYJKoZIhvcNAQcCoIIDAzCCAv8CAQExDzANBglghkgBZQMEAgEFADALBgkq\nhkiG9w0BBwGgggFVMIIBUTCB96ADAgECAgIDCTAKBggqhkjOPQQDAjAnMQswCQYD\nVQQGEwJVUzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5IENBMB4XDTE3MDEwMTEyMDEw\nMFoXDTM4MTIzMTA4MzAwMFowJzELMAkGA1UEBhMCVVMxGDAWBgNVBAMMD2NyeXB0\nb2dyYXBoeSBDQTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABBj/z7v5Obj13cPu\nwECLBnUGq0/N2CxSJE4f4BBGZ7VfFblivTvPDG++Gve0oQ+0uctuhrNQ+WxRv8GC\n",
144         "177F+QWjEzARMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSQAwRgIhANES\n742XWm64tkGnz8DnpG6u2lHkZFQr3oaVvPcemvlbAiEA0WGGzmYx5C9UvfXIK7NE\nziT4pQtyESE0uRVKXw4nMqkxggGBMIIBfQIBATAtMCcxCzAJBgNVBAYTAlVTMRgw\nFgYDVQQDDA9jcnlwdG9ncmFwaHkgQ0ECAgMJMA0GCWCGSAFlAwQCAQUAoIHkMBgG\nCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTIxMDUyMDE4\nNTA0OVowLwYJKoZIhvcNAQkEMSIEIOdwMRgQrqcnmMYvag+BVvErcc6bwUXI94Ds\n",
145         "QkiyIU9pMHkGCSqGSIb3DQEJDzFsMGowCwYJYIZIAWUDBAEqMAsGCWCGSAFlAwQB\nFjALBglghkgBZQMEAQIwCgYIKoZIhvcNAwcwDgYIKoZIhvcNAwICAgCAMA0GCCqG\nSIb3DQMCAgFAMAcGBSsOAwIHMA0GCCqGSIb3DQMCAgEoMAoGCCqGSM49BAMCBEcw\nRQIhANYMJku1fW9T1MIEcAyREArz9kXCY4tWck5Pt0xzrYhaAiBDSP6e43zj4YtI\nuvQW+Lzv+dNF8EPuhgoPNe17RuUSLw==\n\n------9B5319FF2E4428B17CD26B69294E7F31--\n\n"
146     };
147     const char *signed_data = "Content-Type: text/plain\r\n\r\nhello world";
148 
149     if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
150         goto end;
151     for  (i = 0; i < OSSL_NELEM(sig); ++i)
152         BIO_puts(bio, sig[i]);
153 
154     ret = TEST_ptr(msg_bio = BIO_new_mem_buf(signed_data, strlen(signed_data)))
155           && TEST_ptr(x509_bio = BIO_new_mem_buf(cert_der, sizeof(cert_der)))
156           && TEST_ptr(cert = d2i_X509_bio(x509_bio, NULL))
157           && TEST_int_eq(ERR_peek_error(), 0)
158           && TEST_ptr(store = X509_STORE_new())
159           && TEST_true(X509_STORE_add_cert(store, cert))
160           && TEST_ptr(p7 = SMIME_read_PKCS7(bio, NULL))
161           && TEST_int_eq(ERR_peek_error(), 0)
162           && TEST_true(PKCS7_verify(p7, NULL, store, msg_bio, NULL, PKCS7_TEXT))
163           && TEST_int_eq(ERR_peek_error(), 0);
164 end:
165     X509_STORE_free(store);
166     X509_free(cert);
167     PKCS7_free(p7);
168     BIO_free(msg_bio);
169     BIO_free(x509_bio);
170     BIO_free(bio);
171     return ret;
172 }
173 
174 /*
175  * PKCS7_verify() BIO *indata parameter refers to the signed data if the content
176  * is detached from p7. Otherwise indata should be NULL, and then the signed
177  * data must be in p7. The content is written to the BIO out unless it is NULL.
178  *
179  * $ openssl asn1parse -i -inform pem -in sig.pem
180  *    0:d=0  hl=4 l=1571 cons: SEQUENCE
181  *    4:d=1  hl=2 l=   9 prim:  OBJECT            :pkcs7-signedData
182  *   15:d=1  hl=4 l=1556 cons:  cont [ 0 ]
183  *   19:d=2  hl=4 l=1552 cons:   SEQUENCE
184  *** ASN1_INTEGER *version; ***
185  *   23:d=3  hl=2 l=   1 prim:    INTEGER           :01
186  *** STACK_OF(X509_ALGOR) *md_algs;
187  *   26:d=3  hl=2 l=  15 cons:    SET
188  *   28:d=4  hl=2 l=  13 cons:     SEQUENCE
189  *   30:d=5  hl=2 l=   9 prim:      OBJECT            :sha256
190  *   41:d=5  hl=2 l=   0 prim:      NULL
191  *** struct pkcs7_st *contents; ***
192  *   43:d=3  hl=2 l=  92 cons:    SEQUENCE
193  *   45:d=4  hl=2 l=  10 prim:     OBJECT            :1.3.6.1.4.1.311.2.1.4
194  *   57:d=4  hl=2 l=  78 cons:     cont [ 0 ]
195  *   59:d=5  hl=2 l=  76 cons:      SEQUENCE
196  *   61:d=6  hl=2 l=  23 cons:       SEQUENCE
197  *   63:d=7  hl=2 l=  10 prim:        OBJECT            :1.3.6.1.4.1.311.2.1.15
198  *   75:d=7  hl=2 l=   9 cons:        SEQUENCE
199  *   77:d=8  hl=2 l=   1 prim:         BIT STRING
200  *   80:d=8  hl=2 l=   4 cons:         cont [ 0 ]
201  *   82:d=9  hl=2 l=   2 cons:          cont [ 2 ]
202  *   84:d=10 hl=2 l=   0 prim:           cont [ 0 ]
203  *   86:d=6  hl=2 l=  49 cons:       SEQUENCE
204  *   88:d=7  hl=2 l=  13 cons:        SEQUENCE
205  *   90:d=8  hl=2 l=   9 prim:         OBJECT            :sha256
206  *  101:d=8  hl=2 l=   0 prim:         NULL
207  *** Hash of a signed PE file (sha256) ***
208  *  103:d=7  hl=2 l=  32 prim:        OCTET STRING
209  *   [HEX DUMP]:2D2C7B382C8163A419B9FF214A7B651C33F9EA43335907F11377290C5158A7A4
210  *** STACK_OF(X509) *cert; ***
211  *  137:d=3  hl=4 l= 913 cons:    cont [ 0 ]
212  *  141:d=4  hl=4 l= 909 cons:     SEQUENCE
213  *  145:d=5  hl=4 l= 629 cons:      SEQUENCE
214  *  ...
215  *** STACK_OF(PKCS7_SIGNER_INFO) *signer_info; ***
216  * 1054:d=3  hl=4 l= 517 cons:    SET
217  * 1058:d=4  hl=4 l= 513 cons:     SEQUENCE
218  * ...
219  */
220 
pkcs7_inner_content_verify_test(void)221 static int pkcs7_inner_content_verify_test(void)
222 {
223     int ret = 0;
224     BIO *x509_bio = NULL, *bio = NULL;
225     X509 *cert = NULL;
226     X509_STORE *store = NULL;
227     PKCS7 *p7 = NULL;
228     X509_VERIFY_PARAM *param;
229     const unsigned char sig_der[] = {
230         0x30, 0x82, 0x06, 0x23, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
231         0x01, 0x07, 0x02, 0xA0, 0x82, 0x06, 0x14, 0x30, 0x82, 0x06, 0x10, 0x02,
232         0x01, 0x01, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
233         0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x30, 0x5C, 0x06, 0x0A, 0x2B,
234         0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0xA0, 0x4E, 0x30,
235         0x4C, 0x30, 0x17, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37,
236         0x02, 0x01, 0x0F, 0x30, 0x09, 0x03, 0x01, 0x00, 0xA0, 0x04, 0xA2, 0x02,
237         0x80, 0x00, 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
238         0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20, 0x2D, 0x2C, 0x7B,
239         0x38, 0x2C, 0x81, 0x63, 0xA4, 0x19, 0xB9, 0xFF, 0x21, 0x4A, 0x7B, 0x65,
240         0x1C, 0x33, 0xF9, 0xEA, 0x43, 0x33, 0x59, 0x07, 0xF1, 0x13, 0x77, 0x29,
241         0x0C, 0x51, 0x58, 0xA7, 0xA4, 0xA0, 0x82, 0x03, 0x91, 0x30, 0x82, 0x03,
242         0x8D, 0x30, 0x82, 0x02, 0x75, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14,
243         0x1A, 0x2F, 0x94, 0x5F, 0x4D, 0x1E, 0x9B, 0x68, 0xF8, 0xBF, 0xC6, 0xC9,
244         0xD6, 0xC7, 0x07, 0xB0, 0x3E, 0x35, 0xFD, 0xC7, 0x30, 0x0D, 0x06, 0x09,
245         0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x30,
246         0x44, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
247         0x55, 0x4B, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
248         0x0D, 0x4F, 0x70, 0x65, 0x6E, 0x53, 0x53, 0x4C, 0x20, 0x47, 0x72, 0x6F,
249         0x75, 0x70, 0x31, 0x1D, 0x30, 0x1B, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
250         0x14, 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x2F, 0x4D, 0x49, 0x4D, 0x45,
251         0x20, 0x52, 0x53, 0x41, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x30, 0x20, 0x17,
252         0x0D, 0x32, 0x32, 0x30, 0x38, 0x31, 0x36, 0x31, 0x31, 0x35, 0x38, 0x30,
253         0x30, 0x5A, 0x18, 0x0F, 0x32, 0x31, 0x32, 0x32, 0x30, 0x38, 0x31, 0x36,
254         0x31, 0x31, 0x35, 0x38, 0x30, 0x30, 0x5A, 0x30, 0x47, 0x31, 0x0B, 0x30,
255         0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x4B, 0x31, 0x16,
256         0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0D, 0x4F, 0x70, 0x65,
257         0x6E, 0x53, 0x53, 0x4C, 0x20, 0x47, 0x72, 0x6F, 0x75, 0x70, 0x31, 0x20,
258         0x30, 0x1E, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x17, 0x54, 0x65, 0x73,
259         0x74, 0x20, 0x43, 0x6F, 0x64, 0x65, 0x53, 0x69, 0x67, 0x6E, 0x20, 0x45,
260         0x45, 0x20, 0x52, 0x53, 0x41, 0x20, 0x23, 0x31, 0x30, 0x82, 0x01, 0x22,
261         0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
262         0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82, 0x01, 0x0A,
263         0x02, 0x82, 0x01, 0x01, 0x00, 0xA8, 0xFF, 0x89, 0x58, 0x61, 0x85, 0xAE,
264         0xDE, 0xDB, 0x2C, 0x0F, 0xD5, 0x73, 0xFB, 0xE2, 0xB7, 0xA8, 0x1B, 0x7A,
265         0x75, 0x60, 0x2B, 0xD4, 0x7D, 0x3C, 0x73, 0x76, 0xB5, 0xAE, 0xDE, 0x93,
266         0x2F, 0x7F, 0x24, 0x90, 0x6D, 0xC9, 0x1C, 0xF4, 0x8A, 0x38, 0xCD, 0x9F,
267         0xA2, 0x80, 0x16, 0x56, 0xCB, 0xB4, 0x2C, 0xBB, 0x93, 0xE5, 0x17, 0x1F,
268         0xFF, 0x0D, 0xC6, 0x43, 0xFE, 0x31, 0x34, 0xF8, 0x28, 0xD8, 0x75, 0xE9,
269         0xA3, 0xBB, 0x88, 0x82, 0xEF, 0xA3, 0x72, 0xE2, 0x53, 0x62, 0x05, 0x45,
270         0x4D, 0xE0, 0x31, 0x97, 0xEF, 0xAA, 0x9D, 0x10, 0xD2, 0x58, 0x0D, 0x9E,
271         0x06, 0x9E, 0x32, 0xBB, 0x1B, 0x08, 0x56, 0x5D, 0x65, 0x0F, 0x3F, 0x66,
272         0x8E, 0xC2, 0x36, 0xA0, 0x11, 0xFF, 0xC0, 0x59, 0x35, 0xFD, 0x49, 0x1C,
273         0xDD, 0x1C, 0x42, 0x33, 0x06, 0x78, 0xAE, 0x04, 0x38, 0x23, 0xE5, 0xE7,
274         0x75, 0x67, 0xD3, 0x94, 0xAD, 0x5A, 0xA9, 0x40, 0x1F, 0xAC, 0x59, 0x10,
275         0xF5, 0x0C, 0xC1, 0x64, 0xAD, 0x4A, 0xC4, 0xC6, 0x35, 0x53, 0xB3, 0x96,
276         0x6A, 0x82, 0x1B, 0x15, 0x8B, 0xD1, 0x99, 0xAB, 0xE4, 0x9E, 0x6F, 0x94,
277         0xAA, 0xD2, 0x96, 0xE5, 0x57, 0xFB, 0x8C, 0x91, 0x64, 0x8A, 0x3C, 0x24,
278         0x16, 0xC6, 0xD2, 0x19, 0xBE, 0x82, 0x60, 0x13, 0x3B, 0x62, 0xB0, 0xED,
279         0x3E, 0x1D, 0xB3, 0xCA, 0xF9, 0x6B, 0xDF, 0x31, 0xB9, 0x7A, 0x9E, 0x4B,
280         0x47, 0x68, 0xA7, 0x29, 0xC7, 0x3F, 0x4A, 0xE2, 0x22, 0x27, 0xB4, 0xEF,
281         0x90, 0x63, 0xD7, 0xEF, 0xD0, 0x72, 0x49, 0x3E, 0x5B, 0xAD, 0xB9, 0x1B,
282         0xD4, 0x2B, 0x6F, 0x86, 0x68, 0xDD, 0xD5, 0x73, 0x12, 0xB8, 0x43, 0xC9,
283         0xDC, 0x41, 0x0F, 0xA2, 0xA3, 0x46, 0x4F, 0x8B, 0x67, 0x88, 0x84, 0x0F,
284         0x61, 0xFE, 0x79, 0x34, 0x1F, 0x4A, 0x89, 0x1F, 0xB7, 0x02, 0x03, 0x01,
285         0x00, 0x01, 0xA3, 0x72, 0x30, 0x70, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1D,
286         0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x1D, 0x0F,
287         0x01, 0x01, 0xFF, 0x04, 0x04, 0x03, 0x02, 0x07, 0x80, 0x30, 0x13, 0x06,
288         0x03, 0x55, 0x1D, 0x25, 0x04, 0x0C, 0x30, 0x0A, 0x06, 0x08, 0x2B, 0x06,
289         0x01, 0x05, 0x05, 0x07, 0x03, 0x03, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D,
290         0x0E, 0x04, 0x16, 0x04, 0x14, 0xE7, 0x9B, 0xE2, 0x2A, 0xAD, 0x8A, 0x6C,
291         0x3A, 0xCB, 0x76, 0x51, 0xE5, 0x8E, 0x07, 0x98, 0x22, 0x97, 0xE1, 0x73,
292         0xA2, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16,
293         0x80, 0x14, 0x15, 0xC1, 0x13, 0x21, 0x6B, 0xE2, 0x55, 0x5F, 0xA7, 0x6A,
294         0x1D, 0x57, 0x20, 0xD6, 0xCF, 0xF3, 0xA9, 0x73, 0x64, 0x59, 0x30, 0x0D,
295         0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05,
296         0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x92, 0xC7, 0x7B, 0x6A, 0xB7,
297         0x39, 0x82, 0x7C, 0x90, 0xA6, 0x00, 0xBE, 0x34, 0xB9, 0x54, 0x38, 0x84,
298         0x4A, 0xA3, 0xCF, 0x57, 0x26, 0x0C, 0x48, 0xA6, 0xFA, 0x07, 0xB6, 0xEC,
299         0x7B, 0x1F, 0xC1, 0x80, 0x50, 0x2B, 0xC6, 0x69, 0x3E, 0xF2, 0x13, 0xA9,
300         0xBB, 0x2B, 0xAA, 0x4A, 0x87, 0xA3, 0x8B, 0x25, 0x40, 0x3B, 0xDA, 0xDE,
301         0xFC, 0x9A, 0xE6, 0x41, 0xEB, 0x4C, 0xD6, 0xD3, 0x68, 0xEE, 0x47, 0x7D,
302         0x47, 0x75, 0x83, 0x2F, 0x50, 0xF7, 0xE2, 0x11, 0xBB, 0x92, 0x00, 0xD3,
303         0x01, 0xAA, 0x9B, 0x70, 0x13, 0xC3, 0x51, 0xCE, 0xE0, 0x8A, 0x7F, 0x1B,
304         0x4E, 0x46, 0x77, 0x4C, 0x9E, 0x9B, 0xED, 0xC5, 0x09, 0xAF, 0x08, 0x7D,
305         0x5C, 0x2C, 0x16, 0x5D, 0x37, 0x0E, 0x94, 0x9D, 0x4E, 0xE7, 0x05, 0xDE,
306         0x17, 0xD9, 0x80, 0x4F, 0x21, 0xD9, 0x1B, 0x00, 0xA1, 0x3A, 0xBA, 0xE8,
307         0x88, 0x4C, 0xC3, 0x91, 0x1A, 0x0E, 0x75, 0xE3, 0xE3, 0xD2, 0xC2, 0x8D,
308         0x65, 0x94, 0x68, 0xA6, 0x33, 0x95, 0x67, 0x9D, 0x1D, 0xFD, 0x7F, 0x09,
309         0x30, 0x29, 0x96, 0xFF, 0x1F, 0x25, 0xBC, 0xEB, 0xA2, 0x47, 0xEB, 0x83,
310         0x4E, 0x8B, 0x47, 0xF7, 0xEB, 0x1B, 0xDE, 0xD8, 0x21, 0x27, 0x47, 0x26,
311         0xA5, 0x52, 0xA4, 0x14, 0x2D, 0x29, 0x5D, 0x2F, 0xF4, 0x0C, 0x1E, 0x6A,
312         0x54, 0x7A, 0xE8, 0x84, 0x97, 0x64, 0xC0, 0xB4, 0x6E, 0xF0, 0x05, 0xF7,
313         0x09, 0x66, 0xDC, 0x42, 0x01, 0xD6, 0x83, 0xB0, 0x51, 0x65, 0xB4, 0x3A,
314         0x4D, 0xA9, 0x90, 0x07, 0xC0, 0x25, 0x5D, 0xD6, 0x23, 0xF0, 0x5B, 0x3B,
315         0x9B, 0xB0, 0xCC, 0x92, 0x49, 0x81, 0xC6, 0x3A, 0xD0, 0x52, 0xED, 0xEC,
316         0xEC, 0x54, 0x53, 0x00, 0xBC, 0x69, 0xFE, 0x5A, 0x8C, 0x72, 0x86, 0x99,
317         0xCB, 0xE6, 0xDD, 0x07, 0x37, 0x79, 0xF4, 0x66, 0x8E, 0x15, 0x31, 0x82,
318         0x02, 0x05, 0x30, 0x82, 0x02, 0x01, 0x02, 0x01, 0x01, 0x30, 0x5C, 0x30,
319         0x44, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
320         0x55, 0x4B, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
321         0x0D, 0x4F, 0x70, 0x65, 0x6E, 0x53, 0x53, 0x4C, 0x20, 0x47, 0x72, 0x6F,
322         0x75, 0x70, 0x31, 0x1D, 0x30, 0x1B, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C,
323         0x14, 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x2F, 0x4D, 0x49, 0x4D, 0x45,
324         0x20, 0x52, 0x53, 0x41, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x02, 0x14, 0x1A,
325         0x2F, 0x94, 0x5F, 0x4D, 0x1E, 0x9B, 0x68, 0xF8, 0xBF, 0xC6, 0xC9, 0xD6,
326         0xC7, 0x07, 0xB0, 0x3E, 0x35, 0xFD, 0xC7, 0x30, 0x0D, 0x06, 0x09, 0x60,
327         0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0xA0, 0x7C,
328         0x30, 0x10, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02,
329         0x01, 0x0C, 0x31, 0x02, 0x30, 0x00, 0x30, 0x19, 0x06, 0x09, 0x2A, 0x86,
330         0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x03, 0x31, 0x0C, 0x06, 0x0A, 0x2B,
331         0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0x30, 0x1C, 0x06,
332         0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x0B, 0x31,
333         0x0E, 0x30, 0x0C, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37,
334         0x02, 0x01, 0x15, 0x30, 0x2F, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
335         0x0D, 0x01, 0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0x06, 0x1E, 0x2B, 0xC2,
336         0x31, 0xF7, 0x1B, 0xA4, 0x8C, 0x65, 0x4A, 0x26, 0x5B, 0xAD, 0x3E, 0x8C,
337         0x25, 0xB3, 0xAA, 0xAF, 0xB2, 0x0F, 0xA1, 0xF6, 0x8B, 0x6A, 0x6B, 0x87,
338         0x45, 0x47, 0x9E, 0x35, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
339         0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01, 0x00, 0x45,
340         0x98, 0xCB, 0xA9, 0x2B, 0x49, 0xFD, 0x33, 0xA0, 0xBD, 0x27, 0x98, 0xE1,
341         0xA4, 0x2D, 0xD7, 0x73, 0xC1, 0x77, 0x66, 0x97, 0x26, 0x5A, 0xA0, 0x72,
342         0xD9, 0x8E, 0x3B, 0x9B, 0x74, 0x20, 0xCF, 0x13, 0x6A, 0xE1, 0xD3, 0x97,
343         0xAC, 0xAB, 0xEA, 0x4A, 0x43, 0x98, 0xA2, 0x7C, 0x71, 0x95, 0xFF, 0x61,
344         0x7B, 0x9B, 0x56, 0xF7, 0xBF, 0xF0, 0x33, 0xB4, 0x3E, 0xBF, 0xF0, 0x8A,
345         0xA0, 0x78, 0x3B, 0xF9, 0xAF, 0x9E, 0xFD, 0x54, 0x92, 0xC5, 0x81, 0x6D,
346         0xE0, 0x29, 0x78, 0xBC, 0x00, 0xD8, 0xF1, 0xA6, 0xFF, 0x00, 0x03, 0xF9,
347         0x8E, 0x9E, 0xB4, 0x09, 0x10, 0x4A, 0x1D, 0xF6, 0x0E, 0x76, 0x15, 0xAA,
348         0x96, 0x86, 0x38, 0x3A, 0x0B, 0x3C, 0x0E, 0x37, 0x6E, 0xF9, 0xA3, 0x59,
349         0x0F, 0x41, 0x42, 0xC3, 0xA2, 0x74, 0x77, 0x10, 0x7F, 0xA0, 0xA8, 0x17,
350         0xFB, 0xD2, 0x68, 0x8A, 0x17, 0x71, 0x89, 0x2C, 0x30, 0x49, 0x54, 0xDF,
351         0x29, 0x80, 0xC2, 0xB9, 0x13, 0x36, 0x77, 0xF4, 0xBE, 0x6B, 0x76, 0xBC,
352         0x42, 0x1A, 0xB2, 0xE7, 0x4D, 0x36, 0xED, 0x85, 0xB8, 0x25, 0xEC, 0x03,
353         0xF8, 0x1E, 0x2F, 0x41, 0x48, 0x18, 0xAD, 0x58, 0x3D, 0x40, 0xEB, 0x4F,
354         0x5B, 0xFB, 0x15, 0x57, 0xD5, 0xE0, 0x68, 0xC6, 0x84, 0x21, 0x57, 0x9E,
355         0x20, 0x46, 0x45, 0xDB, 0x30, 0xA3, 0x6F, 0x44, 0x35, 0x17, 0xAC, 0xE6,
356         0xD1, 0x0E, 0xEE, 0x43, 0x38, 0x87, 0xCD, 0x22, 0xE9, 0x83, 0xFE, 0x9D,
357         0x30, 0x8F, 0xE0, 0x51, 0xDB, 0xFF, 0x26, 0x5A, 0x53, 0x21, 0xBF, 0xE7,
358         0x1F, 0xD8, 0xA4, 0x53, 0xDD, 0xAE, 0xF2, 0xF5, 0x73, 0xEA, 0xFE, 0x50,
359         0x99, 0x1B, 0x16, 0x1C, 0xBD, 0x4E, 0xFD, 0x8B, 0x2E, 0x22, 0x31, 0x05,
360         0x90, 0x9D, 0x41, 0x7C, 0xA7, 0x83, 0xE9, 0xCA, 0x8E, 0x01, 0x6D, 0xB2,
361         0x2D, 0x6F, 0x81
362     };
363 
364     if (!TEST_ptr(bio = BIO_new_mem_buf(sig_der, sizeof sig_der)))
365         goto end;
366 
367     ret = TEST_ptr(x509_bio = BIO_new_mem_buf(smroot_der, sizeof smroot_der))
368             && TEST_ptr(cert = d2i_X509_bio(x509_bio, NULL))
369             && TEST_int_eq(ERR_peek_error(), 0)
370             && TEST_ptr(store = X509_STORE_new())
371             && TEST_true(X509_STORE_add_cert(store, cert))
372             && TEST_ptr(param = X509_STORE_get0_param(store))
373             && TEST_true(X509_VERIFY_PARAM_set_purpose(param,
374                                                        X509_PURPOSE_CODE_SIGN))
375             && TEST_true(X509_STORE_set1_param(store, param))
376             && TEST_ptr(p7 = d2i_PKCS7_bio(bio, NULL))
377             && TEST_int_eq(ERR_peek_error(), 0)
378             && TEST_true(PKCS7_verify(p7, NULL, store, NULL, NULL, 0))
379             && TEST_int_eq(ERR_peek_error(), 0);
380 end:
381     X509_STORE_free(store);
382     X509_free(cert);
383     PKCS7_free(p7);
384     BIO_free(x509_bio);
385     BIO_free(bio);
386     return ret;
387 }
388 #endif /* OPENSSL_NO_EC */
389 
setup_tests(void)390 int setup_tests(void)
391 {
392 #ifndef OPENSSL_NO_EC
393     ADD_TEST(pkcs7_verify_test);
394     ADD_TEST(pkcs7_inner_content_verify_test);
395 #endif /* OPENSSL_NO_EC */
396     return 1;
397 }
398