2
0

der_writer.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/der.h"
  13. #include "crypto/bn.h"
  14. static int int_start_context(WPACKET *pkt, int tag)
  15. {
  16. if (tag < 0)
  17. return 1;
  18. if (!ossl_assert(tag <= 30))
  19. return 0;
  20. return WPACKET_start_sub_packet(pkt);
  21. }
  22. static int int_end_context(WPACKET *pkt, int tag)
  23. {
  24. /*
  25. * If someone set the flag WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH on this
  26. * sub-packet and this sub-packet has nothing written to it, the DER length
  27. * will not be written, and the total written size will be unchanged before
  28. * and after WPACKET_close(). We use size1 and size2 to determine if
  29. * anything was written, and only write our tag if it has.
  30. *
  31. */
  32. size_t size1, size2;
  33. if (tag < 0)
  34. return 1;
  35. if (!ossl_assert(tag <= 30))
  36. return 0;
  37. /* Context specific are normally (?) constructed */
  38. tag |= DER_F_CONSTRUCTED | DER_C_CONTEXT;
  39. return WPACKET_get_total_written(pkt, &size1)
  40. && WPACKET_close(pkt)
  41. && WPACKET_get_total_written(pkt, &size2)
  42. && (size1 == size2 || WPACKET_put_bytes_u8(pkt, tag));
  43. }
  44. int ossl_DER_w_precompiled(WPACKET *pkt, int tag,
  45. const unsigned char *precompiled,
  46. size_t precompiled_n)
  47. {
  48. return int_start_context(pkt, tag)
  49. && WPACKET_memcpy(pkt, precompiled, precompiled_n)
  50. && int_end_context(pkt, tag);
  51. }
  52. int ossl_DER_w_boolean(WPACKET *pkt, int tag, int b)
  53. {
  54. return int_start_context(pkt, tag)
  55. && WPACKET_start_sub_packet(pkt)
  56. && (!b || WPACKET_put_bytes_u8(pkt, 0xFF))
  57. && !WPACKET_close(pkt)
  58. && !WPACKET_put_bytes_u8(pkt, DER_P_BOOLEAN)
  59. && int_end_context(pkt, tag);
  60. }
  61. int ossl_DER_w_octet_string(WPACKET *pkt, int tag,
  62. const unsigned char *data, size_t data_n)
  63. {
  64. return int_start_context(pkt, tag)
  65. && WPACKET_start_sub_packet(pkt)
  66. && WPACKET_memcpy(pkt, data, data_n)
  67. && WPACKET_close(pkt)
  68. && WPACKET_put_bytes_u8(pkt, DER_P_OCTET_STRING)
  69. && int_end_context(pkt, tag);
  70. }
  71. int ossl_DER_w_octet_string_uint32(WPACKET *pkt, int tag, uint32_t value)
  72. {
  73. unsigned char tmp[4] = { 0, 0, 0, 0 };
  74. unsigned char *pbuf = tmp + (sizeof(tmp) - 1);
  75. while (value > 0) {
  76. *pbuf-- = (value & 0xFF);
  77. value >>= 8;
  78. }
  79. return ossl_DER_w_octet_string(pkt, tag, tmp, sizeof(tmp));
  80. }
  81. static int int_der_w_integer(WPACKET *pkt, int tag,
  82. int (*put_bytes)(WPACKET *pkt, const void *v,
  83. unsigned int *top_byte),
  84. const void *v)
  85. {
  86. unsigned int top_byte = 0;
  87. return int_start_context(pkt, tag)
  88. && WPACKET_start_sub_packet(pkt)
  89. && put_bytes(pkt, v, &top_byte)
  90. && ((top_byte & 0x80) == 0 || WPACKET_put_bytes_u8(pkt, 0))
  91. && WPACKET_close(pkt)
  92. && WPACKET_put_bytes_u8(pkt, DER_P_INTEGER)
  93. && int_end_context(pkt, tag);
  94. }
  95. static int int_put_bytes_uint32(WPACKET *pkt, const void *v,
  96. unsigned int *top_byte)
  97. {
  98. const uint32_t *value = v;
  99. uint32_t tmp = *value;
  100. size_t n = 0;
  101. while (tmp != 0) {
  102. n++;
  103. *top_byte = (tmp & 0xFF);
  104. tmp >>= 8;
  105. }
  106. if (n == 0)
  107. n = 1;
  108. return WPACKET_put_bytes__(pkt, *value, n);
  109. }
  110. /* For integers, we only support unsigned values for now */
  111. int ossl_DER_w_uint32(WPACKET *pkt, int tag, uint32_t v)
  112. {
  113. return int_der_w_integer(pkt, tag, int_put_bytes_uint32, &v);
  114. }
  115. static int int_put_bytes_bn(WPACKET *pkt, const void *v,
  116. unsigned int *top_byte)
  117. {
  118. unsigned char *p = NULL;
  119. size_t n = BN_num_bytes(v);
  120. /* The BIGNUM limbs are in LE order */
  121. *top_byte =
  122. ((bn_get_words(v) [(n - 1) / BN_BYTES]) >> (8 * ((n - 1) % BN_BYTES)))
  123. & 0xFF;
  124. if (!WPACKET_allocate_bytes(pkt, n, &p))
  125. return 0;
  126. if (p != NULL)
  127. BN_bn2bin(v, p);
  128. return 1;
  129. }
  130. int ossl_DER_w_bn(WPACKET *pkt, int tag, const BIGNUM *v)
  131. {
  132. if (v == NULL || BN_is_negative(v))
  133. return 0;
  134. if (BN_is_zero(v))
  135. return ossl_DER_w_uint32(pkt, tag, 0);
  136. return int_der_w_integer(pkt, tag, int_put_bytes_bn, v);
  137. }
  138. int ossl_DER_w_null(WPACKET *pkt, int tag)
  139. {
  140. return int_start_context(pkt, tag)
  141. && WPACKET_start_sub_packet(pkt)
  142. && WPACKET_close(pkt)
  143. && WPACKET_put_bytes_u8(pkt, DER_P_NULL)
  144. && int_end_context(pkt, tag);
  145. }
  146. /* Constructed things need a start and an end */
  147. int ossl_DER_w_begin_sequence(WPACKET *pkt, int tag)
  148. {
  149. return int_start_context(pkt, tag)
  150. && WPACKET_start_sub_packet(pkt);
  151. }
  152. int ossl_DER_w_end_sequence(WPACKET *pkt, int tag)
  153. {
  154. /*
  155. * If someone set the flag WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH on this
  156. * sub-packet and this sub-packet has nothing written to it, the DER length
  157. * will not be written, and the total written size will be unchanged before
  158. * and after WPACKET_close(). We use size1 and size2 to determine if
  159. * anything was written, and only write our tag if it has.
  160. *
  161. * Because we know that int_end_context() needs to do the same check,
  162. * we reproduce this flag if the written length was unchanged, or we will
  163. * have an erroneous context tag.
  164. */
  165. size_t size1, size2;
  166. return WPACKET_get_total_written(pkt, &size1)
  167. && WPACKET_close(pkt)
  168. && WPACKET_get_total_written(pkt, &size2)
  169. && (size1 == size2
  170. ? WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
  171. : WPACKET_put_bytes_u8(pkt, DER_F_CONSTRUCTED | DER_P_SEQUENCE))
  172. && int_end_context(pkt, tag);
  173. }