/src/openssl/crypto/asn1/a_utctm.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (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  |  | #include <stdio.h>  | 
11  |  | #include <time.h>  | 
12  |  | #include "internal/cryptlib.h"  | 
13  |  | #include <openssl/asn1.h>  | 
14  |  | #include "asn1_local.h"  | 
15  |  | #include <openssl/asn1t.h>  | 
16  |  |  | 
17  |  | IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_UTCTIME)  | 
18  |  |  | 
19  |  | /* This is the primary function used to parse ASN1_UTCTIME */  | 
20  |  | int ossl_asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)  | 
21  | 0  | { | 
22  |  |     /* wrapper around ossl_asn1_time_to_tm */  | 
23  | 0  |     if (d->type != V_ASN1_UTCTIME)  | 
24  | 0  |         return 0;  | 
25  | 0  |     return ossl_asn1_time_to_tm(tm, d);  | 
26  | 0  | }  | 
27  |  |  | 
28  |  | int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)  | 
29  | 0  | { | 
30  | 0  |     return ossl_asn1_utctime_to_tm(NULL, d);  | 
31  | 0  | }  | 
32  |  |  | 
33  |  | /* Sets the string via simple copy without cleaning it up */  | 
34  |  | int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)  | 
35  | 0  | { | 
36  | 0  |     ASN1_UTCTIME t;  | 
37  |  | 
  | 
38  | 0  |     t.type = V_ASN1_UTCTIME;  | 
39  | 0  |     t.length = strlen(str);  | 
40  | 0  |     t.data = (unsigned char *)str;  | 
41  | 0  |     t.flags = 0;  | 
42  |  | 
  | 
43  | 0  |     if (!ASN1_UTCTIME_check(&t))  | 
44  | 0  |         return 0;  | 
45  |  |  | 
46  | 0  |     if (s != NULL && !ASN1_STRING_copy(s, &t))  | 
47  | 0  |         return 0;  | 
48  |  |  | 
49  | 0  |     return 1;  | 
50  | 0  | }  | 
51  |  |  | 
52  |  | ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)  | 
53  | 0  | { | 
54  | 0  |     return ASN1_UTCTIME_adj(s, t, 0, 0);  | 
55  | 0  | }  | 
56  |  |  | 
57  |  | ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,  | 
58  |  |                                int offset_day, long offset_sec)  | 
59  | 0  | { | 
60  | 0  |     struct tm *ts;  | 
61  | 0  |     struct tm data;  | 
62  |  | 
  | 
63  | 0  |     ts = OPENSSL_gmtime(&t, &data);  | 
64  | 0  |     if (ts == NULL)  | 
65  | 0  |         return NULL;  | 
66  |  |  | 
67  | 0  |     if (offset_day || offset_sec) { | 
68  | 0  |         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))  | 
69  | 0  |             return NULL;  | 
70  | 0  |     }  | 
71  |  |  | 
72  | 0  |     return ossl_asn1_time_from_tm(s, ts, V_ASN1_UTCTIME);  | 
73  | 0  | }  | 
74  |  |  | 
75  |  | int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)  | 
76  | 0  | { | 
77  | 0  |     struct tm stm, ttm;  | 
78  | 0  |     int day, sec;  | 
79  |  | 
  | 
80  | 0  |     if (!ossl_asn1_utctime_to_tm(&stm, s))  | 
81  | 0  |         return -2;  | 
82  |  |  | 
83  | 0  |     if (OPENSSL_gmtime(&t, &ttm) == NULL)  | 
84  | 0  |         return -2;  | 
85  |  |  | 
86  | 0  |     if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))  | 
87  | 0  |         return -2;  | 
88  |  |  | 
89  | 0  |     if (day > 0 || sec > 0)  | 
90  | 0  |         return 1;  | 
91  | 0  |     if (day < 0 || sec < 0)  | 
92  | 0  |         return -1;  | 
93  | 0  |     return 0;  | 
94  | 0  | }  | 
95  |  |  | 
96  |  | int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)  | 
97  | 0  | { | 
98  | 0  |     if (tm->type != V_ASN1_UTCTIME)  | 
99  | 0  |         return 0;  | 
100  | 0  |     return ASN1_TIME_print(bp, tm);  | 
101  | 0  | }  |