nextcloud/lib/private/Security/Certificate.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
2.5 KiB
PHP
Raw Normal View History

2014-08-15 13:54:03 -04:00
<?php
declare(strict_types=1);
2014-08-15 13:54:03 -04:00
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
2014-08-15 13:54:03 -04:00
*/
2014-08-18 10:30:23 -04:00
namespace OC\Security;
2014-08-15 13:54:03 -04:00
use OCP\ICertificate;
class Certificate implements ICertificate {
protected string $name;
2014-08-15 13:54:03 -04:00
protected ?string $commonName;
2014-08-15 13:54:03 -04:00
protected ?string $organization;
2014-08-15 13:54:03 -04:00
protected \DateTime $issueDate;
2014-08-15 13:54:03 -04:00
protected \DateTime $expireDate;
2014-08-15 13:54:03 -04:00
protected ?string $issuerName;
2014-08-15 13:54:03 -04:00
protected ?string $issuerOrganization;
2014-08-15 13:54:03 -04:00
2014-08-18 07:57:38 -04:00
/**
* @param string $data base64 encoded certificate
* @throws \Exception If the certificate could not get parsed
2014-08-18 07:57:38 -04:00
*/
public function __construct(string $data, string $name) {
2014-08-15 13:54:03 -04:00
$this->name = $name;
$gmt = new \DateTimeZone('GMT');
// If string starts with "file://" ignore the certificate
$query = 'file://';
if (strtolower(substr($data, 0, strlen($query))) === $query) {
throw new \Exception('Certificate could not get parsed.');
}
$info = openssl_x509_parse($data);
if (!is_array($info)) {
// There is a non-standardized certificate format only used by OpenSSL. Replace all
// separators and try again.
$data = str_replace(
['-----BEGIN TRUSTED CERTIFICATE-----', '-----END TRUSTED CERTIFICATE-----'],
['-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----'],
$data,
);
$info = openssl_x509_parse($data);
}
if (!is_array($info)) {
throw new \Exception('Certificate could not get parsed.');
}
$this->commonName = $info['subject']['CN'] ?? null;
$this->organization = $info['subject']['O'] ?? null;
$this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt);
$this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt);
$this->issuerName = $info['issuer']['CN'] ?? null;
$this->issuerOrganization = $info['issuer']['O'] ?? null;
2014-08-15 13:54:03 -04:00
}
public function getName(): string {
2014-08-15 13:54:03 -04:00
return $this->name;
}
public function getCommonName(): ?string {
2014-08-15 13:54:03 -04:00
return $this->commonName;
}
public function getOrganization(): ?string {
2014-08-15 13:54:03 -04:00
return $this->organization;
}
public function getIssueDate(): \DateTime {
2014-08-15 13:54:03 -04:00
return $this->issueDate;
}
public function getExpireDate(): \DateTime {
2014-08-15 13:54:03 -04:00
return $this->expireDate;
}
public function isExpired(): bool {
2014-08-15 13:54:03 -04:00
$now = new \DateTime();
2014-08-18 07:57:38 -04:00
return $this->issueDate > $now or $now > $this->expireDate;
2014-08-15 13:54:03 -04:00
}
public function getIssuerName(): ?string {
2014-08-15 13:54:03 -04:00
return $this->issuerName;
}
public function getIssuerOrganization(): ?string {
2014-08-15 13:54:03 -04:00
return $this->issuerOrganization;
}
}