xref: /illumos-gate/usr/src/lib/libkmf/ber_der/inc/ber_der.h (revision d51f1d338914fe15108ef3fb04d422a459cfdeda)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * File: BER_DER.H
30  *
31  * Copyright (c) 1995-2000 Intel Corporation. All rights reserved.
32  *
33  * Portions:
34  * (C) COPYRIGHT International Business Machines Corp. 1996, 1997
35  * All Rights Reserved
36  *
37  * US Government Users Restricted Rights - Use, duplication or
38  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
39  */
40 
41 /*
42  * This is the header file for some Basic Encoding Rules and Distinguished
43  * Encoding Rules (BER/DER) routines.
44  */
45 
46 #ifndef BER_DER_H
47 #define	BER_DER_H
48 
49 #include <kmfapi.h>
50 
51 #define	BER_BOOLEAN			1
52 #define	BER_INTEGER			2
53 #define	BER_BIT_STRING			3
54 #define	BER_OCTET_STRING		4
55 #define	BER_NULL			5
56 #define	BER_OBJECT_IDENTIFIER		6
57 #define	BER_ENUMERATED			10
58 #define	BER_UTF8_STRING			12
59 #define	BER_SEQUENCE			16
60 #define	BER_SET				17
61 #define	BER_PRINTABLE_STRING		19
62 #define	BER_T61STRING			20
63 #define	BER_IA5STRING			22
64 #define	BER_UTCTIME			23
65 #define	BER_GENTIME			24
66 #define	BER_UNIVERSAL_STRING		28
67 #define	BER_BMP_STRING			30
68 
69 #define	BER_CLASS_MASK			0xc0
70 #define	BER_CLASS_UNIVERSAL		0x00
71 #define	BER_CLASS_APPLICATION		0x40
72 #define	BER_CLASS_CONTEXTSPECIFIC	0x80
73 #define	BER_CLASS_PRIVATE		0xc0
74 #define	BER_CONSTRUCTED			0x20
75 #define	BER_CONSTRUCTED_SEQUENCE	(BER_CONSTRUCTED | BER_SEQUENCE)
76 #define	BER_CONSTRUCTED_SET		(BER_CONSTRUCTED | BER_SET)
77 
78 #define	KMFBER_BIG_TAG_MASK		0x1f
79 #define	KMFBER_MORE_TAG_MASK		0x80
80 
81 #define	KMFBER_DEFAULT		0xFFFFFFFF
82 #define	KMFBER_ERROR		0xFFFFFFFF
83 #define	KMFBER_END_OF_SEQORSET	0xfffffffe
84 
85 /* BerElement set/get options */
86 #define	KMFBER_OPT_REMAINING_BYTES	0x01
87 #define	KMFBER_OPT_TOTAL_BYTES		0x02
88 #define	KMFBER_OPT_USE_DER		0x04
89 #define	KMFBER_OPT_TRANSLATE_STRINGS	0x08
90 #define	KMFBER_OPT_BYTES_TO_WRITE	0x10
91 #define	KMFBER_OPT_DEBUG_LEVEL		0x40
92 
93 typedef size_t		ber_len_t;  /* for BER len */
94 typedef long		ber_slen_t; /* signed equivalent of ber_len_t */
95 typedef int32_t		ber_tag_t;  /* for BER tags */
96 typedef int32_t		ber_int_t;  /* for BER ints, enums, and Booleans */
97 typedef uint32_t	ber_uint_t; /* unsigned equivalent of ber_int_t */
98 
99 typedef struct berelement BerElement;
100 typedef int (*BERTranslateProc)(char **, ber_uint_t *, int);
101 
102 typedef struct berval {
103 	ber_len_t	bv_len;
104 	char		*bv_val;
105 } BerValue;
106 
107 #define	SAFEMEMCPY(d, s, n)	memmove(d, s, n)
108 
109 BerElement *kmfder_init(const struct berval *bv);
110 BerElement *kmfber_init(const struct berval *bv);
111 int kmfber_calc_taglen(ber_tag_t);
112 int kmfber_calc_lenlen(ber_int_t);
113 int kmfber_put_len(BerElement *, ber_int_t, int);
114 
115 /*
116  * public decode routines
117  */
118 ber_tag_t kmfber_first_element(BerElement *, ber_len_t *, char **);
119 ber_tag_t kmfber_next_element(BerElement *, ber_len_t *, char *);
120 ber_tag_t kmfber_scanf(BerElement *, const char *, ...);
121 
122 void kmfber_bvfree(struct berval *);
123 void kmfber_bvecfree(struct berval **);
124 struct berval *kmfber_bvdup(const struct berval *);
125 
126 /*
127  * public encoding routines
128  */
129 extern int kmfber_printf(BerElement *, const char *, ...);
130 extern int kmfber_flatten(BerElement *, struct berval **);
131 
132 /*
133  * miscellaneous public routines
134  */
135 extern void kmfber_free(BerElement *ber, int freebuf);
136 extern BerElement* kmfber_alloc(void);
137 extern BerElement* kmfder_alloc(void);
138 extern BerElement* kmfber_alloc_t(int);
139 extern BerElement* kmfber_dup(BerElement *);
140 extern ber_int_t kmfber_read(BerElement *, char *, ber_len_t);
141 extern ber_int_t kmfber_write(BerElement *, char *, ber_len_t, int);
142 extern void kmfber_reset(BerElement *, int);
143 
144 /* Routines KMF uses to encode/decode Cert objects */
145 extern KMF_RETURN DerDecodeSignedCertificate(const KMF_DATA *,
146 	KMF_X509_CERTIFICATE **);
147 extern KMF_RETURN DerEncodeSignedCertificate(KMF_X509_CERTIFICATE *,
148 	KMF_DATA *);
149 
150 KMF_RETURN DerDecodeTbsCertificate(const KMF_DATA *,
151 	KMF_X509_TBS_CERT **);
152 KMF_RETURN DerEncodeTbsCertificate(KMF_X509_TBS_CERT *, KMF_DATA *);
153 
154 KMF_RETURN DerDecodeSignedCsr(const KMF_DATA *, KMF_CSR_DATA **);
155 extern KMF_RETURN DerEncodeSignedCsr(KMF_CSR_DATA *, KMF_DATA *);
156 extern KMF_RETURN DerDecodeTbsCsr(const KMF_DATA *, KMF_TBS_CSR **);
157 extern KMF_RETURN DerEncodeTbsCsr(KMF_TBS_CSR *, KMF_DATA *);
158 
159 KMF_RETURN ExtractX509CertParts(KMF_DATA *, KMF_DATA *, KMF_DATA *);
160 KMF_RETURN GetKeyFromSpki(KMF_ALGORITHM_INDEX, KMF_X509_SPKI *,
161 	KMF_DATA **);
162 extern KMF_RETURN DerEncodeName(KMF_X509_NAME *, KMF_DATA *);
163 KMF_RETURN DerDecodeName(KMF_DATA *, KMF_X509_NAME *);
164 KMF_RETURN DerDecodeExtension(KMF_DATA *, KMF_X509_EXTENSION **);
165 KMF_RETURN CopyRDN(KMF_X509_NAME *, KMF_X509_NAME **);
166 KMF_RETURN CopySPKI(KMF_X509_SPKI *,
167 		KMF_X509_SPKI **);
168 extern KMF_RETURN DerDecodeSPKI(KMF_DATA *, KMF_X509_SPKI *);
169 extern KMF_RETURN DerDecodeDSASignature(KMF_DATA *, KMF_DATA *);
170 extern KMF_RETURN DerEncodeDSASignature(KMF_DATA *, KMF_DATA *);
171 KMF_RETURN DerEncodeAlgoid(KMF_DATA *, KMF_DATA *);
172 KMF_RETURN DerDecodeSPKI(KMF_DATA *, KMF_X509_SPKI *);
173 KMF_RETURN DerEncodeSPKI(KMF_X509_SPKI *, KMF_DATA *);
174 extern KMF_RETURN ExtractSPKIData(const KMF_X509_SPKI *,
175 	KMF_ALGORITHM_INDEX, KMF_DATA *, uint32_t *);
176 extern KMF_RETURN AddRDN(KMF_X509_NAME *, KMF_X509_RDN *);
177 KMF_RETURN DerEncodeRSAPrivateKey(KMF_DATA *, KMF_RAW_RSA_KEY *);
178 KMF_RETURN DerEncodeDSAPrivateKey(KMF_DATA *, KMF_RAW_DSA_KEY *);
179 
180 #endif /* BER_DER_H */
181