1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is the Netscape security libraries.
16
* The Initial Developer of the Original Code is
17
* Netscape Communications Corporation.
18
* Portions created by the Initial Developer are Copyright (C) 1994-2000
19
* the Initial Developer. All Rights Reserved.
23
* Alternatively, the contents of this file may be used under the terms of
24
* either the GNU General Public License Version 2 or later (the "GPL"), or
25
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
* in which case the provisions of the GPL or the LGPL are applicable instead
27
* of those above. If you wish to allow use of your version of this file only
28
* under the terms of either the GPL or the LGPL, and not to allow others to
29
* use your version of this file under the terms of the MPL, indicate your
30
* decision by deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL or the LGPL. If you do not delete
32
* the provisions above, a recipient may use your version of this file under
33
* the terms of any one of the MPL, the GPL or the LGPL.
35
* ***** END LICENSE BLOCK ***** */
41
static const char ASN1_CVS_ID[] = "@(#) $RCSfile: asn1.h,v $ $Revision: 1.3 $ $Date: 2005/01/20 02:25:44 $";
47
* This file contains the ASN.1 encoder/decoder routines available
48
* internally within NSS. It's not clear right now if this file
49
* will be folded into base.h or something, I just needed to get this
50
* going. At the moment, most of these routines wrap the old SEC_ASN1
67
* ... description here ...
69
* nssASN1Decoder_Create (Factory/Constructor)
70
* nssASN1Decoder_Update
71
* nssASN1Decoder_Finish (Destructor)
72
* nssASN1Decoder_SetFilter
73
* nssASN1Decoder_GetFilter
74
* nssASN1Decoder_SetNotify
75
* nssASN1Decoder_GetNotify
79
* nssASN1Decoder_verify
81
* Related functions that aren't type methods:
88
* nssASN1Decoder_Create
90
* This routine creates an ASN.1 Decoder, which will use the specified
91
* template to decode a datastream into the specified destination
92
* structure. If the optional arena argument is non-NULL, blah blah
93
* blah. XXX fgmr Should we include an nssASN1EncodingType argument,
94
* as a hint? Or is each encoding distinctive? This routine may
95
* return NULL upon error, in which case an error will have been
96
* placed upon the error stack.
98
* The error may be one of the following values:
100
* NSS_ERROR_INVALID_ARENA
101
* NSS_ERROR_INVALID_POINTER
106
* A pointer to an ASN.1 Decoder upon success.
109
NSS_EXTERN nssASN1Decoder *
110
nssASN1Decoder_Create
114
const nssASN1Template template[]
117
extern const NSSError NSS_ERROR_NO_MEMORY;
118
extern const NSSError NSS_ERROR_INVALID_ARENA;
119
extern const NSSError NSS_ERROR_INVALID_POINTER;
122
* nssASN1Decoder_Update
124
* This routine feeds data to the decoder. In the event of an error,
125
* it will place an error on the error stack and return PR_FAILURE.
127
* The error may be one of the following values:
128
* NSS_ERROR_NO_MEMORY
129
* NSS_ERROR_INVALID_POINTER
130
* NSS_ERROR_INVALID_ASN1DECODER
131
* NSS_ERROR_INVALID_BER
135
* PR_FAILURE upon error
136
* PR_SUCCESS upon success.
140
nssASN1Decoder_Update
142
nssASN1Decoder *decoder,
147
extern const NSSError NSS_ERROR_NO_MEMORY;
148
extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
149
extern const NSSError NSS_ERROR_INVALID_BER;
152
* nssASN1Decoder_Finish
154
* This routine finishes the decoding and destroys the decoder.
155
* In the event of an error, it will place an error on the error
156
* stack and return PR_FAILURE.
158
* The error may be one of the following values:
159
* NSS_ERROR_INVALID_ASN1DECODER
162
* PR_FAILURE upon error
163
* PR_SUCCESS upon success
167
nssASN1Decoder_Finish
169
nssASN1Decoder *decoder
172
extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
175
* nssASN1Decoder_SetFilter
177
* This routine registers a callback filter routine with the decoder,
178
* which will be called blah blah blah. The specified argument will
179
* be passed as-is to the filter routine. The routine pointer may
180
* be NULL, in which case no filter callback will be called. If the
181
* noStore boolean is PR_TRUE, then decoded fields will not be stored
182
* in the destination structure specified when the decoder was
183
* created. This routine returns a PRStatus value; in the event of
184
* an error, it will place an error on the error stack and return
187
* The error may be one of the following values:
188
* NSS_ERROR_INVALID_ASN1DECODER
191
* PR_FAILURE upon error
192
* PR_SUCCESS upon success
196
nssASN1Decoder_SetFilter
198
nssASN1Decoder *decoder,
199
nssASN1DecoderFilterFunction *callback,
204
extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
207
* nssASN1Decoder_GetFilter
209
* If the optional pCallbackOpt argument to this routine is non-null,
210
* then the pointer to any callback function established for this
211
* decoder with nssASN1Decoder_SetFilter will be stored at the
212
* location indicated by it. If the optional pArgumentOpt
213
* pointer is non-null, the filter's closure argument will be stored
214
* there. If the optional pNoStoreOpt pointer is non-null, the
215
* noStore value specified when setting the filter will be stored
216
* there. This routine returns a PRStatus value; in the event of
217
* an error it will place an error on the error stack and return
220
* The error may be one of the following values:
221
* NSS_ERROR_INVALID_ASN1DECODER
224
* PR_FAILURE upon error
225
* PR_SUCCESS upon success
229
nssASN1Decoder_GetFilter
231
nssASN1Decoder *decoder,
232
nssASN1DecoderFilterFunction **pCallbackOpt,
237
extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
240
* nssASN1Decoder_SetNotify
242
* This routine registers a callback notify routine with the decoder,
243
* which will be called whenever.. The specified argument will be
244
* passed as-is to the notify routine. The routine pointer may be
245
* NULL, in which case no notify routine will be called. This routine
246
* returns a PRStatus value; in the event of an error it will place
247
* an error on the error stack and return PR_FAILURE.
249
* The error may be one of the following values:
250
* NSS_ERROR_INVALID_ASN1DECODER
253
* PR_FAILURE upon error
254
* PR_SUCCESS upon success
258
nssASN1Decoder_SetNotify
260
nssASN1Decoder *decoder,
261
nssASN1NotifyFunction *callback,
265
extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
268
* nssASN1Decoder_GetNotify
270
* If the optional pCallbackOpt argument to this routine is non-null,
271
* then the pointer to any callback function established for this
272
* decoder with nssASN1Decoder_SetNotify will be stored at the
273
* location indicated by it. If the optional pArgumentOpt pointer is
274
* non-null, the filter's closure argument will be stored there.
275
* This routine returns a PRStatus value; in the event of an error it
276
* will place an error on the error stack and return PR_FAILURE.
278
* The error may be one of the following values:
279
* NSS_ERROR_INVALID_ASN1DECODER
282
* PR_FAILURE upon error
283
* PR_SUCCESS upon success
287
nssASN1Decoder_GetNotify
289
nssASN1Decoder *decoder,
290
nssASN1NotifyFunction **pCallbackOpt,
294
extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
297
* nssASN1Decoder_verify
299
* This routine is only available in debug builds.
301
* If the specified pointer is a valid pointer to an nssASN1Decoder
302
* object, this routine will return PR_SUCCESS. Otherwise, it will
303
* put an error on the error stack and return PR_FAILURE.
305
* The error may be one of the following values:
306
* NSS_ERROR_INVALID_ASN1DECODER
309
* PR_FAILURE upon error
310
* PR_SUCCESS upon success
315
nssASN1Decoder_verify
317
nssASN1Decoder *decoder
320
extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
326
* This routine will decode the specified data into the specified
327
* destination structure, as specified by the specified template.
328
* This routine returns a PRStatus value; in the event of an error
329
* it will place an error on the error stack and return PR_FAILURE.
331
* The error may be one of the following values:
332
* NSS_ERROR_NO_MEMORY
333
* NSS_ERROR_INVALID_ARENA
334
* NSS_ERROR_INVALID_POINTER
335
* NSS_ERROR_INVALID_BER
338
* PR_FAILURE upon error
339
* PR_SUCCESS upon success
347
const nssASN1Template template[],
352
extern const NSSError NSS_ERROR_NO_MEMORY;
353
extern const NSSError NSS_ERROR_INVALID_ARENA;
354
extern const NSSError NSS_ERROR_INVALID_POINTER;
355
extern const NSSError NSS_ERROR_INVALID_BER;
360
* This routine will decode the data in the specified NSSBER
361
* into the destination structure, as specified by the template.
362
* This routine returns a PRStatus value; in the event of an error
363
* it will place an error on the error stack and return PR_FAILURE.
365
* The error may be one of the following values:
366
* NSS_ERROR_NO_MEMORY
367
* NSS_ERROR_INVALID_ARENA
368
* NSS_ERROR_INVALID_POINTER
369
* NSS_ERROR_INVALID_NSSBER
370
* NSS_ERROR_INVALID_BER
373
* PR_FAILURE upon error
374
* PR_SUCCESS upon success
382
const nssASN1Template template[],
386
extern const NSSError NSS_ERROR_NO_MEMORY;
387
extern const NSSError NSS_ERROR_INVALID_ARENA;
388
extern const NSSError NSS_ERROR_INVALID_POINTER;
389
extern const NSSError NSS_ERROR_INVALID_BER;
394
* ... description here ...
396
* nssASN1Encoder_Create (Factory/Constructor)
397
* nssASN1Encoder_Update
398
* nssASN1Encoder_Finish (Destructor)
399
* nssASN1Encoder_SetNotify
400
* nssASN1Encoder_GetNotify
401
* nssASN1Encoder_SetStreaming
402
* nssASN1Encoder_GetStreaming
403
* nssASN1Encoder_SetTakeFromBuffer
404
* nssASN1Encoder_GetTakeFromBuffer
408
* nssASN1Encoder_verify
410
* Related functions that aren't type methods:
417
* nssASN1Encoder_Create
419
* This routine creates an ASN.1 Encoder, blah blah blah. This
420
* may return NULL upon error, in which case an error will have been
421
* placed on the error stack.
423
* The error may be one of the following values:
424
* NSS_ERROR_NO_MEMORY
425
* NSS_ERROR_INVALID_ARENA
426
* NSS_ERROR_INVALID_POINTER
427
* NSS_ERROR_ENCODING_NOT_SUPPORTED
432
* A pointer to an ASN.1 Encoder upon success
435
NSS_EXTERN nssASN1Encoder *
436
nssASN1Encoder_Create
439
const nssASN1Template template[],
440
NSSASN1EncodingType encoding,
441
nssASN1EncoderWriteFunction *sink,
445
extern const NSSError NSS_ERROR_NO_MEMORY;
446
extern const NSSError NSS_ERROR_INVALID_ARENA;
447
extern const NSSError NSS_ERROR_INVALID_POINTER;
448
extern const NSSError NSS_ERROR_ENCODING_NOT_SUPPORTED;
451
* nssASN1Encoder_Update
453
* The error may be one of the following values:
454
* NSS_ERROR_INVALID_ASN1ENCODER
455
* NSS_ERROR_INVALID_POINTER
458
* PR_FAILURE upon error
459
* PR_SUCCESS upon success
463
nssASN1Encoder_Update
465
nssASN1Encoder *encoder,
470
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
471
extern const NSSError NSS_ERROR_INVALID_POINTER;
474
* nssASN1Encoder_Finish
478
* The error may be one of the following values:
479
* NSS_ERROR_INVALID_ASN1ENCODER
482
* PR_FAILURE upon error
483
* PR_SUCCESS upon success
487
nssASN1Encoder_Finish
489
nssASN1Encoder *encoder
492
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
495
* nssASN1Encoder_SetNotify
497
* This routine registers a callback notify routine with the encoder,
498
* which will be called whenever.. The specified argument will be
499
* passed as-is to the notify routine. The routine pointer may be
500
* NULL, in which case no notify routine will be called. This routine
501
* returns a PRStatus value; in the event of an error it will place
502
* an error on the error stack and return PR_FAILURE.
504
* The error may be one of the following values:
505
* NSS_ERROR_INVALID_ASN1DECODER
508
* PR_FAILURE upon error
509
* PR_SUCCESS upon success
513
nssASN1Encoder_SetNotify
515
nssASN1Encoder *encoder,
516
nssASN1NotifyFunction *callback,
520
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
523
* nssASN1Encoder_GetNotify
525
* If the optional pCallbackOpt argument to this routine is non-null,
526
* then the pointer to any callback function established for this
527
* decoder with nssASN1Encoder_SetNotify will be stored at the
528
* location indicated by it. If the optional pArgumentOpt pointer is
529
* non-null, the filter's closure argument will be stored there.
530
* This routine returns a PRStatus value; in the event of an error it
531
* will place an error on the error stack and return PR_FAILURE.
533
* The error may be one of the following values:
534
* NSS_ERROR_INVALID_ASN1ENCODER
537
* PR_FAILURE upon error
538
* PR_SUCCESS upon success
542
nssASN1Encoder_GetNotify
544
nssASN1Encoder *encoder,
545
nssASN1NotifyFunction **pCallbackOpt,
549
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
552
* nssASN1Encoder_SetStreaming
555
* The error may be one of the following values:
556
* NSS_ERROR_INVALID_ASN1ENCODER
559
* PR_FAILURE upon error
560
* PR_SUCCESS upon success
564
nssASN1Encoder_SetStreaming
566
nssASN1Encoder *encoder,
570
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
573
* nssASN1Encoder_GetStreaming
576
* The error may be one of the following values:
577
* NSS_ERROR_INVALID_ASN1ENCODER
578
* NSS_ERROR_INVALID_POINTER
581
* PR_FAILURE upon error
582
* PR_SUCCESS upon success
586
nssASN1Encoder_GetStreaming
588
nssASN1Encoder *encoder,
592
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
593
extern const NSSError NSS_ERROR_INVALID_POINTER;
596
* nssASN1Encoder_SetTakeFromBuffer
599
* The error may be one of the following values:
600
* NSS_ERROR_INVALID_ASN1ENCODER
603
* PR_FAILURE upon error
604
* PR_SUCCESS upon success
608
nssASN1Encoder_SetTakeFromBuffer
610
nssASN1Encoder *encoder,
611
PRBool takeFromBuffer
614
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
617
* nssASN1Encoder_GetTakeFromBuffer
620
* The error may be one of the following values:
621
* NSS_ERROR_INVALID_ASN1ENCODER
622
* NSS_ERROR_INVALID_POINTER
625
* PR_FAILURE upon error
626
* PR_SUCCESS upon success
630
nssASN1Encoder_GetTakeFromBuffer
632
nssASN1Encoder *encoder,
633
PRBool *pTakeFromBuffer
636
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
637
extern const NSSError NSS_ERROR_INVALID_POINTER;
640
* nssASN1Encoder_verify
642
* This routine is only available in debug builds.
644
* If the specified pointer is a valid pointer to an nssASN1Encoder
645
* object, this routine will return PR_SUCCESS. Otherwise, it will
646
* put an error on the error stack and return PR_FAILURE.
648
* The error may be one of the following values:
649
* NSS_ERROR_INVALID_ASN1ENCODER
652
* PR_FAILURE upon error
653
* PR_SUCCESS upon success
658
nssASN1Encoder_verify
660
nssASN1Encoder *encoder
663
extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
670
* The error may be one of the following values:
671
* NSS_ERROR_NO_MEMORY
672
* NSS_ERROR_INVALID_ARENA
673
* NSS_ERROR_INVALID_POINTER
674
* NSS_ERROR_ENCODING_NOT_SUPPORTED
678
* PR_FAILURE upon error
679
* PR_SUCCESS upon success
686
const nssASN1Template template[],
687
NSSASN1EncodingType encoding,
688
nssASN1EncoderWriteFunction *sink,
692
extern const NSSError NSS_ERROR_NO_MEMORY;
693
extern const NSSError NSS_ERROR_INVALID_ARENA;
694
extern const NSSError NSS_ERROR_INVALID_POINTER;
695
extern const NSSError NSS_ERROR_ENCODING_NOT_SUPPORTED;
700
* There must be a better name. If the optional arena argument is
701
* non-null, it'll be used for the space. If the optional rvOpt is
702
* non-null, it'll be the return value-- if it is null, a new one
705
* The error may be one of the following values:
706
* NSS_ERROR_NO_MEMORY
707
* NSS_ERROR_INVALID_ARENA
708
* NSS_ERROR_INVALID_POINTER
709
* NSS_ERROR_ENCODING_NOT_SUPPORTED
713
* A valid pointer to an NSSDER upon success
722
const nssASN1Template template[],
723
NSSASN1EncodingType encoding
726
extern const NSSError NSS_ERROR_NO_MEMORY;
727
extern const NSSError NSS_ERROR_INVALID_ARENA;
728
extern const NSSError NSS_ERROR_INVALID_POINTER;
729
extern const NSSError NSS_ERROR_ENCODING_NOT_SUPPORTED;
732
* Other basic types' encoding and decoding helper functions:
734
* nssASN1_CreatePRUint32FromBER
735
* nssASN1_GetDERFromPRUint32
736
* nssASN1_CreatePRInt32FromBER
737
* nssASN1_GetDERFromPRInt32
742
* nssASN1_CreatePRUint32FromBER
747
nssASN1_CreatePRUint32FromBER
754
* nssASN1_GetDERFromPRUint32
759
nssASN1_GetDERFromPRUint32
767
* nssASN1_CreatePRInt32FromBER
772
nssASN1_CreatePRInt32FromBER
779
* nssASN1_GetDERFromPRInt32
784
nssASN1_GetDERFromPRInt32
797
* One for each of the simple types, plus a special one for ANY, plus:
798
* - a pointer to each one of those
799
* - a set of each one of those
801
* Note that these are alphabetical (case insensitive); please add new
802
* ones in the appropriate place.
805
extern const nssASN1Template *nssASN1Template_Any;
806
extern const nssASN1Template *nssASN1Template_BitString;
807
extern const nssASN1Template *nssASN1Template_BMPString;
808
extern const nssASN1Template *nssASN1Template_Boolean;
809
extern const nssASN1Template *nssASN1Template_Enumerated;
810
extern const nssASN1Template *nssASN1Template_GeneralizedTime;
811
extern const nssASN1Template *nssASN1Template_IA5String;
812
extern const nssASN1Template *nssASN1Template_Integer;
813
extern const nssASN1Template *nssASN1Template_Null;
814
extern const nssASN1Template *nssASN1Template_ObjectID;
815
extern const nssASN1Template *nssASN1Template_OctetString;
816
extern const nssASN1Template *nssASN1Template_PrintableString;
817
extern const nssASN1Template *nssASN1Template_T61String;
818
extern const nssASN1Template *nssASN1Template_UniversalString;
819
extern const nssASN1Template *nssASN1Template_UTCTime;
820
extern const nssASN1Template *nssASN1Template_UTF8String;
821
extern const nssASN1Template *nssASN1Template_VisibleString;
823
extern const nssASN1Template *nssASN1Template_PointerToAny;
824
extern const nssASN1Template *nssASN1Template_PointerToBitString;
825
extern const nssASN1Template *nssASN1Template_PointerToBMPString;
826
extern const nssASN1Template *nssASN1Template_PointerToBoolean;
827
extern const nssASN1Template *nssASN1Template_PointerToEnumerated;
828
extern const nssASN1Template *nssASN1Template_PointerToGeneralizedTime;
829
extern const nssASN1Template *nssASN1Template_PointerToIA5String;
830
extern const nssASN1Template *nssASN1Template_PointerToInteger;
831
extern const nssASN1Template *nssASN1Template_PointerToNull;
832
extern const nssASN1Template *nssASN1Template_PointerToObjectID;
833
extern const nssASN1Template *nssASN1Template_PointerToOctetString;
834
extern const nssASN1Template *nssASN1Template_PointerToPrintableString;
835
extern const nssASN1Template *nssASN1Template_PointerToT61String;
836
extern const nssASN1Template *nssASN1Template_PointerToUniversalString;
837
extern const nssASN1Template *nssASN1Template_PointerToUTCTime;
838
extern const nssASN1Template *nssASN1Template_PointerToUTF8String;
839
extern const nssASN1Template *nssASN1Template_PointerToVisibleString;
841
extern const nssASN1Template *nssASN1Template_SetOfAny;
842
extern const nssASN1Template *nssASN1Template_SetOfBitString;
843
extern const nssASN1Template *nssASN1Template_SetOfBMPString;
844
extern const nssASN1Template *nssASN1Template_SetOfBoolean;
845
extern const nssASN1Template *nssASN1Template_SetOfEnumerated;
846
extern const nssASN1Template *nssASN1Template_SetOfGeneralizedTime;
847
extern const nssASN1Template *nssASN1Template_SetOfIA5String;
848
extern const nssASN1Template *nssASN1Template_SetOfInteger;
849
extern const nssASN1Template *nssASN1Template_SetOfNull;
850
extern const nssASN1Template *nssASN1Template_SetOfObjectID;
851
extern const nssASN1Template *nssASN1Template_SetOfOctetString;
852
extern const nssASN1Template *nssASN1Template_SetOfPrintableString;
853
extern const nssASN1Template *nssASN1Template_SetOfT61String;
854
extern const nssASN1Template *nssASN1Template_SetOfUniversalString;
855
extern const nssASN1Template *nssASN1Template_SetOfUTCTime;
856
extern const nssASN1Template *nssASN1Template_SetOfUTF8String;
857
extern const nssASN1Template *nssASN1Template_SetOfVisibleString;
864
nssUTF8_CreateFromBER
872
nssUTF8_GetDEREncoding
875
/* Should have an NSSDER *rvOpt */
877
const NSSUTF8 *string