xref: /freebsd/contrib/bearssl/inc/bearssl_pem.h (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #ifndef BR_BEARSSL_PEM_H__
26*0957b409SSimon J. Gerraty #define BR_BEARSSL_PEM_H__
27*0957b409SSimon J. Gerraty 
28*0957b409SSimon J. Gerraty #include <stddef.h>
29*0957b409SSimon J. Gerraty #include <stdint.h>
30*0957b409SSimon J. Gerraty 
31*0957b409SSimon J. Gerraty #ifdef __cplusplus
32*0957b409SSimon J. Gerraty extern "C" {
33*0957b409SSimon J. Gerraty #endif
34*0957b409SSimon J. Gerraty 
35*0957b409SSimon J. Gerraty /** \file bearssl_pem.h
36*0957b409SSimon J. Gerraty  *
37*0957b409SSimon J. Gerraty  * # PEM Support
38*0957b409SSimon J. Gerraty  *
39*0957b409SSimon J. Gerraty  * PEM is a traditional encoding layer use to store binary objects (in
40*0957b409SSimon J. Gerraty  * particular X.509 certificates, and private keys) in text files. While
41*0957b409SSimon J. Gerraty  * the acronym comes from an old, defunct standard ("Privacy Enhanced
42*0957b409SSimon J. Gerraty  * Mail"), the format has been reused, with some variations, by many
43*0957b409SSimon J. Gerraty  * systems, and is a _de facto_ standard, even though it is not, actually,
44*0957b409SSimon J. Gerraty  * specified in all clarity anywhere.
45*0957b409SSimon J. Gerraty  *
46*0957b409SSimon J. Gerraty  * ## Format Details
47*0957b409SSimon J. Gerraty  *
48*0957b409SSimon J. Gerraty  * BearSSL contains a generic, streamed PEM decoder, which handles the
49*0957b409SSimon J. Gerraty  * following format:
50*0957b409SSimon J. Gerraty  *
51*0957b409SSimon J. Gerraty  *   - The input source (a sequence of bytes) is assumed to be the
52*0957b409SSimon J. Gerraty  *     encoding of a text file in an ASCII-compatible charset. This
53*0957b409SSimon J. Gerraty  *     includes ISO-8859-1, Windows-1252, and UTF-8 encodings. Each
54*0957b409SSimon J. Gerraty  *     line ends on a newline character (U+000A LINE FEED). The
55*0957b409SSimon J. Gerraty  *     U+000D CARRIAGE RETURN characters are ignored, so the code
56*0957b409SSimon J. Gerraty  *     accepts both Windows-style and Unix-style line endings.
57*0957b409SSimon J. Gerraty  *
58*0957b409SSimon J. Gerraty  *   - Each object begins with a banner that occurs at the start of
59*0957b409SSimon J. Gerraty  *     a line; the first banner characters are "`-----BEGIN `" (five
60*0957b409SSimon J. Gerraty  *     dashes, the word "BEGIN", and a space). The banner matching is
61*0957b409SSimon J. Gerraty  *     not case-sensitive.
62*0957b409SSimon J. Gerraty  *
63*0957b409SSimon J. Gerraty  *   - The _object name_ consists in the characters that follow the
64*0957b409SSimon J. Gerraty  *     banner start sequence, up to the end of the line, but without
65*0957b409SSimon J. Gerraty  *     trailing dashes (in "normal" PEM, there are five trailing
66*0957b409SSimon J. Gerraty  *     dashes, but this implementation is not picky about these dashes).
67*0957b409SSimon J. Gerraty  *     The BearSSL decoder normalises the name characters to uppercase
68*0957b409SSimon J. Gerraty  *     (for ASCII letters only) and accepts names up to 127 characters.
69*0957b409SSimon J. Gerraty  *
70*0957b409SSimon J. Gerraty  *   - The object ends with a banner that again occurs at the start of
71*0957b409SSimon J. Gerraty  *     a line, and starts with "`-----END `" (again case-insensitive).
72*0957b409SSimon J. Gerraty  *
73*0957b409SSimon J. Gerraty  *   - Between that start and end banner, only Base64 data shall occur.
74*0957b409SSimon J. Gerraty  *     Base64 converts each sequence of three bytes into four
75*0957b409SSimon J. Gerraty  *     characters; the four characters are ASCII letters, digits, "`+`"
76*0957b409SSimon J. Gerraty  *     or "`-`" signs, and one or two "`=`" signs may occur in the last
77*0957b409SSimon J. Gerraty  *     quartet. Whitespace is ignored (whitespace is any ASCII character
78*0957b409SSimon J. Gerraty  *     of code 32 or less, so control characters are whitespace) and
79*0957b409SSimon J. Gerraty  *     lines may have arbitrary length; the only restriction is that the
80*0957b409SSimon J. Gerraty  *     four characters of a quartet must appear on the same line (no
81*0957b409SSimon J. Gerraty  *     line break inside a quartet).
82*0957b409SSimon J. Gerraty  *
83*0957b409SSimon J. Gerraty  *   - A single file may contain more than one PEM object. Bytes that
84*0957b409SSimon J. Gerraty  *     occur between objects are ignored.
85*0957b409SSimon J. Gerraty  *
86*0957b409SSimon J. Gerraty  *
87*0957b409SSimon J. Gerraty  * ## PEM Decoder API
88*0957b409SSimon J. Gerraty  *
89*0957b409SSimon J. Gerraty  * The PEM decoder offers a state-machine API. The caller allocates a
90*0957b409SSimon J. Gerraty  * decoder context, then injects source bytes. Source bytes are pushed
91*0957b409SSimon J. Gerraty  * with `br_pem_decoder_push()`. The decoder stops accepting bytes when
92*0957b409SSimon J. Gerraty  * it reaches an "event", which is either the start of an object, the
93*0957b409SSimon J. Gerraty  * end of an object, or a decoding error within an object.
94*0957b409SSimon J. Gerraty  *
95*0957b409SSimon J. Gerraty  * The `br_pem_decoder_event()` function is used to obtain the current
96*0957b409SSimon J. Gerraty  * event; it also clears it, thus allowing the decoder to accept more
97*0957b409SSimon J. Gerraty  * bytes. When a object start event is raised, the decoder context
98*0957b409SSimon J. Gerraty  * offers the found object name (normalised to ASCII uppercase).
99*0957b409SSimon J. Gerraty  *
100*0957b409SSimon J. Gerraty  * When an object is reached, the caller must set an appropriate callback
101*0957b409SSimon J. Gerraty  * function, which will receive (by chunks) the decoded object data.
102*0957b409SSimon J. Gerraty  *
103*0957b409SSimon J. Gerraty  * Since the decoder context makes no dynamic allocation, it requires
104*0957b409SSimon J. Gerraty  * no explicit deallocation.
105*0957b409SSimon J. Gerraty  */
106*0957b409SSimon J. Gerraty 
107*0957b409SSimon J. Gerraty /**
108*0957b409SSimon J. Gerraty  * \brief PEM decoder context.
109*0957b409SSimon J. Gerraty  *
110*0957b409SSimon J. Gerraty  * Contents are opaque (they should not be accessed directly).
111*0957b409SSimon J. Gerraty  */
112*0957b409SSimon J. Gerraty typedef struct {
113*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
114*0957b409SSimon J. Gerraty 	/* CPU for the T0 virtual machine. */
115*0957b409SSimon J. Gerraty 	struct {
116*0957b409SSimon J. Gerraty 		uint32_t *dp;
117*0957b409SSimon J. Gerraty 		uint32_t *rp;
118*0957b409SSimon J. Gerraty 		const unsigned char *ip;
119*0957b409SSimon J. Gerraty 	} cpu;
120*0957b409SSimon J. Gerraty 	uint32_t dp_stack[32];
121*0957b409SSimon J. Gerraty 	uint32_t rp_stack[32];
122*0957b409SSimon J. Gerraty 	int err;
123*0957b409SSimon J. Gerraty 
124*0957b409SSimon J. Gerraty 	const unsigned char *hbuf;
125*0957b409SSimon J. Gerraty 	size_t hlen;
126*0957b409SSimon J. Gerraty 
127*0957b409SSimon J. Gerraty 	void (*dest)(void *dest_ctx, const void *src, size_t len);
128*0957b409SSimon J. Gerraty 	void *dest_ctx;
129*0957b409SSimon J. Gerraty 
130*0957b409SSimon J. Gerraty 	unsigned char event;
131*0957b409SSimon J. Gerraty 	char name[128];
132*0957b409SSimon J. Gerraty 	unsigned char buf[255];
133*0957b409SSimon J. Gerraty 	size_t ptr;
134*0957b409SSimon J. Gerraty #endif
135*0957b409SSimon J. Gerraty } br_pem_decoder_context;
136*0957b409SSimon J. Gerraty 
137*0957b409SSimon J. Gerraty /**
138*0957b409SSimon J. Gerraty  * \brief Initialise a PEM decoder structure.
139*0957b409SSimon J. Gerraty  *
140*0957b409SSimon J. Gerraty  * \param ctx   decoder context to initialise.
141*0957b409SSimon J. Gerraty  */
142*0957b409SSimon J. Gerraty void br_pem_decoder_init(br_pem_decoder_context *ctx);
143*0957b409SSimon J. Gerraty 
144*0957b409SSimon J. Gerraty /**
145*0957b409SSimon J. Gerraty  * \brief Push some bytes into the decoder.
146*0957b409SSimon J. Gerraty  *
147*0957b409SSimon J. Gerraty  * Returned value is the number of bytes actually consumed; this may be
148*0957b409SSimon J. Gerraty  * less than the number of provided bytes if an event is raised. When an
149*0957b409SSimon J. Gerraty  * event is raised, it must be read (with `br_pem_decoder_event()`);
150*0957b409SSimon J. Gerraty  * until the event is read, this function will return 0.
151*0957b409SSimon J. Gerraty  *
152*0957b409SSimon J. Gerraty  * \param ctx    decoder context.
153*0957b409SSimon J. Gerraty  * \param data   new data bytes.
154*0957b409SSimon J. Gerraty  * \param len    number of new data bytes.
155*0957b409SSimon J. Gerraty  * \return  the number of bytes actually received (may be less than `len`).
156*0957b409SSimon J. Gerraty  */
157*0957b409SSimon J. Gerraty size_t br_pem_decoder_push(br_pem_decoder_context *ctx,
158*0957b409SSimon J. Gerraty 	const void *data, size_t len);
159*0957b409SSimon J. Gerraty 
160*0957b409SSimon J. Gerraty /**
161*0957b409SSimon J. Gerraty  * \brief Set the receiver for decoded data.
162*0957b409SSimon J. Gerraty  *
163*0957b409SSimon J. Gerraty  * When an object is entered, the provided function (with opaque context
164*0957b409SSimon J. Gerraty  * pointer) will be called repeatedly with successive chunks of decoded
165*0957b409SSimon J. Gerraty  * data for that object. If `dest` is set to 0, then decoded data is
166*0957b409SSimon J. Gerraty  * simply ignored. The receiver can be set at any time, but, in practice,
167*0957b409SSimon J. Gerraty  * it should be called immediately after receiving a "start of object"
168*0957b409SSimon J. Gerraty  * event.
169*0957b409SSimon J. Gerraty  *
170*0957b409SSimon J. Gerraty  * \param ctx        decoder context.
171*0957b409SSimon J. Gerraty  * \param dest       callback for receiving decoded data.
172*0957b409SSimon J. Gerraty  * \param dest_ctx   opaque context pointer for the `dest` callback.
173*0957b409SSimon J. Gerraty  */
174*0957b409SSimon J. Gerraty static inline void
br_pem_decoder_setdest(br_pem_decoder_context * ctx,void (* dest)(void * dest_ctx,const void * src,size_t len),void * dest_ctx)175*0957b409SSimon J. Gerraty br_pem_decoder_setdest(br_pem_decoder_context *ctx,
176*0957b409SSimon J. Gerraty 	void (*dest)(void *dest_ctx, const void *src, size_t len),
177*0957b409SSimon J. Gerraty 	void *dest_ctx)
178*0957b409SSimon J. Gerraty {
179*0957b409SSimon J. Gerraty 	ctx->dest = dest;
180*0957b409SSimon J. Gerraty 	ctx->dest_ctx = dest_ctx;
181*0957b409SSimon J. Gerraty }
182*0957b409SSimon J. Gerraty 
183*0957b409SSimon J. Gerraty /**
184*0957b409SSimon J. Gerraty  * \brief Get the last event.
185*0957b409SSimon J. Gerraty  *
186*0957b409SSimon J. Gerraty  * If an event was raised, then this function returns the event value, and
187*0957b409SSimon J. Gerraty  * also clears it, thereby allowing the decoder to proceed. If no event
188*0957b409SSimon J. Gerraty  * was raised since the last call to `br_pem_decoder_event()`, then this
189*0957b409SSimon J. Gerraty  * function returns 0.
190*0957b409SSimon J. Gerraty  *
191*0957b409SSimon J. Gerraty  * \param ctx   decoder context.
192*0957b409SSimon J. Gerraty  * \return  the raised event, or 0.
193*0957b409SSimon J. Gerraty  */
194*0957b409SSimon J. Gerraty int br_pem_decoder_event(br_pem_decoder_context *ctx);
195*0957b409SSimon J. Gerraty 
196*0957b409SSimon J. Gerraty /**
197*0957b409SSimon J. Gerraty  * \brief Event: start of object.
198*0957b409SSimon J. Gerraty  *
199*0957b409SSimon J. Gerraty  * This event is raised when the start of a new object has been detected.
200*0957b409SSimon J. Gerraty  * The object name (normalised to uppercase) can be accessed with
201*0957b409SSimon J. Gerraty  * `br_pem_decoder_name()`.
202*0957b409SSimon J. Gerraty  */
203*0957b409SSimon J. Gerraty #define BR_PEM_BEGIN_OBJ   1
204*0957b409SSimon J. Gerraty 
205*0957b409SSimon J. Gerraty /**
206*0957b409SSimon J. Gerraty  * \brief Event: end of object.
207*0957b409SSimon J. Gerraty  *
208*0957b409SSimon J. Gerraty  * This event is raised when the end of the current object is reached
209*0957b409SSimon J. Gerraty  * (normally, i.e. with no decoding error).
210*0957b409SSimon J. Gerraty  */
211*0957b409SSimon J. Gerraty #define BR_PEM_END_OBJ     2
212*0957b409SSimon J. Gerraty 
213*0957b409SSimon J. Gerraty /**
214*0957b409SSimon J. Gerraty  * \brief Event: decoding error.
215*0957b409SSimon J. Gerraty  *
216*0957b409SSimon J. Gerraty  * This event is raised when decoding fails within an object.
217*0957b409SSimon J. Gerraty  * This formally closes the current object and brings the decoder back
218*0957b409SSimon J. Gerraty  * to the "out of any object" state. The offending line in the source
219*0957b409SSimon J. Gerraty  * is consumed.
220*0957b409SSimon J. Gerraty  */
221*0957b409SSimon J. Gerraty #define BR_PEM_ERROR       3
222*0957b409SSimon J. Gerraty 
223*0957b409SSimon J. Gerraty /**
224*0957b409SSimon J. Gerraty  * \brief Get the name of the encountered object.
225*0957b409SSimon J. Gerraty  *
226*0957b409SSimon J. Gerraty  * The encountered object name is defined only when the "start of object"
227*0957b409SSimon J. Gerraty  * event is raised. That name is normalised to uppercase (for ASCII letters
228*0957b409SSimon J. Gerraty  * only) and does not include trailing dashes.
229*0957b409SSimon J. Gerraty  *
230*0957b409SSimon J. Gerraty  * \param ctx   decoder context.
231*0957b409SSimon J. Gerraty  * \return  the current object name.
232*0957b409SSimon J. Gerraty  */
233*0957b409SSimon J. Gerraty static inline const char *
br_pem_decoder_name(br_pem_decoder_context * ctx)234*0957b409SSimon J. Gerraty br_pem_decoder_name(br_pem_decoder_context *ctx)
235*0957b409SSimon J. Gerraty {
236*0957b409SSimon J. Gerraty 	return ctx->name;
237*0957b409SSimon J. Gerraty }
238*0957b409SSimon J. Gerraty 
239*0957b409SSimon J. Gerraty /**
240*0957b409SSimon J. Gerraty  * \brief Encode an object in PEM.
241*0957b409SSimon J. Gerraty  *
242*0957b409SSimon J. Gerraty  * This function encodes the provided binary object (`data`, of length `len`
243*0957b409SSimon J. Gerraty  * bytes) into PEM. The `banner` text will be included in the header and
244*0957b409SSimon J. Gerraty  * footer (e.g. use `"CERTIFICATE"` to get a `"BEGIN CERTIFICATE"` header).
245*0957b409SSimon J. Gerraty  *
246*0957b409SSimon J. Gerraty  * The length (in characters) of the PEM output is returned; that length
247*0957b409SSimon J. Gerraty  * does NOT include the terminating zero, that this function nevertheless
248*0957b409SSimon J. Gerraty  * adds. If using the returned value for allocation purposes, the allocated
249*0957b409SSimon J. Gerraty  * buffer size MUST be at least one byte larger than the returned size.
250*0957b409SSimon J. Gerraty  *
251*0957b409SSimon J. Gerraty  * If `dest` is `NULL`, then the encoding does not happen; however, the
252*0957b409SSimon J. Gerraty  * length of the encoded object is still computed and returned.
253*0957b409SSimon J. Gerraty  *
254*0957b409SSimon J. Gerraty  * The `data` pointer may be `NULL` only if `len` is zero (when encoding
255*0957b409SSimon J. Gerraty  * an object of length zero, which is not very useful), or when `dest`
256*0957b409SSimon J. Gerraty  * is `NULL` (in that case, source data bytes are ignored).
257*0957b409SSimon J. Gerraty  *
258*0957b409SSimon J. Gerraty  * Some `flags` can be specified to alter the encoding behaviour:
259*0957b409SSimon J. Gerraty  *
260*0957b409SSimon J. Gerraty  *   - If `BR_PEM_LINE64` is set, then line-breaking will occur after
261*0957b409SSimon J. Gerraty  *     every 64 characters of output, instead of the default of 76.
262*0957b409SSimon J. Gerraty  *
263*0957b409SSimon J. Gerraty  *   - If `BR_PEM_CRLF` is set, then end-of-line sequence will use
264*0957b409SSimon J. Gerraty  *     CR+LF instead of a single LF.
265*0957b409SSimon J. Gerraty  *
266*0957b409SSimon J. Gerraty  * The `data` and `dest` buffers may overlap, in which case the source
267*0957b409SSimon J. Gerraty  * binary data is destroyed in the process. Note that the PEM-encoded output
268*0957b409SSimon J. Gerraty  * is always larger than the source binary.
269*0957b409SSimon J. Gerraty  *
270*0957b409SSimon J. Gerraty  * \param dest     the destination buffer (or `NULL`).
271*0957b409SSimon J. Gerraty  * \param data     the source buffer (can be `NULL` in some cases).
272*0957b409SSimon J. Gerraty  * \param len      the source length (in bytes).
273*0957b409SSimon J. Gerraty  * \param banner   the PEM banner expression.
274*0957b409SSimon J. Gerraty  * \param flags    the behavioural flags.
275*0957b409SSimon J. Gerraty  * \return  the PEM object length (in characters), EXCLUDING the final zero.
276*0957b409SSimon J. Gerraty  */
277*0957b409SSimon J. Gerraty size_t br_pem_encode(void *dest, const void *data, size_t len,
278*0957b409SSimon J. Gerraty 	const char *banner, unsigned flags);
279*0957b409SSimon J. Gerraty 
280*0957b409SSimon J. Gerraty /**
281*0957b409SSimon J. Gerraty  * \brief PEM encoding flag: split lines at 64 characters.
282*0957b409SSimon J. Gerraty  */
283*0957b409SSimon J. Gerraty #define BR_PEM_LINE64   0x0001
284*0957b409SSimon J. Gerraty 
285*0957b409SSimon J. Gerraty /**
286*0957b409SSimon J. Gerraty  * \brief PEM encoding flag: use CR+LF line endings.
287*0957b409SSimon J. Gerraty  */
288*0957b409SSimon J. Gerraty #define BR_PEM_CRLF     0x0002
289*0957b409SSimon J. Gerraty 
290*0957b409SSimon J. Gerraty #ifdef __cplusplus
291*0957b409SSimon J. Gerraty }
292*0957b409SSimon J. Gerraty #endif
293*0957b409SSimon J. Gerraty 
294*0957b409SSimon J. Gerraty #endif
295