Debian Codegraph @

crt/aws-c-io/source/pem.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/encoding.h>
6 #include <aws/common/string.h>
7 #include <aws/io/pem.h>
8 #include <aws/io/private/pem_utils.h>
9
10 #include <aws/io/logging.h>
11
12 enum aws_pem_parse_state { BEGIN, ON_DATA };
13
14 static const struct aws_byte_cursor begin_header = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("-----BEGIN");
15 static const struct aws_byte_cursor end_header = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("-----END");
16 static const struct aws_byte_cursor dashes = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("-----");
17
18 int aws_sanitize_pem(struct aws_byte_buf *pem, struct aws_allocator *allocator) {
19 if (!pem->len) {
20 /* reject files with no PEM data */
21 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
22 }
23 struct aws_byte_buf clean_pem_buf;
24 if (aws_byte_buf_init(&clean_pem_buf, allocator, pem->len)) {
25 return AWS_OP_ERR;
26 }
27 struct aws_byte_cursor pem_cursor = aws_byte_cursor_from_buf(pem);
28 bool is_on_data = false;
29
30 while (pem_cursor.len > 0) {
31 if (!is_on_data) {
32 if (aws_byte_cursor_starts_with(&pem_cursor, &begin_header)) {
33 /* mini optimization - just copy over begin to avoid all the starts with checks */
34 aws_byte_buf_append_dynamic(&clean_pem_buf, &begin_header);
35 aws_byte_cursor_advance(&pem_cursor, begin_header.len);
36 is_on_data = true;
37 } else {
38 aws_byte_cursor_advance(&pem_cursor, 1);
39 }
40 } else {
41 /* Note this does not validate that end label is same as begin label. */
42 if (aws_byte_cursor_starts_with(&pem_cursor, &end_header)) {
43 /* copy over end */
44 aws_byte_buf_append_dynamic(&clean_pem_buf, &end_header);
45 aws_byte_cursor_advance(&pem_cursor, end_header.len);
46
47 /* copy over label until the closing 5 dashes */
48 while (pem_cursor.len > 0) {
49 aws_byte_buf_append_byte_dynamic(&clean_pem_buf, *pem_cursor.ptr);
50 aws_byte_cursor_advance(&pem_cursor, 1);
51
52 if (aws_byte_cursor_starts_with(&pem_cursor, &dashes)) {
53 aws_byte_buf_append_dynamic(&clean_pem_buf, &dashes);
54 aws_byte_cursor_advance(&pem_cursor, dashes.len);
55 aws_byte_buf_append_byte_dynamic(&clean_pem_buf, (uint8_t)'\n');
56 is_on_data = false;
57 break;
58 }
59 }
60 } else {
61 aws_byte_buf_append_byte_dynamic(&clean_pem_buf, *pem_cursor.ptr);
62 aws_byte_cursor_advance(&pem_cursor, 1);
63 }
64 }
65 }
66
67 if (clean_pem_buf.len == 0 || is_on_data) {
68 /* No valid data remains after sanitization or data block is left hanging.
69 File might have been the wrong format */
70 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
71 goto error;
72 }
73
74 struct aws_byte_cursor clean_pem_cursor = aws_byte_cursor_from_buf(&clean_pem_buf);
75 aws_byte_buf_reset(pem, true);
76 aws_byte_buf_append_dynamic(pem, &clean_pem_cursor);
77 aws_byte_buf_clean_up(&clean_pem_buf);
78 return AWS_OP_SUCCESS;
79
80 error:
81 aws_byte_buf_clean_up(&clean_pem_buf);
82 return AWS_OP_ERR;
83 }
84
85 /*
86 * Possible PEM object types. openssl/pem.h used as a source of truth for
87 * possible types.
88 */
89 static struct aws_byte_cursor s_pem_type_x509_old_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("X509 CERTIFICATE");
90 static struct aws_byte_cursor s_pem_type_x509_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CERTIFICATE");
91 static struct aws_byte_cursor s_pem_type_x509_trusted_cur =
92 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TRUSTED CERTIFICATE");
93 static struct aws_byte_cursor s_pem_type_x509_req_old_cur =
94 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("NEW CERTIFICATE REQUEST");
95 static struct aws_byte_cursor s_pem_type_x509_req_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CERTIFICATE REQUEST");
96 static struct aws_byte_cursor s_pem_type_x509_crl_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("X509 CRL");
97 static struct aws_byte_cursor s_pem_type_evp_pkey_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ANY PRIVATE KEY");
98 static struct aws_byte_cursor s_pem_type_public_key_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PUBLIC KEY");
99 static struct aws_byte_cursor s_pem_type_private_rsa_pkcs1_cur =
100 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("RSA PRIVATE KEY");
101 static struct aws_byte_cursor s_pem_type_public_rsa_pkcs1_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("RSA PUBLIC KEY");
102 static struct aws_byte_cursor s_pem_type_private_dsa_pkcs1_cur =
103 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("DSA PRIVATE KEY");
104 static struct aws_byte_cursor s_pem_type_public_dsa_pkcs1_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("DSA PUBLIC KEY");
105 static struct aws_byte_cursor s_pem_type_pkcs7_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PKCS7");
106 static struct aws_byte_cursor s_pem_type_pkcs7_signed_data_cur =
107 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PKCS #7 SIGNED DATA");
108 static struct aws_byte_cursor s_pem_type_private_pkcs8_encrypted_cur =
109 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ENCRYPTED PRIVATE KEY");
110 static struct aws_byte_cursor s_pem_type_private_pkcs8_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PRIVATE KEY");
111 static struct aws_byte_cursor s_pem_type_dh_parameters_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("DH PARAMETERS");
112 static struct aws_byte_cursor s_pem_type_dh_parameters_x942_cur =
113 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("X9.42 DH PARAMETERS");
114 static struct aws_byte_cursor s_pem_type_ssl_session_parameters_cur =
115 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("SSL SESSION PARAMETERS");
116 static struct aws_byte_cursor s_pem_type_dsa_parameters_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("DSA PARAMETERS");
117 static struct aws_byte_cursor s_pem_type_ecdsa_public_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ECDSA PUBLIC KEY");
118 static struct aws_byte_cursor s_pem_type_ec_parameters_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("EC PARAMETERS");
119 static struct aws_byte_cursor s_pem_type_ec_private_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("EC PRIVATE KEY");
120 static struct aws_byte_cursor s_pem_type_parameters_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PARAMETERS");
121 static struct aws_byte_cursor s_pem_type_cms_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CMS");
122 static struct aws_byte_cursor s_pem_type_sm2_parameters_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("SM2 PARAMETERS");
123
124 void aws_pem_objects_clean_up(struct aws_array_list *pem_objects) {
125 for (size_t i = 0; i < aws_array_list_length(pem_objects); ++i) {
126 struct aws_pem_object *pem_obj_ptr = NULL;
127 aws_array_list_get_at_ptr(pem_objects, (void **)&pem_obj_ptr, i);
128
129 if (pem_obj_ptr != NULL) {
130 aws_byte_buf_clean_up_secure(&pem_obj_ptr->data);
131 aws_string_destroy(pem_obj_ptr->type_string);
132 }
133 }
134
135 aws_array_list_clear(pem_objects);
136 aws_array_list_clean_up(pem_objects);
137 }
138
139 enum aws_pem_object_type s_map_type_cur_to_type(struct aws_byte_cursor type_cur) {
140 /*
141 * Putting all those in a hash table might be a bit faster depending on
142 * hashing function cost, but it complicates code considerably for a
143 * potential small gain. PEM parsing is already slow due to multiple
144 * allocations and should not be used in perf critical places.
145 * So choosing dumb and easy approach over something more complicated and we
146 * can reevaluate decision in the future.
147 */
148 if (aws_byte_cursor_eq(&type_cur, &s_pem_type_x509_old_cur)) {
149 return AWS_PEM_TYPE_X509_OLD;
150 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_x509_cur)) {
151 return AWS_PEM_TYPE_X509;
152 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_x509_trusted_cur)) {
153 return AWS_PEM_TYPE_X509_TRUSTED;
154 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_x509_req_old_cur)) {
155 return AWS_PEM_TYPE_X509_REQ_OLD;
156 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_x509_req_cur)) {
157 return AWS_PEM_TYPE_X509_REQ;
158 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_x509_crl_cur)) {
159 return AWS_PEM_TYPE_X509_CRL;
160 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_evp_pkey_cur)) {
161 return AWS_PEM_TYPE_EVP_PKEY;
162 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_public_key_cur)) {
163 return AWS_PEM_TYPE_PUBLIC_KEY;
164 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_private_rsa_pkcs1_cur)) {
165 return AWS_PEM_TYPE_PRIVATE_RSA_PKCS1;
166 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_public_rsa_pkcs1_cur)) {
167 return AWS_PEM_TYPE_PUBLIC_RSA_PKCS1;
168 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_private_dsa_pkcs1_cur)) {
169 return AWS_PEM_TYPE_PRIVATE_DSA_PKCS1;
170 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_public_dsa_pkcs1_cur)) {
171 return AWS_PEM_TYPE_PUBLIC_DSA_PKCS1;
172 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_pkcs7_cur)) {
173 return AWS_PEM_TYPE_PKCS7;
174 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_pkcs7_signed_data_cur)) {
175 return AWS_PEM_TYPE_PKCS7_SIGNED_DATA;
176 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_private_pkcs8_encrypted_cur)) {
177 return AWS_PEM_TYPE_PRIVATE_PKCS8_ENCRYPTED;
178 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_private_pkcs8_cur)) {
179 return AWS_PEM_TYPE_PRIVATE_PKCS8;
180 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_dh_parameters_cur)) {
181 return AWS_PEM_TYPE_DH_PARAMETERS;
182 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_dh_parameters_x942_cur)) {
183 return AWS_PEM_TYPE_DH_PARAMETERS_X942;
184 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_ssl_session_parameters_cur)) {
185 return AWS_PEM_TYPE_SSL_SESSION_PARAMETERS;
186 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_dsa_parameters_cur)) {
187 return AWS_PEM_TYPE_DSA_PARAMETERS;
188 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_ecdsa_public_cur)) {
189 return AWS_PEM_TYPE_ECDSA_PUBLIC;
190 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_ec_parameters_cur)) {
191 return AWS_PEM_TYPE_EC_PARAMETERS;
192 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_ec_private_cur)) {
193 return AWS_PEM_TYPE_EC_PRIVATE;
194 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_parameters_cur)) {
195 return AWS_PEM_TYPE_PARAMETERS;
196 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_cms_cur)) {
197 return AWS_PEM_TYPE_CMS;
198 } else if (aws_byte_cursor_eq(&type_cur, &s_pem_type_sm2_parameters_cur)) {
199 return AWS_PEM_TYPE_SM2_PARAMETERS;
200 }
201
202 return AWS_PEM_TYPE_UNKNOWN;
203 }
204
205 static struct aws_byte_cursor s_begin_header_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("-----BEGIN");
206 static struct aws_byte_cursor s_end_header_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("-----END");
207 static struct aws_byte_cursor s_delim_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("-----");
208
209 int s_extract_header_type_cur(struct aws_byte_cursor cur, struct aws_byte_cursor *out) {
210 if (!aws_byte_cursor_starts_with(&cur, &s_begin_header_cur)) {
211 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Invalid PEM buffer: invalid begin token");
212 return aws_raise_error(AWS_ERROR_PEM_MALFORMED);
213 }
214
215 aws_byte_cursor_advance(&cur, s_begin_header_cur.len);
216 aws_byte_cursor_advance(&cur, 1); // space after begin
217
218 struct aws_byte_cursor type_cur = aws_byte_cursor_advance(&cur, cur.len - s_delim_cur.len);
219
220 if (!aws_byte_cursor_eq(&cur, &s_delim_cur)) {
221 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Invalid PEM buffer: invalid end token");
222 return aws_raise_error(AWS_ERROR_PEM_MALFORMED);
223 }
224
225 *out = type_cur;
226 return AWS_OP_SUCCESS;
227 }
228
229 static int s_convert_pem_to_raw_base64(
230 struct aws_allocator *allocator,
231 struct aws_byte_cursor pem,
232 struct aws_array_list *pem_objects) {
233
234 struct aws_array_list split_buffers;
235 if (aws_array_list_init_dynamic(&split_buffers, allocator, 16, sizeof(struct aws_byte_cursor))) {
236 return AWS_OP_ERR;
237 }
238
239 if (aws_byte_cursor_split_on_char(&pem, '\n', &split_buffers)) {
240 aws_array_list_clean_up(&split_buffers);
241 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Invalid PEM buffer: failed to split on newline");
242 return aws_raise_error(AWS_ERROR_PEM_MALFORMED);
243 }
244
245 enum aws_pem_parse_state state = BEGIN;
246 bool on_length_calc = true;
247 size_t current_obj_len = 0;
248 size_t current_obj_start_index = 0;
249 struct aws_byte_buf current_obj_buf;
250 AWS_ZERO_STRUCT(current_obj_buf);
251 struct aws_byte_cursor current_obj_type_cur;
252 AWS_ZERO_STRUCT(current_obj_type_cur);
253 enum aws_pem_object_type current_obj_type = AWS_PEM_TYPE_UNKNOWN;
254
255 size_t split_count = aws_array_list_length(&split_buffers);
256 size_t i = 0;
257
258 while (i < split_count) {
259 struct aws_byte_cursor *line_cur_ptr = NULL;
260 int error = aws_array_list_get_at_ptr(&split_buffers, (void **)&line_cur_ptr, i);
261 /* should never fail as we control array size and how we index into list */
262 AWS_FATAL_ASSERT(error == AWS_OP_SUCCESS);
263
264 /* Burn off the padding in the buffer first.
265 * Worst case we'll only have to do this once per line in the buffer. */
266 *line_cur_ptr = aws_byte_cursor_left_trim_pred(line_cur_ptr, aws_isspace);
267
268 /* And make sure remove any space from right side */
269 *line_cur_ptr = aws_byte_cursor_right_trim_pred(line_cur_ptr, aws_isspace);
270
271 switch (state) {
272 case BEGIN:
273 if (aws_byte_cursor_starts_with(line_cur_ptr, &s_begin_header_cur)) {
274 if (s_extract_header_type_cur(*line_cur_ptr, &current_obj_type_cur)) {
275 goto on_end_of_loop;
276 }
277 current_obj_type = s_map_type_cur_to_type(current_obj_type_cur);
278 current_obj_start_index = i + 1;
279 state = ON_DATA;
280 }
281 ++i;
282 break;
283 /* this loops through the lines containing data twice. First to figure out the length, a second
284 * time to actually copy the data. */
285 case ON_DATA:
286 /* Found end tag. */
287 if (aws_byte_cursor_starts_with(line_cur_ptr, &s_end_header_cur)) {
288 if (on_length_calc) {
289 on_length_calc = false;
290 state = ON_DATA;
291 i = current_obj_start_index;
292 aws_byte_buf_init(&current_obj_buf, allocator, current_obj_len);
293
294 } else {
295 struct aws_pem_object pem_object = {
296 .data = current_obj_buf,
297 .type_string = aws_string_new_from_cursor(allocator, &current_obj_type_cur),
298 .type = current_obj_type,
299 };
300
301 if (aws_array_list_push_back(pem_objects, &pem_object)) {
302 goto on_end_of_loop;
303 }
304 state = BEGIN;
305 on_length_calc = true;
306 current_obj_len = 0;
307 ++i;
308 AWS_ZERO_STRUCT(current_obj_buf);
309 AWS_ZERO_STRUCT(current_obj_type_cur);
310 current_obj_type = AWS_PEM_TYPE_UNKNOWN;
311 }
312 /* actually on a line with data in it. */
313 } else {
314 if (on_length_calc) {
315 current_obj_len += line_cur_ptr->len;
316 } else {
317 if (aws_byte_buf_append(&current_obj_buf, line_cur_ptr)) {
318 goto on_end_of_loop;
319 }
320 }
321 ++i;
322 }
323 break;
324 default:
325 AWS_FATAL_ASSERT(false);
326 }
327 }
328
329 /*
330 * Note: this function only hard error if nothing can be parsed out of file.
331 * Otherwise it succeeds and returns whatever was parsed successfully.
332 */
333 on_end_of_loop:
334 aws_array_list_clean_up(&split_buffers);
335 aws_byte_buf_clean_up_secure(&current_obj_buf);
336
337 if (state == BEGIN && aws_array_list_length(pem_objects) > 0) {
338 return AWS_OP_SUCCESS;
339 }
340
341 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Invalid PEM buffer.");
342 aws_pem_objects_clean_up(pem_objects);
343 return aws_raise_error(AWS_ERROR_PEM_MALFORMED);
344 }
345
346 int aws_pem_objects_init_from_file_contents(
347 struct aws_array_list *pem_objects,
348 struct aws_allocator *allocator,
349 struct aws_byte_cursor pem_cursor) {
350 AWS_PRECONDITION(allocator);
351 AWS_PRECONDITION(pem_objects != NULL);
352
353 /* Init empty array list, ideally, the PEM should only has one key included. */
354 if (aws_array_list_init_dynamic(pem_objects, allocator, 1, sizeof(struct aws_pem_object))) {
355 return AWS_OP_ERR;
356 }
357
358 if (s_convert_pem_to_raw_base64(allocator, pem_cursor, pem_objects)) {
359 goto on_error;
360 }
361
362 for (size_t i = 0; i < aws_array_list_length(pem_objects); ++i) {
363 struct aws_pem_object *pem_obj_ptr = NULL;
364 aws_array_list_get_at_ptr(pem_objects, (void **)&pem_obj_ptr, i);
365 struct aws_byte_cursor byte_cur = aws_byte_cursor_from_buf(&pem_obj_ptr->data);
366
367 size_t decoded_len = 0;
368 if (aws_base64_compute_decoded_len(&byte_cur, &decoded_len)) {
369 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Failed to get length for decoded base64 pem object.");
370 aws_raise_error(AWS_ERROR_PEM_MALFORMED);
371 goto on_error;
372 }
373
374 struct aws_byte_buf decoded_buffer;
375 aws_byte_buf_init(&decoded_buffer, allocator, decoded_len);
376
377 if (aws_base64_decode(&byte_cur, &decoded_buffer)) {
378 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Failed to base 64 decode pem object.");
379 aws_raise_error(AWS_ERROR_PEM_MALFORMED);
380 aws_byte_buf_clean_up_secure(&decoded_buffer);
381 goto on_error;
382 }
383
384 aws_byte_buf_clean_up_secure(&pem_obj_ptr->data);
385 pem_obj_ptr->data = decoded_buffer;
386 }
387
388 return AWS_OP_SUCCESS;
389
390 on_error:
391 aws_pem_objects_clean_up(pem_objects);
392 return AWS_OP_ERR;
393 }
394
395 int aws_pem_objects_init_from_file_path(
396 struct aws_array_list *pem_objects,
397 struct aws_allocator *allocator,
398 const char *filename) {
399
400 struct aws_byte_buf raw_file_buffer;
401 if (aws_byte_buf_init_from_file(&raw_file_buffer, allocator, filename)) {
402 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Failed to read file %s.", filename);
403 return AWS_OP_ERR;
404 }
405 AWS_ASSERT(raw_file_buffer.buffer);
406
407 struct aws_byte_cursor file_cursor = aws_byte_cursor_from_buf(&raw_file_buffer);
408 if (aws_pem_objects_init_from_file_contents(pem_objects, allocator, file_cursor)) {
409 aws_byte_buf_clean_up_secure(&raw_file_buffer);
410 AWS_LOGF_ERROR(AWS_LS_IO_PEM, "Failed to decode PEM file %s.", filename);
411 return AWS_OP_ERR;
412 }
413
414 aws_byte_buf_clean_up_secure(&raw_file_buffer);
415
416 return AWS_OP_SUCCESS;
417 }
418
419 #define AWS_PEM_LINE_LENGTH 64
420 #define AWS_DER_MAX_CHUNK_SIZE 48
421 static const struct aws_byte_cursor s_pem_newline = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\n");
422 static const struct aws_byte_cursor s_pem_space = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" ");
423
424 struct aws_string *aws_der_cert_to_pem(struct aws_allocator *alloc, struct aws_byte_cursor der_cert) {
425 AWS_PRECONDITION(alloc);
426 AWS_PRECONDITION(aws_byte_cursor_is_valid(&der_cert));
427
428 if (der_cert.len == 0) {
429 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
430 return NULL;
431 }
432
433 /* header: "-----BEGIN CERTIFICATE-----\n" */
434 size_t header_len = begin_header.len + s_pem_space.len + s_pem_type_x509_cur.len + dashes.len + s_pem_newline.len;
435 /* footer: "-----END CERTIFICATE-----\n" */
436 size_t footer_len = end_header.len + s_pem_space.len + s_pem_type_x509_cur.len + dashes.len + s_pem_newline.len;
437
438 size_t base64_len = ((der_cert.len + 2) / 3) * 4;
439 size_t newlines_num = (base64_len + AWS_PEM_LINE_LENGTH - 1) / AWS_PEM_LINE_LENGTH;
440 size_t pem_capacity = header_len + base64_len + newlines_num + footer_len + 1;
441
442 struct aws_byte_buf out_pem_buf;
443 aws_byte_buf_init(&out_pem_buf, alloc, pem_capacity);
444
445 /* Write header */
446 aws_byte_buf_append(&out_pem_buf, &begin_header);
447 aws_byte_buf_append(&out_pem_buf, &s_pem_space);
448 aws_byte_buf_append(&out_pem_buf, &s_pem_type_x509_cur);
449 aws_byte_buf_append(&out_pem_buf, &dashes);
450 aws_byte_buf_append(&out_pem_buf, &s_pem_newline);
451
452 /* Encode DER to base64 line by line: 48 raw bytes -> 64 base64 chars */
453 size_t der_offset = 0;
454 while (der_offset < der_cert.len) {
455 size_t der_chunk_size = der_cert.len - der_offset;
456 if (der_chunk_size > AWS_DER_MAX_CHUNK_SIZE) {
457 der_chunk_size = AWS_DER_MAX_CHUNK_SIZE;
458 }
459 struct aws_byte_cursor chunk_cur = aws_byte_cursor_from_array(der_cert.ptr + der_offset, der_chunk_size);
460 if (aws_base64_encode(&chunk_cur, &out_pem_buf)) {
461 aws_byte_buf_clean_up(&out_pem_buf);
462 return NULL;
463 }
464 aws_byte_buf_append(&out_pem_buf, &s_pem_newline);
465 der_offset += der_chunk_size;
466 }
467
468 /* Write footer */
469 aws_byte_buf_append(&out_pem_buf, &end_header);
470 aws_byte_buf_append(&out_pem_buf, &s_pem_space);
471 aws_byte_buf_append(&out_pem_buf, &s_pem_type_x509_cur);
472 aws_byte_buf_append(&out_pem_buf, &dashes);
473 aws_byte_buf_append(&out_pem_buf, &s_pem_newline);
474
475 struct aws_string *result = aws_string_new_from_buf(alloc, &out_pem_buf);
476 aws_byte_buf_clean_up(&out_pem_buf);
477 return result;
478 }