]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utils/lib/cuTimeStr.h
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utils / lib / cuTimeStr.h
1 /*
2 * Copyright (c) 2002,2011,2014-2019 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License.
7 * Please obtain a copy of the License at http://www.apple.com/publicsource
8 * and read it before using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
12 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
13 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
15 * Please see the License for the specific language governing rights
16 * and limitations under the License.
17 */
18
19 /*
20 * cuTimeStr.h = Time string utilities.
21 */
22
23 #ifndef _TIME_STR_H_
24 #define _TIME_STR_H_
25
26 #include <time.h>
27 #include <Security/x509defs.h>
28
29 #define UTC_TIME_NOSEC_LEN 11
30 #define UTC_TIME_STRLEN 13
31 #define CSSM_TIME_STRLEN 14 /* no trailing 'Z' */
32 #define GENERALIZED_TIME_STRLEN 15
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /*
39 * Given a string containing either a UTC-style or "generalized time"
40 * time string, convert to a struct tm (in GMT/UTC). Returns nonzero on
41 * error.
42 */
43 int cuTimeStringToTm(
44 const char *str,
45 unsigned len,
46 struct tm *tmp);
47
48 typedef enum {
49 CU_TIME_UTC = 0,
50 CU_TIME_LEGACY = 1, /* do not use; see note below */
51 CU_TIME_GEN = 2,
52 CU_TIME_CSSM = 3
53 } timeSpec;
54
55 /* TIME_UTC was formerly defined in this header with a value of 0. However,
56 * a newer version of <time.h> may define it with a value of 1. Clients which
57 * specify the TIME_UTC constant for a timeSpec parameter will get a value
58 * of 0 under the old header, and a value of 1 under the newer header.
59 * The latter conflicts with the old definition of TIME_CSSM. To resolve this,
60 * we now treat 1 as a legacy value which maps to CU_TIME_UTC if TIME_UTC=1,
61 * otherwise to CU_TIME_CSSM.
62 *
63 * Important: any code which specifies the legacy TIME_CSSM constant must be
64 * recompiled against this header to ensure the correct timeSpec value is used.
65 */
66 #ifndef TIME_UTC
67 #define TIME_UTC CU_TIME_UTC /* time.h has not defined TIME_UTC */
68 #endif
69 #ifndef TIME_GEN
70 #define TIME_GEN CU_TIME_GEN
71 #endif
72 #ifndef TIME_CSSM
73 #define TIME_CSSM CU_TIME_CSSM
74 #endif
75
76 /*
77 * Return an APP_MALLOCd time string, specified format and time relative
78 * to 'now' in seconds.
79 */
80 char *cuTimeAtNowPlus(
81 int secFromNow,
82 timeSpec spec);
83
84 /*
85 * Convert a CSSM_X509_TIME, which can be in any of three forms (UTC,
86 * generalized, or CSSM_TIMESTRING) into a CSSM_TIMESTRING. Caller
87 * must free() the result. Returns NULL if x509time is badly formed.
88 */
89 char *cuX509TimeToCssmTimestring(
90 const CSSM_X509_TIME *x509Time,
91 unsigned *rtnLen); // for caller's convenience
92
93 #ifdef __cplusplus
94 }
95 #endif
96
97 #endif /* _TIME_STR_H_ */