1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/util/sectime.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "prtime.h" 1.9 +#include "secder.h" 1.10 +#include "secitem.h" 1.11 +#include "secerr.h" 1.12 + 1.13 +static char *DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format); 1.14 +static char *DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format); 1.15 + 1.16 +/* convert DER utc time to ascii time string */ 1.17 +char * 1.18 +DER_UTCTimeToAscii(SECItem *utcTime) 1.19 +{ 1.20 + return (DecodeUTCTime2FormattedAscii (utcTime, "%a %b %d %H:%M:%S %Y")); 1.21 +} 1.22 + 1.23 +/* convert DER utc time to ascii time string, only include day, not time */ 1.24 +char * 1.25 +DER_UTCDayToAscii(SECItem *utctime) 1.26 +{ 1.27 + return (DecodeUTCTime2FormattedAscii (utctime, "%a %b %d, %Y")); 1.28 +} 1.29 + 1.30 +/* convert DER generalized time to ascii time string, only include day, 1.31 + not time */ 1.32 +char * 1.33 +DER_GeneralizedDayToAscii(SECItem *gentime) 1.34 +{ 1.35 + return (DecodeGeneralizedTime2FormattedAscii (gentime, "%a %b %d, %Y")); 1.36 +} 1.37 + 1.38 +/* convert DER generalized or UTC time to ascii time string, only include 1.39 + day, not time */ 1.40 +char * 1.41 +DER_TimeChoiceDayToAscii(SECItem *timechoice) 1.42 +{ 1.43 + switch (timechoice->type) { 1.44 + 1.45 + case siUTCTime: 1.46 + return DER_UTCDayToAscii(timechoice); 1.47 + 1.48 + case siGeneralizedTime: 1.49 + return DER_GeneralizedDayToAscii(timechoice); 1.50 + 1.51 + default: 1.52 + PORT_Assert(0); 1.53 + PORT_SetError(SEC_ERROR_INVALID_ARGS); 1.54 + return NULL; 1.55 + } 1.56 +} 1.57 + 1.58 +char * 1.59 +CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format) 1.60 +{ 1.61 + PRExplodedTime printableTime; 1.62 + char *timeString; 1.63 + 1.64 + /* Converse time to local time and decompose it into components */ 1.65 + PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime); 1.66 + 1.67 + timeString = (char *)PORT_Alloc(256); 1.68 + 1.69 + if ( timeString ) { 1.70 + if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) { 1.71 + PORT_Free(timeString); 1.72 + timeString = NULL; 1.73 + } 1.74 + } 1.75 + 1.76 + return (timeString); 1.77 +} 1.78 + 1.79 +char *CERT_GenTime2FormattedAscii(PRTime genTime, char *format) 1.80 +{ 1.81 + PRExplodedTime printableTime; 1.82 + char *timeString; 1.83 + 1.84 + /* Decompose time into components */ 1.85 + PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime); 1.86 + 1.87 + timeString = (char *)PORT_Alloc(256); 1.88 + 1.89 + if ( timeString ) { 1.90 + if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) { 1.91 + PORT_Free(timeString); 1.92 + timeString = NULL; 1.93 + PORT_SetError(SEC_ERROR_OUTPUT_LEN); 1.94 + } 1.95 + } 1.96 + 1.97 + return (timeString); 1.98 +} 1.99 + 1.100 + 1.101 +/* convert DER utc time to ascii time string, The format of the time string 1.102 + depends on the input "format" 1.103 + */ 1.104 +static char * 1.105 +DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format) 1.106 +{ 1.107 + PRTime utcTime; 1.108 + int rv; 1.109 + 1.110 + rv = DER_UTCTimeToTime(&utcTime, utcTimeDER); 1.111 + if (rv) { 1.112 + return(NULL); 1.113 + } 1.114 + return (CERT_UTCTime2FormattedAscii (utcTime, format)); 1.115 +} 1.116 + 1.117 +/* convert DER utc time to ascii time string, The format of the time string 1.118 + depends on the input "format" 1.119 + */ 1.120 +static char * 1.121 +DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format) 1.122 +{ 1.123 + PRTime generalizedTime; 1.124 + int rv; 1.125 + 1.126 + rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER); 1.127 + if (rv) { 1.128 + return(NULL); 1.129 + } 1.130 + return (CERT_GeneralizedTime2FormattedAscii (generalizedTime, format)); 1.131 +} 1.132 + 1.133 +/* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME 1.134 + or a SEC_ASN1_UTC_TIME */ 1.135 + 1.136 +SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input) 1.137 +{ 1.138 + switch (input->type) { 1.139 + case siGeneralizedTime: 1.140 + return DER_GeneralizedTimeToTime(output, input); 1.141 + 1.142 + case siUTCTime: 1.143 + return DER_UTCTimeToTime(output, input); 1.144 + 1.145 + default: 1.146 + PORT_SetError(SEC_ERROR_INVALID_ARGS); 1.147 + PORT_Assert(0); 1.148 + return SECFailure; 1.149 + } 1.150 +} 1.151 + 1.152 +/* encode a PRTime to an ASN.1 DER SECItem containing either a 1.153 + SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */ 1.154 + 1.155 +SECStatus DER_EncodeTimeChoice(PLArenaPool* arena, SECItem* output, PRTime input) 1.156 +{ 1.157 + SECStatus rv; 1.158 + 1.159 + rv = DER_TimeToUTCTimeArena(arena, output, input); 1.160 + if (rv == SECSuccess || PORT_GetError() != SEC_ERROR_INVALID_ARGS) { 1.161 + return rv; 1.162 + } 1.163 + return DER_TimeToGeneralizedTimeArena(arena, output, input); 1.164 +}