asn1write.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * ASN.1 buffer writing functionality
  3. *
  4. * Copyright (C) 2006-2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #if !defined(POLARSSL_CONFIG_FILE)
  26. #include "polarssl/config.h"
  27. #else
  28. #include POLARSSL_CONFIG_FILE
  29. #endif
  30. #if defined(POLARSSL_ASN1_WRITE_C)
  31. #include "polarssl/asn1write.h"
  32. #if defined(POLARSSL_PLATFORM_C)
  33. #include "polarssl/platform.h"
  34. #else
  35. #include <stdlib.h>
  36. #define polarssl_malloc malloc
  37. #define polarssl_free free
  38. #endif
  39. int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
  40. {
  41. if( len < 0x80 )
  42. {
  43. if( *p - start < 1 )
  44. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  45. *--(*p) = (unsigned char) len;
  46. return( 1 );
  47. }
  48. if( len <= 0xFF )
  49. {
  50. if( *p - start < 2 )
  51. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  52. *--(*p) = (unsigned char) len;
  53. *--(*p) = 0x81;
  54. return( 2 );
  55. }
  56. if( *p - start < 3 )
  57. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  58. // We assume we never have lengths larger than 65535 bytes
  59. //
  60. *--(*p) = len % 256;
  61. *--(*p) = ( len / 256 ) % 256;
  62. *--(*p) = 0x82;
  63. return( 3 );
  64. }
  65. int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
  66. {
  67. if( *p - start < 1 )
  68. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  69. *--(*p) = tag;
  70. return( 1 );
  71. }
  72. int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
  73. const unsigned char *buf, size_t size )
  74. {
  75. size_t len = 0;
  76. if( *p - start < (int) size )
  77. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  78. len = size;
  79. (*p) -= len;
  80. memcpy( *p, buf, len );
  81. return( (int) len );
  82. }
  83. #if defined(POLARSSL_BIGNUM_C)
  84. int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
  85. {
  86. int ret;
  87. size_t len = 0;
  88. // Write the MPI
  89. //
  90. len = mpi_size( X );
  91. if( *p - start < (int) len )
  92. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  93. (*p) -= len;
  94. MPI_CHK( mpi_write_binary( X, *p, len ) );
  95. // DER format assumes 2s complement for numbers, so the leftmost bit
  96. // should be 0 for positive numbers and 1 for negative numbers.
  97. //
  98. if( X->s ==1 && **p & 0x80 )
  99. {
  100. if( *p - start < 1 )
  101. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  102. *--(*p) = 0x00;
  103. len += 1;
  104. }
  105. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  106. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
  107. ret = (int) len;
  108. cleanup:
  109. return( ret );
  110. }
  111. #endif /* POLARSSL_BIGNUM_C */
  112. int asn1_write_null( unsigned char **p, unsigned char *start )
  113. {
  114. int ret;
  115. size_t len = 0;
  116. // Write NULL
  117. //
  118. ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
  119. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
  120. return( (int) len );
  121. }
  122. int asn1_write_oid( unsigned char **p, unsigned char *start,
  123. const char *oid, size_t oid_len )
  124. {
  125. int ret;
  126. size_t len = 0;
  127. ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
  128. (const unsigned char *) oid, oid_len ) );
  129. ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
  130. ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
  131. return( (int) len );
  132. }
  133. int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
  134. const char *oid, size_t oid_len,
  135. size_t par_len )
  136. {
  137. int ret;
  138. size_t len = 0;
  139. if( par_len == 0 )
  140. ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
  141. else
  142. len += par_len;
  143. ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
  144. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  145. ASN1_CHK_ADD( len, asn1_write_tag( p, start,
  146. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
  147. return( (int) len );
  148. }
  149. int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
  150. {
  151. int ret;
  152. size_t len = 0;
  153. if( *p - start < 1 )
  154. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  155. *--(*p) = (boolean) ? 1 : 0;
  156. len++;
  157. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  158. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
  159. return( (int) len );
  160. }
  161. int asn1_write_int( unsigned char **p, unsigned char *start, int val )
  162. {
  163. int ret;
  164. size_t len = 0;
  165. // TODO negative values and values larger than 128
  166. // DER format assumes 2s complement for numbers, so the leftmost bit
  167. // should be 0 for positive numbers and 1 for negative numbers.
  168. //
  169. if( *p - start < 1 )
  170. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  171. len += 1;
  172. *--(*p) = val;
  173. if( val > 0 && **p & 0x80 )
  174. {
  175. if( *p - start < 1 )
  176. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  177. *--(*p) = 0x00;
  178. len += 1;
  179. }
  180. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  181. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
  182. return( (int) len );
  183. }
  184. int asn1_write_printable_string( unsigned char **p, unsigned char *start,
  185. const char *text, size_t text_len )
  186. {
  187. int ret;
  188. size_t len = 0;
  189. ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
  190. (const unsigned char *) text, text_len ) );
  191. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  192. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
  193. return( (int) len );
  194. }
  195. int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
  196. const char *text, size_t text_len )
  197. {
  198. int ret;
  199. size_t len = 0;
  200. ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
  201. (const unsigned char *) text, text_len ) );
  202. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  203. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
  204. return( (int) len );
  205. }
  206. int asn1_write_bitstring( unsigned char **p, unsigned char *start,
  207. const unsigned char *buf, size_t bits )
  208. {
  209. int ret;
  210. size_t len = 0, size;
  211. size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
  212. // Calculate byte length
  213. //
  214. if( *p - start < (int) size + 1 )
  215. return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
  216. len = size + 1;
  217. (*p) -= size;
  218. memcpy( *p, buf, size );
  219. // Write unused bits
  220. //
  221. *--(*p) = (unsigned char) (size * 8 - bits);
  222. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  223. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
  224. return( (int) len );
  225. }
  226. int asn1_write_octet_string( unsigned char **p, unsigned char *start,
  227. const unsigned char *buf, size_t size )
  228. {
  229. int ret;
  230. size_t len = 0;
  231. ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
  232. ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
  233. ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
  234. return( (int) len );
  235. }
  236. asn1_named_data *asn1_store_named_data( asn1_named_data **head,
  237. const char *oid, size_t oid_len,
  238. const unsigned char *val,
  239. size_t val_len )
  240. {
  241. asn1_named_data *cur;
  242. if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
  243. {
  244. // Add new entry if not present yet based on OID
  245. //
  246. if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
  247. return( NULL );
  248. memset( cur, 0, sizeof(asn1_named_data) );
  249. cur->oid.len = oid_len;
  250. cur->oid.p = polarssl_malloc( oid_len );
  251. if( cur->oid.p == NULL )
  252. {
  253. polarssl_free( cur );
  254. return( NULL );
  255. }
  256. cur->val.len = val_len;
  257. cur->val.p = polarssl_malloc( val_len );
  258. if( cur->val.p == NULL )
  259. {
  260. polarssl_free( cur->oid.p );
  261. polarssl_free( cur );
  262. return( NULL );
  263. }
  264. memcpy( cur->oid.p, oid, oid_len );
  265. cur->next = *head;
  266. *head = cur;
  267. }
  268. else if( cur->val.len < val_len )
  269. {
  270. // Enlarge existing value buffer if needed
  271. //
  272. polarssl_free( cur->val.p );
  273. cur->val.p = NULL;
  274. cur->val.len = val_len;
  275. cur->val.p = polarssl_malloc( val_len );
  276. if( cur->val.p == NULL )
  277. {
  278. polarssl_free( cur->oid.p );
  279. polarssl_free( cur );
  280. return( NULL );
  281. }
  282. }
  283. if( val != NULL )
  284. memcpy( cur->val.p, val, val_len );
  285. return( cur );
  286. }
  287. #endif /* POLARSSL_ASN1_WRITE_C */