1
0

Certificate.php 3.0 KB

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