| 1 | #ifndef AWS_IO_PEM_H |
| 2 | #define AWS_IO_PEM_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | #include <aws/io/io.h> |
| 9 | |
| 10 | AWS_EXTERN_C_BEGIN |
| 11 | |
| 12 | /* |
| 13 | * Naming follows OpenSSL convention for PEM types. |
| 14 | * Refer to comment after each enum value for the type string it represents. |
| 15 | */ |
| 16 | enum aws_pem_object_type { |
| 17 | AWS_PEM_TYPE_UNKNOWN = 0, |
| 18 | AWS_PEM_TYPE_X509_OLD, /* X509 CERTIFICATE */ |
| 19 | AWS_PEM_TYPE_X509, /* CERTIFICATE */ |
| 20 | AWS_PEM_TYPE_X509_TRUSTED, /* TRUSTED CERTIFICATE */ |
| 21 | AWS_PEM_TYPE_X509_REQ_OLD, /* NEW CERTIFICATE REQUEST */ |
| 22 | AWS_PEM_TYPE_X509_REQ, /* CERTIFICATE REQUEST */ |
| 23 | AWS_PEM_TYPE_X509_CRL, /* X509 CRL */ |
| 24 | AWS_PEM_TYPE_EVP_PKEY, /* ANY PRIVATE KEY */ |
| 25 | AWS_PEM_TYPE_PUBLIC_KEY, /* PUBLIC KEY */ |
| 26 | AWS_PEM_TYPE_PRIVATE_RSA_PKCS1, /* RSA PRIVATE KEY */ |
| 27 | AWS_PEM_TYPE_PUBLIC_RSA_PKCS1, /* RSA PUBLIC KEY */ |
| 28 | AWS_PEM_TYPE_PRIVATE_DSA_PKCS1, /* DSA PRIVATE KEY */ |
| 29 | AWS_PEM_TYPE_PUBLIC_DSA_PKCS1, /* DSA PUBLIC KEY */ |
| 30 | AWS_PEM_TYPE_PKCS7, /* PKCS7 */ |
| 31 | AWS_PEM_TYPE_PKCS7_SIGNED_DATA, /* PKCS #7 SIGNED DATA */ |
| 32 | AWS_PEM_TYPE_PRIVATE_PKCS8_ENCRYPTED, /* ENCRYPTED PRIVATE KEY */ |
| 33 | AWS_PEM_TYPE_PRIVATE_PKCS8, /* PRIVATE KEY */ |
| 34 | AWS_PEM_TYPE_DH_PARAMETERS, /* X9.42 DH PARAMETERS */ |
| 35 | AWS_PEM_TYPE_DH_PARAMETERS_X942, /* X9.42 DH PARAMETERS */ |
| 36 | AWS_PEM_TYPE_SSL_SESSION_PARAMETERS, /* SSL SESSION PARAMETERS */ |
| 37 | AWS_PEM_TYPE_DSA_PARAMETERS, /* DSA PARAMETERS */ |
| 38 | AWS_PEM_TYPE_ECDSA_PUBLIC, /* ECDSA PUBLIC KEY */ |
| 39 | AWS_PEM_TYPE_EC_PARAMETERS, /* EC PARAMETERS */ |
| 40 | AWS_PEM_TYPE_EC_PRIVATE, /* EC PRIVATE KEY */ |
| 41 | AWS_PEM_TYPE_PARAMETERS, /* PARAMETERS */ |
| 42 | AWS_PEM_TYPE_CMS, /* CMS */ |
| 43 | AWS_PEM_TYPE_SM2_PARAMETERS /* SM2 PARAMETERS */ |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Describes PEM object decoded from file. |
| 48 | * data points to raw data bytes of object (decoding will do additional base 64 |
| 49 | * decoding for each object). |
| 50 | * type will be set to object type or to AWS_PEM_TYPE_UNKNOWN if it could not |
| 51 | * figure out type. |
| 52 | * type_string is the string between -----BEGIN and ----- |
| 53 | */ |
| 54 | struct aws_pem_object { |
| 55 | enum aws_pem_object_type type; |
| 56 | struct aws_string *type_string; |
| 57 | struct aws_byte_buf data; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Cleans up elements of pem_objects list 'aws_pem_objects_init_from_file_contents()' |
| 62 | * and 'aws_pem_objects_init_from_file_path()'. |
| 63 | */ |
| 64 | AWS_IO_API void aws_pem_objects_clean_up(struct aws_array_list *pem_objects); |
| 65 | |
| 66 | /** |
| 67 | * Decodes PEM data and reads objects sequentially adding them to pem_objects. |
| 68 | * If it comes across an object it cannot read, list of all object read until |
| 69 | * that point is returned. |
| 70 | * If no objects can be read from PEM or objects could not be base 64 decoded, |
| 71 | * AWS_ERROR_PEM_MALFORMED is raised. |
| 72 | * out_pem_objects stores aws_pem_object struct by value. |
| 73 | * Function will initialize pem_objects list. |
| 74 | * This code is slow, and it allocates, so please try |
| 75 | * not to call this in the middle of something that needs to be fast or resource sensitive. |
| 76 | */ |
| 77 | AWS_IO_API int aws_pem_objects_init_from_file_contents( |
| 78 | struct aws_array_list *pem_objects, |
| 79 | struct aws_allocator *alloc, |
| 80 | struct aws_byte_cursor pem_cursor); |
| 81 | |
| 82 | /** |
| 83 | * Decodes PEM data from file and reads objects sequentially adding them to pem_objects. |
| 84 | * If it comes across an object it cannot read, list of all object read until |
| 85 | * that point is returned. |
| 86 | * If no objects can be read from PEM or objects could not be base 64 decoded, |
| 87 | * AWS_ERROR_PEM_MALFORMED is raised. |
| 88 | * out_pem_objects stores aws_pem_object struct by value. |
| 89 | * Function will initialize pem_objects list. |
| 90 | * This code is slow, and it allocates, so please try |
| 91 | * not to call this in the middle of something that needs to be fast or resource sensitive. |
| 92 | */ |
| 93 | AWS_IO_API int aws_pem_objects_init_from_file_path( |
| 94 | struct aws_array_list *pem_objects, |
| 95 | struct aws_allocator *allocator, |
| 96 | const char *filename); |
| 97 | |
| 98 | /** |
| 99 | * Encodes DER-encoded certificate bytes into a PEM-formatted, null-terminated string. |
| 100 | * Returns a new aws_string on success, or NULL on failure. |
| 101 | * Caller is responsible for calling aws_string_destroy() on the result. |
| 102 | */ |
| 103 | AWS_IO_API struct aws_string *aws_der_cert_to_pem(struct aws_allocator *alloc, struct aws_byte_cursor der_cert); |
| 104 | |
| 105 | AWS_EXTERN_C_END |
| 106 | #endif /* AWS_IO_PEM_H */ |