pcy_data.c 2.3 KB

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