Add missing include of cryptlib.h
[openssl.git] / crypto / asn1 / a_gentm.c
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 <ctype.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1.h>
19 #include "asn1_locl.h"
20
21 /* This is the primary function used to parse ASN1_GENERALIZEDTIME */
22 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
23 {
24     /* wrapper around asn1_time_to_tm */
25     if (d->type != V_ASN1_GENERALIZEDTIME)
26         return 0;
27     return asn1_time_to_tm(tm, d);
28 }
29
30 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
31 {
32     return asn1_generalizedtime_to_tm(NULL, d);
33 }
34
35 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
36 {
37     ASN1_GENERALIZEDTIME t;
38
39     t.type = V_ASN1_GENERALIZEDTIME;
40     t.length = strlen(str);
41     t.data = (unsigned char *)str;
42     t.flags = 0;
43
44     if (!ASN1_GENERALIZEDTIME_check(&t))
45         return 0;
46
47     if (s != NULL && !ASN1_STRING_copy(s, &t))
48         return 0;
49
50     return 1;
51 }
52
53 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
54                                                time_t t)
55 {
56     return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
57 }
58
59 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
60                                                time_t t, int offset_day,
61                                                long offset_sec)
62 {
63     struct tm *ts;
64     struct tm data;
65
66     ts = OPENSSL_gmtime(&t, &data);
67     if (ts == NULL)
68         return NULL;
69
70     if (offset_day || offset_sec) {
71         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
72             return NULL;
73     }
74
75     return asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME);
76 }
77
78 int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
79 {
80     if (tm->type != V_ASN1_GENERALIZEDTIME)
81         return 0;
82     return ASN1_TIME_print(bp, tm);
83 }