]>
Commit | Line | Data |
---|---|---|
1 | /** | |
2 | * \file asn1write.h | |
3 | * | |
4 | * \brief ASN.1 buffer writing functionality | |
5 | */ | |
6 | /* | |
7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | |
8 | * SPDX-License-Identifier: GPL-2.0 | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License along | |
21 | * with this program; if not, write to the Free Software Foundation, Inc., | |
22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
23 | * | |
24 | * This file is part of mbed TLS (https://tls.mbed.org) | |
25 | */ | |
26 | #ifndef MBEDTLS_ASN1_WRITE_H | |
27 | #define MBEDTLS_ASN1_WRITE_H | |
28 | ||
29 | #include "asn1.h" | |
30 | ||
31 | #define MBEDTLS_ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \ | |
32 | g += ret; } while( 0 ) | |
33 | ||
34 | #ifdef __cplusplus | |
35 | extern "C" { | |
36 | #endif | |
37 | ||
38 | /** | |
39 | * \brief Write a length field in ASN.1 format | |
40 | * Note: function works backwards in data buffer | |
41 | * | |
42 | * \param p reference to current position pointer | |
43 | * \param start start of the buffer (for bounds-checking) | |
44 | * \param len the length to write | |
45 | * | |
46 | * \return the length written or a negative error code | |
47 | */ | |
48 | int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len ); | |
49 | ||
50 | /** | |
51 | * \brief Write a ASN.1 tag in ASN.1 format | |
52 | * Note: function works backwards in data buffer | |
53 | * | |
54 | * \param p reference to current position pointer | |
55 | * \param start start of the buffer (for bounds-checking) | |
56 | * \param tag the tag to write | |
57 | * | |
58 | * \return the length written or a negative error code | |
59 | */ | |
60 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, | |
61 | unsigned char tag ); | |
62 | ||
63 | /** | |
64 | * \brief Write raw buffer data | |
65 | * Note: function works backwards in data buffer | |
66 | * | |
67 | * \param p reference to current position pointer | |
68 | * \param start start of the buffer (for bounds-checking) | |
69 | * \param buf data buffer to write | |
70 | * \param size length of the data buffer | |
71 | * | |
72 | * \return the length written or a negative error code | |
73 | */ | |
74 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, | |
75 | const unsigned char *buf, size_t size ); | |
76 | ||
77 | #if defined(MBEDTLS_BIGNUM_C) | |
78 | /** | |
79 | * \brief Write a big number (MBEDTLS_ASN1_INTEGER) in ASN.1 format | |
80 | * Note: function works backwards in data buffer | |
81 | * | |
82 | * \param p reference to current position pointer | |
83 | * \param start start of the buffer (for bounds-checking) | |
84 | * \param X the MPI to write | |
85 | * | |
86 | * \return the length written or a negative error code | |
87 | */ | |
88 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X ); | |
89 | #endif /* MBEDTLS_BIGNUM_C */ | |
90 | ||
91 | /** | |
92 | * \brief Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format | |
93 | * Note: function works backwards in data buffer | |
94 | * | |
95 | * \param p reference to current position pointer | |
96 | * \param start start of the buffer (for bounds-checking) | |
97 | * | |
98 | * \return the length written or a negative error code | |
99 | */ | |
100 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ); | |
101 | ||
102 | /** | |
103 | * \brief Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format | |
104 | * Note: function works backwards in data buffer | |
105 | * | |
106 | * \param p reference to current position pointer | |
107 | * \param start start of the buffer (for bounds-checking) | |
108 | * \param oid the OID to write | |
109 | * \param oid_len length of the OID | |
110 | * | |
111 | * \return the length written or a negative error code | |
112 | */ | |
113 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, | |
114 | const char *oid, size_t oid_len ); | |
115 | ||
116 | /** | |
117 | * \brief Write an AlgorithmIdentifier sequence in ASN.1 format | |
118 | * Note: function works backwards in data buffer | |
119 | * | |
120 | * \param p reference to current position pointer | |
121 | * \param start start of the buffer (for bounds-checking) | |
122 | * \param oid the OID of the algorithm | |
123 | * \param oid_len length of the OID | |
124 | * \param par_len length of parameters, which must be already written. | |
125 | * If 0, NULL parameters are added | |
126 | * | |
127 | * \return the length written or a negative error code | |
128 | */ | |
129 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, | |
130 | const char *oid, size_t oid_len, | |
131 | size_t par_len ); | |
132 | ||
133 | /** | |
134 | * \brief Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format | |
135 | * Note: function works backwards in data buffer | |
136 | * | |
137 | * \param p reference to current position pointer | |
138 | * \param start start of the buffer (for bounds-checking) | |
139 | * \param boolean 0 or 1 | |
140 | * | |
141 | * \return the length written or a negative error code | |
142 | */ | |
143 | int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean ); | |
144 | ||
145 | /** | |
146 | * \brief Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format | |
147 | * Note: function works backwards in data buffer | |
148 | * | |
149 | * \param p reference to current position pointer | |
150 | * \param start start of the buffer (for bounds-checking) | |
151 | * \param val the integer value | |
152 | * | |
153 | * \return the length written or a negative error code | |
154 | */ | |
155 | int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val ); | |
156 | ||
157 | /** | |
158 | * \brief Write a printable string tag (MBEDTLS_ASN1_PRINTABLE_STRING) and | |
159 | * value in ASN.1 format | |
160 | * Note: function works backwards in data buffer | |
161 | * | |
162 | * \param p reference to current position pointer | |
163 | * \param start start of the buffer (for bounds-checking) | |
164 | * \param text the text to write | |
165 | * \param text_len length of the text | |
166 | * | |
167 | * \return the length written or a negative error code | |
168 | */ | |
169 | int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start, | |
170 | const char *text, size_t text_len ); | |
171 | ||
172 | /** | |
173 | * \brief Write an IA5 string tag (MBEDTLS_ASN1_IA5_STRING) and | |
174 | * value in ASN.1 format | |
175 | * Note: function works backwards in data buffer | |
176 | * | |
177 | * \param p reference to current position pointer | |
178 | * \param start start of the buffer (for bounds-checking) | |
179 | * \param text the text to write | |
180 | * \param text_len length of the text | |
181 | * | |
182 | * \return the length written or a negative error code | |
183 | */ | |
184 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, | |
185 | const char *text, size_t text_len ); | |
186 | ||
187 | /** | |
188 | * \brief Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and | |
189 | * value in ASN.1 format | |
190 | * Note: function works backwards in data buffer | |
191 | * | |
192 | * \param p reference to current position pointer | |
193 | * \param start start of the buffer (for bounds-checking) | |
194 | * \param buf the bitstring | |
195 | * \param bits the total number of bits in the bitstring | |
196 | * | |
197 | * \return the length written or a negative error code | |
198 | */ | |
199 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, | |
200 | const unsigned char *buf, size_t bits ); | |
201 | ||
202 | /** | |
203 | * \brief Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and | |
204 | * value in ASN.1 format | |
205 | * Note: function works backwards in data buffer | |
206 | * | |
207 | * \param p reference to current position pointer | |
208 | * \param start start of the buffer (for bounds-checking) | |
209 | * \param buf data buffer to write | |
210 | * \param size length of the data buffer | |
211 | * | |
212 | * \return the length written or a negative error code | |
213 | */ | |
214 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start, | |
215 | const unsigned char *buf, size_t size ); | |
216 | ||
217 | /** | |
218 | * \brief Create or find a specific named_data entry for writing in a | |
219 | * sequence or list based on the OID. If not already in there, | |
220 | * a new entry is added to the head of the list. | |
221 | * Warning: Destructive behaviour for the val data! | |
222 | * | |
223 | * \param list Pointer to the location of the head of the list to seek | |
224 | * through (will be updated in case of a new entry) | |
225 | * \param oid The OID to look for | |
226 | * \param oid_len Size of the OID | |
227 | * \param val Data to store (can be NULL if you want to fill it by hand) | |
228 | * \param val_len Minimum length of the data buffer needed | |
229 | * | |
230 | * \return NULL if if there was a memory allocation error, or a pointer | |
231 | * to the new / existing entry. | |
232 | */ | |
233 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **list, | |
234 | const char *oid, size_t oid_len, | |
235 | const unsigned char *val, | |
236 | size_t val_len ); | |
237 | ||
238 | #ifdef __cplusplus | |
239 | } | |
240 | #endif | |
241 | ||
242 | #endif /* MBEDTLS_ASN1_WRITE_H */ |