asn1write.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. if( len < 0x80 )
  39. {
  40. if( *p - start < 1 )
  41. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  42. *--(*p) = (unsigned char) len;
  43. return( 1 );
  44. }
  45. if( len <= 0xFF )
  46. {
  47. if( *p - start < 2 )
  48. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  49. *--(*p) = (unsigned char) len;
  50. *--(*p) = 0x81;
  51. return( 2 );
  52. }
  53. if( len <= 0xFFFF )
  54. {
  55. if( *p - start < 3 )
  56. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  57. *--(*p) = ( len ) & 0xFF;
  58. *--(*p) = ( len >> 8 ) & 0xFF;
  59. *--(*p) = 0x82;
  60. return( 3 );
  61. }
  62. if( len <= 0xFFFFFF )
  63. {
  64. if( *p - start < 4 )
  65. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  66. *--(*p) = ( len ) & 0xFF;
  67. *--(*p) = ( len >> 8 ) & 0xFF;
  68. *--(*p) = ( len >> 16 ) & 0xFF;
  69. *--(*p) = 0x83;
  70. return( 4 );
  71. }
  72. if( len <= 0xFFFFFFFF )
  73. {
  74. if( *p - start < 5 )
  75. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  76. *--(*p) = ( len ) & 0xFF;
  77. *--(*p) = ( len >> 8 ) & 0xFF;
  78. *--(*p) = ( len >> 16 ) & 0xFF;
  79. *--(*p) = ( len >> 24 ) & 0xFF;
  80. *--(*p) = 0x84;
  81. return( 5 );
  82. }
  83. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  84. }
  85. int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
  86. {
  87. if( *p - start < 1 )
  88. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  89. *--(*p) = tag;
  90. return( 1 );
  91. }
  92. int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
  93. const unsigned char *buf, size_t size )
  94. {
  95. size_t len = 0;
  96. if( *p < start || (size_t)( *p - start ) < size )
  97. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  98. len = size;
  99. (*p) -= len;
  100. memcpy( *p, buf, len );
  101. return( (int) len );
  102. }
  103. #if defined(MBEDTLS_BIGNUM_C)
  104. int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
  105. {
  106. int ret;
  107. size_t len = 0;
  108. // Write the MPI
  109. //
  110. len = mbedtls_mpi_size( X );
  111. if( *p < start || (size_t)( *p - start ) < len )
  112. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  113. (*p) -= len;
  114. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
  115. // DER format assumes 2s complement for numbers, so the leftmost bit
  116. // should be 0 for positive numbers and 1 for negative numbers.
  117. //
  118. if( X->s ==1 && **p & 0x80 )
  119. {
  120. if( *p - start < 1 )
  121. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  122. *--(*p) = 0x00;
  123. len += 1;
  124. }
  125. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  126. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  127. ret = (int) len;
  128. cleanup:
  129. return( ret );
  130. }
  131. #endif /* MBEDTLS_BIGNUM_C */
  132. int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
  133. {
  134. int ret;
  135. size_t len = 0;
  136. // Write NULL
  137. //
  138. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
  139. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
  140. return( (int) len );
  141. }
  142. int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
  143. const char *oid, size_t oid_len )
  144. {
  145. int ret;
  146. size_t len = 0;
  147. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  148. (const unsigned char *) oid, oid_len ) );
  149. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
  150. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
  151. return( (int) len );
  152. }
  153. int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
  154. const char *oid, size_t oid_len,
  155. size_t par_len )
  156. {
  157. int ret;
  158. size_t len = 0;
  159. if( par_len == 0 )
  160. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
  161. else
  162. len += par_len;
  163. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  164. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  165. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
  166. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  167. return( (int) len );
  168. }
  169. int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
  170. {
  171. int ret;
  172. size_t len = 0;
  173. if( *p - start < 1 )
  174. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  175. *--(*p) = (boolean) ? 255 : 0;
  176. len++;
  177. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  178. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
  179. return( (int) len );
  180. }
  181. int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
  182. {
  183. int ret;
  184. size_t len = 0;
  185. // TODO negative values and values larger than 128
  186. // DER format assumes 2s complement for numbers, so the leftmost bit
  187. // should be 0 for positive numbers and 1 for negative numbers.
  188. //
  189. if( *p - start < 1 )
  190. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  191. len += 1;
  192. *--(*p) = val;
  193. if( val > 0 && **p & 0x80 )
  194. {
  195. if( *p - start < 1 )
  196. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  197. *--(*p) = 0x00;
  198. len += 1;
  199. }
  200. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  201. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  202. return( (int) len );
  203. }
  204. int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
  205. const char *text, size_t text_len )
  206. {
  207. int ret;
  208. size_t len = 0;
  209. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  210. (const unsigned char *) text, text_len ) );
  211. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  212. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
  213. return( (int) len );
  214. }
  215. int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
  216. const char *text, size_t text_len )
  217. {
  218. int ret;
  219. size_t len = 0;
  220. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  221. (const unsigned char *) text, text_len ) );
  222. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  223. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
  224. return( (int) len );
  225. }
  226. int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
  227. const unsigned char *buf, size_t bits )
  228. {
  229. int ret;
  230. size_t len = 0, size;
  231. size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
  232. // Calculate byte length
  233. //
  234. if( *p < start || (size_t)( *p - start ) < size + 1 )
  235. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  236. len = size + 1;
  237. (*p) -= size;
  238. memcpy( *p, buf, size );
  239. // Write unused bits
  240. //
  241. *--(*p) = (unsigned char) (size * 8 - bits);
  242. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  243. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
  244. return( (int) len );
  245. }
  246. int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
  247. const unsigned char *buf, size_t size )
  248. {
  249. int ret;
  250. size_t len = 0;
  251. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
  252. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  253. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
  254. return( (int) len );
  255. }
  256. mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
  257. const char *oid, size_t oid_len,
  258. const unsigned char *val,
  259. size_t val_len )
  260. {
  261. mbedtls_asn1_named_data *cur;
  262. if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
  263. {
  264. // Add new entry if not present yet based on OID
  265. //
  266. cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
  267. sizeof(mbedtls_asn1_named_data) );
  268. if( cur == NULL )
  269. return( NULL );
  270. cur->oid.len = oid_len;
  271. cur->oid.p = mbedtls_calloc( 1, oid_len );
  272. if( cur->oid.p == NULL )
  273. {
  274. mbedtls_free( cur );
  275. return( NULL );
  276. }
  277. memcpy( cur->oid.p, oid, oid_len );
  278. cur->val.len = val_len;
  279. cur->val.p = mbedtls_calloc( 1, val_len );
  280. if( cur->val.p == NULL )
  281. {
  282. mbedtls_free( cur->oid.p );
  283. mbedtls_free( cur );
  284. return( NULL );
  285. }
  286. cur->next = *head;
  287. *head = cur;
  288. }
  289. else if( cur->val.len < val_len )
  290. {
  291. /*
  292. * Enlarge existing value buffer if needed
  293. * Preserve old data until the allocation succeeded, to leave list in
  294. * a consistent state in case allocation fails.
  295. */
  296. void *p = mbedtls_calloc( 1, val_len );
  297. if( p == NULL )
  298. return( NULL );
  299. mbedtls_free( cur->val.p );
  300. cur->val.p = p;
  301. cur->val.len = val_len;
  302. }
  303. if( val != NULL )
  304. memcpy( cur->val.p, val, val_len );
  305. return( cur );
  306. }
  307. #endif /* MBEDTLS_ASN1_WRITE_C */