summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/a_time.c')
-rw-r--r--src/lib/libcrypto/asn1/a_time.c79
1 files changed, 57 insertions, 22 deletions
diff --git a/src/lib/libcrypto/asn1/a_time.c b/src/lib/libcrypto/asn1/a_time.c
index c1690a5694..27ddd30899 100644
--- a/src/lib/libcrypto/asn1/a_time.c
+++ b/src/lib/libcrypto/asn1/a_time.c
@@ -64,8 +64,14 @@
64#include <stdio.h> 64#include <stdio.h>
65#include <time.h> 65#include <time.h>
66#include "cryptlib.h" 66#include "cryptlib.h"
67#include <openssl/asn1.h> 67#include "o_time.h"
68#include <openssl/asn1t.h>
68 69
70IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
71
72IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
73
74#if 0
69int i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **pp) 75int i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **pp)
70 { 76 {
71#ifdef CHARSET_EBCDIC 77#ifdef CHARSET_EBCDIC
@@ -89,35 +95,64 @@ int i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **pp)
89 ASN1err(ASN1_F_I2D_ASN1_TIME,ASN1_R_EXPECTING_A_TIME); 95 ASN1err(ASN1_F_I2D_ASN1_TIME,ASN1_R_EXPECTING_A_TIME);
90 return -1; 96 return -1;
91 } 97 }
92 98#endif
93
94ASN1_TIME *d2i_ASN1_TIME(ASN1_TIME **a, unsigned char **pp, long length)
95 {
96 unsigned char tag;
97 tag = **pp & ~V_ASN1_CONSTRUCTED;
98 if(tag == (V_ASN1_UTCTIME|V_ASN1_UNIVERSAL))
99 return d2i_ASN1_UTCTIME(a, pp, length);
100 if(tag == (V_ASN1_GENERALIZEDTIME|V_ASN1_UNIVERSAL))
101 return d2i_ASN1_GENERALIZEDTIME(a, pp, length);
102 ASN1err(ASN1_F_D2I_ASN1_TIME,ASN1_R_EXPECTING_A_TIME);
103 return(NULL);
104 }
105 99
106 100
107ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t) 101ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
108 { 102 {
109 struct tm *ts; 103 struct tm *ts;
110#if defined(THREADS) && !defined(WIN32)
111 struct tm data; 104 struct tm data;
112#endif
113 105
114#if defined(THREADS) && !defined(WIN32) 106 ts=OPENSSL_gmtime(&t,&data);
115 gmtime_r(&t,&data); 107 if (ts == NULL)
116 ts=&data; /* should return &data, but doesn't on some systems, so we don't even look at the return value */ 108 return NULL;
117#else
118 ts=gmtime(&t);
119#endif
120 if((ts->tm_year >= 50) && (ts->tm_year < 150)) 109 if((ts->tm_year >= 50) && (ts->tm_year < 150))
121 return ASN1_UTCTIME_set(s, t); 110 return ASN1_UTCTIME_set(s, t);
122 return ASN1_GENERALIZEDTIME_set(s,t); 111 return ASN1_GENERALIZEDTIME_set(s,t);
123 } 112 }
113
114int ASN1_TIME_check(ASN1_TIME *t)
115 {
116 if (t->type == V_ASN1_GENERALIZEDTIME)
117 return ASN1_GENERALIZEDTIME_check(t);
118 else if (t->type == V_ASN1_UTCTIME)
119 return ASN1_UTCTIME_check(t);
120 return 0;
121 }
122
123/* Convert an ASN1_TIME structure to GeneralizedTime */
124ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out)
125 {
126 ASN1_GENERALIZEDTIME *ret;
127 char *str;
128
129 if (!ASN1_TIME_check(t)) return NULL;
130
131 if (!out || !*out)
132 {
133 if (!(ret = ASN1_GENERALIZEDTIME_new ()))
134 return NULL;
135 if (out) *out = ret;
136 }
137 else ret = *out;
138
139 /* If already GeneralizedTime just copy across */
140 if (t->type == V_ASN1_GENERALIZEDTIME)
141 {
142 if(!ASN1_STRING_set(ret, t->data, t->length))
143 return NULL;
144 return ret;
145 }
146
147 /* grow the string */
148 if (!ASN1_STRING_set(ret, NULL, t->length + 2))
149 return NULL;
150 str = (char *)ret->data;
151 /* Work out the century and prepend */
152 if (t->data[0] >= '5') strcpy(str, "19");
153 else strcpy(str, "20");
154
155 strcat(str, (char *)t->data);
156
157 return ret;
158 }