pcy_node.c 4.1 KB

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