v3_pmaps.c 3.7 KB

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