certificate.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Security;
  24. use OCP\ICertificate;
  25. class Certificate implements ICertificate {
  26. protected $name;
  27. protected $commonName;
  28. protected $organization;
  29. protected $serial;
  30. protected $issueDate;
  31. protected $expireDate;
  32. protected $issuerName;
  33. protected $issuerOrganization;
  34. /**
  35. * @param string $data base64 encoded certificate
  36. * @param string $name
  37. * @throws \Exception If the certificate could not get parsed
  38. */
  39. public function __construct($data, $name) {
  40. $this->name = $name;
  41. $gmt = new \DateTimeZone('GMT');
  42. $info = openssl_x509_parse($data);
  43. if(!is_array($info)) {
  44. throw new \Exception('Certificate could not get parsed.');
  45. }
  46. $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null;
  47. $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null;
  48. $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt);
  49. $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt);
  50. $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null;
  51. $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null;
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function getName() {
  57. return $this->name;
  58. }
  59. /**
  60. * @return string|null
  61. */
  62. public function getCommonName() {
  63. return $this->commonName;
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getOrganization() {
  69. return $this->organization;
  70. }
  71. /**
  72. * @return \DateTime
  73. */
  74. public function getIssueDate() {
  75. return $this->issueDate;
  76. }
  77. /**
  78. * @return \DateTime
  79. */
  80. public function getExpireDate() {
  81. return $this->expireDate;
  82. }
  83. /**
  84. * @return bool
  85. */
  86. public function isExpired() {
  87. $now = new \DateTime();
  88. return $this->issueDate > $now or $now > $this->expireDate;
  89. }
  90. /**
  91. * @return string|null
  92. */
  93. public function getIssuerName() {
  94. return $this->issuerName;
  95. }
  96. /**
  97. * @return string|null
  98. */
  99. public function getIssuerOrganization() {
  100. return $this->issuerOrganization;
  101. }
  102. }