Coverage Report

Created: 2025-06-11 06:41

/src/boringssl/crypto/asn1/a_gentm.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/asn1.h>
16
#include <openssl/bytestring.h>
17
#include <openssl/err.h>
18
#include <openssl/mem.h>
19
#include <openssl/posix_time.h>
20
21
#include <stdlib.h>
22
#include <string.h>
23
#include <time.h>
24
25
#include "internal.h"
26
27
0
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d) {
28
0
  if (d->type != V_ASN1_GENERALIZEDTIME) {
29
0
    return 0;
30
0
  }
31
0
  CBS cbs;
32
0
  CBS_init(&cbs, d->data, (size_t)d->length);
33
0
  if (!CBS_parse_generalized_time(&cbs, tm, /*allow_timezone_offset=*/0)) {
34
0
    return 0;
35
0
  }
36
0
  return 1;
37
0
}
38
39
0
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) {
40
0
  return asn1_generalizedtime_to_tm(NULL, d);
41
0
}
42
43
0
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) {
44
0
  size_t len = strlen(str);
45
0
  CBS cbs;
46
0
  CBS_init(&cbs, (const uint8_t *)str, len);
47
0
  if (!CBS_parse_generalized_time(&cbs, /*out_tm=*/NULL,
48
0
                                  /*allow_timezone_offset=*/0)) {
49
0
    return 0;
50
0
  }
51
0
  if (s != NULL) {
52
0
    if (!ASN1_STRING_set(s, str, len)) {
53
0
      return 0;
54
0
    }
55
0
    s->type = V_ASN1_GENERALIZEDTIME;
56
0
  }
57
0
  return 1;
58
0
}
59
60
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
61
0
                                               int64_t posix_time) {
62
0
  return ASN1_GENERALIZEDTIME_adj(s, posix_time, 0, 0);
63
0
}
64
65
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
66
                                               int64_t posix_time,
67
                                               int offset_day,
68
0
                                               long offset_sec) {
69
0
  struct tm data;
70
0
  if (!OPENSSL_posix_to_tm(posix_time, &data)) {
71
0
    return NULL;
72
0
  }
73
74
0
  if (offset_day || offset_sec) {
75
0
    if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
76
0
      return NULL;
77
0
    }
78
0
  }
79
80
0
  if (data.tm_year < 0 - 1900 || data.tm_year > 9999 - 1900) {
81
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
82
0
    return NULL;
83
0
  }
84
85
0
  char buf[16];
86
0
  int ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
87
0
                     data.tm_year + 1900, data.tm_mon + 1, data.tm_mday,
88
0
                     data.tm_hour, data.tm_min, data.tm_sec);
89
  // |snprintf| must write exactly 15 bytes (plus the NUL) to the buffer.
90
0
  BSSL_CHECK(ret == static_cast<int>(sizeof(buf) - 1));
91
92
0
  int free_s = 0;
93
0
  if (s == NULL) {
94
0
    free_s = 1;
95
0
    s = ASN1_UTCTIME_new();
96
0
    if (s == NULL) {
97
0
      return NULL;
98
0
    }
99
0
  }
100
101
0
  if (!ASN1_STRING_set(s, buf, strlen(buf))) {
102
0
    if (free_s) {
103
0
      ASN1_UTCTIME_free(s);
104
0
    }
105
0
    return NULL;
106
0
  }
107
0
  s->type = V_ASN1_GENERALIZEDTIME;
108
0
  return s;
109
0
}