Skip to content

Commit 4cec53a

Browse files
committed
Added whole bunch of PKCS7 (S/MIME) functions and made the key/cert params
a bit more friendly to use. See the README for more info.
1 parent 2348bb3 commit 4cec53a

File tree

4 files changed

+1372
-195
lines changed

4 files changed

+1372
-195
lines changed

ext/openssl/CREDITS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
OpenSSL
2-
Stig Venaas
2+
Stig Venaas, Wez Furlong

ext/openssl/README

Lines changed: 171 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,210 @@
11
OpenSSL extension for PHP4
22
$Id$
33

4-
The functions implemented so far make it possible to seal and open data,
5-
and also create and verify signatures. To enable the extension, configure
6-
PHP with --with-openssl.
4+
The functions implemented so far make it possible to seal and open data, and
5+
also create and verify signatures.
76

7+
NEW: support for S/MIME encrypt/decrypt/sign/verify, as well as more
8+
flexibility for specifying certificates/keys.
89

9-
Functions:
10+
To enable the extension, configure PHP with --with-openssl.
1011

11-
int openssl_get_privatekey(string key [, string passphrase])
12+
Specifying keys/certificates
13+
----------------------------
14+
15+
Most of the functions require a key or a certificate as a parameter; to make
16+
things easy for you to use openssl, this extension allows you
17+
to specify certificates in the following way:
18+
19+
1. As an X.509 resource returned from openssl_x509_read
20+
2. As a string in the format file://filename, where filename is the path to the
21+
certificate file (it will be opened and read automatically)
22+
3. As a string containing the data from the certificate file
23+
24+
Similarly, you can use the following methods of specifying a public key:
25+
26+
1. As a key resource returned from openssl_get_publickey
27+
2. An X509 resource - public key only
28+
3. As a string in the format file://filename
29+
4. As a string containing the data from the key file
30+
31+
Additionally, for a private key, when the openssl extension function does not
32+
allow you to enter the passphrase as a parameter you may use the syntax
33+
array($key, "passphrase") where $key can be a key specified using one of the
34+
methods listed above.
35+
36+
Certificate Verification
37+
------------------------
38+
When calling a function that will verify a signature/certificate, the cainfo
39+
parameter is an array containing file and directory names that specifiy the
40+
locations of trusted CA files. If a directory is specified, then it must be a
41+
correctly hashed directory.
42+
43+
Misc:
44+
-----
45+
46+
mixed openssl_error_string()
47+
48+
returns the message from the last error that the OpenSSL library encountered
49+
and moves it's internal error pointer to the next message. If there are no
50+
more error messages, returns false.
51+
52+
General Key/Cert Functions:
53+
---------------------------
54+
55+
resource openssl_get_privatekey(mixed key [, string passphrase])
1256

1357
Parses the key data and returns a key resource identifier. If the key is
1458
encrypted a passphrase is needed. This can be supplied as second argument.
1559

1660

17-
int openssl_get_publickey(string cert)
61+
resource openssl_get_publickey(mixed cert)
1862

1963
Extracts the public key from the given certificate and returns a key
2064
resource identifier.
2165

2266

23-
void openssl_free_key(int key)
67+
void openssl_free_key(resource key)
2468

2569
Frees the resource given by the key resource identifier.
70+
Note that this function does not accept the extended key specification
71+
syntax mentioned above, as it doesn't make sense in this case!
72+
73+
array openssl_x509_parse(mixed x509[, bool shortnames=true])
74+
75+
Parses the certificate data and returns an array containing information
76+
about the certificate, it's intended purposes, subject, issuer, validity
77+
etc. etc. If shortnames is true (the default) then the fields will be
78+
keyed by the shortname forms eg: CN as opposed to commonName (shortnames
79+
= false).
80+
81+
82+
bool openssl_x509_checkpurpose(mixed x509cert, int purpose,
83+
array cainfo[, string untrustedfile])
84+
85+
Verifies if the certificate can be used for a specific purpose.
86+
Purpose can be one of the following values:
87+
X509_PURPOSE_SSL_CLIENT
88+
X509_PURPOSE_SSL_SERVER
89+
X509_PURPOSE_NS_SSL_SERVER
90+
X509_PURPOSE_SMIME_SIGN
91+
X509_PURPOSE_SMIME_ENCRYPT
92+
X509_PURPOSE_CRL_SIGN
93+
X509_PURPOSE_ANY
94+
95+
cainfo is an array of CA information (as mentioned above).
96+
untrusted file specifies a file containing a bunch of certs that
97+
are not trusted but may be useful in validating the certificate.
98+
99+
100+
resource openssl_read_x509(mixed cert)
101+
102+
Parses the cert and returns a resource that can be used with the
103+
other openssl functions
104+
105+
106+
void openssl_free_x509(resource x509)
107+
108+
Frees the resource given by the x509 resource identifier.
109+
Note that this function does not accept the extended cert specification
110+
syntax mentioned above, as it doesn't make sense in this case!
111+
112+
113+
PKCS7 (S/MIME) Sign/Verify/Encrypt/Decrypt Functions:
114+
-----------------------------------------------------
115+
116+
These functions allow you to manipulate S/MIME messages!
117+
118+
They are based on apps/smime.c from the openssl dist, so for information,
119+
see the documentation for openssl.
120+
121+
You may pass in some flags that affect how these functions work using
122+
and array containing the following values:
123+
"detached", "nodetached", "text", "nointern", "noverify", "nochain",
124+
"nocerts", "noattr", "binary", "nosigs".
125+
The options correspond to the options of the same name for the
126+
"openssl smime" command (smime(1)).
127+
128+
129+
bool openssl_pkcs7_verify(string filename, array flags[, string signerscerts][,
130+
array cainfo])
131+
132+
Verifies that the signature on the MIME message contained in the file
133+
named by filename is valid. If signerscerts is passed in, it holds the
134+
name of a file into which the certificates of those that signed the
135+
message will be stored.
136+
cainfo and flags are CA information and flag information as described
137+
above.
138+
139+
140+
bool openssl_pkcs7_encrypt(string infile, string outfile, array recipcerts,
141+
array headers[, array flags])
142+
143+
Encrypts the MIME message contained in the file named by infile using
144+
the certificates held in recipcerts. The result is place in the file
145+
named outfile.
146+
recipcerts is an array of certificate identifiers representing the certs
147+
of the intended recipients of the message.
148+
headers is an array of headers to prepend to the message: they will
149+
not be included in the encoded section.
150+
flags is flag information as described above.
151+
Hint: you will want to put "To", "From", and "Subject" headers in headers.
152+
Headers can be either an assoc array keyed by header named, or can be
153+
and indexed array containing a single header line per value.
154+
The message will be encoded using a RC2-40 bit cipher.
155+
TODO: allow user to specify cipher.
156+
157+
bool openssl_pkcs7_sign(string infile, string outfile, mixed signcert, mixed
158+
signkey, array headers[, array flags][, string extracertsfilename])
159+
160+
Signs the MIME message contained in the file named by infile using the
161+
certificate and key pair identified by signcert/signkey.
162+
Signkey must be the private key corresponding to signcert.
163+
The result is placed in the file named by outfile.
164+
Headers and flags have the same effects as mentioned above.
165+
extracertsfilename names a file containing a bunch of additional certificates
166+
to include in the signature, in order to aid the recipient in verifying the
167+
message.
168+
169+
170+
bool openssl_pkcs7_decrypt(string infilename, string outfilename, mixed
171+
recipcert, mixed recipkey)
172+
173+
Decrypts the MIME message contained in the file named by infilename
174+
using the certificate and private key pair recipcert/recipkey.
175+
The descrypted result is placed in outfilename.
176+
TODO: add flags parameter, if needed?
177+
26178

179+
EVP Sign/Verify/Encrypt/Decrypt Functions:
180+
------------------------------------------
27181

28-
bool openssl_sign(string data, string signature, int key)
182+
bool openssl_sign(string data, &string signature, mixed key)
29183

30184
Uses key to create signature for data, returns true on success and false
31-
on failure.
185+
on failure. signature is passed by reference and contains the newly created
186+
signature on success.
32187

33188

34-
int openssl_verify(string data, string signature, int key)
189+
int openssl_verify(string data, string signature, mixed key)
35190

36191
Uses key to verify that the signature is correct for the given data.
37192
Returns 1 if correct, 0 if incorrect, and -1 on error.
38193

39194

40-
int openssl_seal(string data, string sealdata, array ekeys, array pubkeys)
195+
int openssl_seal(string data, &string sealdata, &array ekeys, array pubkeys)
41196

42197
Encrypts data using pubkeys, so that only owners of the respective private
43198
keys and ekeys can decrypt and read the data. Returns the length of the
44-
sealed data on success, else false.
199+
sealed data on success, else false. On success, sealdata and ekeys hold
200+
the sealed data and envelope keys.
45201

46202

47-
bool openssl_open(string data, string opendata, string ekey, int privkey)
203+
bool openssl_open(string data, &string opendata, string ekey, int privkey)
48204

49205
Opens (decrypts) sealed data using a private key and the corresponding
50-
envelope key. Returns true on success and false on failure.
206+
envelope key. Returns true on success and false on failure. On success,
207+
opendata will hold the descypted data.
51208

52209

53210
See below for more details on usage. Also feel free to mail me at

0 commit comments

Comments
 (0)