pcy_node.c 4.1 KB

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