2
0

pcy_map.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2004-2020 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 "internal/cryptlib.h"
  10. #include <openssl/x509.h>
  11. #include <openssl/x509v3.h>
  12. #include "crypto/x509.h"
  13. #include "pcy_local.h"
  14. DEFINE_STACK_OF(POLICY_MAPPING)
  15. DEFINE_STACK_OF(ASN1_OBJECT)
  16. /*
  17. * Set policy mapping entries in cache. Note: this modifies the passed
  18. * POLICY_MAPPINGS structure
  19. */
  20. int policy_cache_set_mapping(X509 *x, POLICY_MAPPINGS *maps)
  21. {
  22. POLICY_MAPPING *map;
  23. X509_POLICY_DATA *data;
  24. X509_POLICY_CACHE *cache = x->policy_cache;
  25. int i;
  26. int ret = 0;
  27. if (sk_POLICY_MAPPING_num(maps) == 0) {
  28. ret = -1;
  29. goto bad_mapping;
  30. }
  31. for (i = 0; i < sk_POLICY_MAPPING_num(maps); i++) {
  32. map = sk_POLICY_MAPPING_value(maps, i);
  33. /* Reject if map to or from anyPolicy */
  34. if ((OBJ_obj2nid(map->subjectDomainPolicy) == NID_any_policy)
  35. || (OBJ_obj2nid(map->issuerDomainPolicy) == NID_any_policy)) {
  36. ret = -1;
  37. goto bad_mapping;
  38. }
  39. /* Attempt to find matching policy data */
  40. data = policy_cache_find_data(cache, map->issuerDomainPolicy);
  41. /* If we don't have anyPolicy can't map */
  42. if (data == NULL && !cache->anyPolicy)
  43. continue;
  44. /* Create a NODE from anyPolicy */
  45. if (data == NULL) {
  46. data = policy_data_new(NULL, map->issuerDomainPolicy,
  47. cache->anyPolicy->flags
  48. & POLICY_DATA_FLAG_CRITICAL);
  49. if (data == NULL)
  50. goto bad_mapping;
  51. data->qualifier_set = cache->anyPolicy->qualifier_set;
  52. /*
  53. * map->issuerDomainPolicy = NULL;
  54. */
  55. data->flags |= POLICY_DATA_FLAG_MAPPED_ANY;
  56. data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
  57. if (!sk_X509_POLICY_DATA_push(cache->data, data)) {
  58. policy_data_free(data);
  59. goto bad_mapping;
  60. }
  61. } else
  62. data->flags |= POLICY_DATA_FLAG_MAPPED;
  63. if (!sk_ASN1_OBJECT_push(data->expected_policy_set,
  64. map->subjectDomainPolicy))
  65. goto bad_mapping;
  66. map->subjectDomainPolicy = NULL;
  67. }
  68. ret = 1;
  69. bad_mapping:
  70. if (ret == -1)
  71. x->ex_flags |= EXFLAG_INVALID_POLICY;
  72. sk_POLICY_MAPPING_pop_free(maps, POLICY_MAPPING_free);
  73. return ret;
  74. }