1--TEST--
2openssl_cms_sign() and verify detached tests
3--EXTENSIONS--
4openssl
5--FILE--
6<?php
7$infile = __DIR__ . "/plain.txt";
8$outfile = tempnam(sys_get_temp_dir(), "ssl");
9$vout= $outfile . ".vout";
10
11if ($outfile === false) {
12    die("failed to get a temporary filename!");
13}
14
15$privkey = "file://" . __DIR__ . "/private_rsa_1024.key";
16$single_cert = "file://" . __DIR__ . "/cert.crt";
17$assoc_headers = array("To" => "test@test", "Subject" => "testing openssl_cms_sign()");
18$headers = array("test@test", "testing openssl_cms_sign()");
19$empty_headers = array();
20$wrong = "wrong";
21$empty = "";
22print("S/MIME attached\nPlain text:\n");
23readfile($infile);
24var_dump(openssl_cms_sign($infile, $outfile, openssl_x509_read($single_cert), $privkey, $headers));
25var_dump(openssl_cms_verify($outfile,OPENSSL_CMS_NOVERIFY, NULL, array(), NULL, $vout));
26print("\nValidated content:\n");
27readfile($vout);
28
29if (file_exists($outfile)) {
30    echo "true\n";
31    unlink($outfile);
32}
33
34if (file_exists($vout)) {
35    echo "true\n";
36    unlink($vout);
37}
38
39// test three forms of detached signatures:
40// PEM first
41print("\nPEM Detached:\n");
42var_dump(openssl_cms_sign($infile, $outfile, openssl_x509_read($single_cert), $privkey, $headers,
43	     OPENSSL_CMS_DETACHED|OPENSSL_CMS_BINARY,OPENSSL_ENCODING_PEM));
44var_dump(openssl_cms_verify($infile,OPENSSL_CMS_NOVERIFY|OPENSSL_CMS_DETACHED|OPENSSL_CMS_BINARY,
45         NULL, array(), NULL, $vout, NULL, $outfile, OPENSSL_ENCODING_PEM));
46print("\nValidated content:\n");
47readfile($vout);
48if (file_exists($outfile)) {
49    echo "true\n";
50    unlink($outfile);
51}
52
53if (file_exists($vout)) {
54    echo "true\n";
55    unlink($vout);
56}
57
58// DER next
59print("\nDER Detached:\n");
60var_dump(openssl_cms_sign($infile, $outfile, openssl_x509_read($single_cert), $privkey, $headers,
61	     OPENSSL_CMS_DETACHED|OPENSSL_CMS_BINARY,OPENSSL_ENCODING_DER));
62var_dump(openssl_cms_verify($infile,OPENSSL_CMS_NOVERIFY|OPENSSL_CMS_DETACHED|OPENSSL_CMS_BINARY,
63         NULL, array(), NULL, $vout, NULL, $outfile, OPENSSL_ENCODING_DER));
64print("\nValidated content:\n");
65readfile($vout);
66// extreme measures to avoid stupid temporary errors for failure to unlink a file.
67if (file_exists($outfile)) {
68    echo "true\n";
69    unlink($outfile);
70}
71$outfile=$outfile . "x";
72if (file_exists($vout)) {
73    echo "true\n";
74    unlink($vout);
75}
76
77// S/MIME next
78print("\nS/MIME Detached (an error):\n");
79var_dump(openssl_cms_sign($infile, $outfile, openssl_x509_read($single_cert), $privkey, $headers,
80	     OPENSSL_CMS_DETACHED,OPENSSL_ENCODING_SMIME));
81var_dump(openssl_cms_verify($infile,OPENSSL_CMS_NOVERIFY|OPENSSL_CMS_DETACHED,
82         NULL, array(), NULL, $vout, NULL, $outfile, OPENSSL_ENCODING_SMIME));
83if (file_exists($outfile)) {
84    echo "true\n";
85    unlink($outfile);
86}
87
88if (file_exists($vout)) {
89    echo "true\n";
90    unlink($vout);
91}
92?>
93--EXPECTF--
94S/MIME attached
95Plain text:
96Now is the winter of our discontent.
97bool(true)
98bool(true)
99
100Validated content:
101Now is the winter of our discontent.
102true
103true
104
105PEM Detached:
106bool(true)
107bool(true)
108
109Validated content:
110Now is the winter of our discontent.
111true
112true
113
114DER Detached:
115bool(true)
116bool(true)
117
118Validated content:
119Now is the winter of our discontent.
120true
121true
122
123S/MIME Detached (an error):
124
125Warning: openssl_cms_sign(): Detached signatures not possible with S/MIME encoding in %s on line %d
126bool(false)
127
128Warning: openssl_cms_verify(): Detached signatures not possible with S/MIME encoding in %s on line %d
129bool(false)
130