pem_common.h
Go to the documentation of this file.
1 /**
2  * @file pem_common.h
3  * @brief PEM common definitions
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _PEM_COMMON_H
32 #define _PEM_COMMON_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 
37 //Encrypted private key support
38 #ifndef PEM_ENCRYPTED_KEY_SUPPORT
39  #define PEM_ENCRYPTED_KEY_SUPPORT DISABLED
40 #elif (PEM_ENCRYPTED_KEY_SUPPORT != ENABLED && PEM_ENCRYPTED_KEY_SUPPORT != DISABLED)
41  #error PEM_ENCRYPTED_KEY_SUPPORT parameter is not valid
42 #endif
43 
44 //DES encryption support (insecure)
45 #ifndef PEM_DES_SUPPORT
46  #define PEM_DES_SUPPORT DISABLED
47 #elif (PEM_DES_SUPPORT != ENABLED && PEM_DES_SUPPORT != DISABLED)
48  #error PEM_DES_SUPPORT parameter is not valid
49 #endif
50 
51 //Triple DES encryption support (weak)
52 #ifndef PEM_3DES_SUPPORT
53  #define PEM_3DES_SUPPORT DISABLED
54 #elif (PEM_3DES_SUPPORT != ENABLED && PEM_3DES_SUPPORT != DISABLED)
55  #error PEM_3DES_SUPPORT parameter is not valid
56 #endif
57 
58 //AES encryption support
59 #ifndef PEM_AES_SUPPORT
60  #define PEM_AES_SUPPORT ENABLED
61 #elif (PEM_AES_SUPPORT != ENABLED && PEM_AES_SUPPORT != DISABLED)
62  #error PEM_AES_SUPPORT parameter is not valid
63 #endif
64 
65 //Camellia cipher support?
66 #ifndef PEM_CAMELLIA_SUPPORT
67  #define PEM_CAMELLIA_SUPPORT DISABLED
68 #elif (PEM_CAMELLIA_SUPPORT != ENABLED && PEM_CAMELLIA_SUPPORT != DISABLED)
69  #error PEM_CAMELLIA_SUPPORT parameter is not valid
70 #endif
71 
72 //ARIA cipher support?
73 #ifndef PEM_ARIA_SUPPORT
74  #define PEM_ARIA_SUPPORT DISABLED
75 #elif (PEM_ARIA_SUPPORT != ENABLED && PEM_ARIA_SUPPORT != DISABLED)
76  #error PEM_ARIA_SUPPORT parameter is not valid
77 #endif
78 
79 //SM4 encryption support
80 #ifndef PEM_SM4_SUPPORT
81  #define PEM_SM4_SUPPORT DISABLED
82 #elif (PEM_SM4_SUPPORT != ENABLED && PEM_SM4_SUPPORT != DISABLED)
83  #error PEM_SM4_SUPPORT parameter is not valid
84 #endif
85 
86 //C++ guard
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
91 
92 /**
93  * @brief String representation
94  **/
95 
96 typedef struct
97 {
98  const char_t *value;
99  size_t length;
100 } PemString;
101 
102 
103 /**
104  * @brief "Proc-Type" header field
105  **/
106 
107 typedef struct
108 {
111 } PemProcType;
112 
113 
114 /**
115  * @brief "DEK-Info" header field
116  **/
117 
118 typedef struct
119 {
122 } PemDekInfo;
123 
124 
125 /**
126  * @brief PEM encapsulated header
127  **/
128 
129 typedef struct
130 {
133 } PemHeader;
134 
135 
136 //PEM related functions
137 error_t pemDecodeFile(const char_t *input, size_t inputLen, const char_t *label,
138  uint8_t *output, size_t *outputLen, PemHeader *header, size_t *consumed);
139 
140 error_t pemEncodeFile(const void *input, size_t inputLen, const char_t *label,
141  char_t *output, size_t *outputLen);
142 
143 error_t pemParseHeader(const char_t *input, size_t inputLen,
144  PemHeader *header, size_t *consumed);
145 
146 void pemParseHeaderField(PemString *line, PemHeader *header);
147 
148 int_t pemFindTag(const char_t *input, size_t inputLen, const char_t *tag1,
149  const char_t *tag2, const char_t *tag3);
150 
152 bool_t pemCompareString(const PemString *string, const char_t *value);
155 
156 const CipherAlgo *pemGetCipherAlgo(const PemString *algo);
157 uint_t pemGetKeyLength(const PemString *algo);
158 
159 //C++ guard
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif
uint8_t token[]
Definition: coap_common.h:181
signed int int_t
Definition: compiler_port.h:49
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
General definitions for cryptographic algorithms.
error_t
Error codes.
Definition: error.h:43
uint8_t c
Definition: ndp.h:514
uint8_t s
Definition: ndp.h:345
error_t pemDecodeFile(const char_t *input, size_t inputLen, const char_t *label, uint8_t *output, size_t *outputLen, PemHeader *header, size_t *consumed)
Convert PEM container to ASN.1 format.
Definition: pem_common.c:58
error_t pemEncodeFile(const void *input, size_t inputLen, const char_t *label, char_t *output, size_t *outputLen)
Convert ASN.1 data to PEM encoding.
Definition: pem_common.c:118
uint_t pemGetKeyLength(const PemString *algo)
Get the encryption key length to be used for PEM encryption/decryption.
Definition: pem_common.c:624
bool_t pemTokenizeString(PemString *s, char_t c, PemString *token)
Split a string into tokens.
Definition: pem_common.c:452
void pemParseHeaderField(PemString *line, PemHeader *header)
Parse header field.
Definition: pem_common.c:251
int_t pemFindTag(const char_t *input, size_t inputLen, const char_t *tag1, const char_t *tag2, const char_t *tag3)
Search a string for a given tag.
Definition: pem_common.c:323
void pemTrimWhitespace(PemString *s)
Removes all leading and trailing whitespace from a string.
Definition: pem_common.c:503
const CipherAlgo * pemGetCipherAlgo(const PemString *algo)
Get the cipher algorithm to be used for PEM encryption/decryption.
Definition: pem_common.c:526
int_t pemFindChar(const PemString *s, char_t c)
Search a string for a given character.
Definition: pem_common.c:384
bool_t pemCompareString(const PemString *string, const char_t *value)
Compare a string against the supplied value.
Definition: pem_common.c:414
error_t pemParseHeader(const char_t *input, size_t inputLen, PemHeader *header, size_t *consumed)
Parse PEM encapsulated header.
Definition: pem_common.c:181
Common interface for encryption algorithms.
Definition: crypto.h:1036
"DEK-Info" header field
Definition: pem_common.h:119
PemString algo
Definition: pem_common.h:120
PemString iv
Definition: pem_common.h:121
PEM encapsulated header.
Definition: pem_common.h:130
PemProcType procType
Definition: pem_common.h:131
PemDekInfo dekInfo
Definition: pem_common.h:132
"Proc-Type" header field
Definition: pem_common.h:108
PemString version
Definition: pem_common.h:109
PemString type
Definition: pem_common.h:110
String representation.
Definition: pem_common.h:97
const char_t * value
Definition: pem_common.h:98
size_t length
Definition: pem_common.h:99
uint8_t value[]
Definition: tcp.h:369