Back to index

lightning-sunbird  0.9+nobinonly
secder.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifndef _SECDER_H_
00038 #define _SECDER_H_
00039 
00040 /*
00041  * secder.h - public data structures and prototypes for the DER encoding and
00042  *           decoding utilities library
00043  *
00044  * $Id: secder.h,v 1.7.28.2 2007/10/01 14:54:47 biswatosh.chakraborty%sun.com Exp $
00045  */
00046 
00047 #if defined(_WIN32_WCE)
00048 #else
00049 #include <time.h>
00050 #endif
00051 
00052 #include "plarena.h"
00053 #include "prlong.h"
00054 
00055 #include "seccomon.h"
00056 #include "secdert.h"
00057 #include "prtime.h"
00058 
00059 SEC_BEGIN_PROTOS
00060 
00061 /*
00062 ** Encode a data structure into DER.
00063 **     "dest" will be filled in (and memory allocated) to hold the der
00064 **        encoded structure in "src"
00065 **     "t" is a template structure which defines the shape of the
00066 **        stored data
00067 **     "src" is a pointer to the structure that will be encoded
00068 */
00069 extern SECStatus DER_Encode(PRArenaPool *arena, SECItem *dest, DERTemplate *t,
00070                         void *src);
00071 
00072 extern SECStatus DER_Lengths(SECItem *item, int *header_len_p, uint32 *contents_len_p);
00073 
00074 /*
00075 ** Lower level der subroutine that stores the standard header into "to".
00076 ** The header is of variable length, based on encodingLen.
00077 ** The return value is the new value of "to" after skipping over the header.
00078 **     "to" is where the header will be stored
00079 **     "code" is the der code to write
00080 **     "encodingLen" is the number of bytes of data that will follow
00081 **        the header
00082 */
00083 extern unsigned char *DER_StoreHeader(unsigned char *to, unsigned int code,
00084                                   uint32 encodingLen);
00085 
00086 /*
00087 ** Return the number of bytes it will take to hold a der encoded length.
00088 */
00089 extern int DER_LengthLength(uint32 len);
00090 
00091 /*
00092 ** Store a der encoded *signed* integer (whose value is "src") into "dst".
00093 ** XXX This should really be enhanced to take a long.
00094 */
00095 extern SECStatus DER_SetInteger(PRArenaPool *arena, SECItem *dst, int32 src);
00096 
00097 /*
00098 ** Store a der encoded *unsigned* integer (whose value is "src") into "dst".
00099 ** XXX This should really be enhanced to take an unsigned long.
00100 */
00101 extern SECStatus DER_SetUInteger(PRArenaPool *arena, SECItem *dst, uint32 src);
00102 
00103 /*
00104 ** Decode a der encoded *signed* integer that is stored in "src".
00105 ** If "-1" is returned, then the caller should check the error in
00106 ** XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER).
00107 */
00108 extern long DER_GetInteger(SECItem *src);
00109 
00110 /*
00111 ** Decode a der encoded *unsigned* integer that is stored in "src".
00112 ** If the ULONG_MAX is returned, then the caller should check the error
00113 ** in XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER).
00114 */
00115 extern unsigned long DER_GetUInteger(SECItem *src);
00116 
00117 /*
00118 ** Convert a "UNIX" time value to a der encoded time value.
00119 **     "result" is the der encoded time (memory is allocated)
00120 **     "time" is the "UNIX" time value (Since Jan 1st, 1970).
00121 **      time must be on or after January 1, 1950, and
00122 **      before January 1, 2050
00123 ** The caller is responsible for freeing up the buffer which
00124 ** result->data points to upon a successful operation.
00125 */
00126 extern SECStatus DER_TimeToUTCTime(SECItem *result, int64 time);
00127 extern SECStatus DER_TimeToUTCTimeArena(PRArenaPool* arenaOpt,
00128                                         SECItem *dst, int64 gmttime);
00129 
00130 
00131 /*
00132 ** Convert an ascii encoded time value (according to DER rules) into
00133 ** a UNIX time value.
00134 **     "result" the resulting "UNIX" time
00135 **     "string" the der notation ascii value to decode
00136 */
00137 extern SECStatus DER_AsciiToTime(int64 *result, const char *string);
00138 
00139 /*
00140 ** Same as DER_AsciiToTime except takes an SECItem instead of a string
00141 */
00142 extern SECStatus DER_UTCTimeToTime(int64 *result, const SECItem *time);
00143 
00144 /*
00145 ** Convert a DER encoded UTC time to an ascii time representation
00146 ** "utctime" is the DER encoded UTC time to be converted. The
00147 ** caller is responsible for deallocating the returned buffer.
00148 */
00149 extern char *DER_UTCTimeToAscii(SECItem *utcTime);
00150 
00151 /*
00152 ** Convert a DER encoded UTC time to an ascii time representation, but only
00153 ** include the day, not the time.
00154 **     "utctime" is the DER encoded UTC time to be converted.
00155 ** The caller is responsible for deallocating the returned buffer.
00156 */
00157 extern char *DER_UTCDayToAscii(SECItem *utctime);
00158 /* same thing for DER encoded GeneralizedTime */
00159 extern char *DER_GeneralizedDayToAscii(SECItem *gentime);
00160 /* same thing for either DER UTCTime or GeneralizedTime */
00161 extern char *DER_TimeChoiceDayToAscii(SECItem *timechoice);
00162 
00163 /*
00164 ** Convert a int64 time to a DER encoded Generalized time
00165 ** gmttime must be on or after January 1, year 1 and
00166 ** before January 1, 10000.
00167 */
00168 extern SECStatus DER_TimeToGeneralizedTime(SECItem *dst, int64 gmttime);
00169 extern SECStatus DER_TimeToGeneralizedTimeArena(PRArenaPool* arenaOpt,
00170                                                 SECItem *dst, int64 gmttime);
00171 
00172 /*
00173 ** Convert a DER encoded Generalized time value into a UNIX time value.
00174 **     "dst" the resulting "UNIX" time
00175 **     "string" the der notation ascii value to decode
00176 */
00177 extern SECStatus DER_GeneralizedTimeToTime(int64 *dst, const SECItem *time);
00178 
00179 /*
00180 ** Convert from a int64 UTC time value to a formatted ascii value. The
00181 ** caller is responsible for deallocating the returned buffer.
00182 */
00183 extern char *CERT_UTCTime2FormattedAscii (int64 utcTime, char *format);
00184 #define CERT_GeneralizedTime2FormattedAscii CERT_UTCTime2FormattedAscii
00185 
00186 /*
00187 ** Convert from a int64 Generalized time value to a formatted ascii value. The
00188 ** caller is responsible for deallocating the returned buffer.
00189 */
00190 extern char *CERT_GenTime2FormattedAscii (int64 genTime, char *format);
00191 
00192 /*
00193 ** decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME 
00194 ** or a SEC_ASN1_UTC_TIME
00195 */
00196 
00197 extern SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input);
00198 
00199 /* encode a PRTime to an ASN.1 DER SECItem containing either a
00200    SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
00201 
00202 extern SECStatus DER_EncodeTimeChoice(PRArenaPool* arena, SECItem* output,
00203                                        PRTime input);
00204 
00205 SEC_END_PROTOS
00206 
00207 #endif /* _SECDER_H_ */
00208