v3_pmaps.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2003-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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/conf.h>
  13. #include <openssl/x509v3.h>
  14. #include "ext_dat.h"
  15. DEFINE_STACK_OF(POLICY_MAPPING)
  16. DEFINE_STACK_OF(CONF_VALUE)
  17. static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method,
  18. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
  19. static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD
  20. *method, void *pmps, STACK_OF(CONF_VALUE)
  21. *extlist);
  22. const X509V3_EXT_METHOD v3_policy_mappings = {
  23. NID_policy_mappings, 0,
  24. ASN1_ITEM_ref(POLICY_MAPPINGS),
  25. 0, 0, 0, 0,
  26. 0, 0,
  27. i2v_POLICY_MAPPINGS,
  28. v2i_POLICY_MAPPINGS,
  29. 0, 0,
  30. NULL
  31. };
  32. ASN1_SEQUENCE(POLICY_MAPPING) = {
  33. ASN1_SIMPLE(POLICY_MAPPING, issuerDomainPolicy, ASN1_OBJECT),
  34. ASN1_SIMPLE(POLICY_MAPPING, subjectDomainPolicy, ASN1_OBJECT)
  35. } ASN1_SEQUENCE_END(POLICY_MAPPING)
  36. ASN1_ITEM_TEMPLATE(POLICY_MAPPINGS) =
  37. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, POLICY_MAPPINGS,
  38. POLICY_MAPPING)
  39. ASN1_ITEM_TEMPLATE_END(POLICY_MAPPINGS)
  40. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING)
  41. static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD
  42. *method, void *a, STACK_OF(CONF_VALUE)
  43. *ext_list)
  44. {
  45. POLICY_MAPPINGS *pmaps = a;
  46. POLICY_MAPPING *pmap;
  47. int i;
  48. char obj_tmp1[80];
  49. char obj_tmp2[80];
  50. for (i = 0; i < sk_POLICY_MAPPING_num(pmaps); i++) {
  51. pmap = sk_POLICY_MAPPING_value(pmaps, i);
  52. i2t_ASN1_OBJECT(obj_tmp1, 80, pmap->issuerDomainPolicy);
  53. i2t_ASN1_OBJECT(obj_tmp2, 80, pmap->subjectDomainPolicy);
  54. X509V3_add_value(obj_tmp1, obj_tmp2, &ext_list);
  55. }
  56. return ext_list;
  57. }
  58. static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method,
  59. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  60. {
  61. POLICY_MAPPING *pmap = NULL;
  62. ASN1_OBJECT *obj1 = NULL, *obj2 = NULL;
  63. CONF_VALUE *val;
  64. POLICY_MAPPINGS *pmaps;
  65. const int num = sk_CONF_VALUE_num(nval);
  66. int i;
  67. if ((pmaps = sk_POLICY_MAPPING_new_reserve(NULL, num)) == NULL) {
  68. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS, ERR_R_MALLOC_FAILURE);
  69. return NULL;
  70. }
  71. for (i = 0; i < num; i++) {
  72. val = sk_CONF_VALUE_value(nval, i);
  73. if (!val->value || !val->name) {
  74. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS,
  75. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  76. X509V3_conf_err(val);
  77. goto err;
  78. }
  79. obj1 = OBJ_txt2obj(val->name, 0);
  80. obj2 = OBJ_txt2obj(val->value, 0);
  81. if (!obj1 || !obj2) {
  82. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS,
  83. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  84. X509V3_conf_err(val);
  85. goto err;
  86. }
  87. pmap = POLICY_MAPPING_new();
  88. if (pmap == NULL) {
  89. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS, ERR_R_MALLOC_FAILURE);
  90. goto err;
  91. }
  92. pmap->issuerDomainPolicy = obj1;
  93. pmap->subjectDomainPolicy = obj2;
  94. obj1 = obj2 = NULL;
  95. sk_POLICY_MAPPING_push(pmaps, pmap); /* no failure as it was reserved */
  96. }
  97. return pmaps;
  98. err:
  99. ASN1_OBJECT_free(obj1);
  100. ASN1_OBJECT_free(obj2);
  101. sk_POLICY_MAPPING_pop_free(pmaps, POLICY_MAPPING_free);
  102. return NULL;
  103. }