xref: /illumos-gate/usr/src/lib/libkmf/ber_der/inc/ber_der.h (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
1 /*
2  * Copyright 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 /*
28  * This is the header file for some Basic Encoding Rules and Distinguished
29  * Encoding Rules (BER/DER) routines.
30  */
31 
32 #ifndef BER_DER_H
33 #define	BER_DER_H
34 
35 #include <kmfapi.h>
36 
37 #define	BER_BOOLEAN			1
38 #define	BER_INTEGER			2
39 #define	BER_BIT_STRING			3
40 #define	BER_OCTET_STRING		4
41 #define	BER_NULL			5
42 #define	BER_OBJECT_IDENTIFIER		6
43 #define	BER_ENUMERATED			10
44 #define	BER_UTF8_STRING			12
45 #define	BER_SEQUENCE			16
46 #define	BER_SET				17
47 #define	BER_PRINTABLE_STRING		19
48 #define	BER_T61STRING			20
49 #define	BER_IA5STRING			22
50 #define	BER_UTCTIME			23
51 #define	BER_GENTIME			24
52 #define	BER_UNIVERSAL_STRING		28
53 #define	BER_BMP_STRING			30
54 
55 #define	BER_CLASS_MASK			0xc0
56 #define	BER_CLASS_UNIVERSAL		0x00
57 #define	BER_CLASS_APPLICATION		0x40
58 #define	BER_CLASS_CONTEXTSPECIFIC	0x80
59 #define	BER_CLASS_PRIVATE		0xc0
60 #define	BER_CONSTRUCTED			0x20
61 #define	BER_CONSTRUCTED_SEQUENCE	(BER_CONSTRUCTED | BER_SEQUENCE)
62 #define	BER_CONSTRUCTED_SET		(BER_CONSTRUCTED | BER_SET)
63 
64 #define	KMFBER_BIG_TAG_MASK		0x1f
65 #define	KMFBER_MORE_TAG_MASK		0x80
66 
67 #define	KMFBER_DEFAULT		0xFFFFFFFF
68 #define	KMFBER_ERROR		0xFFFFFFFF
69 #define	KMFBER_END_OF_SEQORSET	0xfffffffe
70 
71 /* BerElement set/get options */
72 #define	KMFBER_OPT_REMAINING_BYTES	0x01
73 #define	KMFBER_OPT_TOTAL_BYTES		0x02
74 #define	KMFBER_OPT_USE_DER		0x04
75 #define	KMFBER_OPT_TRANSLATE_STRINGS	0x08
76 #define	KMFBER_OPT_BYTES_TO_WRITE	0x10
77 #define	KMFBER_OPT_DEBUG_LEVEL		0x40
78 
79 typedef size_t		ber_len_t;  /* for BER len */
80 typedef long		ber_slen_t; /* signed equivalent of ber_len_t */
81 typedef int32_t		ber_tag_t;  /* for BER tags */
82 typedef int32_t		ber_int_t;  /* for BER ints, enums, and Booleans */
83 typedef uint32_t	ber_uint_t; /* unsigned equivalent of ber_int_t */
84 
85 typedef struct berelement BerElement;
86 typedef int (*BERTranslateProc)(char **, ber_uint_t *, int);
87 
88 typedef struct berval {
89 	ber_len_t	bv_len;
90 	char		*bv_val;
91 } BerValue;
92 
93 #define	SAFEMEMCPY(d, s, n)	memmove(d, s, n)
94 
95 BerElement *kmfder_init(const struct berval *bv);
96 BerElement *kmfber_init(const struct berval *bv);
97 int kmfber_calc_taglen(ber_tag_t);
98 int kmfber_calc_lenlen(ber_int_t);
99 int kmfber_put_len(BerElement *, ber_int_t, int);
100 
101 /*
102  * public decode routines
103  */
104 ber_tag_t kmfber_first_element(BerElement *, ber_len_t *, char **);
105 ber_tag_t kmfber_next_element(BerElement *, ber_len_t *, char *);
106 ber_tag_t kmfber_scanf(BerElement *, const char *, ...);
107 
108 void kmfber_bvfree(struct berval *);
109 void kmfber_bvecfree(struct berval **);
110 struct berval *kmfber_bvdup(const struct berval *);
111 
112 /*
113  * public encoding routines
114  */
115 extern int kmfber_printf(BerElement *, const char *, ...);
116 extern int kmfber_flatten(BerElement *, struct berval **);
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