certificate.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Security;
  9. use OCP\ICertificate;
  10. class Certificate implements ICertificate {
  11. protected $name;
  12. protected $commonName;
  13. protected $organization;
  14. protected $serial;
  15. protected $issueDate;
  16. protected $expireDate;
  17. protected $issuerName;
  18. protected $issuerOrganization;
  19. /**
  20. * @param string $data base64 encoded certificate
  21. * @param string $name
  22. * @throws \Exception If the certificate could not get parsed
  23. */
  24. public function __construct($data, $name) {
  25. $this->name = $name;
  26. try {
  27. $gmt = new \DateTimeZone('GMT');
  28. $info = openssl_x509_parse($data);
  29. $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null;
  30. $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null;
  31. $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt);
  32. $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt);
  33. $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null;
  34. $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null;
  35. } catch (\Exception $e) {
  36. throw new \Exception('Certificate could not get parsed.');
  37. }
  38. }
  39. /**
  40. * @return string
  41. */
  42. public function getName() {
  43. return $this->name;
  44. }
  45. /**
  46. * @return string|null
  47. */
  48. public function getCommonName() {
  49. return $this->commonName;
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function getOrganization() {
  55. return $this->organization;
  56. }
  57. /**
  58. * @return \DateTime
  59. */
  60. public function getIssueDate() {
  61. return $this->issueDate;
  62. }
  63. /**
  64. * @return \DateTime
  65. */
  66. public function getExpireDate() {
  67. return $this->expireDate;
  68. }
  69. /**
  70. * @return bool
  71. */
  72. public function isExpired() {
  73. $now = new \DateTime();
  74. return $this->issueDate > $now or $now > $this->expireDate;
  75. }
  76. /**
  77. * @return string|null
  78. */
  79. public function getIssuerName() {
  80. return $this->issuerName;
  81. }
  82. /**
  83. * @return string|null
  84. */
  85. public function getIssuerOrganization() {
  86. return $this->issuerOrganization;
  87. }
  88. }