Skip to content

Commit cdef0dd

Browse files
fix(Certificate): use safe fallbacks for cert expiry info
1 parent 6f918ee commit cdef0dd

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/Certificate.inc

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,27 @@ class Certificate extends Model {
7171
$this->prv = new Base64Field(
7272
required: true,
7373
sensitive: true,
74-
validators: [new X509Validator(allow_prv: true, allow_ecprv: true, allow_rsa: true)],
74+
validators: [new X509Validator(allow_prv: true, allow_rsa: true, allow_ecprv: true)],
7575
help_text: 'The X509 private key string.',
7676
);
7777
$this->valid_from = new DateTimeField(
78+
allow_null: true,
7879
read_only: true,
7980
representation_only: true,
8081
help_text: 'The start date from which this certificate is valid.',
8182
);
8283
$this->valid_until = new DateTimeField(
84+
allow_null: true,
8385
read_only: true,
8486
representation_only: true,
8587
help_text: 'The date until which this certificate is valid.',
8688
);
8789
$this->valid_days_left = new IntegerField(
90+
allow_null: true,
8891
read_only: true,
92+
representation_only: true,
8993
minimum: 1,
9094
maximum: 12000,
91-
representation_only: true,
9295
help_text: 'The number of days remaining until this certificate expires.',
9396
);
9497

@@ -97,31 +100,41 @@ class Certificate extends Model {
97100

98101
/**
99102
* Gets the start date of this certificate.
100-
* @returns string The start date in 'Y-m-d H:i:s' format.
103+
* @returns string|null The start date in 'Y-m-d H:i:s' format.
101104
*/
102-
protected function from_internal_valid_from(): string {
103-
return cert_get_dates($this->crt->value, false, false)[0]->format('Y-m-d H:i:s');
105+
protected function from_internal_valid_from(): string|null {
106+
$from_date = cert_get_dates($this->crt->value, false, false)[0];
107+
if (!$from_date) {
108+
return null;
109+
}
110+
111+
return $from_date->format('Y-m-d H:i:s');
104112
}
105113

106114
/**
107115
* Gets the expiration date of this certificate.
108-
* @returns string The expiration date in 'Y-m-d H:i:s' format.
116+
* @returns string|null The expiration date in 'Y-m-d H:i:s' format.
109117
*/
110-
protected function from_internal_valid_until(): string {
111-
return cert_get_dates($this->crt->value, false, false)[1]->format('Y-m-d H:i:s');
118+
protected function from_internal_valid_until(): string|null {
119+
$until_date = cert_get_dates($this->crt->value, false, false)[1];
120+
if (!$until_date) {
121+
return null;
122+
}
123+
124+
return $until_date->format('Y-m-d H:i:s');
112125
}
113126

114127
/**
115128
* Calculates the number of days left until this certificate expires.
116129
* @returns int The number of entire days left until expiration. -1 if already expired.
117130
*/
118-
protected function from_internal_valid_days_left(): int {
131+
protected function from_internal_valid_days_left(): int|null {
119132
$enddate = cert_get_dates($this->crt->value, false, false)[1];
120133
if (!$enddate) {
121-
return false;
134+
return null;
122135
}
123136
$interval = (new \DateTime())->diff($enddate);
124-
return $interval->invert ? -1 : $interval->days;
137+
return $interval->invert ? 0 - $interval->days : $interval->days;
125138
}
126139

127140
/**

0 commit comments

Comments
 (0)