Certificate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // There is a non-standardized certificate format only used by OpenSSL. Replace all
  51. // separators and try again.
  52. $data = str_replace(
  53. ['-----BEGIN TRUSTED CERTIFICATE-----', '-----END TRUSTED CERTIFICATE-----'],
  54. ['-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----'],
  55. $data,
  56. );
  57. $info = openssl_x509_parse($data);
  58. }
  59. if (!is_array($info)) {
  60. throw new \Exception('Certificate could not get parsed.');
  61. }
  62. $this->commonName = $info['subject']['CN'] ?? null;
  63. $this->organization = $info['subject']['O'] ?? null;
  64. $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt);
  65. $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt);
  66. $this->issuerName = $info['issuer']['CN'] ?? null;
  67. $this->issuerOrganization = $info['issuer']['O'] ?? null;
  68. }
  69. public function getName(): string {
  70. return $this->name;
  71. }
  72. public function getCommonName(): ?string {
  73. return $this->commonName;
  74. }
  75. public function getOrganization(): ?string {
  76. return $this->organization;
  77. }
  78. public function getIssueDate(): \DateTime {
  79. return $this->issueDate;
  80. }
  81. public function getExpireDate(): \DateTime {
  82. return $this->expireDate;
  83. }
  84. public function isExpired(): bool {
  85. $now = new \DateTime();
  86. return $this->issueDate > $now or $now > $this->expireDate;
  87. }
  88. public function getIssuerName(): ?string {
  89. return $this->issuerName;
  90. }
  91. public function getIssuerOrganization(): ?string {
  92. return $this->issuerOrganization;
  93. }
  94. }