2
0

evp_asn1.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1.h>
  12. #include <openssl/asn1t.h>
  13. int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
  14. {
  15. ASN1_STRING *os;
  16. if ((os = ASN1_OCTET_STRING_new()) == NULL)
  17. return 0;
  18. if (!ASN1_OCTET_STRING_set(os, data, len)) {
  19. ASN1_OCTET_STRING_free(os);
  20. return 0;
  21. }
  22. ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os);
  23. return 1;
  24. }
  25. /* int max_len: for returned value */
  26. int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len)
  27. {
  28. int ret, num;
  29. const unsigned char *p;
  30. if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
  31. ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
  32. return -1;
  33. }
  34. p = ASN1_STRING_get0_data(a->value.octet_string);
  35. ret = ASN1_STRING_length(a->value.octet_string);
  36. if (ret < max_len)
  37. num = ret;
  38. else
  39. num = max_len;
  40. memcpy(data, p, num);
  41. return ret;
  42. }
  43. typedef struct {
  44. int32_t num;
  45. ASN1_OCTET_STRING *oct;
  46. } asn1_int_oct;
  47. ASN1_SEQUENCE(asn1_int_oct) = {
  48. ASN1_EMBED(asn1_int_oct, num, INT32),
  49. ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
  50. } static_ASN1_SEQUENCE_END(asn1_int_oct)
  51. DECLARE_ASN1_ITEM(asn1_int_oct)
  52. int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
  53. int len)
  54. {
  55. asn1_int_oct atmp;
  56. ASN1_OCTET_STRING oct;
  57. atmp.num = num;
  58. atmp.oct = &oct;
  59. oct.data = data;
  60. oct.type = V_ASN1_OCTET_STRING;
  61. oct.length = len;
  62. oct.flags = 0;
  63. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
  64. return 1;
  65. return 0;
  66. }
  67. /*
  68. * we return the actual length...
  69. */
  70. /* int max_len: for returned value */
  71. int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
  72. unsigned char *data, int max_len)
  73. {
  74. asn1_int_oct *atmp = NULL;
  75. int ret = -1, n;
  76. if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
  77. goto err;
  78. }
  79. atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
  80. if (atmp == NULL)
  81. goto err;
  82. if (num != NULL)
  83. *num = atmp->num;
  84. ret = ASN1_STRING_length(atmp->oct);
  85. if (max_len > ret)
  86. n = ret;
  87. else
  88. n = max_len;
  89. if (data != NULL)
  90. memcpy(data, ASN1_STRING_get0_data(atmp->oct), n);
  91. if (ret == -1) {
  92. err:
  93. ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
  94. }
  95. M_ASN1_free_of(atmp, asn1_int_oct);
  96. return ret;
  97. }