pcy_node.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2004-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 <openssl/asn1.h>
  10. #include <openssl/x509.h>
  11. #include <openssl/x509v3.h>
  12. #include "pcy_int.h"
  13. static int node_cmp(const X509_POLICY_NODE *const *a,
  14. const X509_POLICY_NODE *const *b)
  15. {
  16. return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy);
  17. }
  18. STACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void)
  19. {
  20. return sk_X509_POLICY_NODE_new(node_cmp);
  21. }
  22. X509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes,
  23. const ASN1_OBJECT *id)
  24. {
  25. X509_POLICY_DATA n;
  26. X509_POLICY_NODE l;
  27. int idx;
  28. n.valid_policy = (ASN1_OBJECT *)id;
  29. l.data = &n;
  30. idx = sk_X509_POLICY_NODE_find(nodes, &l);
  31. if (idx == -1)
  32. return NULL;
  33. return sk_X509_POLICY_NODE_value(nodes, idx);
  34. }
  35. X509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
  36. const X509_POLICY_NODE *parent,
  37. const ASN1_OBJECT *id)
  38. {
  39. X509_POLICY_NODE *node;
  40. int i;
  41. for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
  42. node = sk_X509_POLICY_NODE_value(level->nodes, i);
  43. if (node->parent == parent) {
  44. if (!OBJ_cmp(node->data->valid_policy, id))
  45. return node;
  46. }
  47. }
  48. return NULL;
  49. }
  50. X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
  51. X509_POLICY_DATA *data,
  52. X509_POLICY_NODE *parent,
  53. X509_POLICY_TREE *tree)
  54. {
  55. X509_POLICY_NODE *node;
  56. node = OPENSSL_zalloc(sizeof(*node));
  57. if (node == NULL)
  58. return NULL;
  59. node->data = data;
  60. node->parent = parent;
  61. if (level) {
  62. if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
  63. if (level->anyPolicy)
  64. goto node_error;
  65. level->anyPolicy = node;
  66. } else {
  67. if (level->nodes == NULL)
  68. level->nodes = policy_node_cmp_new();
  69. if (level->nodes == NULL)
  70. goto node_error;
  71. if (!sk_X509_POLICY_NODE_push(level->nodes, node))
  72. goto node_error;
  73. }
  74. }
  75. if (tree) {
  76. if (tree->extra_data == NULL)
  77. tree->extra_data = sk_X509_POLICY_DATA_new_null();
  78. if (tree->extra_data == NULL)
  79. goto node_error;
  80. if (!sk_X509_POLICY_DATA_push(tree->extra_data, data))
  81. goto node_error;
  82. }
  83. if (parent)
  84. parent->nchild++;
  85. return node;
  86. node_error:
  87. policy_node_free(node);
  88. return NULL;
  89. }
  90. void policy_node_free(X509_POLICY_NODE *node)
  91. {
  92. OPENSSL_free(node);
  93. }
  94. /*
  95. * See if a policy node matches a policy OID. If mapping enabled look through
  96. * expected policy set otherwise just valid policy.
  97. */
  98. int policy_node_match(const X509_POLICY_LEVEL *lvl,
  99. const X509_POLICY_NODE *node, const ASN1_OBJECT *oid)
  100. {
  101. int i;
  102. ASN1_OBJECT *policy_oid;
  103. const X509_POLICY_DATA *x = node->data;
  104. if ((lvl->flags & X509_V_FLAG_INHIBIT_MAP)
  105. || !(x->flags & POLICY_DATA_FLAG_MAP_MASK)) {
  106. if (!OBJ_cmp(x->valid_policy, oid))
  107. return 1;
  108. return 0;
  109. }
  110. for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++) {
  111. policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i);
  112. if (!OBJ_cmp(policy_oid, oid))
  113. return 1;
  114. }
  115. return 0;
  116. }