PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/main/php/security/cert/CSR.class.php

http://github.com/xp-framework/xp-framework
PHP | 103 lines | 34 code | 8 blank | 61 comment | 3 complexity | 5ab1127ce02f626a7ec15052b24f75c2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses('security.cert.X509Certificate', 'security.KeyPair');
  7. /**
  8. * Certificate signing requests
  9. *
  10. * Example [Creating a self-signed-certificate]:
  11. * <code>
  12. * uses('security.cert.CSR');
  13. *
  14. * try {
  15. * if ($keypair= KeyPair::generate('md5', OPENSSL_KEYTYPE_RSA)) {
  16. * $csr= new CSR(new Principal(array(
  17. * 'C' => 'DE',
  18. * 'ST' => 'Baden-Württemberg',
  19. * 'L' => 'Karlsruhe',
  20. * 'O' => 'XP',
  21. * 'OU' => 'XP Team',
  22. * 'CN' => 'Timm Friebe',
  23. * 'EMAIL' => 'xp@php3.de'
  24. * )), $keypair);
  25. * $cert= $csr->sign($keypair);
  26. * }
  27. * } catch(XPException $e) {
  28. * $e->printStackTrace();
  29. * exit();
  30. * }
  31. *
  32. * var_dump(
  33. * $keypair,
  34. * $keypair->export('password'),
  35. * $csr,
  36. * $csr->export(),
  37. * $cert,
  38. * $cert->export()
  39. * );
  40. * </code>
  41. *
  42. * @ext openssl
  43. * @purpose Represent a CSR
  44. */
  45. class CSR extends Object {
  46. /**
  47. * Constructor
  48. *
  49. * @param security.Principal principal
  50. * @param security.KeyPair keypair
  51. */
  52. public function __construct($principal, $keypair) {
  53. $this->_res= openssl_csr_new(array(
  54. 'countryName' => $principal->getCountryName(),
  55. 'stateOrProvinceName' => $principal->getStateOrProvinceName(),
  56. 'localityName' => $principal->getLocalityName(),
  57. 'organizationName' => $principal->getOrganizationName(),
  58. 'organizationalUnitName' => $principal->getOrganizationalUnitName(),
  59. 'commonName' => $principal->getCommonName(),
  60. 'emailAddress' => $principal->getEmailAddress()
  61. ), $keypair->_res);
  62. }
  63. /**
  64. * Export this CSR
  65. *
  66. * @return string CSR
  67. */
  68. public function export() {
  69. if (FALSE === openssl_csr_export($this->_res, $out)) {
  70. trigger_error(implode("\n @", OpenSslUtil::getErrors()), E_USER_NOTICE);
  71. throw new XPException('Could not export CSR');
  72. }
  73. return $out;
  74. }
  75. /**
  76. * Sign this CSR
  77. *
  78. * @param security.KeyPair keypair
  79. * @param int days default 365
  80. * @param var cacert default NULL
  81. * @return security.cert.X509Certificate
  82. */
  83. public function sign($keypair, $days= 365, $cacert= NULL) {
  84. if (FALSE === ($x509= openssl_csr_sign($this->_res, $cacert, $keypair->_res, $days))) {
  85. trigger_error(implode("\n @", OpenSslUtil::getErrors()), E_USER_NOTICE);
  86. throw new CertificateException('Cannot sign certificate');
  87. }
  88. if (FALSE === openssl_x509_export($x509, $str)) {
  89. trigger_error(implode("\n @", OpenSslUtil::getErrors()), E_USER_NOTICE);
  90. throw new CertificateException('Cannot export certificate');
  91. }
  92. return X509Certificate::fromString($str);
  93. }
  94. }
  95. ?>