pcy_data.c 2.3 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 "pcy_local.h"
  13. DEFINE_STACK_OF(ASN1_OBJECT)
  14. DEFINE_STACK_OF(POLICYQUALINFO)
  15. /* Policy Node routines */
  16. void policy_data_free(X509_POLICY_DATA *data)
  17. {
  18. if (data == NULL)
  19. return;
  20. ASN1_OBJECT_free(data->valid_policy);
  21. /* Don't free qualifiers if shared */
  22. if (!(data->flags & POLICY_DATA_FLAG_SHARED_QUALIFIERS))
  23. sk_POLICYQUALINFO_pop_free(data->qualifier_set, POLICYQUALINFO_free);
  24. sk_ASN1_OBJECT_pop_free(data->expected_policy_set, ASN1_OBJECT_free);
  25. OPENSSL_free(data);
  26. }
  27. /*
  28. * Create a data based on an existing policy. If 'id' is NULL use the OID in
  29. * the policy, otherwise use 'id'. This behaviour covers the two types of
  30. * data in RFC3280: data with from a CertificatePolicies extension and
  31. * additional data with just the qualifiers of anyPolicy and ID from another
  32. * source.
  33. */
  34. X509_POLICY_DATA *policy_data_new(POLICYINFO *policy,
  35. const ASN1_OBJECT *cid, int crit)
  36. {
  37. X509_POLICY_DATA *ret;
  38. ASN1_OBJECT *id;
  39. if (policy == NULL && cid == NULL)
  40. return NULL;
  41. if (cid) {
  42. id = OBJ_dup(cid);
  43. if (id == NULL)
  44. return NULL;
  45. } else
  46. id = NULL;
  47. ret = OPENSSL_zalloc(sizeof(*ret));
  48. if (ret == NULL) {
  49. X509V3err(X509V3_F_POLICY_DATA_NEW, ERR_R_MALLOC_FAILURE);
  50. return NULL;
  51. }
  52. ret->expected_policy_set = sk_ASN1_OBJECT_new_null();
  53. if (ret->expected_policy_set == NULL) {
  54. OPENSSL_free(ret);
  55. ASN1_OBJECT_free(id);
  56. X509V3err(X509V3_F_POLICY_DATA_NEW, ERR_R_MALLOC_FAILURE);
  57. return NULL;
  58. }
  59. if (crit)
  60. ret->flags = POLICY_DATA_FLAG_CRITICAL;
  61. if (id)
  62. ret->valid_policy = id;
  63. else {
  64. ret->valid_policy = policy->policyid;
  65. policy->policyid = NULL;
  66. }
  67. if (policy) {
  68. ret->qualifier_set = policy->qualifiers;
  69. policy->qualifiers = NULL;
  70. }
  71. return ret;
  72. }