Coverage Report

Created: 2018-08-29 13:53

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