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