Certificate.php 3.2 KB

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