security/nss/lib/util/secasn1.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 /*
michael@0 6 * Support for encoding/decoding of ASN.1 using BER/DER (Basic/Distinguished
michael@0 7 * Encoding Rules). The routines are found in and used extensively by the
michael@0 8 * security library, but exported for other use.
michael@0 9 */
michael@0 10
michael@0 11 #ifndef _SECASN1_H_
michael@0 12 #define _SECASN1_H_
michael@0 13
michael@0 14 #include "utilrename.h"
michael@0 15 #include "plarena.h"
michael@0 16
michael@0 17 #include "seccomon.h"
michael@0 18 #include "secasn1t.h"
michael@0 19
michael@0 20
michael@0 21 /************************************************************************/
michael@0 22 SEC_BEGIN_PROTOS
michael@0 23
michael@0 24 /*
michael@0 25 * XXX These function prototypes need full, explanatory comments.
michael@0 26 */
michael@0 27
michael@0 28 /*
michael@0 29 ** Decoding.
michael@0 30 */
michael@0 31
michael@0 32 extern SEC_ASN1DecoderContext *SEC_ASN1DecoderStart(PLArenaPool *pool,
michael@0 33 void *dest,
michael@0 34 const SEC_ASN1Template *t);
michael@0 35
michael@0 36 /* XXX char or unsigned char? */
michael@0 37 extern SECStatus SEC_ASN1DecoderUpdate(SEC_ASN1DecoderContext *cx,
michael@0 38 const char *buf,
michael@0 39 unsigned long len);
michael@0 40
michael@0 41 extern SECStatus SEC_ASN1DecoderFinish(SEC_ASN1DecoderContext *cx);
michael@0 42
michael@0 43 /* Higher level code detected an error, abort the rest of the processing */
michael@0 44 extern void SEC_ASN1DecoderAbort(SEC_ASN1DecoderContext *cx, int error);
michael@0 45
michael@0 46 extern void SEC_ASN1DecoderSetFilterProc(SEC_ASN1DecoderContext *cx,
michael@0 47 SEC_ASN1WriteProc fn,
michael@0 48 void *arg, PRBool no_store);
michael@0 49
michael@0 50 extern void SEC_ASN1DecoderClearFilterProc(SEC_ASN1DecoderContext *cx);
michael@0 51
michael@0 52 extern void SEC_ASN1DecoderSetNotifyProc(SEC_ASN1DecoderContext *cx,
michael@0 53 SEC_ASN1NotifyProc fn,
michael@0 54 void *arg);
michael@0 55
michael@0 56 extern void SEC_ASN1DecoderClearNotifyProc(SEC_ASN1DecoderContext *cx);
michael@0 57
michael@0 58 extern SECStatus SEC_ASN1Decode(PLArenaPool *pool, void *dest,
michael@0 59 const SEC_ASN1Template *t,
michael@0 60 const char *buf, long len);
michael@0 61
michael@0 62 /* Both classic ASN.1 and QuickDER have a feature that removes leading zeroes
michael@0 63 out of SEC_ASN1_INTEGER if the caller sets siUnsignedInteger in the type
michael@0 64 field of the target SECItem prior to calling the decoder. Otherwise, the
michael@0 65 type field is ignored and untouched. For SECItem that are dynamically
michael@0 66 allocated (from POINTER, SET OF, SEQUENCE OF) the decoder sets the type
michael@0 67 field to siBuffer. */
michael@0 68
michael@0 69 extern SECStatus SEC_ASN1DecodeItem(PLArenaPool *pool, void *dest,
michael@0 70 const SEC_ASN1Template *t,
michael@0 71 const SECItem *src);
michael@0 72
michael@0 73 extern SECStatus SEC_QuickDERDecodeItem(PLArenaPool* arena, void* dest,
michael@0 74 const SEC_ASN1Template* templateEntry,
michael@0 75 const SECItem* src);
michael@0 76
michael@0 77 /*
michael@0 78 ** Encoding.
michael@0 79 */
michael@0 80
michael@0 81 extern SEC_ASN1EncoderContext *SEC_ASN1EncoderStart(const void *src,
michael@0 82 const SEC_ASN1Template *t,
michael@0 83 SEC_ASN1WriteProc fn,
michael@0 84 void *output_arg);
michael@0 85
michael@0 86 /* XXX char or unsigned char? */
michael@0 87 extern SECStatus SEC_ASN1EncoderUpdate(SEC_ASN1EncoderContext *cx,
michael@0 88 const char *buf,
michael@0 89 unsigned long len);
michael@0 90
michael@0 91 extern void SEC_ASN1EncoderFinish(SEC_ASN1EncoderContext *cx);
michael@0 92
michael@0 93 /* Higher level code detected an error, abort the rest of the processing */
michael@0 94 extern void SEC_ASN1EncoderAbort(SEC_ASN1EncoderContext *cx, int error);
michael@0 95
michael@0 96 extern void SEC_ASN1EncoderSetNotifyProc(SEC_ASN1EncoderContext *cx,
michael@0 97 SEC_ASN1NotifyProc fn,
michael@0 98 void *arg);
michael@0 99
michael@0 100 extern void SEC_ASN1EncoderClearNotifyProc(SEC_ASN1EncoderContext *cx);
michael@0 101
michael@0 102 extern void SEC_ASN1EncoderSetStreaming(SEC_ASN1EncoderContext *cx);
michael@0 103
michael@0 104 extern void SEC_ASN1EncoderClearStreaming(SEC_ASN1EncoderContext *cx);
michael@0 105
michael@0 106 extern void sec_ASN1EncoderSetDER(SEC_ASN1EncoderContext *cx);
michael@0 107
michael@0 108 extern void sec_ASN1EncoderClearDER(SEC_ASN1EncoderContext *cx);
michael@0 109
michael@0 110 extern void SEC_ASN1EncoderSetTakeFromBuf(SEC_ASN1EncoderContext *cx);
michael@0 111
michael@0 112 extern void SEC_ASN1EncoderClearTakeFromBuf(SEC_ASN1EncoderContext *cx);
michael@0 113
michael@0 114 extern SECStatus SEC_ASN1Encode(const void *src, const SEC_ASN1Template *t,
michael@0 115 SEC_ASN1WriteProc output_proc,
michael@0 116 void *output_arg);
michael@0 117
michael@0 118 /*
michael@0 119 * If both pool and dest are NULL, the caller should free the returned SECItem
michael@0 120 * with a SECITEM_FreeItem(..., PR_TRUE) call. If pool is NULL but dest is
michael@0 121 * not NULL, the caller should free the data buffer pointed to by dest with a
michael@0 122 * SECITEM_FreeItem(dest, PR_FALSE) or PORT_Free(dest->data) call.
michael@0 123 */
michael@0 124 extern SECItem * SEC_ASN1EncodeItem(PLArenaPool *pool, SECItem *dest,
michael@0 125 const void *src, const SEC_ASN1Template *t);
michael@0 126
michael@0 127 extern SECItem * SEC_ASN1EncodeInteger(PLArenaPool *pool,
michael@0 128 SECItem *dest, long value);
michael@0 129
michael@0 130 extern SECItem * SEC_ASN1EncodeUnsignedInteger(PLArenaPool *pool,
michael@0 131 SECItem *dest,
michael@0 132 unsigned long value);
michael@0 133
michael@0 134 extern SECStatus SEC_ASN1DecodeInteger(SECItem *src,
michael@0 135 unsigned long *value);
michael@0 136
michael@0 137 /*
michael@0 138 ** Utilities.
michael@0 139 */
michael@0 140
michael@0 141 /*
michael@0 142 * We have a length that needs to be encoded; how many bytes will the
michael@0 143 * encoding take?
michael@0 144 */
michael@0 145 extern int SEC_ASN1LengthLength (unsigned long len);
michael@0 146
michael@0 147 /* encode the length and return the number of bytes we encoded. Buffer
michael@0 148 * must be pre allocated */
michael@0 149 extern int SEC_ASN1EncodeLength(unsigned char *buf,int value);
michael@0 150
michael@0 151 /*
michael@0 152 * Find the appropriate subtemplate for the given template.
michael@0 153 * This may involve calling a "chooser" function, or it may just
michael@0 154 * be right there. In either case, it is expected to *have* a
michael@0 155 * subtemplate; this is asserted in debug builds (in non-debug
michael@0 156 * builds, NULL will be returned).
michael@0 157 *
michael@0 158 * "thing" is a pointer to the structure being encoded/decoded
michael@0 159 * "encoding", when true, means that we are in the process of encoding
michael@0 160 * (as opposed to in the process of decoding)
michael@0 161 */
michael@0 162 extern const SEC_ASN1Template *
michael@0 163 SEC_ASN1GetSubtemplate (const SEC_ASN1Template *inTemplate, void *thing,
michael@0 164 PRBool encoding);
michael@0 165
michael@0 166 /* whether the template is for a primitive type or a choice of
michael@0 167 * primitive types
michael@0 168 */
michael@0 169 extern PRBool SEC_ASN1IsTemplateSimple(const SEC_ASN1Template *theTemplate);
michael@0 170
michael@0 171 /************************************************************************/
michael@0 172
michael@0 173 /*
michael@0 174 * Generic Templates
michael@0 175 * One for each of the simple types, plus a special one for ANY, plus:
michael@0 176 * - a pointer to each one of those
michael@0 177 * - a set of each one of those
michael@0 178 * - a sequence of each one of those
michael@0 179 *
michael@0 180 * Note that these are alphabetical (case insensitive); please add new
michael@0 181 * ones in the appropriate place.
michael@0 182 */
michael@0 183
michael@0 184 extern const SEC_ASN1Template SEC_AnyTemplate[];
michael@0 185 extern const SEC_ASN1Template SEC_BitStringTemplate[];
michael@0 186 extern const SEC_ASN1Template SEC_BMPStringTemplate[];
michael@0 187 extern const SEC_ASN1Template SEC_BooleanTemplate[];
michael@0 188 extern const SEC_ASN1Template SEC_EnumeratedTemplate[];
michael@0 189 extern const SEC_ASN1Template SEC_GeneralizedTimeTemplate[];
michael@0 190 extern const SEC_ASN1Template SEC_IA5StringTemplate[];
michael@0 191 extern const SEC_ASN1Template SEC_IntegerTemplate[];
michael@0 192 extern const SEC_ASN1Template SEC_NullTemplate[];
michael@0 193 extern const SEC_ASN1Template SEC_ObjectIDTemplate[];
michael@0 194 extern const SEC_ASN1Template SEC_OctetStringTemplate[];
michael@0 195 extern const SEC_ASN1Template SEC_PrintableStringTemplate[];
michael@0 196 extern const SEC_ASN1Template SEC_T61StringTemplate[];
michael@0 197 extern const SEC_ASN1Template SEC_UniversalStringTemplate[];
michael@0 198 extern const SEC_ASN1Template SEC_UTCTimeTemplate[];
michael@0 199 extern const SEC_ASN1Template SEC_UTF8StringTemplate[];
michael@0 200 extern const SEC_ASN1Template SEC_VisibleStringTemplate[];
michael@0 201
michael@0 202 extern const SEC_ASN1Template SEC_PointerToAnyTemplate[];
michael@0 203 extern const SEC_ASN1Template SEC_PointerToBitStringTemplate[];
michael@0 204 extern const SEC_ASN1Template SEC_PointerToBMPStringTemplate[];
michael@0 205 extern const SEC_ASN1Template SEC_PointerToBooleanTemplate[];
michael@0 206 extern const SEC_ASN1Template SEC_PointerToEnumeratedTemplate[];
michael@0 207 extern const SEC_ASN1Template SEC_PointerToGeneralizedTimeTemplate[];
michael@0 208 extern const SEC_ASN1Template SEC_PointerToIA5StringTemplate[];
michael@0 209 extern const SEC_ASN1Template SEC_PointerToIntegerTemplate[];
michael@0 210 extern const SEC_ASN1Template SEC_PointerToNullTemplate[];
michael@0 211 extern const SEC_ASN1Template SEC_PointerToObjectIDTemplate[];
michael@0 212 extern const SEC_ASN1Template SEC_PointerToOctetStringTemplate[];
michael@0 213 extern const SEC_ASN1Template SEC_PointerToPrintableStringTemplate[];
michael@0 214 extern const SEC_ASN1Template SEC_PointerToT61StringTemplate[];
michael@0 215 extern const SEC_ASN1Template SEC_PointerToUniversalStringTemplate[];
michael@0 216 extern const SEC_ASN1Template SEC_PointerToUTCTimeTemplate[];
michael@0 217 extern const SEC_ASN1Template SEC_PointerToUTF8StringTemplate[];
michael@0 218 extern const SEC_ASN1Template SEC_PointerToVisibleStringTemplate[];
michael@0 219
michael@0 220 extern const SEC_ASN1Template SEC_SequenceOfAnyTemplate[];
michael@0 221 extern const SEC_ASN1Template SEC_SequenceOfBitStringTemplate[];
michael@0 222 extern const SEC_ASN1Template SEC_SequenceOfBMPStringTemplate[];
michael@0 223 extern const SEC_ASN1Template SEC_SequenceOfBooleanTemplate[];
michael@0 224 extern const SEC_ASN1Template SEC_SequenceOfEnumeratedTemplate[];
michael@0 225 extern const SEC_ASN1Template SEC_SequenceOfGeneralizedTimeTemplate[];
michael@0 226 extern const SEC_ASN1Template SEC_SequenceOfIA5StringTemplate[];
michael@0 227 extern const SEC_ASN1Template SEC_SequenceOfIntegerTemplate[];
michael@0 228 extern const SEC_ASN1Template SEC_SequenceOfNullTemplate[];
michael@0 229 extern const SEC_ASN1Template SEC_SequenceOfObjectIDTemplate[];
michael@0 230 extern const SEC_ASN1Template SEC_SequenceOfOctetStringTemplate[];
michael@0 231 extern const SEC_ASN1Template SEC_SequenceOfPrintableStringTemplate[];
michael@0 232 extern const SEC_ASN1Template SEC_SequenceOfT61StringTemplate[];
michael@0 233 extern const SEC_ASN1Template SEC_SequenceOfUniversalStringTemplate[];
michael@0 234 extern const SEC_ASN1Template SEC_SequenceOfUTCTimeTemplate[];
michael@0 235 extern const SEC_ASN1Template SEC_SequenceOfUTF8StringTemplate[];
michael@0 236 extern const SEC_ASN1Template SEC_SequenceOfVisibleStringTemplate[];
michael@0 237
michael@0 238 extern const SEC_ASN1Template SEC_SetOfAnyTemplate[];
michael@0 239 extern const SEC_ASN1Template SEC_SetOfBitStringTemplate[];
michael@0 240 extern const SEC_ASN1Template SEC_SetOfBMPStringTemplate[];
michael@0 241 extern const SEC_ASN1Template SEC_SetOfBooleanTemplate[];
michael@0 242 extern const SEC_ASN1Template SEC_SetOfEnumeratedTemplate[];
michael@0 243 extern const SEC_ASN1Template SEC_SetOfGeneralizedTimeTemplate[];
michael@0 244 extern const SEC_ASN1Template SEC_SetOfIA5StringTemplate[];
michael@0 245 extern const SEC_ASN1Template SEC_SetOfIntegerTemplate[];
michael@0 246 extern const SEC_ASN1Template SEC_SetOfNullTemplate[];
michael@0 247 extern const SEC_ASN1Template SEC_SetOfObjectIDTemplate[];
michael@0 248 extern const SEC_ASN1Template SEC_SetOfOctetStringTemplate[];
michael@0 249 extern const SEC_ASN1Template SEC_SetOfPrintableStringTemplate[];
michael@0 250 extern const SEC_ASN1Template SEC_SetOfT61StringTemplate[];
michael@0 251 extern const SEC_ASN1Template SEC_SetOfUniversalStringTemplate[];
michael@0 252 extern const SEC_ASN1Template SEC_SetOfUTCTimeTemplate[];
michael@0 253 extern const SEC_ASN1Template SEC_SetOfUTF8StringTemplate[];
michael@0 254 extern const SEC_ASN1Template SEC_SetOfVisibleStringTemplate[];
michael@0 255
michael@0 256 /*
michael@0 257 * Template for skipping a subitem; this only makes sense when decoding.
michael@0 258 */
michael@0 259 extern const SEC_ASN1Template SEC_SkipTemplate[];
michael@0 260
michael@0 261 /* These functions simply return the address of the above-declared templates.
michael@0 262 ** This is necessary for Windows DLLs. Sigh.
michael@0 263 */
michael@0 264 SEC_ASN1_CHOOSER_DECLARE(SEC_AnyTemplate)
michael@0 265 SEC_ASN1_CHOOSER_DECLARE(SEC_BMPStringTemplate)
michael@0 266 SEC_ASN1_CHOOSER_DECLARE(SEC_BooleanTemplate)
michael@0 267 SEC_ASN1_CHOOSER_DECLARE(SEC_BitStringTemplate)
michael@0 268 SEC_ASN1_CHOOSER_DECLARE(SEC_GeneralizedTimeTemplate)
michael@0 269 SEC_ASN1_CHOOSER_DECLARE(SEC_IA5StringTemplate)
michael@0 270 SEC_ASN1_CHOOSER_DECLARE(SEC_IntegerTemplate)
michael@0 271 SEC_ASN1_CHOOSER_DECLARE(SEC_NullTemplate)
michael@0 272 SEC_ASN1_CHOOSER_DECLARE(SEC_ObjectIDTemplate)
michael@0 273 SEC_ASN1_CHOOSER_DECLARE(SEC_OctetStringTemplate)
michael@0 274 SEC_ASN1_CHOOSER_DECLARE(SEC_UTCTimeTemplate)
michael@0 275 SEC_ASN1_CHOOSER_DECLARE(SEC_UTF8StringTemplate)
michael@0 276
michael@0 277 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToAnyTemplate)
michael@0 278 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToOctetStringTemplate)
michael@0 279
michael@0 280 SEC_ASN1_CHOOSER_DECLARE(SEC_SetOfAnyTemplate)
michael@0 281
michael@0 282 SEC_ASN1_CHOOSER_DECLARE(SEC_EnumeratedTemplate)
michael@0 283 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToEnumeratedTemplate)
michael@0 284 SEC_ASN1_CHOOSER_DECLARE(SEC_SequenceOfAnyTemplate)
michael@0 285 SEC_ASN1_CHOOSER_DECLARE(SEC_SequenceOfObjectIDTemplate)
michael@0 286 SEC_ASN1_CHOOSER_DECLARE(SEC_SkipTemplate)
michael@0 287 SEC_ASN1_CHOOSER_DECLARE(SEC_UniversalStringTemplate)
michael@0 288 SEC_ASN1_CHOOSER_DECLARE(SEC_PrintableStringTemplate)
michael@0 289 SEC_ASN1_CHOOSER_DECLARE(SEC_T61StringTemplate)
michael@0 290 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToGeneralizedTimeTemplate)
michael@0 291 SEC_END_PROTOS
michael@0 292 #endif /* _SECASN1_H_ */

mercurial