der_writer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if (tag < 0)
  25. return 1;
  26. if (!ossl_assert(tag <= 30))
  27. return 0;
  28. return WPACKET_close(pkt)
  29. && WPACKET_put_bytes_u8(pkt, DER_C_CONTEXT | tag);
  30. }
  31. int DER_w_precompiled(WPACKET *pkt, int tag,
  32. const unsigned char *precompiled, size_t precompiled_n)
  33. {
  34. return int_start_context(pkt, tag)
  35. && WPACKET_memcpy(pkt, precompiled, precompiled_n)
  36. && int_end_context(pkt, tag);
  37. }
  38. int DER_w_boolean(WPACKET *pkt, int tag, int b)
  39. {
  40. return int_start_context(pkt, tag)
  41. && WPACKET_start_sub_packet(pkt)
  42. && (!b || WPACKET_put_bytes_u8(pkt, 0xFF))
  43. && !WPACKET_close(pkt)
  44. && !WPACKET_put_bytes_u8(pkt, DER_P_BOOLEAN)
  45. && int_end_context(pkt, tag);
  46. }
  47. static int int_der_w_integer(WPACKET *pkt, int tag,
  48. int (*put_bytes)(WPACKET *pkt, const void *v,
  49. unsigned int *top_byte),
  50. const void *v)
  51. {
  52. unsigned int top_byte = 0;
  53. return int_start_context(pkt, tag)
  54. && WPACKET_start_sub_packet(pkt)
  55. && put_bytes(pkt, v, &top_byte)
  56. && ((top_byte & 0x80) == 0 || WPACKET_put_bytes_u8(pkt, 0))
  57. && WPACKET_close(pkt)
  58. && WPACKET_put_bytes_u8(pkt, DER_P_INTEGER)
  59. && int_end_context(pkt, tag);
  60. }
  61. static int int_put_bytes_ulong(WPACKET *pkt, const void *v,
  62. unsigned int *top_byte)
  63. {
  64. const unsigned long *value = v;
  65. unsigned long tmp = *value;
  66. size_t n = 0;
  67. while (tmp != 0) {
  68. n++;
  69. *top_byte = (tmp & 0xFF);
  70. tmp >>= 8;
  71. }
  72. if (n == 0)
  73. n = 1;
  74. return WPACKET_put_bytes__(pkt, *value, n);
  75. }
  76. /* For integers, we only support unsigned values for now */
  77. int DER_w_ulong(WPACKET *pkt, int tag, unsigned long v)
  78. {
  79. return int_der_w_integer(pkt, tag, int_put_bytes_ulong, &v);
  80. }
  81. static int int_put_bytes_bn(WPACKET *pkt, const void *v,
  82. unsigned int *top_byte)
  83. {
  84. unsigned char *p = NULL;
  85. size_t n = BN_num_bytes(v);
  86. /* The BIGNUM limbs are in LE order */
  87. *top_byte =
  88. ((bn_get_words(v) [(n - 1) / BN_BYTES]) >> (8 * ((n - 1) % BN_BYTES)))
  89. & 0xFF;
  90. if (!WPACKET_allocate_bytes(pkt, n, &p))
  91. return 0;
  92. if (p != NULL)
  93. BN_bn2bin(v, p);
  94. return 1;
  95. }
  96. int DER_w_bn(WPACKET *pkt, int tag, const BIGNUM *v)
  97. {
  98. if (v == NULL || BN_is_negative(v))
  99. return 0;
  100. if (BN_is_zero(v))
  101. return DER_w_ulong(pkt, tag, 0);
  102. return int_der_w_integer(pkt, tag, int_put_bytes_bn, v);
  103. }
  104. int DER_w_null(WPACKET *pkt, int tag)
  105. {
  106. return int_start_context(pkt, tag)
  107. && WPACKET_start_sub_packet(pkt)
  108. && WPACKET_close(pkt)
  109. && WPACKET_put_bytes_u8(pkt, DER_P_NULL)
  110. && int_end_context(pkt, tag);
  111. }
  112. /* Constructed things need a start and an end */
  113. int DER_w_begin_sequence(WPACKET *pkt, int tag)
  114. {
  115. return int_start_context(pkt, tag)
  116. && WPACKET_start_sub_packet(pkt);
  117. }
  118. int DER_w_end_sequence(WPACKET *pkt, int tag)
  119. {
  120. return WPACKET_close(pkt)
  121. && WPACKET_put_bytes_u8(pkt, DER_F_CONSTRUCTED | DER_P_SEQUENCE)
  122. && int_end_context(pkt, tag);
  123. }