Coverage Report

Created: 2026-08-28 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/asn1/a_gentm.cc
Line
Count
Source
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
28
using namespace bssl;
29
30
int bssl::asn1_generalizedtime_to_tm(struct tm *tm,
31
0
                                     const ASN1_GENERALIZEDTIME *d) {
32
0
  if (d->type != V_ASN1_GENERALIZEDTIME) {
33
0
    return 0;
34
0
  }
35
0
  CBS cbs;
36
0
  CBS_init(&cbs, d->data, (size_t)d->length);
37
0
  if (!CBS_parse_generalized_time(&cbs, tm, /*allow_timezone_offset=*/0)) {
38
0
    return 0;
39
0
  }
40
0
  return 1;
41
0
}
42
43
int bssl::asn1_parse_generalized_time(CBS *cbs, ASN1_GENERALIZEDTIME *out,
44
3.74k
                                      CBS_ASN1_TAG tag) {
45
3.74k
  tag = tag == 0 ? CBS_ASN1_GENERALIZEDTIME : tag;
46
3.74k
  CBS child;
47
3.74k
  if (!CBS_get_asn1(cbs, &child, tag) ||
48
2.96k
      !CBS_parse_generalized_time(&child, nullptr,
49
3.01k
                                  /*allow_timezone_offset=*/0)) {
50
3.01k
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
51
3.01k
    return 0;
52
3.01k
  }
53
733
  if (!ASN1_STRING_set(out, CBS_data(&child), CBS_len(&child))) {
54
0
    return 0;
55
0
  }
56
733
  out->type = V_ASN1_GENERALIZEDTIME;
57
733
  return 1;
58
733
}
59
60
0
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) {
61
0
  return asn1_generalizedtime_to_tm(nullptr, d);
62
0
}
63
64
0
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) {
65
0
  size_t len = strlen(str);
66
0
  CBS cbs;
67
0
  CBS_init(&cbs, (const uint8_t *)str, len);
68
0
  if (!CBS_parse_generalized_time(&cbs, /*out_tm=*/nullptr,
69
0
                                  /*allow_timezone_offset=*/0)) {
70
0
    return 0;
71
0
  }
72
0
  if (s != nullptr) {
73
0
    if (!ASN1_STRING_set(s, str, len)) {
74
0
      return 0;
75
0
    }
76
0
    s->type = V_ASN1_GENERALIZEDTIME;
77
0
  }
78
0
  return 1;
79
0
}
80
81
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
82
0
                                               int64_t posix_time) {
83
0
  return ASN1_GENERALIZEDTIME_adj(s, posix_time, 0, 0);
84
0
}
85
86
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
87
                                               int64_t posix_time,
88
                                               int offset_day,
89
0
                                               long offset_sec) {
90
0
  struct tm data;
91
0
  if (!OPENSSL_posix_to_tm(posix_time, &data)) {
92
0
    return nullptr;
93
0
  }
94
95
0
  if (offset_day || offset_sec) {
96
0
    if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
97
0
      return nullptr;
98
0
    }
99
0
  }
100
101
0
  if (data.tm_year < 0 - 1900 || data.tm_year > 9999 - 1900) {
102
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
103
0
    return nullptr;
104
0
  }
105
106
0
  char buf[16];
107
0
  int ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
108
0
                     data.tm_year + 1900, data.tm_mon + 1, data.tm_mday,
109
0
                     data.tm_hour, data.tm_min, data.tm_sec);
110
  // `snprintf` must write exactly 15 bytes (plus the NUL) to the buffer.
111
0
  BSSL_CHECK(ret == static_cast<int>(sizeof(buf) - 1));
112
113
0
  int free_s = 0;
114
0
  if (s == nullptr) {
115
0
    free_s = 1;
116
0
    s = ASN1_UTCTIME_new();
117
0
    if (s == nullptr) {
118
0
      return nullptr;
119
0
    }
120
0
  }
121
122
0
  if (!ASN1_STRING_set(s, buf, strlen(buf))) {
123
0
    if (free_s) {
124
0
      ASN1_UTCTIME_free(s);
125
0
    }
126
0
    return nullptr;
127
0
  }
128
0
  s->type = V_ASN1_GENERALIZEDTIME;
129
0
  return s;
130
0
}