summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_utctm.c
diff options
context:
space:
mode:
authorjsing <>2014-04-19 13:29:11 +0000
committerjsing <>2014-04-19 13:29:11 +0000
commit7e0c192deac57ad25c3b253ccb32dfe88cfef27f (patch)
tree03243ed125b0122180311a953d14f9126e96cdf5 /src/lib/libcrypto/asn1/a_utctm.c
parent4208104cba46786606cd522b3b0f9b2d5a6a65a2 (diff)
downloadopenbsd-7e0c192deac57ad25c3b253ccb32dfe88cfef27f.tar.gz
openbsd-7e0c192deac57ad25c3b253ccb32dfe88cfef27f.tar.bz2
openbsd-7e0c192deac57ad25c3b253ccb32dfe88cfef27f.zip
More KNF.
Diffstat (limited to 'src/lib/libcrypto/asn1/a_utctm.c')
-rw-r--r--src/lib/libcrypto/asn1/a_utctm.c218
1 files changed, 116 insertions, 102 deletions
diff --git a/src/lib/libcrypto/asn1/a_utctm.c b/src/lib/libcrypto/asn1/a_utctm.c
index 0120b952b3..0cd0382fe9 100644
--- a/src/lib/libcrypto/asn1/a_utctm.c
+++ b/src/lib/libcrypto/asn1/a_utctm.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -63,113 +63,127 @@
63#include <openssl/asn1.h> 63#include <openssl/asn1.h>
64 64
65#if 0 65#if 0
66int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a, unsigned char **pp) 66int
67i2d_ASN1_UTCTIME(ASN1_UTCTIME *a, unsigned char **pp)
67{ 68{
68 return(i2d_ASN1_bytes((ASN1_STRING *)a,pp, 69 return(i2d_ASN1_bytes((ASN1_STRING *)a, pp,
69 V_ASN1_UTCTIME,V_ASN1_UNIVERSAL)); 70 V_ASN1_UTCTIME, V_ASN1_UNIVERSAL));
70} 71}
71 72
72 73ASN1_UTCTIME *
73ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **a, unsigned char **pp, 74d2i_ASN1_UTCTIME(ASN1_UTCTIME **a, unsigned char **pp, long length)
74 long length)
75{ 75{
76 ASN1_UTCTIME *ret=NULL; 76 ASN1_UTCTIME *ret = NULL;
77 77
78 ret=(ASN1_UTCTIME *)d2i_ASN1_bytes((ASN1_STRING **)a,pp,length, 78 ret = (ASN1_UTCTIME *)d2i_ASN1_bytes((ASN1_STRING **)a, pp, length,
79 V_ASN1_UTCTIME,V_ASN1_UNIVERSAL); 79 V_ASN1_UTCTIME, V_ASN1_UNIVERSAL);
80 if (ret == NULL) { 80 if (ret == NULL) {
81 ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ERR_R_NESTED_ASN1_ERROR); 81 ASN1err(ASN1_F_D2I_ASN1_UTCTIME, ERR_R_NESTED_ASN1_ERROR);
82 return(NULL); 82 return (NULL);
83 } 83 }
84 if (!ASN1_UTCTIME_check(ret)) { 84 if (!ASN1_UTCTIME_check(ret)) {
85 ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_INVALID_TIME_FORMAT); 85 ASN1err(ASN1_F_D2I_ASN1_UTCTIME, ASN1_R_INVALID_TIME_FORMAT);
86 goto err; 86 goto err;
87 } 87 }
88 return (ret);
88 89
89 return(ret);
90err: 90err:
91 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 91 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
92 M_ASN1_UTCTIME_free(ret); 92 M_ASN1_UTCTIME_free(ret);
93 return(NULL); 93 return (NULL);
94} 94}
95 95
96#endif 96#endif
97 97
98int ASN1_UTCTIME_check(ASN1_UTCTIME *d) 98int
99ASN1_UTCTIME_check(ASN1_UTCTIME *d)
99{ 100{
100 static const int min[8]={ 0, 1, 1, 0, 0, 0, 0, 0}; 101 static const int min[8] = {0, 1, 1, 0, 0, 0, 0, 0};
101 static const int max[8]={99,12,31,23,59,59,12,59}; 102 static const int max[8] = {99, 12, 31, 23, 59, 59, 12, 59};
102 char *a; 103 char *a;
103 int n,i,l,o; 104 int n, i, l, o;
105
106 if (d->type != V_ASN1_UTCTIME)
107 return (0);
108 l = d->length;
109 a = (char *)d->data;
110 o = 0;
104 111
105 if (d->type != V_ASN1_UTCTIME) return(0); 112 if (l < 11)
106 l=d->length;
107 a=(char *)d->data;
108 o=0;
109 113
110 if (l < 11) goto err; 114 goto err;
111 for (i=0; i<6; i++) { 115 for (i = 0; i < 6; i++) {
112 if ((i == 5) && ((a[o] == 'Z') || 116 if ((i == 5) && ((a[o] == 'Z') ||
113 (a[o] == '+') || (a[o] == '-'))) { 117 (a[o] == '+') || (a[o] == '-'))) {
114 i++; 118 i++;
115 break; 119 break;
116 } 120 }
117 if ((a[o] < '0') || (a[o] > '9')) goto err; 121 if ((a[o] < '0') || (a[o] > '9'))
118 n= a[o]-'0'; 122 goto err;
119 if (++o > l) goto err; 123 n = a[o]-'0';
120 124 if (++o > l)
121 if ((a[o] < '0') || (a[o] > '9')) goto err; 125 goto err;
122 n=(n*10)+ a[o]-'0'; 126 if ((a[o] < '0') || (a[o] > '9'))
123 if (++o > l) goto err; 127 goto err;
124 128 n = (n * 10) + a[o] - '0';
125 if ((n < min[i]) || (n > max[i])) goto err; 129 if (++o > l)
130 goto err;
131 if ((n < min[i]) || (n > max[i]))
132 goto err;
126 } 133 }
127 if (a[o] == 'Z') 134 if (a[o] == 'Z')
128 o++; 135 o++;
129 else if ((a[o] == '+') || (a[o] == '-')) { 136 else if ((a[o] == '+') || (a[o] == '-')) {
130 o++; 137 o++;
131 if (o+4 > l) goto err; 138 if (o + 4 > l)
132 for (i=6; i<8; i++) { 139 goto err;
133 if ((a[o] < '0') || (a[o] > '9')) goto err; 140 for (i = 6; i < 8; i++) {
134 n= a[o]-'0'; 141 if ((a[o] < '0') || (a[o] > '9'))
142 goto err;
143 n = a[o] -'0';
135 o++; 144 o++;
136 if ((a[o] < '0') || (a[o] > '9')) goto err; 145 if ((a[o] < '0') || (a[o] > '9'))
137 n=(n*10)+ a[o]-'0'; 146 goto err;
138 if ((n < min[i]) || (n > max[i])) goto err; 147 n = (n * 10) + a[o] - '0';
148 if ((n < min[i]) || (n > max[i]))
149 goto err;
139 o++; 150 o++;
140 } 151 }
141 } 152 }
142 return(o == l); 153 return (o == l);
154
143err: 155err:
144 return(0); 156 return (0);
145} 157}
146 158
147int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) 159int
160ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
148{ 161{
149 ASN1_UTCTIME t; 162 ASN1_UTCTIME t;
150 163
151 t.type=V_ASN1_UTCTIME; 164 t.type = V_ASN1_UTCTIME;
152 t.length=strlen(str); 165 t.length = strlen(str);
153 t.data=(unsigned char *)str; 166 t.data = (unsigned char *)str;
154 if (ASN1_UTCTIME_check(&t)) { 167 if (ASN1_UTCTIME_check(&t)) {
155 if (s != NULL) { 168 if (s != NULL) {
156 if (!ASN1_STRING_set((ASN1_STRING *)s, 169 if (!ASN1_STRING_set((ASN1_STRING *)s,
157 (unsigned char *)str,t.length)) 170 (unsigned char *)str, t.length))
158 return 0; 171 return 0;
159 s->type = V_ASN1_UTCTIME; 172 s->type = V_ASN1_UTCTIME;
160 } 173 }
161 return(1); 174 return (1);
162 } else 175 } else
163 return(0); 176 return (0);
164} 177}
165 178
166ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) 179ASN1_UTCTIME *
180ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
167{ 181{
168 return ASN1_UTCTIME_adj(s, t, 0, 0); 182 return ASN1_UTCTIME_adj(s, t, 0, 0);
169} 183}
170 184
171ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, 185ASN1_UTCTIME *
172 int offset_day, long offset_sec) 186ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, int offset_day, long offset_sec)
173{ 187{
174 char *p; 188 char *p;
175 struct tm *ts; 189 struct tm *ts;
@@ -177,43 +191,43 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
177 size_t len = 20; 191 size_t len = 20;
178 192
179 if (s == NULL) 193 if (s == NULL)
180 s=M_ASN1_UTCTIME_new(); 194 s = M_ASN1_UTCTIME_new();
181 if (s == NULL) 195 if (s == NULL)
182 return(NULL); 196 return (NULL);
183 197
184 ts=gmtime_r(&t, &data); 198 ts = gmtime_r(&t, &data);
185 if (ts == NULL) 199 if (ts == NULL)
186 return(NULL); 200 return (NULL);
187 201
188 if (offset_day || offset_sec) { 202 if (offset_day || offset_sec) {
189 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) 203 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
190 return NULL; 204 return NULL;
191 } 205 }
192 206
193 if((ts->tm_year < 50) || (ts->tm_year >= 150)) 207 if ((ts->tm_year < 50) || (ts->tm_year >= 150))
194 return NULL; 208 return NULL;
195 209
196 p=(char *)s->data; 210 p = (char *)s->data;
197 if ((p == NULL) || ((size_t)s->length < len)) { 211 if ((p == NULL) || ((size_t)s->length < len)) {
198 p=malloc(len); 212 p = malloc(len);
199 if (p == NULL) { 213 if (p == NULL) {
200 ASN1err(ASN1_F_ASN1_UTCTIME_ADJ,ERR_R_MALLOC_FAILURE); 214 ASN1err(ASN1_F_ASN1_UTCTIME_ADJ, ERR_R_MALLOC_FAILURE);
201 return(NULL); 215 return (NULL);
202 } 216 }
203 if (s->data != NULL) 217 if (s->data != NULL)
204 free(s->data); 218 free(s->data);
205 s->data=(unsigned char *)p; 219 s->data = (unsigned char *)p;
206 } 220 }
207 221
208 snprintf(p,len,"%02d%02d%02d%02d%02d%02dZ",ts->tm_year%100, 222 snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", ts->tm_year % 100,
209 ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec); 223 ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
210 s->length=strlen(p); 224 s->length = strlen(p);
211 s->type=V_ASN1_UTCTIME; 225 s->type = V_ASN1_UTCTIME;
212 return(s); 226 return (s);
213} 227}
214 228
215 229int
216int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) 230ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
217{ 231{
218 struct tm *tm; 232 struct tm *tm;
219 struct tm data; 233 struct tm data;
@@ -223,57 +237,57 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
223#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0') 237#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
224 238
225 if (s->data[12] == 'Z') 239 if (s->data[12] == 'Z')
226 offset=0; 240 offset = 0;
227 else { 241 else {
228 offset = g2(s->data+13)*60+g2(s->data+15); 242 offset = g2(s->data + 13)*60 + g2(s->data + 15);
229 if (s->data[12] == '-') 243 if (s->data[12] == '-')
230 offset = -offset; 244 offset = -offset;
231 } 245 }
232 246
233 t -= offset*60; /* FIXME: may overflow in extreme cases */ 247 t -= offset * 60; /* FIXME: may overflow in extreme cases */
234 248
235 tm = gmtime_r(&t, &data); 249 tm = gmtime_r(&t, &data);
236 250
237#define return_cmp(a,b) if ((a)<(b)) return -1; else if ((a)>(b)) return 1 251#define return_cmp(a,b) if ((a)<(b)) return -1; else if ((a)>(b)) return 1
238 year = g2(s->data); 252 year = g2(s->data);
239 if (year < 50) 253 if (year < 50)
240 year += 100; 254 year += 100;
241 return_cmp(year, tm->tm_year); 255 return_cmp(year, tm->tm_year);
242 return_cmp(g2(s->data+2) - 1, tm->tm_mon); 256 return_cmp(g2(s->data + 2) - 1, tm->tm_mon);
243 return_cmp(g2(s->data+4), tm->tm_mday); 257 return_cmp(g2(s->data + 4), tm->tm_mday);
244 return_cmp(g2(s->data+6), tm->tm_hour); 258 return_cmp(g2(s->data + 6), tm->tm_hour);
245 return_cmp(g2(s->data+8), tm->tm_min); 259 return_cmp(g2(s->data + 8), tm->tm_min);
246 return_cmp(g2(s->data+10), tm->tm_sec); 260 return_cmp(g2(s->data + 10), tm->tm_sec);
247#undef g2 261#undef g2
248#undef return_cmp 262#undef return_cmp
249 263
250 return 0; 264 return 0;
251} 265}
252 266
253
254#if 0 267#if 0
255time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s) 268time_t
269ASN1_UTCTIME_get(const ASN1_UTCTIME *s)
256{ 270{
257 struct tm tm; 271 struct tm tm;
258 int offset; 272 int offset;
259 273
260 memset(&tm,'\0',sizeof tm); 274 memset(&tm, '\0', sizeof tm);
261 275
262#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0') 276#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
263 tm.tm_year=g2(s->data); 277 tm.tm_year = g2(s->data);
264 if(tm.tm_year < 50) 278 if (tm.tm_year < 50)
265 tm.tm_year+=100; 279 tm.tm_year += 100;
266 tm.tm_mon=g2(s->data+2)-1; 280 tm.tm_mon = g2(s->data + 2) - 1;
267 tm.tm_mday=g2(s->data+4); 281 tm.tm_mday = g2(s->data + 4);
268 tm.tm_hour=g2(s->data+6); 282 tm.tm_hour = g2(s->data + 6);
269 tm.tm_min=g2(s->data+8); 283 tm.tm_min = g2(s->data + 8);
270 tm.tm_sec=g2(s->data+10); 284 tm.tm_sec = g2(s->data + 10);
271 if(s->data[12] == 'Z') 285 if (s->data[12] == 'Z')
272 offset=0; 286 offset = 0;
273 else { 287 else {
274 offset=g2(s->data+13)*60+g2(s->data+15); 288 offset = g2(s->data + 13) * 60 + g2(s->data + 15);
275 if(s->data[12] == '-') 289 if (s->data[12] == '-')
276 offset= -offset; 290 offset = -offset;
277 } 291 }
278#undef g2 292#undef g2
279 293