v3_pcons.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/asn1.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/conf.h>
  14. #include <openssl/x509v3.h>
  15. #include "ext_dat.h"
  16. DEFINE_STACK_OF(CONF_VALUE)
  17. static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD
  18. *method, void *bcons, STACK_OF(CONF_VALUE)
  19. *extlist);
  20. static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  21. X509V3_CTX *ctx,
  22. STACK_OF(CONF_VALUE) *values);
  23. const X509V3_EXT_METHOD v3_policy_constraints = {
  24. NID_policy_constraints, 0,
  25. ASN1_ITEM_ref(POLICY_CONSTRAINTS),
  26. 0, 0, 0, 0,
  27. 0, 0,
  28. i2v_POLICY_CONSTRAINTS,
  29. v2i_POLICY_CONSTRAINTS,
  30. NULL, NULL,
  31. NULL
  32. };
  33. ASN1_SEQUENCE(POLICY_CONSTRAINTS) = {
  34. ASN1_IMP_OPT(POLICY_CONSTRAINTS, requireExplicitPolicy, ASN1_INTEGER,0),
  35. ASN1_IMP_OPT(POLICY_CONSTRAINTS, inhibitPolicyMapping, ASN1_INTEGER,1)
  36. } ASN1_SEQUENCE_END(POLICY_CONSTRAINTS)
  37. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS)
  38. static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD
  39. *method, void *a, STACK_OF(CONF_VALUE)
  40. *extlist)
  41. {
  42. POLICY_CONSTRAINTS *pcons = a;
  43. X509V3_add_value_int("Require Explicit Policy",
  44. pcons->requireExplicitPolicy, &extlist);
  45. X509V3_add_value_int("Inhibit Policy Mapping",
  46. pcons->inhibitPolicyMapping, &extlist);
  47. return extlist;
  48. }
  49. static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method,
  50. X509V3_CTX *ctx,
  51. STACK_OF(CONF_VALUE) *values)
  52. {
  53. POLICY_CONSTRAINTS *pcons = NULL;
  54. CONF_VALUE *val;
  55. int i;
  56. if ((pcons = POLICY_CONSTRAINTS_new()) == NULL) {
  57. X509V3err(X509V3_F_V2I_POLICY_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
  58. return NULL;
  59. }
  60. for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
  61. val = sk_CONF_VALUE_value(values, i);
  62. if (strcmp(val->name, "requireExplicitPolicy") == 0) {
  63. if (!X509V3_get_value_int(val, &pcons->requireExplicitPolicy))
  64. goto err;
  65. } else if (strcmp(val->name, "inhibitPolicyMapping") == 0) {
  66. if (!X509V3_get_value_int(val, &pcons->inhibitPolicyMapping))
  67. goto err;
  68. } else {
  69. X509V3err(X509V3_F_V2I_POLICY_CONSTRAINTS, X509V3_R_INVALID_NAME);
  70. X509V3_conf_err(val);
  71. goto err;
  72. }
  73. }
  74. if (pcons->inhibitPolicyMapping == NULL
  75. && pcons->requireExplicitPolicy == NULL) {
  76. X509V3err(X509V3_F_V2I_POLICY_CONSTRAINTS,
  77. X509V3_R_ILLEGAL_EMPTY_EXTENSION);
  78. goto err;
  79. }
  80. return pcons;
  81. err:
  82. POLICY_CONSTRAINTS_free(pcons);
  83. return NULL;
  84. }