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