xref: /illumos-gate/usr/src/lib/libkmf/ber_der/inc/ber_der.h (revision e86372a01d2d16a5dd4a64e144ed978ba17fe7dd)
1 /*
2  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * The contents of this file are subject to the Netscape Public License
8  * Version 1.0(the "NPL"); you may not use this file except in
9  * compliance with the NPL.  You may obtain a copy of the NPL at
10  * http:/ /www.mozilla.org/NPL/
11  *
12  * Software distributed under the NPL is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
14  * for the specific language governing rights and limitations under the
15  * NPL.
16  *
17  * The Initial Developer of this code under the NPL is Netscape
18  * Communications Corporation.  Portions created by Netscape are
19  * Copyright(C) 1998 Netscape Communications Corporation.  All Rights
20  * Reserved.
21  */
22 
23 /*
24  * This is the header file for some Basic Encoding Rules and Distinguished
25  * Encoding Rules (BER/DER) routines.
26  */
27 
28 #ifndef BER_DER_H
29 #define	BER_DER_H
30 
31 #include <kmfapi.h>
32 
33 #define	BER_BOOLEAN			1
34 #define	BER_INTEGER			2
35 #define	BER_BIT_STRING			3
36 #define	BER_OCTET_STRING		4
37 #define	BER_NULL			5
38 #define	BER_OBJECT_IDENTIFIER		6
39 #define	BER_ENUMERATED			10
40 #define	BER_UTF8_STRING			12
41 #define	BER_SEQUENCE			16
42 #define	BER_SET				17
43 #define	BER_PRINTABLE_STRING		19
44 #define	BER_T61STRING			20
45 #define	BER_IA5STRING			22
46 #define	BER_UTCTIME			23
47 #define	BER_GENTIME			24
48 #define	BER_GENERALSTRING		27
49 #define	BER_UNIVERSAL_STRING		28
50 #define	BER_BMP_STRING			30
51 
52 #define	BER_CLASS_MASK			0xc0
53 #define	BER_CLASS_UNIVERSAL		0x00
54 #define	BER_CLASS_APPLICATION		0x40
55 #define	BER_CLASS_CONTEXTSPECIFIC	0x80
56 #define	BER_CLASS_PRIVATE		0xc0
57 #define	BER_CONSTRUCTED			0x20
58 #define	BER_CONSTRUCTED_SEQUENCE	(BER_CONSTRUCTED | BER_SEQUENCE)
59 #define	BER_CONSTRUCTED_SET		(BER_CONSTRUCTED | BER_SET)
60 
61 #define	KMFBER_BIG_TAG_MASK		0x1f
62 #define	KMFBER_MORE_TAG_MASK		0x80
63 
64 #define	KMFBER_DEFAULT		0xFFFFFFFF
65 #define	KMFBER_ERROR		0xFFFFFFFF
66 #define	KMFBER_END_OF_SEQORSET	0xfffffffe
67 
68 /* BerElement set/get options */
69 #define	KMFBER_OPT_REMAINING_BYTES	0x01
70 #define	KMFBER_OPT_TOTAL_BYTES		0x02
71 #define	KMFBER_OPT_USE_DER		0x04
72 #define	KMFBER_OPT_TRANSLATE_STRINGS	0x08
73 #define	KMFBER_OPT_BYTES_TO_WRITE	0x10
74 #define	KMFBER_OPT_DEBUG_LEVEL		0x40
75 
76 typedef size_t		ber_len_t;  /* for BER len */
77 typedef long		ber_slen_t; /* signed equivalent of ber_len_t */
78 typedef int32_t		ber_tag_t;  /* for BER tags */
79 typedef int32_t		ber_int_t;  /* for BER ints, enums, and Booleans */
80 typedef uint32_t	ber_uint_t; /* unsigned equivalent of ber_int_t */
81 
82 typedef struct berelement BerElement;
83 typedef int (*BERTranslateProc)(char **, ber_uint_t *, int);
84 
85 typedef struct berval {
86 	ber_len_t	bv_len;
87 	char		*bv_val;
88 } BerValue;
89 
90 #define	SAFEMEMCPY(d, s, n)	memmove(d, s, n)
91 
92 BerElement *kmfder_init(const struct berval *bv);
93 BerElement *kmfber_init(const struct berval *bv);
94 int kmfber_calc_taglen(ber_tag_t);
95 int kmfber_calc_lenlen(ber_int_t);
96 int kmfber_put_len(BerElement *, ber_int_t, int);
97 
98 /*
99  * public decode routines
100  */
101 ber_tag_t kmfber_first_element(BerElement *, ber_len_t *, char **);
102 ber_tag_t kmfber_next_element(BerElement *, ber_len_t *, char *);
103 ber_tag_t kmfber_scanf(BerElement *, const char *, ...);
104 
105 void kmfber_bvfree(struct berval *);
106 void kmfber_bvecfree(struct berval **);
107 struct berval *kmfber_bvdup(const struct berval *);
108 
109 /*
110  * public encoding routines
111  */
112 extern int kmfber_printf(BerElement *, const char *, ...);
113 extern int kmfber_flatten(BerElement *, struct berval **);
114 extern int kmfber_realloc(BerElement *, ber_len_t);
115 
116 /*
117  * miscellaneous public routines
118  */
119 extern void kmfber_free(BerElement *ber, int freebuf);
120 extern BerElement* kmfber_alloc(void);
121 extern BerElement* kmfder_alloc(void);
122 extern BerElement* kmfber_alloc_t(int);
123 extern BerElement* kmfber_dup(BerElement *);
124 extern ber_int_t kmfber_read(BerElement *, char *, ber_len_t);
125 extern ber_int_t kmfber_write(BerElement *, char *, ber_len_t, int);
126 extern void kmfber_reset(BerElement *, int);
127 
128 /* Routines KMF uses to encode/decode Cert objects */
129 extern KMF_RETURN DerDecodeSignedCertificate(const KMF_DATA *,
130 	KMF_X509_CERTIFICATE **);
131 extern KMF_RETURN DerEncodeSignedCertificate(KMF_X509_CERTIFICATE *,
132 	KMF_DATA *);
133 
134 KMF_RETURN DerDecodeTbsCertificate(const KMF_DATA *,
135 	KMF_X509_TBS_CERT **);
136 KMF_RETURN DerEncodeTbsCertificate(KMF_X509_TBS_CERT *, KMF_DATA *);
137 
138 KMF_RETURN DerDecodeSignedCsr(const KMF_DATA *, KMF_CSR_DATA **);
139 extern KMF_RETURN DerEncodeSignedCsr(KMF_CSR_DATA *, KMF_DATA *);
140 extern KMF_RETURN DerDecodeTbsCsr(const KMF_DATA *, KMF_TBS_CSR **);
141 extern KMF_RETURN DerEncodeTbsCsr(KMF_TBS_CSR *, KMF_DATA *);
142 
143 KMF_RETURN ExtractX509CertParts(KMF_DATA *, KMF_DATA *, KMF_DATA *);
144 extern KMF_RETURN DerEncodeName(KMF_X509_NAME *, KMF_DATA *);
145 KMF_RETURN DerDecodeName(KMF_DATA *, KMF_X509_NAME *);
146 KMF_RETURN DerDecodeExtension(KMF_DATA *, KMF_X509_EXTENSION **);
147 KMF_RETURN CopyRDN(KMF_X509_NAME *, KMF_X509_NAME **);
148 KMF_RETURN CopySPKI(KMF_X509_SPKI *,
149 		KMF_X509_SPKI **);
150 extern KMF_RETURN DerDecodeSPKI(KMF_DATA *, KMF_X509_SPKI *);
151 extern KMF_RETURN DerDecodeDSASignature(KMF_DATA *, KMF_DATA *);
152 extern KMF_RETURN DerEncodeDSASignature(KMF_DATA *, KMF_DATA *);
153 extern KMF_RETURN DerEncodeECDSASignature(KMF_DATA *, KMF_DATA *);
154 extern KMF_RETURN DerDecodeECDSASignature(KMF_DATA *, KMF_DATA *);
155 KMF_RETURN DerEncodeAlgoid(KMF_DATA *, KMF_DATA *);
156 KMF_RETURN DerDecodeSPKI(KMF_DATA *, KMF_X509_SPKI *);
157 KMF_RETURN DerEncodeSPKI(KMF_X509_SPKI *, KMF_DATA *);
158 extern KMF_RETURN ExtractSPKIData(const KMF_X509_SPKI *,
159 	KMF_ALGORITHM_INDEX, KMF_DATA *, uint32_t *);
160 extern KMF_RETURN AddRDN(KMF_X509_NAME *, KMF_X509_RDN *);
161 KMF_RETURN DerEncodeRSAPrivateKey(KMF_DATA *, KMF_RAW_RSA_KEY *);
162 KMF_RETURN DerEncodeDSAPrivateKey(KMF_DATA *, KMF_RAW_DSA_KEY *);
163 KMF_RETURN DerEncodeECPrivateKey(KMF_DATA *, KMF_RAW_EC_KEY *);
164 
165 #endif /* BER_DER_H */
166