![]() |
|
|||
File indexing completed on 2025-08-27 09:37:29
0001 /** 0002 * \file asn1write.h 0003 * 0004 * \brief ASN.1 buffer writing functionality 0005 */ 0006 /* 0007 * Copyright The Mbed TLS Contributors 0008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0009 */ 0010 #ifndef MBEDTLS_ASN1_WRITE_H 0011 #define MBEDTLS_ASN1_WRITE_H 0012 0013 #include "mbedtls/build_info.h" 0014 0015 #include "mbedtls/asn1.h" 0016 0017 #define MBEDTLS_ASN1_CHK_ADD(g, f) \ 0018 do \ 0019 { \ 0020 if ((ret = (f)) < 0) \ 0021 return ret; \ 0022 else \ 0023 (g) += ret; \ 0024 } while (0) 0025 0026 #define MBEDTLS_ASN1_CHK_CLEANUP_ADD(g, f) \ 0027 do \ 0028 { \ 0029 if ((ret = (f)) < 0) \ 0030 goto cleanup; \ 0031 else \ 0032 (g) += ret; \ 0033 } while (0) 0034 0035 #ifdef __cplusplus 0036 extern "C" { 0037 #endif 0038 0039 #if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C) || \ 0040 defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA) 0041 /** 0042 * \brief Write a length field in ASN.1 format. 0043 * 0044 * \note This function works backwards in data buffer. 0045 * 0046 * \param p The reference to the current position pointer. 0047 * \param start The start of the buffer, for bounds-checking. 0048 * \param len The length value to write. 0049 * 0050 * \return The number of bytes written to \p p on success. 0051 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0052 */ 0053 int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, 0054 size_t len); 0055 /** 0056 * \brief Write an ASN.1 tag in ASN.1 format. 0057 * 0058 * \note This function works backwards in data buffer. 0059 * 0060 * \param p The reference to the current position pointer. 0061 * \param start The start of the buffer, for bounds-checking. 0062 * \param tag The tag to write. 0063 * 0064 * \return The number of bytes written to \p p on success. 0065 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0066 */ 0067 int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, 0068 unsigned char tag); 0069 #endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C || MBEDTLS_PSA_UTIL_HAVE_ECDSA*/ 0070 0071 #if defined(MBEDTLS_ASN1_WRITE_C) 0072 /** 0073 * \brief Write raw buffer data. 0074 * 0075 * \note This function works backwards in data buffer. 0076 * 0077 * \param p The reference to the current position pointer. 0078 * \param start The start of the buffer, for bounds-checking. 0079 * \param buf The data buffer to write. 0080 * \param size The length of the data buffer. 0081 * 0082 * \return The number of bytes written to \p p on success. 0083 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0084 */ 0085 int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start, 0086 const unsigned char *buf, size_t size); 0087 0088 #if defined(MBEDTLS_BIGNUM_C) 0089 /** 0090 * \brief Write an arbitrary-precision number (#MBEDTLS_ASN1_INTEGER) 0091 * in ASN.1 format. 0092 * 0093 * \note This function works backwards in data buffer. 0094 * 0095 * \param p The reference to the current position pointer. 0096 * \param start The start of the buffer, for bounds-checking. 0097 * \param X The MPI to write. 0098 * It must be non-negative. 0099 * 0100 * \return The number of bytes written to \p p on success. 0101 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0102 */ 0103 int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, 0104 const mbedtls_mpi *X); 0105 #endif /* MBEDTLS_BIGNUM_C */ 0106 0107 /** 0108 * \brief Write a NULL tag (#MBEDTLS_ASN1_NULL) with zero data 0109 * in ASN.1 format. 0110 * 0111 * \note This function works backwards in data buffer. 0112 * 0113 * \param p The reference to the current position pointer. 0114 * \param start The start of the buffer, for bounds-checking. 0115 * 0116 * \return The number of bytes written to \p p on success. 0117 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0118 */ 0119 int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start); 0120 0121 /** 0122 * \brief Write an OID tag (#MBEDTLS_ASN1_OID) and data 0123 * in ASN.1 format. 0124 * 0125 * \note This function works backwards in data buffer. 0126 * 0127 * \param p The reference to the current position pointer. 0128 * \param start The start of the buffer, for bounds-checking. 0129 * \param oid The OID to write. 0130 * \param oid_len The length of the OID. 0131 * 0132 * \return The number of bytes written to \p p on success. 0133 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0134 */ 0135 int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start, 0136 const char *oid, size_t oid_len); 0137 0138 /** 0139 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format. 0140 * 0141 * \note This function works backwards in data buffer. 0142 * 0143 * \param p The reference to the current position pointer. 0144 * \param start The start of the buffer, for bounds-checking. 0145 * \param oid The OID of the algorithm to write. 0146 * \param oid_len The length of the algorithm's OID. 0147 * \param par_len The length of the parameters, which must be already written. 0148 * If 0, NULL parameters are added 0149 * 0150 * \return The number of bytes written to \p p on success. 0151 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0152 */ 0153 int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, 0154 const unsigned char *start, 0155 const char *oid, size_t oid_len, 0156 size_t par_len); 0157 0158 /** 0159 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format. 0160 * 0161 * \note This function works backwards in data buffer. 0162 * 0163 * \param p The reference to the current position pointer. 0164 * \param start The start of the buffer, for bounds-checking. 0165 * \param oid The OID of the algorithm to write. 0166 * \param oid_len The length of the algorithm's OID. 0167 * \param par_len The length of the parameters, which must be already written. 0168 * \param has_par If there are any parameters. If 0, par_len must be 0. If 1 0169 * and \p par_len is 0, NULL parameters are added. 0170 * 0171 * \return The number of bytes written to \p p on success. 0172 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0173 */ 0174 int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, 0175 const unsigned char *start, 0176 const char *oid, size_t oid_len, 0177 size_t par_len, int has_par); 0178 0179 /** 0180 * \brief Write a boolean tag (#MBEDTLS_ASN1_BOOLEAN) and value 0181 * in ASN.1 format. 0182 * 0183 * \note This function works backwards in data buffer. 0184 * 0185 * \param p The reference to the current position pointer. 0186 * \param start The start of the buffer, for bounds-checking. 0187 * \param boolean The boolean value to write, either \c 0 or \c 1. 0188 * 0189 * \return The number of bytes written to \p p on success. 0190 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0191 */ 0192 int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, 0193 int boolean); 0194 0195 /** 0196 * \brief Write an int tag (#MBEDTLS_ASN1_INTEGER) and value 0197 * in ASN.1 format. 0198 * 0199 * \note This function works backwards in data buffer. 0200 * 0201 * \param p The reference to the current position pointer. 0202 * \param start The start of the buffer, for bounds-checking. 0203 * \param val The integer value to write. 0204 * It must be non-negative. 0205 * 0206 * \return The number of bytes written to \p p on success. 0207 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0208 */ 0209 int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val); 0210 0211 /** 0212 * \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value 0213 * in ASN.1 format. 0214 * 0215 * \note This function works backwards in data buffer. 0216 * 0217 * \param p The reference to the current position pointer. 0218 * \param start The start of the buffer, for bounds-checking. 0219 * \param val The integer value to write. 0220 * 0221 * \return The number of bytes written to \p p on success. 0222 * \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure. 0223 */ 0224 int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val); 0225 0226 /** 0227 * \brief Write a string in ASN.1 format using a specific 0228 * string encoding tag. 0229 0230 * \note This function works backwards in data buffer. 0231 * 0232 * \param p The reference to the current position pointer. 0233 * \param start The start of the buffer, for bounds-checking. 0234 * \param tag The string encoding tag to write, e.g. 0235 * #MBEDTLS_ASN1_UTF8_STRING. 0236 * \param text The string to write. 0237 * \param text_len The length of \p text in bytes (which might 0238 * be strictly larger than the number of characters). 0239 * 0240 * \return The number of bytes written to \p p on success. 0241 * \return A negative error code on failure. 0242 */ 0243 int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, 0244 int tag, const char *text, 0245 size_t text_len); 0246 0247 /** 0248 * \brief Write a string in ASN.1 format using the PrintableString 0249 * string encoding tag (#MBEDTLS_ASN1_PRINTABLE_STRING). 0250 * 0251 * \note This function works backwards in data buffer. 0252 * 0253 * \param p The reference to the current position pointer. 0254 * \param start The start of the buffer, for bounds-checking. 0255 * \param text The string to write. 0256 * \param text_len The length of \p text in bytes (which might 0257 * be strictly larger than the number of characters). 0258 * 0259 * \return The number of bytes written to \p p on success. 0260 * \return A negative error code on failure. 0261 */ 0262 int mbedtls_asn1_write_printable_string(unsigned char **p, 0263 const unsigned char *start, 0264 const char *text, size_t text_len); 0265 0266 /** 0267 * \brief Write a UTF8 string in ASN.1 format using the UTF8String 0268 * string encoding tag (#MBEDTLS_ASN1_UTF8_STRING). 0269 * 0270 * \note This function works backwards in data buffer. 0271 * 0272 * \param p The reference to the current position pointer. 0273 * \param start The start of the buffer, for bounds-checking. 0274 * \param text The string to write. 0275 * \param text_len The length of \p text in bytes (which might 0276 * be strictly larger than the number of characters). 0277 * 0278 * \return The number of bytes written to \p p on success. 0279 * \return A negative error code on failure. 0280 */ 0281 int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start, 0282 const char *text, size_t text_len); 0283 0284 /** 0285 * \brief Write a string in ASN.1 format using the IA5String 0286 * string encoding tag (#MBEDTLS_ASN1_IA5_STRING). 0287 * 0288 * \note This function works backwards in data buffer. 0289 * 0290 * \param p The reference to the current position pointer. 0291 * \param start The start of the buffer, for bounds-checking. 0292 * \param text The string to write. 0293 * \param text_len The length of \p text in bytes (which might 0294 * be strictly larger than the number of characters). 0295 * 0296 * \return The number of bytes written to \p p on success. 0297 * \return A negative error code on failure. 0298 */ 0299 int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start, 0300 const char *text, size_t text_len); 0301 0302 /** 0303 * \brief Write a bitstring tag (#MBEDTLS_ASN1_BIT_STRING) and 0304 * value in ASN.1 format. 0305 * 0306 * \note This function works backwards in data buffer. 0307 * 0308 * \param p The reference to the current position pointer. 0309 * \param start The start of the buffer, for bounds-checking. 0310 * \param buf The bitstring to write. 0311 * \param bits The total number of bits in the bitstring. 0312 * 0313 * \return The number of bytes written to \p p on success. 0314 * \return A negative error code on failure. 0315 */ 0316 int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start, 0317 const unsigned char *buf, size_t bits); 0318 0319 /** 0320 * \brief This function writes a named bitstring tag 0321 * (#MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format. 0322 * 0323 * As stated in RFC 5280 Appendix B, trailing zeroes are 0324 * omitted when encoding named bitstrings in DER. 0325 * 0326 * \note This function works backwards within the data buffer. 0327 * 0328 * \param p The reference to the current position pointer. 0329 * \param start The start of the buffer which is used for bounds-checking. 0330 * \param buf The bitstring to write. 0331 * \param bits The total number of bits in the bitstring. 0332 * 0333 * \return The number of bytes written to \p p on success. 0334 * \return A negative error code on failure. 0335 */ 0336 int mbedtls_asn1_write_named_bitstring(unsigned char **p, 0337 const unsigned char *start, 0338 const unsigned char *buf, 0339 size_t bits); 0340 0341 /** 0342 * \brief Write an octet string tag (#MBEDTLS_ASN1_OCTET_STRING) 0343 * and value in ASN.1 format. 0344 * 0345 * \note This function works backwards in data buffer. 0346 * 0347 * \param p The reference to the current position pointer. 0348 * \param start The start of the buffer, for bounds-checking. 0349 * \param buf The buffer holding the data to write. 0350 * \param size The length of the data buffer \p buf. 0351 * 0352 * \return The number of bytes written to \p p on success. 0353 * \return A negative error code on failure. 0354 */ 0355 int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start, 0356 const unsigned char *buf, size_t size); 0357 0358 /** 0359 * \brief Create or find a specific named_data entry for writing in a 0360 * sequence or list based on the OID. If not already in there, 0361 * a new entry is added to the head of the list. 0362 * Warning: Destructive behaviour for the val data! 0363 * 0364 * \param list The pointer to the location of the head of the list to seek 0365 * through (will be updated in case of a new entry). 0366 * \param oid The OID to look for. 0367 * \param oid_len The size of the OID. 0368 * \param val The associated data to store. If this is \c NULL, 0369 * no data is copied to the new or existing buffer. 0370 * \param val_len The minimum length of the data buffer needed. 0371 * If this is 0, do not allocate a buffer for the associated 0372 * data. 0373 * If the OID was already present, enlarge, shrink or free 0374 * the existing buffer to fit \p val_len. 0375 * 0376 * \return A pointer to the new / existing entry on success. 0377 * \return \c NULL if there was a memory allocation error. 0378 */ 0379 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **list, 0380 const char *oid, size_t oid_len, 0381 const unsigned char *val, 0382 size_t val_len); 0383 0384 #ifdef __cplusplus 0385 } 0386 #endif 0387 0388 #endif /* MBEDTLS_ASN1_WRITE_C */ 0389 0390 #endif /* MBEDTLS_ASN1_WRITE_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |