asn1write.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * ASN.1 buffer writing functionality
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_ASN1_WRITE_C)
  27. #include "mbedtls/asn1write.h"
  28. #include <string.h>
  29. #if defined(MBEDTLS_PLATFORM_C)
  30. #include "mbedtls/platform.h"
  31. #else
  32. #include <stdlib.h>
  33. #define mbedtls_calloc calloc
  34. #define mbedtls_free free
  35. #endif
  36. int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
  37. {
  38. size_t upper_bound = 0xFFFFFFFF;
  39. if( len < 0x80 )
  40. {
  41. if( *p - start < 1 )
  42. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  43. *--(*p) = (unsigned char) len;
  44. return( 1 );
  45. }
  46. if( len <= 0xFF )
  47. {
  48. if( *p - start < 2 )
  49. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  50. *--(*p) = (unsigned char) len;
  51. *--(*p) = 0x81;
  52. return( 2 );
  53. }
  54. if( len <= 0xFFFF )
  55. {
  56. if( *p - start < 3 )
  57. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  58. *--(*p) = ( len ) & 0xFF;
  59. *--(*p) = ( len >> 8 ) & 0xFF;
  60. *--(*p) = 0x82;
  61. return( 3 );
  62. }
  63. if( len <= 0xFFFFFF )
  64. {
  65. if( *p - start < 4 )
  66. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  67. *--(*p) = ( len ) & 0xFF;
  68. *--(*p) = ( len >> 8 ) & 0xFF;
  69. *--(*p) = ( len >> 16 ) & 0xFF;
  70. *--(*p) = 0x83;
  71. return( 4 );
  72. }
  73. if( len <= upper_bound )
  74. {
  75. if( *p - start < 5 )
  76. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  77. *--(*p) = ( len ) & 0xFF;
  78. *--(*p) = ( len >> 8 ) & 0xFF;
  79. *--(*p) = ( len >> 16 ) & 0xFF;
  80. *--(*p) = ( len >> 24 ) & 0xFF;
  81. *--(*p) = 0x84;
  82. return( 5 );
  83. }
  84. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  85. }
  86. int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
  87. {
  88. if( *p - start < 1 )
  89. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  90. *--(*p) = tag;
  91. return( 1 );
  92. }
  93. int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
  94. const unsigned char *buf, size_t size )
  95. {
  96. size_t len = 0;
  97. if( *p < start || (size_t)( *p - start ) < size )
  98. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  99. len = size;
  100. (*p) -= len;
  101. memcpy( *p, buf, len );
  102. return( (int) len );
  103. }
  104. #if defined(MBEDTLS_BIGNUM_C)
  105. int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
  106. {
  107. int ret;
  108. size_t len = 0;
  109. // Write the MPI
  110. //
  111. len = mbedtls_mpi_size( X );
  112. if( *p < start || (size_t)( *p - start ) < len )
  113. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  114. (*p) -= len;
  115. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
  116. // DER format assumes 2s complement for numbers, so the leftmost bit
  117. // should be 0 for positive numbers and 1 for negative numbers.
  118. //
  119. if( X->s ==1 && **p & 0x80 )
  120. {
  121. if( *p - start < 1 )
  122. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  123. *--(*p) = 0x00;
  124. len += 1;
  125. }
  126. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  127. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  128. ret = (int) len;
  129. cleanup:
  130. return( ret );
  131. }
  132. #endif /* MBEDTLS_BIGNUM_C */
  133. int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
  134. {
  135. int ret;
  136. size_t len = 0;
  137. // Write NULL
  138. //
  139. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
  140. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
  141. return( (int) len );
  142. }
  143. int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
  144. const char *oid, size_t oid_len )
  145. {
  146. int ret;
  147. size_t len = 0;
  148. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  149. (const unsigned char *) oid, oid_len ) );
  150. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
  151. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
  152. return( (int) len );
  153. }
  154. int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
  155. const char *oid, size_t oid_len,
  156. size_t par_len )
  157. {
  158. int ret;
  159. size_t len = 0;
  160. if( par_len == 0 )
  161. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
  162. else
  163. len += par_len;
  164. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  165. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  166. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
  167. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  168. return( (int) len );
  169. }
  170. int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
  171. {
  172. int ret;
  173. size_t len = 0;
  174. if( *p - start < 1 )
  175. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  176. *--(*p) = (boolean) ? 255 : 0;
  177. len++;
  178. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  179. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
  180. return( (int) len );
  181. }
  182. int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
  183. {
  184. int ret;
  185. size_t len = 0;
  186. // TODO negative values and values larger than 128
  187. // DER format assumes 2s complement for numbers, so the leftmost bit
  188. // should be 0 for positive numbers and 1 for negative numbers.
  189. //
  190. if( *p - start < 1 )
  191. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  192. len += 1;
  193. *--(*p) = val;
  194. if( val > 0 && **p & 0x80 )
  195. {
  196. if( *p - start < 1 )
  197. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  198. *--(*p) = 0x00;
  199. len += 1;
  200. }
  201. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  202. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  203. return( (int) len );
  204. }
  205. int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
  206. const char *text, size_t text_len )
  207. {
  208. int ret;
  209. size_t len = 0;
  210. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  211. (const unsigned char *) text, text_len ) );
  212. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  213. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
  214. return( (int) len );
  215. }
  216. int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
  217. const char *text, size_t text_len )
  218. {
  219. int ret;
  220. size_t len = 0;
  221. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  222. (const unsigned char *) text, text_len ) );
  223. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  224. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
  225. return( (int) len );
  226. }
  227. int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
  228. const unsigned char *buf, size_t bits )
  229. {
  230. int ret;
  231. size_t len = 0, size;
  232. size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
  233. // Calculate byte length
  234. //
  235. if( *p < start || (size_t)( *p - start ) < size + 1 )
  236. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  237. len = size + 1;
  238. (*p) -= size;
  239. memcpy( *p, buf, size );
  240. // Write unused bits
  241. //
  242. *--(*p) = (unsigned char) (size * 8 - bits);
  243. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  244. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
  245. return( (int) len );
  246. }
  247. int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
  248. const unsigned char *buf, size_t size )
  249. {
  250. int ret;
  251. size_t len = 0;
  252. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
  253. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  254. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
  255. return( (int) len );
  256. }
  257. mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
  258. const char *oid, size_t oid_len,
  259. const unsigned char *val,
  260. size_t val_len )
  261. {
  262. mbedtls_asn1_named_data *cur;
  263. if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
  264. {
  265. // Add new entry if not present yet based on OID
  266. //
  267. cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
  268. sizeof(mbedtls_asn1_named_data) );
  269. if( cur == NULL )
  270. return( NULL );
  271. cur->oid.len = oid_len;
  272. cur->oid.p = mbedtls_calloc( 1, oid_len );
  273. if( cur->oid.p == NULL )
  274. {
  275. mbedtls_free( cur );
  276. return( NULL );
  277. }
  278. memcpy( cur->oid.p, oid, oid_len );
  279. cur->val.len = val_len;
  280. cur->val.p = mbedtls_calloc( 1, val_len );
  281. if( cur->val.p == NULL )
  282. {
  283. mbedtls_free( cur->oid.p );
  284. mbedtls_free( cur );
  285. return( NULL );
  286. }
  287. cur->next = *head;
  288. *head = cur;
  289. }
  290. else if( cur->val.len < val_len )
  291. {
  292. /*
  293. * Enlarge existing value buffer if needed
  294. * Preserve old data until the allocation succeeded, to leave list in
  295. * a consistent state in case allocation fails.
  296. */
  297. void *p = mbedtls_calloc( 1, val_len );
  298. if( p == NULL )
  299. return( NULL );
  300. mbedtls_free( cur->val.p );
  301. cur->val.p = p;
  302. cur->val.len = val_len;
  303. }
  304. if( val != NULL )
  305. memcpy( cur->val.p, val, val_len );
  306. return( cur );
  307. }
  308. #endif /* MBEDTLS_ASN1_WRITE_C */