a_type.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 1995-2021 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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/objects.h>
  13. #include "asn1_local.h"
  14. int ASN1_TYPE_get(const ASN1_TYPE *a)
  15. {
  16. if (a->type == V_ASN1_BOOLEAN
  17. || a->type == V_ASN1_NULL
  18. || a->value.ptr != NULL)
  19. return a->type;
  20. else
  21. return 0;
  22. }
  23. void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
  24. {
  25. if (a->type != V_ASN1_BOOLEAN
  26. && a->type != V_ASN1_NULL
  27. && a->value.ptr != NULL) {
  28. ASN1_TYPE **tmp_a = &a;
  29. ossl_asn1_primitive_free((ASN1_VALUE **)tmp_a, NULL, 0);
  30. }
  31. a->type = type;
  32. if (type == V_ASN1_BOOLEAN)
  33. a->value.boolean = value ? 0xff : 0;
  34. else
  35. a->value.ptr = value;
  36. }
  37. int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value)
  38. {
  39. if (!value || (type == V_ASN1_BOOLEAN)) {
  40. void *p = (void *)value;
  41. ASN1_TYPE_set(a, type, p);
  42. } else if (type == V_ASN1_OBJECT) {
  43. ASN1_OBJECT *odup;
  44. odup = OBJ_dup(value);
  45. if (!odup)
  46. return 0;
  47. ASN1_TYPE_set(a, type, odup);
  48. } else {
  49. ASN1_STRING *sdup;
  50. sdup = ASN1_STRING_dup(value);
  51. if (!sdup)
  52. return 0;
  53. ASN1_TYPE_set(a, type, sdup);
  54. }
  55. return 1;
  56. }
  57. /* Returns 0 if they are equal, != 0 otherwise. */
  58. int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
  59. {
  60. int result = -1;
  61. if (!a || !b || a->type != b->type)
  62. return -1;
  63. switch (a->type) {
  64. case V_ASN1_OBJECT:
  65. result = OBJ_cmp(a->value.object, b->value.object);
  66. break;
  67. case V_ASN1_BOOLEAN:
  68. result = a->value.boolean - b->value.boolean;
  69. break;
  70. case V_ASN1_NULL:
  71. result = 0; /* They do not have content. */
  72. break;
  73. case V_ASN1_INTEGER:
  74. case V_ASN1_ENUMERATED:
  75. case V_ASN1_BIT_STRING:
  76. case V_ASN1_OCTET_STRING:
  77. case V_ASN1_SEQUENCE:
  78. case V_ASN1_SET:
  79. case V_ASN1_NUMERICSTRING:
  80. case V_ASN1_PRINTABLESTRING:
  81. case V_ASN1_T61STRING:
  82. case V_ASN1_VIDEOTEXSTRING:
  83. case V_ASN1_IA5STRING:
  84. case V_ASN1_UTCTIME:
  85. case V_ASN1_GENERALIZEDTIME:
  86. case V_ASN1_GRAPHICSTRING:
  87. case V_ASN1_VISIBLESTRING:
  88. case V_ASN1_GENERALSTRING:
  89. case V_ASN1_UNIVERSALSTRING:
  90. case V_ASN1_BMPSTRING:
  91. case V_ASN1_UTF8STRING:
  92. case V_ASN1_OTHER:
  93. default:
  94. result = ASN1_STRING_cmp((ASN1_STRING *)a->value.ptr,
  95. (ASN1_STRING *)b->value.ptr);
  96. break;
  97. }
  98. return result;
  99. }
  100. ASN1_TYPE *ASN1_TYPE_pack_sequence(const ASN1_ITEM *it, void *s, ASN1_TYPE **t)
  101. {
  102. ASN1_OCTET_STRING *oct;
  103. ASN1_TYPE *rt;
  104. oct = ASN1_item_pack(s, it, NULL);
  105. if (oct == NULL)
  106. return NULL;
  107. if (t && *t) {
  108. rt = *t;
  109. } else {
  110. rt = ASN1_TYPE_new();
  111. if (rt == NULL) {
  112. ASN1_OCTET_STRING_free(oct);
  113. return NULL;
  114. }
  115. if (t)
  116. *t = rt;
  117. }
  118. ASN1_TYPE_set(rt, V_ASN1_SEQUENCE, oct);
  119. return rt;
  120. }
  121. void *ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t)
  122. {
  123. if (t == NULL || t->type != V_ASN1_SEQUENCE || t->value.sequence == NULL)
  124. return NULL;
  125. return ASN1_item_unpack(t->value.sequence, it);
  126. }