secder.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef _SECDER_H_
  5. #define _SECDER_H_
  6. #include "utilrename.h"
  7. /*
  8. * secder.h - public data structures and prototypes for the DER encoding and
  9. * decoding utilities library
  10. */
  11. #include <time.h>
  12. #include "plarena.h"
  13. #include "prlong.h"
  14. #include "seccomon.h"
  15. #include "secdert.h"
  16. #include "prtime.h"
  17. SEC_BEGIN_PROTOS
  18. /*
  19. ** Encode a data structure into DER.
  20. ** "dest" will be filled in (and memory allocated) to hold the der
  21. ** encoded structure in "src"
  22. ** "t" is a template structure which defines the shape of the
  23. ** stored data
  24. ** "src" is a pointer to the structure that will be encoded
  25. */
  26. extern SECStatus DER_Encode(PLArenaPool *arena, SECItem *dest, DERTemplate *t,
  27. void *src);
  28. /*
  29. ** This function is deprecated.
  30. */
  31. extern SECStatus DER_Lengths(SECItem *item, int *header_len_p,
  32. PRUint32 *contents_len_p);
  33. /*
  34. ** Lower level der subroutine that stores the standard header into "to".
  35. ** The header is of variable length, based on encodingLen.
  36. ** The return value is the new value of "to" after skipping over the header.
  37. ** "to" is where the header will be stored
  38. ** "code" is the der code to write
  39. ** "encodingLen" is the number of bytes of data that will follow
  40. ** the header
  41. */
  42. extern unsigned char *DER_StoreHeader(unsigned char *to, unsigned int code,
  43. PRUint32 encodingLen);
  44. /*
  45. ** Return the number of bytes it will take to hold a der encoded length.
  46. */
  47. extern int DER_LengthLength(PRUint32 len);
  48. /*
  49. ** Store a der encoded *signed* integer (whose value is "src") into "dst".
  50. ** XXX This should really be enhanced to take a long.
  51. */
  52. extern SECStatus DER_SetInteger(PLArenaPool *arena, SECItem *dst, PRInt32 src);
  53. /*
  54. ** Store a der encoded *unsigned* integer (whose value is "src") into "dst".
  55. ** XXX This should really be enhanced to take an unsigned long.
  56. */
  57. extern SECStatus DER_SetUInteger(PLArenaPool *arena, SECItem *dst, PRUint32 src);
  58. /*
  59. ** Decode a der encoded *signed* integer that is stored in "src".
  60. ** If "-1" is returned, then the caller should check the error in
  61. ** XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER).
  62. */
  63. extern long DER_GetInteger(const SECItem *src);
  64. /*
  65. ** Decode a der encoded *unsigned* integer that is stored in "src".
  66. ** If the ULONG_MAX is returned, then the caller should check the error
  67. ** in XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER).
  68. */
  69. extern unsigned long DER_GetUInteger(SECItem *src);
  70. /*
  71. ** Convert an NSPR time value to a der encoded time value.
  72. ** "result" is the der encoded time (memory is allocated)
  73. ** "time" is the NSPR time value (Since Jan 1st, 1970).
  74. ** time must be on or after January 1, 1950, and
  75. ** before January 1, 2050
  76. ** The caller is responsible for freeing up the buffer which
  77. ** result->data points to upon a successful operation.
  78. */
  79. extern SECStatus DER_TimeToUTCTime(SECItem *result, PRTime time);
  80. extern SECStatus DER_TimeToUTCTimeArena(PLArenaPool *arenaOpt,
  81. SECItem *dst, PRTime gmttime);
  82. /*
  83. ** Convert an ascii encoded time value (according to DER rules) into
  84. ** an NSPR time value.
  85. ** "result" the resulting NSPR time
  86. ** "string" the der notation ascii value to decode
  87. */
  88. extern SECStatus DER_AsciiToTime(PRTime *result, const char *string);
  89. /*
  90. ** Same as DER_AsciiToTime except takes an SECItem instead of a string
  91. */
  92. extern SECStatus DER_UTCTimeToTime(PRTime *result, const SECItem *time);
  93. /*
  94. ** Convert a DER encoded UTC time to an ascii time representation
  95. ** "utctime" is the DER encoded UTC time to be converted. The
  96. ** caller is responsible for deallocating the returned buffer.
  97. */
  98. extern char *DER_UTCTimeToAscii(SECItem *utcTime);
  99. /*
  100. ** Convert a DER encoded UTC time to an ascii time representation, but only
  101. ** include the day, not the time.
  102. ** "utctime" is the DER encoded UTC time to be converted.
  103. ** The caller is responsible for deallocating the returned buffer.
  104. */
  105. extern char *DER_UTCDayToAscii(SECItem *utctime);
  106. /* same thing for DER encoded GeneralizedTime */
  107. extern char *DER_GeneralizedDayToAscii(SECItem *gentime);
  108. /* same thing for either DER UTCTime or GeneralizedTime */
  109. extern char *DER_TimeChoiceDayToAscii(SECItem *timechoice);
  110. /*
  111. ** Convert a PRTime to a DER encoded Generalized time
  112. ** gmttime must be on or after January 1, year 1 and
  113. ** before January 1, 10000.
  114. */
  115. extern SECStatus DER_TimeToGeneralizedTime(SECItem *dst, PRTime gmttime);
  116. extern SECStatus DER_TimeToGeneralizedTimeArena(PLArenaPool *arenaOpt,
  117. SECItem *dst, PRTime gmttime);
  118. /*
  119. ** Convert a DER encoded Generalized time value into an NSPR time value.
  120. ** "dst" the resulting NSPR time
  121. ** "string" the der notation ascii value to decode
  122. */
  123. extern SECStatus DER_GeneralizedTimeToTime(PRTime *dst, const SECItem *time);
  124. /*
  125. ** Convert from a PRTime UTC time value to a formatted ascii value. The
  126. ** caller is responsible for deallocating the returned buffer.
  127. */
  128. extern char *CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format);
  129. #define CERT_GeneralizedTime2FormattedAscii CERT_UTCTime2FormattedAscii
  130. /*
  131. ** Convert from a PRTime Generalized time value to a formatted ascii value. The
  132. ** caller is responsible for deallocating the returned buffer.
  133. */
  134. extern char *CERT_GenTime2FormattedAscii(PRTime genTime, char *format);
  135. /*
  136. ** decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME
  137. ** or a SEC_ASN1_UTC_TIME
  138. */
  139. extern SECStatus DER_DecodeTimeChoice(PRTime *output, const SECItem *input);
  140. /* encode a PRTime to an ASN.1 DER SECItem containing either a
  141. SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
  142. extern SECStatus DER_EncodeTimeChoice(PLArenaPool *arena, SECItem *output,
  143. PRTime input);
  144. SEC_END_PROTOS
  145. #endif /* _SECDER_H_ */