1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #ifndef _PEM_ENCODE_H
7 #define	_PEM_ENCODE_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*
14  * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
15  * All rights reserved.
16  *
17  * This package is an SSL implementation written
18  * by Eric Young (eay@cryptsoft.com).
19  * The implementation was written so as to conform with Netscapes SSL.
20  *
21  * This library is free for commercial and non-commercial use as long as
22  * the following conditions are aheared to.  The following conditions
23  * apply to all code found in this distribution, be it the RC4, RSA,
24  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
25  * included with this distribution is covered by the same copyright terms
26  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
27  *
28  * Copyright remains Eric Young's, and as such any Copyright notices in
29  * the code are not to be removed.
30  * If this package is used in a product, Eric Young should be given attribution
31  * as the author of the parts of the library used.
32  * This can be in the form of a textual message at program startup or
33  * in documentation (online or textual) provided with the package.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *    "This product includes cryptographic software written by
46  *     Eric Young (eay@cryptsoft.com)"
47  *    The word 'cryptographic' can be left out if the rouines from the library
48  *    being used are not cryptographic related :-).
49  * 4. If you include any Windows specific code (or a derivative thereof) from
50  *    the apps directory (application code) you must include an acknowledgement:
51  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
52  *
53  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  * The licence and distribution terms for any publically available version or
66  * derivative of this code cannot be changed.  i.e. this code cannot simply be
67  * copied and put under another distribution licence
68  * [including the GNU Public Licence.]
69  */
70 #define	PEM_STRING_X509		"CERTIFICATE"
71 #define	PEM_STRING_X509_REQ	"CERTIFICATE REQUEST"
72 #define	PEM_STRING_X509_CRL	"X509 CRL"
73 #define	PEM_BUFSIZE		1024
74 
75 /*
76  * 0xF0 is a EOLN
77  * 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing).
78  * 0xF2 is EOF
79  * 0xE0 is ignore at start of line.
80  * 0xFF is error
81  */
82 
83 #define	B64_EOLN		0xF0
84 #define	B64_CR			0xF1
85 #define	B64_EOF			0xF2
86 #define	B64_WS			0xE0
87 #define	B64_ERROR		0xFF
88 #define	B64_NOT_BASE64(a)	(((a)|0x13) == 0xF3)
89 
90 typedef struct pem_encode_ctx_st
91 {
92 	int num;	/* number saved in a partial encode/decode */
93 	/*
94 	 * The length is either the output line length
95 	 * (in input bytes) or the shortest input line
96 	 * length that is ok.  Once decoding begins,
97 	 * the length is adjusted up each time a longer
98 	 * line is decoded.
99 	 */
100 	int length;
101 	unsigned char enc_data[80];	/* data to encode */
102 	int line_num;	/* number read on current line */
103 	int expect_nl;
104 } PEM_ENCODE_CTX;
105 
106 KMF_RETURN
107 Der2Pem(KMF_OBJECT_TYPE, unsigned char *, int, unsigned char **, int *);
108 
109 KMF_RETURN
110 Pem2Der(unsigned char *, int, unsigned char **, int *);
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 #endif /* _PEM_ENCODE_H */
116