Coverage Report

Created: 2026-08-14 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/asn1/a_utctm.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_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
31
0
                             int allow_timezone_offset) {
32
0
  if (d->type != V_ASN1_UTCTIME) {
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_utc_time(&cbs, tm, allow_timezone_offset)) {
38
0
    return 0;
39
0
  }
40
0
  return 1;
41
0
}
42
43
int bssl::asn1_parse_utc_time(CBS *cbs, ASN1_UTCTIME *out, CBS_ASN1_TAG tag,
44
353k
                              int allow_timezone_offset) {
45
353k
  tag = tag == 0 ? CBS_ASN1_UTCTIME : tag;
46
353k
  CBS child;
47
353k
  if (!CBS_get_asn1(cbs, &child, tag) ||
48
353k
      !CBS_parse_utc_time(&child, nullptr, allow_timezone_offset)) {
49
1.35k
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
50
1.35k
    return 0;
51
1.35k
  }
52
351k
  if (!ASN1_STRING_set(out, CBS_data(&child), CBS_len(&child))) {
53
0
    return 0;
54
0
  }
55
351k
  out->type = V_ASN1_UTCTIME;
56
351k
  return 1;
57
351k
}
58
59
0
int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) {
60
0
  return asn1_utctime_to_tm(nullptr, d, /*allow_timezone_offset=*/1);
61
0
}
62
63
0
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) {
64
  // Although elsewhere we allow timezone offsets with UTCTime, to be compatible
65
  // with some existing misissued certificates, this function is used to
66
  // construct new certificates and can be stricter.
67
0
  size_t len = strlen(str);
68
0
  CBS cbs;
69
0
  CBS_init(&cbs, (const uint8_t *)str, len);
70
0
  if (!CBS_parse_utc_time(&cbs, /*out_tm=*/nullptr,
71
0
                          /*allow_timezone_offset=*/0)) {
72
0
    return 0;
73
0
  }
74
0
  if (s != nullptr) {
75
0
    if (!ASN1_STRING_set(s, str, len)) {
76
0
      return 0;
77
0
    }
78
0
    s->type = V_ASN1_UTCTIME;
79
0
  }
80
0
  return 1;
81
0
}
82
83
0
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time) {
84
0
  return ASN1_UTCTIME_adj(s, posix_time, 0, 0);
85
0
}
86
87
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time,
88
0
                               int offset_day, long offset_sec) {
89
0
  struct tm data;
90
0
  if (!OPENSSL_posix_to_tm(posix_time, &data)) {
91
0
    return nullptr;
92
0
  }
93
94
0
  if (offset_day || offset_sec) {
95
0
    if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
96
0
      return nullptr;
97
0
    }
98
0
  }
99
100
0
  if (data.tm_year < 50 || data.tm_year >= 150) {
101
0
    return nullptr;
102
0
  }
103
104
0
  char buf[14];
105
0
  int ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
106
0
                     data.tm_year % 100, data.tm_mon + 1, data.tm_mday,
107
0
                     data.tm_hour, data.tm_min, data.tm_sec);
108
  // `snprintf` must write exactly 15 bytes (plus the NUL) to the buffer.
109
0
  BSSL_CHECK(ret == static_cast<int>(sizeof(buf) - 1));
110
111
0
  int free_s = 0;
112
0
  if (s == nullptr) {
113
0
    free_s = 1;
114
0
    s = ASN1_UTCTIME_new();
115
0
    if (s == nullptr) {
116
0
      return nullptr;
117
0
    }
118
0
  }
119
120
0
  if (!ASN1_STRING_set(s, buf, strlen(buf))) {
121
0
    if (free_s) {
122
0
      ASN1_UTCTIME_free(s);
123
0
    }
124
0
    return nullptr;
125
0
  }
126
0
  s->type = V_ASN1_UTCTIME;
127
0
  return s;
128
0
}