der_writer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. return WPACKET_get_total_written(pkt, &size1)
  38. && WPACKET_close(pkt)
  39. && WPACKET_get_total_written(pkt, &size2)
  40. && (size1 == size2 || WPACKET_put_bytes_u8(pkt, DER_C_CONTEXT | tag));
  41. }
  42. int DER_w_precompiled(WPACKET *pkt, int tag,
  43. const unsigned char *precompiled, size_t precompiled_n)
  44. {
  45. return int_start_context(pkt, tag)
  46. && WPACKET_memcpy(pkt, precompiled, precompiled_n)
  47. && int_end_context(pkt, tag);
  48. }
  49. int DER_w_boolean(WPACKET *pkt, int tag, int b)
  50. {
  51. return int_start_context(pkt, tag)
  52. && WPACKET_start_sub_packet(pkt)
  53. && (!b || WPACKET_put_bytes_u8(pkt, 0xFF))
  54. && !WPACKET_close(pkt)
  55. && !WPACKET_put_bytes_u8(pkt, DER_P_BOOLEAN)
  56. && int_end_context(pkt, tag);
  57. }
  58. static int int_der_w_integer(WPACKET *pkt, int tag,
  59. int (*put_bytes)(WPACKET *pkt, const void *v,
  60. unsigned int *top_byte),
  61. const void *v)
  62. {
  63. unsigned int top_byte = 0;
  64. return int_start_context(pkt, tag)
  65. && WPACKET_start_sub_packet(pkt)
  66. && put_bytes(pkt, v, &top_byte)
  67. && ((top_byte & 0x80) == 0 || WPACKET_put_bytes_u8(pkt, 0))
  68. && WPACKET_close(pkt)
  69. && WPACKET_put_bytes_u8(pkt, DER_P_INTEGER)
  70. && int_end_context(pkt, tag);
  71. }
  72. static int int_put_bytes_ulong(WPACKET *pkt, const void *v,
  73. unsigned int *top_byte)
  74. {
  75. const unsigned long *value = v;
  76. unsigned long tmp = *value;
  77. size_t n = 0;
  78. while (tmp != 0) {
  79. n++;
  80. *top_byte = (tmp & 0xFF);
  81. tmp >>= 8;
  82. }
  83. if (n == 0)
  84. n = 1;
  85. return WPACKET_put_bytes__(pkt, *value, n);
  86. }
  87. /* For integers, we only support unsigned values for now */
  88. int DER_w_ulong(WPACKET *pkt, int tag, unsigned long v)
  89. {
  90. return int_der_w_integer(pkt, tag, int_put_bytes_ulong, &v);
  91. }
  92. static int int_put_bytes_bn(WPACKET *pkt, const void *v,
  93. unsigned int *top_byte)
  94. {
  95. unsigned char *p = NULL;
  96. size_t n = BN_num_bytes(v);
  97. /* The BIGNUM limbs are in LE order */
  98. *top_byte =
  99. ((bn_get_words(v) [(n - 1) / BN_BYTES]) >> (8 * ((n - 1) % BN_BYTES)))
  100. & 0xFF;
  101. if (!WPACKET_allocate_bytes(pkt, n, &p))
  102. return 0;
  103. if (p != NULL)
  104. BN_bn2bin(v, p);
  105. return 1;
  106. }
  107. int DER_w_bn(WPACKET *pkt, int tag, const BIGNUM *v)
  108. {
  109. if (v == NULL || BN_is_negative(v))
  110. return 0;
  111. if (BN_is_zero(v))
  112. return DER_w_ulong(pkt, tag, 0);
  113. return int_der_w_integer(pkt, tag, int_put_bytes_bn, v);
  114. }
  115. int DER_w_null(WPACKET *pkt, int tag)
  116. {
  117. return int_start_context(pkt, tag)
  118. && WPACKET_start_sub_packet(pkt)
  119. && WPACKET_close(pkt)
  120. && WPACKET_put_bytes_u8(pkt, DER_P_NULL)
  121. && int_end_context(pkt, tag);
  122. }
  123. /* Constructed things need a start and an end */
  124. int DER_w_begin_sequence(WPACKET *pkt, int tag)
  125. {
  126. return int_start_context(pkt, tag)
  127. && WPACKET_start_sub_packet(pkt);
  128. }
  129. int DER_w_end_sequence(WPACKET *pkt, int tag)
  130. {
  131. /*
  132. * If someone set the flag WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH on this
  133. * sub-packet and this sub-packet has nothing written to it, the DER length
  134. * will not be written, and the total written size will be unchanged before
  135. * and after WPACKET_close(). We use size1 and size2 to determine if
  136. * anything was written, and only write our tag if it has.
  137. *
  138. * Because we know that int_end_context() needs to do the same check,
  139. * we reproduce this flag if the written length was unchanged, or we will
  140. * have an erroneous context tag.
  141. */
  142. size_t size1, size2;
  143. return WPACKET_get_total_written(pkt, &size1)
  144. && WPACKET_close(pkt)
  145. && WPACKET_get_total_written(pkt, &size2)
  146. && (size1 == size2
  147. ? WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
  148. : WPACKET_put_bytes_u8(pkt, DER_F_CONSTRUCTED | DER_P_SEQUENCE))
  149. && int_end_context(pkt, tag);
  150. }