/src/openssl111/crypto/asn1/a_gentm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* |
11 | | * GENERALIZEDTIME implementation. Based on UTCTIME |
12 | | */ |
13 | | |
14 | | #include <stdio.h> |
15 | | #include <time.h> |
16 | | #include "internal/cryptlib.h" |
17 | | #include <openssl/asn1.h> |
18 | | #include "asn1_local.h" |
19 | | |
20 | | /* This is the primary function used to parse ASN1_GENERALIZEDTIME */ |
21 | | int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d) |
22 | 6.82k | { |
23 | | /* wrapper around asn1_time_to_tm */ |
24 | 6.82k | if (d->type != V_ASN1_GENERALIZEDTIME) |
25 | 0 | return 0; |
26 | 6.82k | return asn1_time_to_tm(tm, d); |
27 | 6.82k | } |
28 | | |
29 | | int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) |
30 | 6.82k | { |
31 | 6.82k | return asn1_generalizedtime_to_tm(NULL, d); |
32 | 6.82k | } |
33 | | |
34 | | int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) |
35 | 6.82k | { |
36 | 6.82k | ASN1_GENERALIZEDTIME t; |
37 | | |
38 | 6.82k | t.type = V_ASN1_GENERALIZEDTIME; |
39 | 6.82k | t.length = strlen(str); |
40 | 6.82k | t.data = (unsigned char *)str; |
41 | 6.82k | t.flags = 0; |
42 | | |
43 | 6.82k | if (!ASN1_GENERALIZEDTIME_check(&t)) |
44 | 2.58k | return 0; |
45 | | |
46 | 4.23k | if (s != NULL && !ASN1_STRING_copy(s, &t)) |
47 | 0 | return 0; |
48 | | |
49 | 4.23k | return 1; |
50 | 4.23k | } |
51 | | |
52 | | ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, |
53 | | time_t t) |
54 | 0 | { |
55 | 0 | return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0); |
56 | 0 | } |
57 | | |
58 | | ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, |
59 | | time_t t, int offset_day, |
60 | | long offset_sec) |
61 | 6.82k | { |
62 | 6.82k | struct tm *ts; |
63 | 6.82k | struct tm data; |
64 | | |
65 | 6.82k | ts = OPENSSL_gmtime(&t, &data); |
66 | 6.82k | if (ts == NULL) |
67 | 0 | return NULL; |
68 | | |
69 | 6.82k | if (offset_day || offset_sec) { |
70 | 6.19k | if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) |
71 | 2.58k | return NULL; |
72 | 6.19k | } |
73 | | |
74 | 4.23k | return asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME); |
75 | 6.82k | } |
76 | | |
77 | | int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) |
78 | 4.23k | { |
79 | 4.23k | if (tm->type != V_ASN1_GENERALIZEDTIME) |
80 | 0 | return 0; |
81 | 4.23k | return ASN1_TIME_print(bp, tm); |
82 | 4.23k | } |