v3_aaa.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2024-2025 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 <openssl/asn1t.h>
  10. #include <openssl/x509v3.h>
  11. #include <openssl/x509.h>
  12. #include <crypto/x509.h>
  13. #include "ext_dat.h"
  14. ASN1_CHOICE(OSSL_ALLOWED_ATTRIBUTES_CHOICE) = {
  15. ASN1_IMP(OSSL_ALLOWED_ATTRIBUTES_CHOICE, choice.attributeType, ASN1_OBJECT,
  16. OSSL_AAA_ATTRIBUTE_TYPE),
  17. ASN1_IMP(OSSL_ALLOWED_ATTRIBUTES_CHOICE, choice.attributeTypeandValues,
  18. X509_ATTRIBUTE, OSSL_AAA_ATTRIBUTE_VALUES),
  19. } ASN1_CHOICE_END(OSSL_ALLOWED_ATTRIBUTES_CHOICE)
  20. ASN1_SEQUENCE(OSSL_ALLOWED_ATTRIBUTES_ITEM) = {
  21. ASN1_IMP_SET_OF(OSSL_ALLOWED_ATTRIBUTES_ITEM, attributes,
  22. OSSL_ALLOWED_ATTRIBUTES_CHOICE, 0),
  23. /* This MUST be EXPLICIT, because it contains a CHOICE. */
  24. ASN1_EXP(OSSL_ALLOWED_ATTRIBUTES_ITEM, holderDomain, GENERAL_NAME, 1),
  25. } ASN1_SEQUENCE_END(OSSL_ALLOWED_ATTRIBUTES_ITEM)
  26. ASN1_ITEM_TEMPLATE(OSSL_ALLOWED_ATTRIBUTES_SYNTAX) =
  27. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, OSSL_ALLOWED_ATTRIBUTES_SYNTAX,
  28. OSSL_ALLOWED_ATTRIBUTES_ITEM)
  29. ASN1_ITEM_TEMPLATE_END(OSSL_ALLOWED_ATTRIBUTES_SYNTAX)
  30. IMPLEMENT_ASN1_FUNCTIONS(OSSL_ALLOWED_ATTRIBUTES_CHOICE)
  31. IMPLEMENT_ASN1_FUNCTIONS(OSSL_ALLOWED_ATTRIBUTES_ITEM)
  32. IMPLEMENT_ASN1_FUNCTIONS(OSSL_ALLOWED_ATTRIBUTES_SYNTAX)
  33. static int i2r_ALLOWED_ATTRIBUTES_CHOICE(X509V3_EXT_METHOD *method,
  34. OSSL_ALLOWED_ATTRIBUTES_CHOICE *a,
  35. BIO *out, int indent)
  36. {
  37. ASN1_OBJECT *attr_obj;
  38. int attr_nid, j;
  39. X509_ATTRIBUTE *attr;
  40. ASN1_TYPE *av;
  41. switch (a->type) {
  42. case (OSSL_AAA_ATTRIBUTE_TYPE):
  43. if (BIO_printf(out, "%*sAttribute Type: ", indent, "") <= 0)
  44. return 0;
  45. if (i2a_ASN1_OBJECT(out, a->choice.attributeType) <= 0)
  46. return 0;
  47. return BIO_puts(out, "\n") > 0;
  48. case (OSSL_AAA_ATTRIBUTE_VALUES):
  49. attr = a->choice.attributeTypeandValues;
  50. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  51. attr_nid = OBJ_obj2nid(attr_obj);
  52. if (BIO_printf(out, "%*sAttribute Values: ", indent, "") <= 0)
  53. return 0;
  54. if (i2a_ASN1_OBJECT(out, attr_obj) <= 0)
  55. return 0;
  56. if (BIO_puts(out, "\n") <= 0)
  57. return 0;
  58. for (j = 0; j < X509_ATTRIBUTE_count(attr); j++) {
  59. av = X509_ATTRIBUTE_get0_type(attr, j);
  60. if (ossl_print_attribute_value(out, attr_nid, av, indent + 4) <= 0)
  61. return 0;
  62. if (BIO_puts(out, "\n") <= 0)
  63. return 0;
  64. }
  65. break;
  66. default:
  67. return 0;
  68. }
  69. return 1;
  70. }
  71. static int i2r_ALLOWED_ATTRIBUTES_ITEM(X509V3_EXT_METHOD *method,
  72. OSSL_ALLOWED_ATTRIBUTES_ITEM *aai,
  73. BIO *out, int indent)
  74. {
  75. int i;
  76. OSSL_ALLOWED_ATTRIBUTES_CHOICE *a;
  77. for (i = 0; i < sk_OSSL_ALLOWED_ATTRIBUTES_CHOICE_num(aai->attributes); i++) {
  78. if (BIO_printf(out, "%*sAllowed Attribute Type or Values:\n", indent, "") <= 0)
  79. return 0;
  80. a = sk_OSSL_ALLOWED_ATTRIBUTES_CHOICE_value(aai->attributes, i);
  81. if (i2r_ALLOWED_ATTRIBUTES_CHOICE(method, a, out, indent + 4) <= 0)
  82. return 0;
  83. }
  84. if (BIO_printf(out, "%*sHolder Domain: ", indent, "") <= 0)
  85. return 0;
  86. if (GENERAL_NAME_print(out, aai->holderDomain) <= 0)
  87. return 0;
  88. if (BIO_puts(out, "\n") <= 0)
  89. return 0;
  90. return 1;
  91. }
  92. static int i2r_ALLOWED_ATTRIBUTES_SYNTAX(X509V3_EXT_METHOD *method,
  93. OSSL_ALLOWED_ATTRIBUTES_SYNTAX *aaa,
  94. BIO *out, int indent)
  95. {
  96. int i;
  97. OSSL_ALLOWED_ATTRIBUTES_ITEM *aai;
  98. for (i = 0; i < sk_OSSL_ALLOWED_ATTRIBUTES_ITEM_num(aaa); i++) {
  99. if (BIO_printf(out, "%*sAllowed Attributes:\n", indent, "") <= 0)
  100. return 0;
  101. aai = sk_OSSL_ALLOWED_ATTRIBUTES_ITEM_value(aaa, i);
  102. if (i2r_ALLOWED_ATTRIBUTES_ITEM(method, aai, out, indent + 4) <= 0)
  103. return 0;
  104. }
  105. return 1;
  106. }
  107. const X509V3_EXT_METHOD ossl_v3_allowed_attribute_assignments = {
  108. NID_allowed_attribute_assignments, 0,
  109. ASN1_ITEM_ref(OSSL_ALLOWED_ATTRIBUTES_SYNTAX),
  110. 0, 0, 0, 0,
  111. 0, 0,
  112. 0,
  113. 0,
  114. (X509V3_EXT_I2R)i2r_ALLOWED_ATTRIBUTES_SYNTAX,
  115. 0,
  116. NULL
  117. };