asn1write.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 SIZE_MAX > 0xFFFFFFFF
  73. if( len <= 0xFFFFFFFF )
  74. #endif
  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. #if SIZE_MAX > 0xFFFFFFFF
  86. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  87. #endif
  88. }
  89. int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
  90. {
  91. if( *p - start < 1 )
  92. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  93. *--(*p) = tag;
  94. return( 1 );
  95. }
  96. int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
  97. const unsigned char *buf, size_t size )
  98. {
  99. size_t len = 0;
  100. if( *p < start || (size_t)( *p - start ) < size )
  101. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  102. len = size;
  103. (*p) -= len;
  104. memcpy( *p, buf, len );
  105. return( (int) len );
  106. }
  107. #if defined(MBEDTLS_BIGNUM_C)
  108. int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
  109. {
  110. int ret;
  111. size_t len = 0;
  112. // Write the MPI
  113. //
  114. len = mbedtls_mpi_size( X );
  115. if( *p < start || (size_t)( *p - start ) < len )
  116. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  117. (*p) -= len;
  118. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
  119. // DER format assumes 2s complement for numbers, so the leftmost bit
  120. // should be 0 for positive numbers and 1 for negative numbers.
  121. //
  122. if( X->s ==1 && **p & 0x80 )
  123. {
  124. if( *p - start < 1 )
  125. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  126. *--(*p) = 0x00;
  127. len += 1;
  128. }
  129. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  130. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  131. ret = (int) len;
  132. cleanup:
  133. return( ret );
  134. }
  135. #endif /* MBEDTLS_BIGNUM_C */
  136. int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
  137. {
  138. int ret;
  139. size_t len = 0;
  140. // Write NULL
  141. //
  142. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
  143. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
  144. return( (int) len );
  145. }
  146. int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
  147. const char *oid, size_t oid_len )
  148. {
  149. int ret;
  150. size_t len = 0;
  151. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  152. (const unsigned char *) oid, oid_len ) );
  153. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
  154. MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
  155. return( (int) len );
  156. }
  157. int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
  158. const char *oid, size_t oid_len,
  159. size_t par_len )
  160. {
  161. int ret;
  162. size_t len = 0;
  163. if( par_len == 0 )
  164. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
  165. else
  166. len += par_len;
  167. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  168. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  169. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
  170. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  171. return( (int) len );
  172. }
  173. int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
  174. {
  175. int ret;
  176. size_t len = 0;
  177. if( *p - start < 1 )
  178. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  179. *--(*p) = (boolean) ? 255 : 0;
  180. len++;
  181. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  182. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
  183. return( (int) len );
  184. }
  185. int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
  186. {
  187. int ret;
  188. size_t len = 0;
  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_tagged_string( unsigned char **p, unsigned char *start, int tag,
  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, tag ) );
  213. return( (int) len );
  214. }
  215. int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
  216. const char *text, size_t text_len )
  217. {
  218. return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
  219. }
  220. int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
  221. const char *text, size_t text_len )
  222. {
  223. return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
  224. }
  225. int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
  226. const char *text, size_t text_len )
  227. {
  228. return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
  229. }
  230. int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
  231. const unsigned char *buf, size_t bits )
  232. {
  233. int ret;
  234. size_t len = 0;
  235. size_t unused_bits, byte_len;
  236. byte_len = ( bits + 7 ) / 8;
  237. unused_bits = ( byte_len * 8 ) - bits;
  238. if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
  239. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  240. len = byte_len + 1;
  241. /* Write the bitstring. Ensure the unused bits are zeroed */
  242. if( byte_len > 0 )
  243. {
  244. byte_len--;
  245. *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
  246. ( *p ) -= byte_len;
  247. memcpy( *p, buf, byte_len );
  248. }
  249. /* Write unused bits */
  250. *--( *p ) = (unsigned char)unused_bits;
  251. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  252. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
  253. return( (int) len );
  254. }
  255. int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
  256. const unsigned char *buf, size_t size )
  257. {
  258. int ret;
  259. size_t len = 0;
  260. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
  261. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  262. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
  263. return( (int) len );
  264. }
  265. /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
  266. * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
  267. static mbedtls_asn1_named_data *asn1_find_named_data(
  268. mbedtls_asn1_named_data *list,
  269. const char *oid, size_t len )
  270. {
  271. while( list != NULL )
  272. {
  273. if( list->oid.len == len &&
  274. memcmp( list->oid.p, oid, len ) == 0 )
  275. {
  276. break;
  277. }
  278. list = list->next;
  279. }
  280. return( list );
  281. }
  282. mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
  283. mbedtls_asn1_named_data **head,
  284. const char *oid, size_t oid_len,
  285. const unsigned char *val,
  286. size_t val_len )
  287. {
  288. mbedtls_asn1_named_data *cur;
  289. if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
  290. {
  291. // Add new entry if not present yet based on OID
  292. //
  293. cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
  294. sizeof(mbedtls_asn1_named_data) );
  295. if( cur == NULL )
  296. return( NULL );
  297. cur->oid.len = oid_len;
  298. cur->oid.p = mbedtls_calloc( 1, oid_len );
  299. if( cur->oid.p == NULL )
  300. {
  301. mbedtls_free( cur );
  302. return( NULL );
  303. }
  304. memcpy( cur->oid.p, oid, oid_len );
  305. cur->val.len = val_len;
  306. cur->val.p = mbedtls_calloc( 1, val_len );
  307. if( cur->val.p == NULL )
  308. {
  309. mbedtls_free( cur->oid.p );
  310. mbedtls_free( cur );
  311. return( NULL );
  312. }
  313. cur->next = *head;
  314. *head = cur;
  315. }
  316. else if( cur->val.len < val_len )
  317. {
  318. /*
  319. * Enlarge existing value buffer if needed
  320. * Preserve old data until the allocation succeeded, to leave list in
  321. * a consistent state in case allocation fails.
  322. */
  323. void *p = mbedtls_calloc( 1, val_len );
  324. if( p == NULL )
  325. return( NULL );
  326. mbedtls_free( cur->val.p );
  327. cur->val.p = p;
  328. cur->val.len = val_len;
  329. }
  330. if( val != NULL )
  331. memcpy( cur->val.p, val, val_len );
  332. return( cur );
  333. }
  334. #endif /* MBEDTLS_ASN1_WRITE_C */