asn1write.c 11 KB

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