1+ <?php
2+ /**
3+ * This file is part of the demo pacakge of the SetaPDF-Signer Component
4+ *
5+ * @copyright Copyright (c) 2015 Setasign - Jan Slabon (http://www.setasign.com)
6+ * @category SetaPDF
7+ * @package SetaPDF_Signer
8+ * @license http://www.apache.org/licenses/LICENSE-2.0
9+ */
10+
11+ /**
12+ * A class offering various helper methods.
13+ *
14+ * @link https://www.swisscom.ch/en/business/enterprise/offer/security/identity-access-security/signing-service.html
15+ * @copyright Copyright (c) 2015 Setasign - Jan Slabon (http://www.setasign.com)
16+ * @category SetaPDF
17+ * @package SetaPDF_Signer
18+ * @license http://www.apache.org/licenses/LICENSE-2.0
19+ */
20+ class SetaPDF_Signer_SwisscomAIS_Helper
21+ {
22+ /**
23+ * Get signature data from a single signature container or by a module instance.
24+ *
25+ * @param string|SetaPDF_Signer_SwisscomAIS_AbstractModule $signature
26+ * @return array
27+ * @throws SetaPDF_Signer_Asn1_Exception|InvalidArgumentException
28+ */
29+ static public function getSignatureData ($ signature )
30+ {
31+ if ($ signature instanceof SetaPDF_Signer_SwisscomAIS_Batch) {
32+ $ result = array ();
33+
34+ $ lastResult = $ signature ->getLastResult ();
35+ $ signatures = $ lastResult ->SignResponse ->SignatureObject ->Other ->SignatureObjects ->ExtendedSignatureObject ;
36+ if (!is_array ($ signatures )) {
37+ $ signatures = array ($ signatures );
38+ }
39+
40+ foreach ($ signatures AS $ signatureData ) {
41+ if (isset ($ signatureData ->Timestamp ->RFC3161TimeStampToken )) {
42+ $ signature = $ signatureData ->Timestamp ->RFC3161TimeStampToken ;;
43+ } else {
44+ $ signature = $ signatureData ->Base64Signature ->_ ;
45+ }
46+
47+ $ no = $ signatureData ->WhichDocument ;
48+
49+ $ result [$ no ] = self ::_getSignatureData ($ signature );
50+ }
51+
52+ return $ result ;
53+
54+ } elseif ($ signature instanceof SetaPDF_Signer_SwisscomAIS_Module) {
55+ $ lastResult = $ signature ->getLastResult ();
56+ // signature
57+ if (isset ($ lastResult ->SignResponse ->SignatureObject ->Base64Signature )) {
58+ $ signature = $ lastResult ->SignResponse ->SignatureObject ->Base64Signature ->_ ;
59+ } elseif (isset ($ lastResult ->SignResponse ->SignatureObject ->Timestamp ->RFC3161TimeStampToken )) {
60+ $ signature = $ lastResult ->SignResponse ->SignatureObject ->Timestamp ->RFC3161TimeStampToken ;
61+ } else {
62+ throw new InvalidArgumentException ('Unable to get signature from module. ' );
63+ }
64+ }
65+
66+ return self ::_getSignatureData ($ signature );
67+ }
68+
69+ /**
70+ * Get signature data from a single signature container.
71+ *
72+ * @param string $signature
73+ * @return array
74+ * @throws SetaPDF_Signer_Asn1_Exception
75+ */
76+ static private function _getSignatureData ($ signature )
77+ {
78+ $ data = array (
79+ 'certificates ' => array (),
80+ 'signerCertificate ' => null ,
81+ 'subject ' => null ,
82+ 'MIDSN ' => null
83+ );
84+
85+ $ asn1 = SetaPDF_Signer_Asn1_Element::parse ($ signature );
86+ $ certificates = SetaPDF_Signer_Asn1_Element::findByPath ('1/0/3 ' , $ asn1 );
87+ $ certificates = $ certificates ->getChildren ();
88+
89+ $ lastValidToTime = PHP_INT_MAX ;
90+
91+ for ($ no = 0 ; $ no < count ($ certificates ); $ no ++) {
92+ $ certificate = $ certificates [$ no ];
93+ $ certificate = $ certificate ->__toString ();
94+ $ certificate = "-----BEGIN CERTIFICATE----- \n" . chunk_split (base64_encode ($ certificate )) . "-----END CERTIFICATE----- " ;
95+
96+ $ certificateInfo = openssl_x509_parse ($ certificate );
97+
98+ $ data ['certificates ' ][] = $ certificateInfo ;
99+
100+ if (isset ($ certificateInfo ['validTo_time_t ' ]) && $ certificateInfo ['validTo_time_t ' ] <= $ lastValidToTime ) {
101+ $ lastValidToTime = $ certificateInfo ['validTo_time_t ' ];
102+ $ data ['signerCertificate ' ] = $ certificateInfo ;
103+ }
104+ }
105+
106+ $ data ['subject ' ] = $ data ['signerCertificate ' ]['name ' ];
107+
108+ // extract MIDSN
109+ if (isset ($ data ['signerCertificate ' ]['extensions ' ]['subjectAltName ' ])) {
110+ $ subjectAltName = $ data ['signerCertificate ' ]['extensions ' ]['subjectAltName ' ];
111+ // Format: 'DirName: serialNumber = ID-16981fa2-8998-4125-9a93-5fecbff74515, name = "+41798...", description = test.ch: Signer le document?, pseudonym = MIDCHEGU8GSH6K83'
112+ $ subjectAltNameArray = explode (', ' , $ subjectAltName );
113+ foreach ($ subjectAltNameArray as $ value ) {
114+ if (preg_match ("/pseudonym = (.*)/ " , $ value , $ match ))
115+ $ data ['MIDSN ' ] = $ match [1 ];
116+ }
117+
118+ // isn't this the same?
119+ // if (preg_match("/pseudonym = (.*)/", $subjectAltName, $match)) {
120+ // $data['MIDSN'] = $match[1];
121+ // }
122+ }
123+
124+ return $ data ;
125+ }
126+ }
0 commit comments