a_type.c 3.4 KB

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