]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
60eba30f | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
365a2d99 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
f6aed2cd DSH |
8 | */ |
9 | ||
0f113f3e | 10 | /* |
b1322259 | 11 | * GENERALIZEDTIME implementation. Based on UTCTIME |
0f113f3e | 12 | */ |
f6aed2cd DSH |
13 | |
14 | #include <stdio.h> | |
15 | #include <time.h> | |
b39fc560 | 16 | #include "internal/cryptlib.h" |
ec577822 | 17 | #include <openssl/asn1.h> |
706457b7 | 18 | #include "asn1_local.h" |
f6aed2cd | 19 | |
cf37aaa3 | 20 | /* This is the primary function used to parse ASN1_GENERALIZEDTIME */ |
1c455bc0 | 21 | int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d) |
0f113f3e | 22 | { |
3d0f1cb9 | 23 | /* wrapper around asn1_time_to_tm */ |
0f113f3e | 24 | if (d->type != V_ASN1_GENERALIZEDTIME) |
3d0f1cb9 PY |
25 | return 0; |
26 | return asn1_time_to_tm(tm, d); | |
52b6e17d | 27 | } |
f6aed2cd | 28 | |
1c455bc0 | 29 | int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) |
0f113f3e MC |
30 | { |
31 | return asn1_generalizedtime_to_tm(NULL, d); | |
32 | } | |
1c455bc0 | 33 | |
875a644a | 34 | int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) |
0f113f3e MC |
35 | { |
36 | ASN1_GENERALIZEDTIME t; | |
f6aed2cd | 37 | |
0f113f3e MC |
38 | t.type = V_ASN1_GENERALIZEDTIME; |
39 | t.length = strlen(str); | |
40 | t.data = (unsigned char *)str; | |
04e62715 RS |
41 | t.flags = 0; |
42 | ||
cf37aaa3 TS |
43 | if (!ASN1_GENERALIZEDTIME_check(&t)) |
44 | return 0; | |
45 | ||
46 | if (s != NULL && !ASN1_STRING_copy(s, &t)) | |
47 | return 0; | |
48 | ||
49 | return 1; | |
0f113f3e | 50 | } |
f6aed2cd | 51 | |
6b691a5c | 52 | ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, |
0f113f3e MC |
53 | time_t t) |
54 | { | |
55 | return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0); | |
56 | } | |
87d3a0cd DSH |
57 | |
58 | ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, | |
0f113f3e MC |
59 | time_t t, int offset_day, |
60 | long offset_sec) | |
61 | { | |
0f113f3e MC |
62 | struct tm *ts; |
63 | struct tm data; | |
f6aed2cd | 64 | |
0f113f3e MC |
65 | ts = OPENSSL_gmtime(&t, &data); |
66 | if (ts == NULL) | |
cf37aaa3 | 67 | return NULL; |
b8e35bd6 | 68 | |
0f113f3e MC |
69 | if (offset_day || offset_sec) { |
70 | if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) | |
cf37aaa3 | 71 | return NULL; |
0f113f3e | 72 | } |
f6aed2cd | 73 | |
cf37aaa3 | 74 | return asn1_time_from_tm(s, ts, V_ASN1_GENERALIZEDTIME); |
0f113f3e | 75 | } |
0d0099ea | 76 | |
0d0099ea DSH |
77 | int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) |
78 | { | |
f673c4f8 PY |
79 | if (tm->type != V_ASN1_GENERALIZEDTIME) |
80 | return 0; | |
81 | return ASN1_TIME_print(bp, tm); | |
0d0099ea | 82 | } |