a_gentm.c 3.1 KB
Newer Older
R
Rich Salz 已提交
1
/*
P
Pauli 已提交
2
 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9
 */

10
/*
R
Rich Salz 已提交
11
 * GENERALIZEDTIME implementation. Based on UTCTIME
12
 */
13 14 15

#include <stdio.h>
#include <time.h>
16
#include "internal/cryptlib.h"
17
#include <openssl/asn1.h>
18
#include "asn1_locl.h"
19

20
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
21
{
22
    /* wrapper around asn1_time_to_tm */
23
    if (d->type != V_ASN1_GENERALIZEDTIME)
24 25
        return 0;
    return asn1_time_to_tm(tm, d);
26
}
27

28
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
29 30 31
{
    return asn1_generalizedtime_to_tm(NULL, d);
}
32

33
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
34 35
{
    ASN1_GENERALIZEDTIME t;
36

37 38 39
    t.type = V_ASN1_GENERALIZEDTIME;
    t.length = strlen(str);
    t.data = (unsigned char *)str;
40 41
    t.flags = 0;

42 43
    if (ASN1_GENERALIZEDTIME_check(&t)) {
        if (s != NULL) {
F
FdaSilvaYY 已提交
44
            if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
45 46 47
                return 0;
            s->type = V_ASN1_GENERALIZEDTIME;
        }
48 49 50
        return 1;
    }
    return 0;
51
}
52

U
Ulf Möller 已提交
53
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
54 55 56 57
                                               time_t t)
{
    return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
}
58 59

ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
60 61 62 63 64 65
                                               time_t t, int offset_day,
                                               long offset_sec)
{
    char *p;
    struct tm *ts;
    struct tm data;
P
Pauli 已提交
66
    const size_t len = 20;
67
    ASN1_GENERALIZEDTIME *tmps = NULL;
68

69
    if (s == NULL)
70 71 72 73 74
        tmps = ASN1_GENERALIZEDTIME_new();
    else
        tmps = s;
    if (tmps == NULL)
        return NULL;
75

76 77
    ts = OPENSSL_gmtime(&t, &data);
    if (ts == NULL)
78
        goto err;
79

80 81
    if (offset_day || offset_sec) {
        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
82
            goto err;
83
    }
84

85 86
    p = (char *)tmps->data;
    if ((p == NULL) || ((size_t)tmps->length < len)) {
87 88 89
        p = OPENSSL_malloc(len);
        if (p == NULL) {
            ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
90
            goto err;
91
        }
92 93
        OPENSSL_free(tmps->data);
        tmps->data = (unsigned char *)p;
94
    }
95

P
Pauli 已提交
96 97 98 99
    tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
                                ts->tm_year + 1900, ts->tm_mon + 1,
                                ts->tm_mday, ts->tm_hour, ts->tm_min,
                                ts->tm_sec);
100
    tmps->type = V_ASN1_GENERALIZEDTIME;
101
#ifdef CHARSET_EBCDIC_not
102
    ebcdic2ascii(tmps->data, tmps->data, tmps->length);
103
#endif
104 105 106 107 108
    return tmps;
 err:
    if (s == NULL)
        ASN1_GENERALIZEDTIME_free(tmps);
    return NULL;
109
}
D
Dr. Stephen Henson 已提交
110 111 112

int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
{
P
Paul Yang 已提交
113 114 115
    if (tm->type != V_ASN1_GENERALIZEDTIME)
        return 0;
    return ASN1_TIME_print(bp, tm);
D
Dr. Stephen Henson 已提交
116
}