1
0

Certificate.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 string $name;
  30. protected ?string $commonName;
  31. protected ?string $organization;
  32. protected \DateTime $issueDate;
  33. protected \DateTime $expireDate;
  34. protected ?string $issuerName;
  35. protected ?string $issuerOrganization;
  36. /**
  37. * @param string $data base64 encoded certificate
  38. * @throws \Exception If the certificate could not get parsed
  39. */
  40. public function __construct(string $data, string $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 = $info['subject']['CN'] ?? null;
  53. $this->organization = $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 = $info['issuer']['CN'] ?? null;
  57. $this->issuerOrganization = $info['issuer']['O'] ?? null;
  58. }
  59. public function getName(): string {
  60. return $this->name;
  61. }
  62. public function getCommonName(): ?string {
  63. return $this->commonName;
  64. }
  65. public function getOrganization(): ?string {
  66. return $this->organization;
  67. }
  68. public function getIssueDate(): \DateTime {
  69. return $this->issueDate;
  70. }
  71. public function getExpireDate(): \DateTime {
  72. return $this->expireDate;
  73. }
  74. public function isExpired(): bool {
  75. $now = new \DateTime();
  76. return $this->issueDate > $now or $now > $this->expireDate;
  77. }
  78. public function getIssuerName(): ?string {
  79. return $this->issuerName;
  80. }
  81. public function getIssuerOrganization(): ?string {
  82. return $this->issuerOrganization;
  83. }
  84. }