README 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. OpenSSL extension for PHP
  2. The functions implemented so far make it possible to seal and open data, and
  3. also create and verify signatures.
  4. NEW: support for S/MIME encrypt/decrypt/sign/verify, as well as more
  5. flexibility for specifying certificates/keys.
  6. To enable the extension, configure PHP with --with-openssl.
  7. Specifying keys/certificates
  8. ----------------------------
  9. Most of the functions require a key or a certificate as a parameter; to make
  10. things easy for you to use openssl, this extension allows you
  11. to specify certificates in the following way:
  12. 1. As an X.509 resource returned from openssl_x509_read
  13. 2. As a string in the format file://filename, where filename is the path to the
  14. certificate file (it will be opened and read automatically)
  15. 3. As a string containing the data from the certificate file
  16. Similarly, you can use the following methods of specifying a public key:
  17. 1. As a key resource returned from openssl_get_publickey
  18. 2. An X509 resource - public key only
  19. 3. As a string in the format file://filename
  20. 4. As a string containing the data from the key file
  21. Additionally, for a private key, when the openssl extension function does not
  22. allow you to enter the passphrase as a parameter you may use the syntax
  23. array($key, "passphrase") where $key can be a key specified using one of the
  24. methods listed above.
  25. Certificate Verification
  26. ------------------------
  27. When calling a function that will verify a signature/certificate, the cainfo
  28. parameter is an array containing file and directory names that specifiy the
  29. locations of trusted CA files. If a directory is specified, then it must be a
  30. correctly hashed directory.
  31. Misc:
  32. -----
  33. mixed openssl_error_string()
  34. returns the message from the last error that the OpenSSL library encountered
  35. and moves it's internal error pointer to the next message. If there are no
  36. more error messages, returns false.
  37. General Key/Cert Functions:
  38. ---------------------------
  39. resource openssl_get_privatekey(mixed key [, string passphrase])
  40. Parses the key data and returns a key resource identifier. If the key is
  41. encrypted a passphrase is needed. This can be supplied as second argument.
  42. resource openssl_get_publickey(mixed cert)
  43. Extracts the public key from the given certificate or public key and returns
  44. a key resource identifier.
  45. void openssl_free_key(resource key)
  46. Frees the resource given by the key resource identifier.
  47. Note that this function does not accept the extended key specification
  48. syntax mentioned above, as it doesn't make sense in this case!
  49. array openssl_x509_parse(mixed x509[, bool shortnames=true])
  50. Parses the certificate data and returns an array containing information
  51. about the certificate, it's intended purposes, subject, issuer, validity
  52. etc. etc. If shortnames is true (the default) then the fields will be
  53. keyed by the shortname forms eg: CN as opposed to commonName (shortnames
  54. = false).
  55. bool openssl_x509_checkpurpose(mixed x509cert, int purpose,
  56. array cainfo[, string untrustedfile])
  57. Verifies if the certificate can be used for a specific purpose.
  58. Purpose can be one of the following values:
  59. X509_PURPOSE_SSL_CLIENT
  60. X509_PURPOSE_SSL_SERVER
  61. X509_PURPOSE_NS_SSL_SERVER
  62. X509_PURPOSE_SMIME_SIGN
  63. X509_PURPOSE_SMIME_ENCRYPT
  64. X509_PURPOSE_CRL_SIGN
  65. X509_PURPOSE_ANY
  66. cainfo is an array of CA information (as mentioned above).
  67. untrusted file specifies a file containing a bunch of certs that
  68. are not trusted but may be useful in validating the certificate.
  69. resource openssl_read_x509(mixed cert)
  70. Parses the cert and returns a resource that can be used with the
  71. other openssl functions
  72. void openssl_free_x509(resource x509)
  73. Frees the resource given by the x509 resource identifier.
  74. Note that this function does not accept the extended cert specification
  75. syntax mentioned above, as it doesn't make sense in this case!
  76. PKCS7 (S/MIME) Sign/Verify/Encrypt/Decrypt Functions:
  77. -----------------------------------------------------
  78. These functions allow you to manipulate S/MIME messages!
  79. They are based on apps/smime.c from the openssl dist, so for information,
  80. see the documentation for openssl.
  81. You may pass in some flags that affect how these functions work using
  82. and array containing the following values:
  83. "detached", "nodetached", "text", "nointern", "noverify", "nochain",
  84. "nocerts", "noattr", "binary", "nosigs".
  85. The options correspond to the options of the same name for the
  86. "openssl smime" command (smime(1)).
  87. bool openssl_pkcs7_verify(string filename, array flags[, string signerscerts][,
  88. array cainfo])
  89. Verifies that the signature on the MIME message contained in the file
  90. named by filename is valid. If signerscerts is passed in, it holds the
  91. name of a file into which the certificates of those that signed the
  92. message will be stored.
  93. cainfo and flags are CA information and flag information as described
  94. above.
  95. bool openssl_pkcs7_encrypt(string infile, string outfile, array recipcerts,
  96. array headers[, array flags])
  97. Encrypts the MIME message contained in the file named by infile using
  98. the certificates held in recipcerts. The result is place in the file
  99. named outfile.
  100. recipcerts is an array of certificate identifiers representing the certs
  101. of the intended recipients of the message.
  102. headers is an array of headers to prepend to the message: they will
  103. not be included in the encoded section.
  104. flags is flag information as described above.
  105. Hint: you will want to put "To", "From", and "Subject" headers in headers.
  106. Headers can be either an assoc array keyed by header named, or can be
  107. and indexed array containing a single header line per value.
  108. The message will be encoded using a RC2-40 bit cipher.
  109. TODO: allow user to specify cipher.
  110. bool openssl_pkcs7_sign(string infile, string outfile, mixed signcert, mixed
  111. signkey, array headers[, array flags][, string extracertsfilename])
  112. Signs the MIME message contained in the file named by infile using the
  113. certificate and key pair identified by signcert/signkey.
  114. Signkey must be the private key corresponding to signcert.
  115. The result is placed in the file named by outfile.
  116. Headers and flags have the same effects as mentioned above.
  117. extracertsfilename names a file containing a bunch of additional certificates
  118. to include in the signature, in order to aid the recipient in verifying the
  119. message.
  120. bool openssl_pkcs7_decrypt(string infilename, string outfilename, mixed
  121. recipcert, mixed recipkey)
  122. Decrypts the MIME message contained in the file named by infilename
  123. using the certificate and private key pair recipcert/recipkey.
  124. The descrypted result is placed in outfilename.
  125. TODO: add flags parameter, if needed?
  126. EVP Sign/Verify/Encrypt/Decrypt Functions:
  127. ------------------------------------------
  128. bool openssl_sign(string data, &string signature, mixed key)
  129. Uses key to create signature for data, returns true on success and false
  130. on failure. signature is passed by reference and contains the newly created
  131. signature on success.
  132. int openssl_verify(string data, string signature, mixed key)
  133. Uses key to verify that the signature is correct for the given data.
  134. Returns 1 if correct, 0 if incorrect, and -1 on error.
  135. int openssl_seal(string data, &string sealdata, &array ekeys, array pubkeys)
  136. Encrypts data using pubkeys, so that only owners of the respective private
  137. keys and ekeys can decrypt and read the data. Returns the length of the
  138. sealed data on success, else false. On success, sealdata and ekeys hold
  139. the sealed data and envelope keys.
  140. bool openssl_open(string data, &string opendata, string ekey, int privkey)
  141. Opens (decrypts) sealed data using a private key and the corresponding
  142. envelope key. Returns true on success and false on failure. On success,
  143. opendata will hold the descypted data.
  144. See below for more details on usage. Also feel free to mail me at
  145. venaas@php.net if you have questions. The OpenSSL documentation,
  146. especially the EVP documentation at
  147. http://www.openssl.org/docs/crypto/evp.html, might also be of help.
  148. HOWTO:
  149. To do anything you need a private key and a certificate containing the
  150. corresponding public key. This is similar to what you have using say an
  151. Apache webserver with OpenSSL. For testing you could try keys that come
  152. with OpenSSL, that's what the sample scripts below do. You can also get
  153. keys from some CA, or you can create them yourself.
  154. Creating private key
  155. To generate an unprotected 1024 bit RSA private key you can do
  156. openssl genrsa -out /tmp/test.key 1024
  157. Private keys should be protected by a passphrase though.
  158. Creating a self signed certificate
  159. To generate a self signed certificate from the key that is valid for
  160. 365 days, do
  161. openssl req -new -key /tmp/test.key -out /tmp/test.crt -days 365 -x509
  162. Example usage
  163. These examples use keys that come with OpenSSL, you should perhaps test with
  164. those first.
  165. Seal and open
  166. <?php
  167. $data = "Follow the white rabbit";
  168. // Get certificate into a string
  169. // this file comes with OpenSSL 0.9.6
  170. $fp = fopen("/src/openssl-0.9.6/demos/maurice/cert.pem", "r");
  171. $cert = fread($fp, 8192);
  172. fclose($fp);
  173. // get public key from certificate
  174. $pk1 = openssl_get_publickey($cert);
  175. // $pk1 is an encryption key resource id if success, else false
  176. // Repeat if want public keys for multiple parties
  177. $fp = fopen("/src/openssl-0.9.6/demos/sign/cert.pem", "r");
  178. $cert = fread($fp, 8192);
  179. fclose($fp);
  180. $pk2 = openssl_get_publickey($cert);
  181. // seal data, only owners of $pk1 and $pk2 can decrypt $sealed with keys
  182. // $ekeys[0] and $ekeys[1] respectively.
  183. openssl_seal($data, $sealed, $ekeys, array($pk1,$pk2));
  184. openssl_free_key($pk1);
  185. openssl_free_key($pk2);
  186. // now we try to decrypt data for one of the recipients
  187. $fp = fopen("/src/openssl-0.9.6/demos/sign/key.pem", "r");
  188. // Get PEM coded key into $pkey
  189. $pkey = fread($fp, 8192);
  190. fclose($fp);
  191. // $key will be resource id for unpacked $pkey
  192. $key = openssl_get_privatekey($pkey);
  193. openssl_open($sealed, $open, $ekeys[1], $key);
  194. openssl_free_key($key);
  195. echo "$open\n";
  196. ?>
  197. Sign and verify
  198. <?php
  199. $data = "Follow the white rabbit";
  200. // First we need to have a string containing the private key in PEM format
  201. // this file comes with OpenSSL 0.9.6
  202. $fp = fopen("/src/openssl-0.9.6/demos/sign/key.pem", "r");
  203. $pkey = fread($fp, 8192);
  204. fclose($fp);
  205. // get private key from the PEM format
  206. // $key is an encr key resource id if success, else false
  207. $key = openssl_get_privatekey($pkey);
  208. // calculate signature
  209. openssl_sign($data, $signature, $key);
  210. openssl_free_key($key);
  211. // recipient verifies signature
  212. // read certificate
  213. $fp = fopen("/src/openssl-0.9.6/demos/sign/cert.pem", "r");
  214. $cert = fread($fp, 8192);
  215. fclose($fp);
  216. // Get public key from the certificate
  217. $pubkey = openssl_get_publickey($cert);
  218. // state whether signature is okay or not
  219. echo openssl_verify($data, $signature, $pubkey) == 1 ? "ok\n" : "bad\n";
  220. // free key
  221. openssl_free_key($pubkey);
  222. ?>