2
0

v3_pmaps.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2003-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 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. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS, ERR_R_MALLOC_FAILURE);
  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. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS,
  73. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  74. X509V3_conf_err(val);
  75. goto err;
  76. }
  77. obj1 = OBJ_txt2obj(val->name, 0);
  78. obj2 = OBJ_txt2obj(val->value, 0);
  79. if (!obj1 || !obj2) {
  80. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS,
  81. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  82. X509V3_conf_err(val);
  83. goto err;
  84. }
  85. pmap = POLICY_MAPPING_new();
  86. if (pmap == NULL) {
  87. X509V3err(X509V3_F_V2I_POLICY_MAPPINGS, ERR_R_MALLOC_FAILURE);
  88. goto err;
  89. }
  90. pmap->issuerDomainPolicy = obj1;
  91. pmap->subjectDomainPolicy = obj2;
  92. obj1 = obj2 = NULL;
  93. sk_POLICY_MAPPING_push(pmaps, pmap); /* no failure as it was reserved */
  94. }
  95. return pmaps;
  96. err:
  97. ASN1_OBJECT_free(obj1);
  98. ASN1_OBJECT_free(obj2);
  99. sk_POLICY_MAPPING_pop_free(pmaps, POLICY_MAPPING_free);
  100. return NULL;
  101. }