asn1_encode.c (505d05c7) asn1_encode.c (159d09a2)
1/*
1/*
2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
3 * Use is subject to license terms.
4 */
5
6#pragma ident "%Z%%M% %I% %E% SMI"
7
8/*
9 * src/lib/krb5/asn.1/asn1_encode.c
10 *
11 * Copyright 1994 by the Massachusetts Institute of Technology.
12 * All Rights Reserved.
13 *
14 * Export of this software from the United States of America may

--- 221 unchanged lines hidden (view full) ---

236 char s[16], *sp;
237 unsigned int length, sum=0;
238 time_t gmt_time = val;
239
240 /*
241 * Time encoding: YYYYMMDDhhmmssZ
242 */
243 if (gmt_time == 0) {
6
7/*
8 * src/lib/krb5/asn.1/asn1_encode.c
9 *
10 * Copyright 1994 by the Massachusetts Institute of Technology.
11 * All Rights Reserved.
12 *
13 * Export of this software from the United States of America may

--- 221 unchanged lines hidden (view full) ---

235 char s[16], *sp;
236 unsigned int length, sum=0;
237 time_t gmt_time = val;
238
239 /*
240 * Time encoding: YYYYMMDDhhmmssZ
241 */
242 if (gmt_time == 0) {
244 sp = "19700101000000Z";
245 } else {
246
247 /*
248 * Sanity check this just to be paranoid, as gmtime can return NULL,
249 * and some bogus implementations might overrun on the sprintf.
250 */
243 sp = "19700101000000Z";
244 } else {
245
246 /*
247 * Sanity check this just to be paranoid, as gmtime can return NULL,
248 * and some bogus implementations might overrun on the sprintf.
249 */
251#ifdef HAVE_GMTIME_R
250#ifdef HAVE_GMTIME_R
252 if (gmtime_r(&gmt_time, &gtimebuf) == NULL)
253 return ASN1_BAD_GMTIME;
251# ifdef GMTIME_R_RETURNS_INT
252 if (gmtime_r(&gmt_time, &gtimebuf) != 0)
253 return ASN1_BAD_GMTIME;
254# else
255 if (gmtime_r(&gmt_time, &gtimebuf) == NULL)
256 return ASN1_BAD_GMTIME;
257# endif
254#else
258#else
255 gtime = gmtime(&gmt_time);
256 if (gtime == NULL)
257 return ASN1_BAD_GMTIME;
258 memcpy(&gtimebuf, gtime, sizeof(gtimebuf));
259 gtime = gmtime(&gmt_time);
260 if (gtime == NULL)
261 return ASN1_BAD_GMTIME;
262 memcpy(&gtimebuf, gtime, sizeof(gtimebuf));
259#endif
263#endif
260 gtime = &gtimebuf;
261
262 if (gtime->tm_year > 8099 || gtime->tm_mon > 11 ||
263 gtime->tm_mday > 31 || gtime->tm_hour > 23 ||
264 gtime->tm_min > 59 || gtime->tm_sec > 59)
265 return ASN1_BAD_GMTIME;
266 sprintf(s, "%04d%02d%02d%02d%02d%02dZ",
267 1900+gtime->tm_year, gtime->tm_mon+1, gtime->tm_mday,
268 gtime->tm_hour, gtime->tm_min, gtime->tm_sec);
269 sp = s;
270 }
264 gtime = &gtimebuf;
271
265
266 if (gtime->tm_year > 8099 || gtime->tm_mon > 11 ||
267 gtime->tm_mday > 31 || gtime->tm_hour > 23 ||
268 gtime->tm_min > 59 || gtime->tm_sec > 59)
269 return ASN1_BAD_GMTIME;
270 sprintf(s, "%04d%02d%02d%02d%02d%02dZ",
271 1900+gtime->tm_year, gtime->tm_mon+1, gtime->tm_mday,
272 gtime->tm_hour, gtime->tm_min, gtime->tm_sec);
273 sp = s;
274 }
275
272 retval = asn1buf_insert_charstring(buf,15,sp);
273 if(retval) return retval;
274 sum = 15;
275
276 retval = asn1buf_insert_charstring(buf,15,sp);
277 if(retval) return retval;
278 sum = 15;
279
276 retval = asn1_make_tag(buf,UNIVERSAL,PRIMITIVE,ASN1_GENERALTIME,sum,&length);
277 if(retval) return retval;
278 sum += length;
279
280 *retlen = sum;
281 return 0;
280 retval = asn1_make_tag(buf,UNIVERSAL,PRIMITIVE,ASN1_GENERALTIME,sum,&length);
281 if(retval) return retval;
282 sum += length;
283
284 *retlen = sum;
285 return 0;
282}
283
284asn1_error_code asn1_encode_generalstring(asn1buf *buf, unsigned int len,
285 const char *val,
286 unsigned int *retlen)
287{
288 asn1_error_code retval;
289 unsigned int length;
290
291 retval = asn1buf_insert_charstring(buf,len,val);
292 if(retval) return retval;
293 retval = asn1_make_tag(buf,UNIVERSAL,PRIMITIVE,ASN1_GENERALSTRING,len,
294 &length);
295 if(retval) return retval;
296
297 *retlen = len + length;
298 return 0;
299}
286}
287
288asn1_error_code asn1_encode_generalstring(asn1buf *buf, unsigned int len,
289 const char *val,
290 unsigned int *retlen)
291{
292 asn1_error_code retval;
293 unsigned int length;
294
295 retval = asn1buf_insert_charstring(buf,len,val);
296 if(retval) return retval;
297 retval = asn1_make_tag(buf,UNIVERSAL,PRIMITIVE,ASN1_GENERALSTRING,len,
298 &length);
299 if(retval) return retval;
300
301 *retlen = len + length;
302 return 0;
303}