Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "prtime.h" |
michael@0 | 6 | #include "secder.h" |
michael@0 | 7 | #include "secitem.h" |
michael@0 | 8 | #include "secerr.h" |
michael@0 | 9 | |
michael@0 | 10 | static char *DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format); |
michael@0 | 11 | static char *DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format); |
michael@0 | 12 | |
michael@0 | 13 | /* convert DER utc time to ascii time string */ |
michael@0 | 14 | char * |
michael@0 | 15 | DER_UTCTimeToAscii(SECItem *utcTime) |
michael@0 | 16 | { |
michael@0 | 17 | return (DecodeUTCTime2FormattedAscii (utcTime, "%a %b %d %H:%M:%S %Y")); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | /* convert DER utc time to ascii time string, only include day, not time */ |
michael@0 | 21 | char * |
michael@0 | 22 | DER_UTCDayToAscii(SECItem *utctime) |
michael@0 | 23 | { |
michael@0 | 24 | return (DecodeUTCTime2FormattedAscii (utctime, "%a %b %d, %Y")); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | /* convert DER generalized time to ascii time string, only include day, |
michael@0 | 28 | not time */ |
michael@0 | 29 | char * |
michael@0 | 30 | DER_GeneralizedDayToAscii(SECItem *gentime) |
michael@0 | 31 | { |
michael@0 | 32 | return (DecodeGeneralizedTime2FormattedAscii (gentime, "%a %b %d, %Y")); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | /* convert DER generalized or UTC time to ascii time string, only include |
michael@0 | 36 | day, not time */ |
michael@0 | 37 | char * |
michael@0 | 38 | DER_TimeChoiceDayToAscii(SECItem *timechoice) |
michael@0 | 39 | { |
michael@0 | 40 | switch (timechoice->type) { |
michael@0 | 41 | |
michael@0 | 42 | case siUTCTime: |
michael@0 | 43 | return DER_UTCDayToAscii(timechoice); |
michael@0 | 44 | |
michael@0 | 45 | case siGeneralizedTime: |
michael@0 | 46 | return DER_GeneralizedDayToAscii(timechoice); |
michael@0 | 47 | |
michael@0 | 48 | default: |
michael@0 | 49 | PORT_Assert(0); |
michael@0 | 50 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
michael@0 | 51 | return NULL; |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | char * |
michael@0 | 56 | CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format) |
michael@0 | 57 | { |
michael@0 | 58 | PRExplodedTime printableTime; |
michael@0 | 59 | char *timeString; |
michael@0 | 60 | |
michael@0 | 61 | /* Converse time to local time and decompose it into components */ |
michael@0 | 62 | PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime); |
michael@0 | 63 | |
michael@0 | 64 | timeString = (char *)PORT_Alloc(256); |
michael@0 | 65 | |
michael@0 | 66 | if ( timeString ) { |
michael@0 | 67 | if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) { |
michael@0 | 68 | PORT_Free(timeString); |
michael@0 | 69 | timeString = NULL; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | return (timeString); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | char *CERT_GenTime2FormattedAscii(PRTime genTime, char *format) |
michael@0 | 77 | { |
michael@0 | 78 | PRExplodedTime printableTime; |
michael@0 | 79 | char *timeString; |
michael@0 | 80 | |
michael@0 | 81 | /* Decompose time into components */ |
michael@0 | 82 | PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime); |
michael@0 | 83 | |
michael@0 | 84 | timeString = (char *)PORT_Alloc(256); |
michael@0 | 85 | |
michael@0 | 86 | if ( timeString ) { |
michael@0 | 87 | if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) { |
michael@0 | 88 | PORT_Free(timeString); |
michael@0 | 89 | timeString = NULL; |
michael@0 | 90 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | return (timeString); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | /* convert DER utc time to ascii time string, The format of the time string |
michael@0 | 99 | depends on the input "format" |
michael@0 | 100 | */ |
michael@0 | 101 | static char * |
michael@0 | 102 | DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format) |
michael@0 | 103 | { |
michael@0 | 104 | PRTime utcTime; |
michael@0 | 105 | int rv; |
michael@0 | 106 | |
michael@0 | 107 | rv = DER_UTCTimeToTime(&utcTime, utcTimeDER); |
michael@0 | 108 | if (rv) { |
michael@0 | 109 | return(NULL); |
michael@0 | 110 | } |
michael@0 | 111 | return (CERT_UTCTime2FormattedAscii (utcTime, format)); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | /* convert DER utc time to ascii time string, The format of the time string |
michael@0 | 115 | depends on the input "format" |
michael@0 | 116 | */ |
michael@0 | 117 | static char * |
michael@0 | 118 | DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format) |
michael@0 | 119 | { |
michael@0 | 120 | PRTime generalizedTime; |
michael@0 | 121 | int rv; |
michael@0 | 122 | |
michael@0 | 123 | rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER); |
michael@0 | 124 | if (rv) { |
michael@0 | 125 | return(NULL); |
michael@0 | 126 | } |
michael@0 | 127 | return (CERT_GeneralizedTime2FormattedAscii (generalizedTime, format)); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | /* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME |
michael@0 | 131 | or a SEC_ASN1_UTC_TIME */ |
michael@0 | 132 | |
michael@0 | 133 | SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input) |
michael@0 | 134 | { |
michael@0 | 135 | switch (input->type) { |
michael@0 | 136 | case siGeneralizedTime: |
michael@0 | 137 | return DER_GeneralizedTimeToTime(output, input); |
michael@0 | 138 | |
michael@0 | 139 | case siUTCTime: |
michael@0 | 140 | return DER_UTCTimeToTime(output, input); |
michael@0 | 141 | |
michael@0 | 142 | default: |
michael@0 | 143 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
michael@0 | 144 | PORT_Assert(0); |
michael@0 | 145 | return SECFailure; |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | /* encode a PRTime to an ASN.1 DER SECItem containing either a |
michael@0 | 150 | SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */ |
michael@0 | 151 | |
michael@0 | 152 | SECStatus DER_EncodeTimeChoice(PLArenaPool* arena, SECItem* output, PRTime input) |
michael@0 | 153 | { |
michael@0 | 154 | SECStatus rv; |
michael@0 | 155 | |
michael@0 | 156 | rv = DER_TimeToUTCTimeArena(arena, output, input); |
michael@0 | 157 | if (rv == SECSuccess || PORT_GetError() != SEC_ERROR_INVALID_ARGS) { |
michael@0 | 158 | return rv; |
michael@0 | 159 | } |
michael@0 | 160 | return DER_TimeToGeneralizedTimeArena(arena, output, input); |
michael@0 | 161 | } |