summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_utctm.c
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:52 +0000
committermarkus <>2002-09-05 12:51:52 +0000
commit5514995a9d5ed91db089875adb509c7781357c0e (patch)
tree2484410a46ba6c05ef94c253da36fbceef990b64 /src/lib/libcrypto/asn1/a_utctm.c
parentfd9566423b542798f5c8b06e68101a9ea5bb9885 (diff)
downloadopenbsd-5514995a9d5ed91db089875adb509c7781357c0e.tar.gz
openbsd-5514995a9d5ed91db089875adb509c7781357c0e.tar.bz2
openbsd-5514995a9d5ed91db089875adb509c7781357c0e.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/asn1/a_utctm.c')
-rw-r--r--src/lib/libcrypto/asn1/a_utctm.c148
1 files changed, 116 insertions, 32 deletions
diff --git a/src/lib/libcrypto/asn1/a_utctm.c b/src/lib/libcrypto/asn1/a_utctm.c
index 17a7abbb67..ed2d827db2 100644
--- a/src/lib/libcrypto/asn1/a_utctm.c
+++ b/src/lib/libcrypto/asn1/a_utctm.c
@@ -59,25 +59,31 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <time.h> 60#include <time.h>
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include "asn1.h" 62#include "o_time.h"
63#include <openssl/asn1.h>
63 64
64/* ASN1err(ASN1_F_ASN1_UTCTIME_NEW,ASN1_R_UTCTIME_TOO_LONG); 65#if 0
65 * ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_EXPECTING_A_UTCTIME); 66int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a, unsigned char **pp)
66 */
67
68int i2d_ASN1_UTCTIME(a,pp)
69ASN1_UTCTIME *a;
70unsigned char **pp;
71 { 67 {
68#ifndef CHARSET_EBCDIC
72 return(i2d_ASN1_bytes((ASN1_STRING *)a,pp, 69 return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
73 V_ASN1_UTCTIME,V_ASN1_UNIVERSAL)); 70 V_ASN1_UTCTIME,V_ASN1_UNIVERSAL));
71#else
72 /* KLUDGE! We convert to ascii before writing DER */
73 int len;
74 char tmp[24];
75 ASN1_STRING x = *(ASN1_STRING *)a;
76
77 len = x.length;
78 ebcdic2ascii(tmp, x.data, (len >= sizeof tmp) ? sizeof tmp : len);
79 x.data = tmp;
80 return i2d_ASN1_bytes(&x, pp, V_ASN1_UTCTIME,V_ASN1_UNIVERSAL);
81#endif
74 } 82 }
75 83
76 84
77ASN1_UTCTIME *d2i_ASN1_UTCTIME(a, pp, length) 85ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **a, unsigned char **pp,
78ASN1_UTCTIME **a; 86 long length)
79unsigned char **pp;
80long length;
81 { 87 {
82 ASN1_UTCTIME *ret=NULL; 88 ASN1_UTCTIME *ret=NULL;
83 89
@@ -85,9 +91,12 @@ long length;
85 V_ASN1_UTCTIME,V_ASN1_UNIVERSAL); 91 V_ASN1_UTCTIME,V_ASN1_UNIVERSAL);
86 if (ret == NULL) 92 if (ret == NULL)
87 { 93 {
88 ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_ERROR_STACK); 94 ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ERR_R_NESTED_ASN1_ERROR);
89 return(NULL); 95 return(NULL);
90 } 96 }
97#ifdef CHARSET_EBCDIC
98 ascii2ebcdic(ret->data, ret->data, ret->length);
99#endif
91 if (!ASN1_UTCTIME_check(ret)) 100 if (!ASN1_UTCTIME_check(ret))
92 { 101 {
93 ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_INVALID_TIME_FORMAT); 102 ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_INVALID_TIME_FORMAT);
@@ -97,12 +106,13 @@ long length;
97 return(ret); 106 return(ret);
98err: 107err:
99 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 108 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
100 ASN1_UTCTIME_free(ret); 109 M_ASN1_UTCTIME_free(ret);
101 return(NULL); 110 return(NULL);
102 } 111 }
103 112
104int ASN1_UTCTIME_check(d) 113#endif
105ASN1_UTCTIME *d; 114
115int ASN1_UTCTIME_check(ASN1_UTCTIME *d)
106 { 116 {
107 static int min[8]={ 0, 1, 1, 0, 0, 0, 0, 0}; 117 static int min[8]={ 0, 1, 1, 0, 0, 0, 0, 0};
108 static int max[8]={99,12,31,23,59,59,12,59}; 118 static int max[8]={99,12,31,23,59,59,12,59};
@@ -152,9 +162,7 @@ err:
152 return(0); 162 return(0);
153 } 163 }
154 164
155int ASN1_UTCTIME_set_string(s,str) 165int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, char *str)
156ASN1_UTCTIME *s;
157char *str;
158 { 166 {
159 ASN1_UTCTIME t; 167 ASN1_UTCTIME t;
160 168
@@ -167,6 +175,7 @@ char *str;
167 { 175 {
168 ASN1_STRING_set((ASN1_STRING *)s, 176 ASN1_STRING_set((ASN1_STRING *)s,
169 (unsigned char *)str,t.length); 177 (unsigned char *)str,t.length);
178 s->type = V_ASN1_UTCTIME;
170 } 179 }
171 return(1); 180 return(1);
172 } 181 }
@@ -174,33 +183,28 @@ char *str;
174 return(0); 183 return(0);
175 } 184 }
176 185
177ASN1_UTCTIME *ASN1_UTCTIME_set(s, t) 186ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
178ASN1_UTCTIME *s;
179time_t t;
180 { 187 {
181 char *p; 188 char *p;
182 struct tm *ts; 189 struct tm *ts;
183#if defined(THREADS)
184 struct tm data; 190 struct tm data;
185#endif
186 191
187 if (s == NULL) 192 if (s == NULL)
188 s=ASN1_UTCTIME_new(); 193 s=M_ASN1_UTCTIME_new();
189 if (s == NULL) 194 if (s == NULL)
190 return(NULL); 195 return(NULL);
191 196
192#if defined(THREADS) 197 ts=OPENSSL_gmtime(&t, &data);
193 ts=(struct tm *)gmtime_r(&t,&data); 198 if (ts == NULL)
194#else 199 return(NULL);
195 ts=(struct tm *)gmtime(&t); 200
196#endif
197 p=(char *)s->data; 201 p=(char *)s->data;
198 if ((p == NULL) || (s->length < 14)) 202 if ((p == NULL) || (s->length < 14))
199 { 203 {
200 p=Malloc(20); 204 p=OPENSSL_malloc(20);
201 if (p == NULL) return(NULL); 205 if (p == NULL) return(NULL);
202 if (s->data != NULL) 206 if (s->data != NULL)
203 Free(s->data); 207 OPENSSL_free(s->data);
204 s->data=(unsigned char *)p; 208 s->data=(unsigned char *)p;
205 } 209 }
206 210
@@ -208,5 +212,85 @@ time_t t;
208 ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec); 212 ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec);
209 s->length=strlen(p); 213 s->length=strlen(p);
210 s->type=V_ASN1_UTCTIME; 214 s->type=V_ASN1_UTCTIME;
215#ifdef CHARSET_EBCDIC_not
216 ebcdic2ascii(s->data, s->data, s->length);
217#endif
211 return(s); 218 return(s);
212 } 219 }
220
221
222int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
223 {
224 struct tm *tm;
225 int offset;
226 int year;
227
228#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
229
230 if (s->data[12] == 'Z')
231 offset=0;
232 else
233 {
234 offset = g2(s->data+13)*60+g2(s->data+15);
235 if (s->data[12] == '-')
236 offset = -offset;
237 }
238
239 t -= offset*60; /* FIXME: may overflow in extreme cases */
240
241 { struct tm data; tm = OPENSSL_gmtime(&t, &data); }
242
243#define return_cmp(a,b) if ((a)<(b)) return -1; else if ((a)>(b)) return 1
244 year = g2(s->data);
245 if (year < 50)
246 year += 100;
247 return_cmp(year, tm->tm_year);
248 return_cmp(g2(s->data+2) - 1, tm->tm_mon);
249 return_cmp(g2(s->data+4), tm->tm_mday);
250 return_cmp(g2(s->data+6), tm->tm_hour);
251 return_cmp(g2(s->data+8), tm->tm_min);
252 return_cmp(g2(s->data+10), tm->tm_sec);
253#undef g2
254#undef return_cmp
255
256 return 0;
257 }
258
259
260#if 0
261time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s)
262 {
263 struct tm tm;
264 int offset;
265
266 memset(&tm,'\0',sizeof tm);
267
268#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
269 tm.tm_year=g2(s->data);
270 if(tm.tm_year < 50)
271 tm.tm_year+=100;
272 tm.tm_mon=g2(s->data+2)-1;
273 tm.tm_mday=g2(s->data+4);
274 tm.tm_hour=g2(s->data+6);
275 tm.tm_min=g2(s->data+8);
276 tm.tm_sec=g2(s->data+10);
277 if(s->data[12] == 'Z')
278 offset=0;
279 else
280 {
281 offset=g2(s->data+13)*60+g2(s->data+15);
282 if(s->data[12] == '-')
283 offset= -offset;
284 }
285#undef g2
286
287 return mktime(&tm)-offset*60; /* FIXME: mktime assumes the current timezone
288 * instead of UTC, and unless we rewrite OpenSSL
289 * in Lisp we cannot locally change the timezone
290 * without possibly interfering with other parts
291 * of the program. timegm, which uses UTC, is
292 * non-standard.
293 * Also time_t is inappropriate for general
294 * UTC times because it may a 32 bit type. */
295 }
296#endif