@@ -20,18 +20,22 @@ if ($code > 0) die("skip couldn't locate pkcs11-dump binary");
20
20
21
21
/* simple exec */
22
22
function sexec ($ cmd , &$ stdout =null , &$ stderr =null ) {
23
- $ proc = proc_open ($ cmd ,[
24
- 1 => ['pipe ' ,'w ' ],
25
- 2 => ['pipe ' ,'w ' ],
26
- ],$ pipes );
27
-
28
- $ stdout = stream_get_contents ($ pipes [1 ]);
29
- fclose ($ pipes [1 ]);
30
-
31
- $ stderr = stream_get_contents ($ pipes [2 ]);
32
- fclose ($ pipes [2 ]);
33
-
34
- return proc_close ($ proc );
23
+ $ proc = proc_open (
24
+ $ cmd ,
25
+ [
26
+ 1 => ['pipe ' ,'w ' ],
27
+ 2 => ['pipe ' ,'w ' ],
28
+ ],
29
+ $ pipes
30
+ );
31
+
32
+ $ stdout = stream_get_contents ($ pipes [1 ]);
33
+ fclose ($ pipes [1 ]);
34
+
35
+ $ stderr = stream_get_contents ($ pipes [2 ]);
36
+ fclose ($ pipes [2 ]);
37
+
38
+ return proc_close ($ proc );
35
39
}
36
40
37
41
$ PKCS11_MODULE_PATH ="/usr/lib/softhsm/libsofthsm2.so " ;
@@ -44,21 +48,21 @@ $PHP11_PIN=123456;
44
48
$ PHP11_SOPIN =12345678 ;
45
49
46
50
file_put_contents ($ SOFTHSM2_CONF , sprintf (
47
- "directories.tokendir = %s " .PHP_EOL .
48
- "objectstore.backend = file " .PHP_EOL .
49
- "log.level = DEBUG " .PHP_EOL .
50
- "slots.removable = false " .PHP_EOL .
51
- "slots.mechanisms = ALL "
52
- , $ SOFTHSM2_TOKENDIR ));
51
+ "directories.tokendir = %s " .PHP_EOL .
52
+ "objectstore.backend = file " .PHP_EOL .
53
+ "log.level = DEBUG " .PHP_EOL .
54
+ "slots.removable = false " .PHP_EOL .
55
+ "slots.mechanisms = ALL " ,
56
+ $ SOFTHSM2_TOKENDIR ));
53
57
54
58
putenv (sprintf ("SOFTHSM2_CONF=%s " , $ SOFTHSM2_CONF ));
55
59
sexec ("softhsm2-util --show-slots | grep ^Slot | cut -d ' ' -f 2 " , $ out );
56
60
$ INIT11_SLOT =(int )$ out [0 ];
57
61
if ($ INIT11_SLOT != 0 ) {
58
- echo "Error slot " ;
59
- exec ("softhsm2-util --show-slots " , $ out );
60
- var_dump ($ out );
61
- exit (1 );
62
+ echo "Error slot " ;
63
+ exec ("softhsm2-util --show-slots " , $ out );
64
+ var_dump ($ out );
65
+ exit (1 );
62
66
}
63
67
64
68
sexec (sprintf ("softhsm2-util --init-token --free --slot %d --label TestVJToken --pin %s --so-pin %s " ,
@@ -68,8 +72,8 @@ sexec(sprintf("softhsm2-util --init-token --free --slot %d --label TestVJToken -
68
72
sexec (sprintf ("pkcs11-dump slotlist %s 2>/dev/null | grep SoftHSM | head -1 | cut -f 1 " ,
69
73
$ PKCS11_MODULE_PATH ), $ PHP11_SLOT );
70
74
if (!is_string ($ PHP11_SLOT )) {
71
- echo "Cannot detect the slot " .PHP_EOL ;
72
- exit (1 );
75
+ echo "Cannot detect the slot " .PHP_EOL ;
76
+ exit (1 );
73
77
}
74
78
$ PHP11_SLOT =(int )$ PHP11_SLOT ;
75
79
@@ -105,29 +109,29 @@ sexec(sprintf("pkcs11-tool --login --pin %s --write-object %s --type cert --l
105
109
106
110
$ key = openssl_pkey_get_private (sprintf ("pkcs11:object=VJPrivKey;type=private;pin-value=%s " , $ PHP11_PIN ));
107
111
if (!($ key instanceof OpenSSLAsymmetricKey)) {
108
- echo "Private Key NOK " .PHP_EOL ;
109
- exit (1 );
112
+ echo "Private Key NOK " .PHP_EOL ;
113
+ exit (1 );
110
114
}
111
115
echo "Private Key OK " .PHP_EOL ;
112
116
113
117
$ key = openssl_pkey_get_public (sprintf ("pkcs11:object=VJPubKey;type=public " ));
114
118
if (!($ key instanceof OpenSSLAsymmetricKey)) {
115
- echo "Public Key NOK " .PHP_EOL ;
116
- exit (1 );
119
+ echo "Public Key NOK " .PHP_EOL ;
120
+ exit (1 );
117
121
}
118
122
echo "Public Key OK " .PHP_EOL ;
119
123
120
124
$ cert = openssl_x509_read (sprintf ("pkcs11:object=VJCert;type=cert " ));
121
125
if (!($ cert instanceof OpenSSLCertificate)) {
122
- echo "Cert NOK " .PHP_EOL ;
123
- exit (1 );
126
+ echo "Cert NOK " .PHP_EOL ;
127
+ exit (1 );
124
128
}
125
129
echo "Cert OK " .PHP_EOL ;
126
130
$ certArray =openssl_x509_parse ($ cert );
127
131
128
132
if ($ certArray ['name ' ] !== "/CN=MyCertVJ " ) {
129
- echo "Cert content NOK " .PHP_EOL ;
130
- exit (1 );
133
+ echo "Cert content NOK " .PHP_EOL ;
134
+ exit (1 );
131
135
}
132
136
echo "Cert content OK " .PHP_EOL ;
133
137
0 commit comments