asn1write.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * ASN.1 buffer writing functionality
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_ASN1_WRITE_C)
  52. #include "mbedtls/asn1write.h"
  53. #include <string.h>
  54. #if defined(MBEDTLS_PLATFORM_C)
  55. #include "mbedtls/platform.h"
  56. #else
  57. #include <stdlib.h>
  58. #define mbedtls_calloc calloc
  59. #define mbedtls_free free
  60. #endif
  61. int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
  62. {
  63. if( len < 0x80 )
  64. {
  65. if( *p - start < 1 )
  66. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  67. *--(*p) = (unsigned char) len;
  68. return( 1 );
  69. }
  70. if( len <= 0xFF )
  71. {
  72. if( *p - start < 2 )
  73. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  74. *--(*p) = (unsigned char) len;
  75. *--(*p) = 0x81;
  76. return( 2 );
  77. }
  78. if( len <= 0xFFFF )
  79. {
  80. if( *p - start < 3 )
  81. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  82. *--(*p) = ( len ) & 0xFF;
  83. *--(*p) = ( len >> 8 ) & 0xFF;
  84. *--(*p) = 0x82;
  85. return( 3 );
  86. }
  87. if( len <= 0xFFFFFF )
  88. {
  89. if( *p - start < 4 )
  90. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  91. *--(*p) = ( len ) & 0xFF;
  92. *--(*p) = ( len >> 8 ) & 0xFF;
  93. *--(*p) = ( len >> 16 ) & 0xFF;
  94. *--(*p) = 0x83;
  95. return( 4 );
  96. }
  97. #if SIZE_MAX > 0xFFFFFFFF
  98. if( len <= 0xFFFFFFFF )
  99. #endif
  100. {
  101. if( *p - start < 5 )
  102. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  103. *--(*p) = ( len ) & 0xFF;
  104. *--(*p) = ( len >> 8 ) & 0xFF;
  105. *--(*p) = ( len >> 16 ) & 0xFF;
  106. *--(*p) = ( len >> 24 ) & 0xFF;
  107. *--(*p) = 0x84;
  108. return( 5 );
  109. }
  110. #if SIZE_MAX > 0xFFFFFFFF
  111. return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  112. #endif
  113. }
  114. int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
  115. {
  116. if( *p - start < 1 )
  117. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  118. *--(*p) = tag;
  119. return( 1 );
  120. }
  121. int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
  122. const unsigned char *buf, size_t size )
  123. {
  124. size_t len = 0;
  125. if( *p < start || (size_t)( *p - start ) < size )
  126. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  127. len = size;
  128. (*p) -= len;
  129. memcpy( *p, buf, len );
  130. return( (int) len );
  131. }
  132. #if defined(MBEDTLS_BIGNUM_C)
  133. int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
  134. {
  135. int ret;
  136. size_t len = 0;
  137. // Write the MPI
  138. //
  139. len = mbedtls_mpi_size( X );
  140. if( *p < start || (size_t)( *p - start ) < len )
  141. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  142. (*p) -= len;
  143. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
  144. // DER format assumes 2s complement for numbers, so the leftmost bit
  145. // should be 0 for positive numbers and 1 for negative numbers.
  146. //
  147. if( X->s ==1 && **p & 0x80 )
  148. {
  149. if( *p - start < 1 )
  150. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  151. *--(*p) = 0x00;
  152. len += 1;
  153. }
  154. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  155. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  156. ret = (int) len;
  157. cleanup:
  158. return( ret );
  159. }
  160. #endif /* MBEDTLS_BIGNUM_C */
  161. int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
  162. {
  163. int ret;
  164. size_t len = 0;
  165. // Write NULL
  166. //
  167. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
  168. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
  169. return( (int) len );
  170. }
  171. int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
  172. const char *oid, size_t oid_len )
  173. {
  174. int ret;
  175. size_t len = 0;
  176. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  177. (const unsigned char *) oid, oid_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_OID ) );
  180. return( (int) len );
  181. }
  182. int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
  183. const char *oid, size_t oid_len,
  184. size_t par_len )
  185. {
  186. int ret;
  187. size_t len = 0;
  188. if( par_len == 0 )
  189. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
  190. else
  191. len += par_len;
  192. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  193. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  194. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
  195. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  196. return( (int) len );
  197. }
  198. int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
  199. {
  200. int ret;
  201. size_t len = 0;
  202. if( *p - start < 1 )
  203. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  204. *--(*p) = (boolean) ? 255 : 0;
  205. len++;
  206. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  207. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
  208. return( (int) len );
  209. }
  210. int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
  211. {
  212. int ret;
  213. size_t len = 0;
  214. if( *p - start < 1 )
  215. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  216. len += 1;
  217. *--(*p) = val;
  218. if( val > 0 && **p & 0x80 )
  219. {
  220. if( *p - start < 1 )
  221. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  222. *--(*p) = 0x00;
  223. len += 1;
  224. }
  225. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  226. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
  227. return( (int) len );
  228. }
  229. int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
  230. const char *text, size_t text_len )
  231. {
  232. int ret;
  233. size_t len = 0;
  234. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  235. (const unsigned char *) text, text_len ) );
  236. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  237. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
  238. return( (int) len );
  239. }
  240. int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
  241. const char *text, size_t text_len )
  242. {
  243. return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
  244. }
  245. int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
  246. const char *text, size_t text_len )
  247. {
  248. return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
  249. }
  250. int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
  251. const char *text, size_t text_len )
  252. {
  253. return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
  254. }
  255. int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
  256. const unsigned char *buf, size_t bits )
  257. {
  258. int ret;
  259. size_t len = 0;
  260. size_t unused_bits, byte_len;
  261. byte_len = ( bits + 7 ) / 8;
  262. unused_bits = ( byte_len * 8 ) - bits;
  263. if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
  264. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  265. len = byte_len + 1;
  266. /* Write the bitstring. Ensure the unused bits are zeroed */
  267. if( byte_len > 0 )
  268. {
  269. byte_len--;
  270. *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
  271. ( *p ) -= byte_len;
  272. memcpy( *p, buf, byte_len );
  273. }
  274. /* Write unused bits */
  275. *--( *p ) = (unsigned char)unused_bits;
  276. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  277. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
  278. return( (int) len );
  279. }
  280. int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
  281. const unsigned char *buf, size_t size )
  282. {
  283. int ret;
  284. size_t len = 0;
  285. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
  286. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  287. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
  288. return( (int) len );
  289. }
  290. /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
  291. * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
  292. static mbedtls_asn1_named_data *asn1_find_named_data(
  293. mbedtls_asn1_named_data *list,
  294. const char *oid, size_t len )
  295. {
  296. while( list != NULL )
  297. {
  298. if( list->oid.len == len &&
  299. memcmp( list->oid.p, oid, len ) == 0 )
  300. {
  301. break;
  302. }
  303. list = list->next;
  304. }
  305. return( list );
  306. }
  307. mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
  308. mbedtls_asn1_named_data **head,
  309. const char *oid, size_t oid_len,
  310. const unsigned char *val,
  311. size_t val_len )
  312. {
  313. mbedtls_asn1_named_data *cur;
  314. if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
  315. {
  316. // Add new entry if not present yet based on OID
  317. //
  318. cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
  319. sizeof(mbedtls_asn1_named_data) );
  320. if( cur == NULL )
  321. return( NULL );
  322. cur->oid.len = oid_len;
  323. cur->oid.p = mbedtls_calloc( 1, oid_len );
  324. if( cur->oid.p == NULL )
  325. {
  326. mbedtls_free( cur );
  327. return( NULL );
  328. }
  329. memcpy( cur->oid.p, oid, oid_len );
  330. cur->val.len = val_len;
  331. cur->val.p = mbedtls_calloc( 1, val_len );
  332. if( cur->val.p == NULL )
  333. {
  334. mbedtls_free( cur->oid.p );
  335. mbedtls_free( cur );
  336. return( NULL );
  337. }
  338. cur->next = *head;
  339. *head = cur;
  340. }
  341. else if( cur->val.len < val_len )
  342. {
  343. /*
  344. * Enlarge existing value buffer if needed
  345. * Preserve old data until the allocation succeeded, to leave list in
  346. * a consistent state in case allocation fails.
  347. */
  348. void *p = mbedtls_calloc( 1, val_len );
  349. if( p == NULL )
  350. return( NULL );
  351. mbedtls_free( cur->val.p );
  352. cur->val.p = p;
  353. cur->val.len = val_len;
  354. }
  355. if( val != NULL )
  356. memcpy( cur->val.p, val, val_len );
  357. return( cur );
  358. }
  359. #endif /* MBEDTLS_ASN1_WRITE_C */