a_gentm.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/asn1.h>
58 
59 #include <string.h>
60 #include <time.h>
61 
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 
65 #include "internal.h"
66 
68 {
69  static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
70  static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
71  char *a;
72  int n, i, l, o;
73 
74  if (d->type != V_ASN1_GENERALIZEDTIME)
75  return (0);
76  l = d->length;
77  a = (char *)d->data;
78  o = 0;
79  /*
80  * GENERALIZEDTIME is similar to UTCTIME except the year is represented
81  * as YYYY. This stuff treats everything as a two digit field so make
82  * first two fields 00 to 99
83  */
84  if (l < 13)
85  goto err;
86  for (i = 0; i < 7; i++) {
87  if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
88  i++;
89  if (tm)
90  tm->tm_sec = 0;
91  break;
92  }
93  if ((a[o] < '0') || (a[o] > '9'))
94  goto err;
95  n = a[o] - '0';
96  if (++o > l)
97  goto err;
98 
99  if ((a[o] < '0') || (a[o] > '9'))
100  goto err;
101  n = (n * 10) + a[o] - '0';
102  if (++o > l)
103  goto err;
104 
105  if ((n < min[i]) || (n > max[i]))
106  goto err;
107  if (tm) {
108  switch (i) {
109  case 0:
110  tm->tm_year = n * 100 - 1900;
111  break;
112  case 1:
113  tm->tm_year += n;
114  break;
115  case 2:
116  tm->tm_mon = n - 1;
117  break;
118  case 3:
119  tm->tm_mday = n;
120  break;
121  case 4:
122  tm->tm_hour = n;
123  break;
124  case 5:
125  tm->tm_min = n;
126  break;
127  case 6:
128  tm->tm_sec = n;
129  break;
130  }
131  }
132  }
133  /*
134  * Optional fractional seconds: decimal point followed by one or more
135  * digits.
136  */
137  if (a[o] == '.') {
138  if (++o > l)
139  goto err;
140  i = o;
141  while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
142  o++;
143  /* Must have at least one digit after decimal point */
144  if (i == o)
145  goto err;
146  }
147 
148  if (a[o] == 'Z')
149  o++;
150  else if ((a[o] == '+') || (a[o] == '-')) {
151  int offsign = a[o] == '-' ? 1 : -1, offset = 0;
152  o++;
153  if (o + 4 > l)
154  goto err;
155  for (i = 7; i < 9; i++) {
156  if ((a[o] < '0') || (a[o] > '9'))
157  goto err;
158  n = a[o] - '0';
159  o++;
160  if ((a[o] < '0') || (a[o] > '9'))
161  goto err;
162  n = (n * 10) + a[o] - '0';
163  if ((n < min[i]) || (n > max[i]))
164  goto err;
165  if (tm) {
166  if (i == 7)
167  offset = n * 3600;
168  else if (i == 8)
169  offset += n * 60;
170  }
171  o++;
172  }
173  if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
174  return 0;
175  } else if (a[o]) {
176  /* Missing time zone information. */
177  goto err;
178  }
179  return (o == l);
180  err:
181  return (0);
182 }
183 
185 {
186  return asn1_generalizedtime_to_tm(NULL, d);
187 }
188 
190 {
192 
193  t.type = V_ASN1_GENERALIZEDTIME;
194  t.length = strlen(str);
195  t.data = (unsigned char *)str;
196  if (ASN1_GENERALIZEDTIME_check(&t)) {
197  if (s != NULL) {
198  if (!ASN1_STRING_set((ASN1_STRING *)s,
199  (unsigned char *)str, t.length))
200  return 0;
201  s->type = V_ASN1_GENERALIZEDTIME;
202  }
203  return (1);
204  } else
205  return (0);
206 }
207 
209  time_t t)
210 {
211  return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
212 }
213 
215  time_t t, int offset_day,
216  long offset_sec)
217 {
218  char *p;
219  struct tm *ts;
220  struct tm data;
221  size_t len = 20;
222  ASN1_GENERALIZEDTIME *tmps = NULL;
223 
224  if (s == NULL)
225  tmps = ASN1_GENERALIZEDTIME_new();
226  else
227  tmps = s;
228  if (tmps == NULL)
229  return NULL;
230 
231  ts = OPENSSL_gmtime(&t, &data);
232  if (ts == NULL)
233  goto err;
234 
235  if (offset_day || offset_sec) {
236  if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
237  goto err;
238  }
239 
240  if (ts->tm_year < 0 - 1900 || ts->tm_year > 9999 - 1900) {
242  goto err;
243  }
244 
245  p = (char *)tmps->data;
246  if ((p == NULL) || ((size_t)tmps->length < len)) {
247  p = OPENSSL_malloc(len);
248  if (p == NULL) {
250  goto err;
251  }
252  OPENSSL_free(tmps->data);
253  tmps->data = (unsigned char *)p;
254  }
255 
256  BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
257  ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
258  ts->tm_sec);
259  tmps->length = strlen(p);
261  return tmps;
262  err:
263  if (s == NULL)
265  return NULL;
266 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
ASN1_GENERALIZEDTIME_new
OPENSSL_EXPORT ASN1_GENERALIZEDTIME * ASN1_GENERALIZEDTIME_new(void)
error_ref_leak.err
err
Definition: error_ref_leak.py:35
ASN1_GENERALIZEDTIME_free
OPENSSL_EXPORT void ASN1_GENERALIZEDTIME_free(ASN1_GENERALIZEDTIME *str)
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
xds_manager.p
p
Definition: xds_manager.py:60
asn1_string_st::data
unsigned char * data
Definition: asn1.h:546
V_ASN1_GENERALIZEDTIME
#define V_ASN1_GENERALIZEDTIME
Definition: asn1.h:145
asn1_generalizedtime_to_tm
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
Definition: a_gentm.c:67
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
OPENSSL_gmtime_adj
#define OPENSSL_gmtime_adj
Definition: boringssl_prefix_symbols.h:1871
ASN1_STRING_set
#define ASN1_STRING_set
Definition: boringssl_prefix_symbols.h:688
internal.h
asn1_string_st::length
int length
Definition: asn1.h:544
ASN1_GENERALIZEDTIME_set
ASN1_GENERALIZEDTIME * ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, time_t t)
Definition: a_gentm.c:208
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
ASN1_R_ILLEGAL_TIME_VALUE
#define ASN1_R_ILLEGAL_TIME_VALUE
Definition: asn1.h:1981
err.h
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
min
#define min(a, b)
Definition: qsort.h:83
d
static const fe d
Definition: curve25519_tables.h:19
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
BIO_snprintf
#define BIO_snprintf
Definition: boringssl_prefix_symbols.h:864
tm
static uv_timer_t tm
Definition: test-tcp-open.c:41
asn1_string_st::type
int type
Definition: asn1.h:545
ASN1_GENERALIZEDTIME_adj
ASN1_GENERALIZEDTIME * ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, time_t t, int offset_day, long offset_sec)
Definition: a_gentm.c:214
ASN1_GENERALIZEDTIME_check
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
Definition: a_gentm.c:184
mem.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
run_grpclb_interop_tests.l
dictionary l
Definition: run_grpclb_interop_tests.py:410
ASN1_GENERALIZEDTIME_set_string
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
Definition: a_gentm.c:189
OPENSSL_gmtime
#define OPENSSL_gmtime
Definition: boringssl_prefix_symbols.h:1870
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
if
if(p->owned &&p->wrapped !=NULL)
Definition: call.c:42
asn1_string_st
Definition: asn1.h:543
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:39