v3_ac_tgt.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 1999-2024 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 <openssl/x509_acert.h>
  11. #include <crypto/x509_acert.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/asn1.h>
  14. #include <openssl/asn1t.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/x509v3.h>
  17. #include "ext_dat.h"
  18. #include "x509_local.h"
  19. #include "crypto/asn1.h"
  20. static int i2r_ISSUER_SERIAL(X509V3_EXT_METHOD *method,
  21. OSSL_ISSUER_SERIAL *iss,
  22. BIO *out, int indent);
  23. static int i2r_OBJECT_DIGEST_INFO(X509V3_EXT_METHOD *method,
  24. OSSL_OBJECT_DIGEST_INFO *odi,
  25. BIO *out, int indent);
  26. static int i2r_TARGET_CERT(X509V3_EXT_METHOD *method,
  27. OSSL_TARGET_CERT *tc,
  28. BIO *out, int indent);
  29. static int i2r_TARGET(X509V3_EXT_METHOD *method,
  30. OSSL_TARGET *target,
  31. BIO *out, int indent);
  32. static int i2r_TARGETING_INFORMATION(X509V3_EXT_METHOD *method,
  33. OSSL_TARGETING_INFORMATION *tinfo,
  34. BIO *out, int indent);
  35. ASN1_SEQUENCE(OSSL_ISSUER_SERIAL) = {
  36. ASN1_SEQUENCE_OF(OSSL_ISSUER_SERIAL, issuer, GENERAL_NAME),
  37. ASN1_EMBED(OSSL_ISSUER_SERIAL, serial, ASN1_INTEGER),
  38. ASN1_OPT(OSSL_ISSUER_SERIAL, issuerUID, ASN1_BIT_STRING),
  39. } static_ASN1_SEQUENCE_END(OSSL_ISSUER_SERIAL)
  40. ASN1_SEQUENCE(OSSL_OBJECT_DIGEST_INFO) = {
  41. ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, digestedObjectType, ASN1_ENUMERATED),
  42. ASN1_OPT(OSSL_OBJECT_DIGEST_INFO, otherObjectTypeID, ASN1_OBJECT),
  43. ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, digestAlgorithm, X509_ALGOR),
  44. ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, objectDigest, ASN1_BIT_STRING),
  45. } static_ASN1_SEQUENCE_END(OSSL_OBJECT_DIGEST_INFO)
  46. ASN1_SEQUENCE(OSSL_TARGET_CERT) = {
  47. ASN1_SIMPLE(OSSL_TARGET_CERT, targetCertificate, OSSL_ISSUER_SERIAL),
  48. ASN1_OPT(OSSL_TARGET_CERT, targetName, GENERAL_NAME),
  49. ASN1_OPT(OSSL_TARGET_CERT, certDigestInfo, OSSL_OBJECT_DIGEST_INFO),
  50. } static_ASN1_SEQUENCE_END(OSSL_TARGET_CERT)
  51. ASN1_CHOICE(OSSL_TARGET) = {
  52. ASN1_EXP(OSSL_TARGET, choice.targetName, GENERAL_NAME, 0),
  53. ASN1_EXP(OSSL_TARGET, choice.targetGroup, GENERAL_NAME, 1),
  54. ASN1_IMP(OSSL_TARGET, choice.targetCert, OSSL_TARGET_CERT, 2),
  55. } ASN1_CHOICE_END(OSSL_TARGET)
  56. ASN1_ITEM_TEMPLATE(OSSL_TARGETS) =
  57. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Targets, OSSL_TARGET)
  58. ASN1_ITEM_TEMPLATE_END(OSSL_TARGETS)
  59. ASN1_ITEM_TEMPLATE(OSSL_TARGETING_INFORMATION) =
  60. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, TargetingInformation, OSSL_TARGETS)
  61. ASN1_ITEM_TEMPLATE_END(OSSL_TARGETING_INFORMATION)
  62. IMPLEMENT_ASN1_FUNCTIONS(OSSL_TARGET)
  63. IMPLEMENT_ASN1_FUNCTIONS(OSSL_TARGETS)
  64. IMPLEMENT_ASN1_FUNCTIONS(OSSL_TARGETING_INFORMATION)
  65. static int i2r_ISSUER_SERIAL(X509V3_EXT_METHOD *method,
  66. OSSL_ISSUER_SERIAL *iss,
  67. BIO *out, int indent)
  68. {
  69. if (iss->issuer != NULL) {
  70. BIO_printf(out, "%*sIssuer Names:\n", indent, "");
  71. OSSL_GENERAL_NAMES_print(out, iss->issuer, indent);
  72. BIO_puts(out, "\n");
  73. } else {
  74. BIO_printf(out, "%*sIssuer Names: <none>\n", indent, "");
  75. }
  76. BIO_printf(out, "%*sIssuer Serial: ", indent, "");
  77. if (i2a_ASN1_INTEGER(out, &(iss->serial)) <= 0)
  78. return 0;
  79. BIO_puts(out, "\n");
  80. if (iss->issuerUID != NULL) {
  81. BIO_printf(out, "%*sIssuer UID: ", indent, "");
  82. if (i2a_ASN1_STRING(out, iss->issuerUID, V_ASN1_BIT_STRING) <= 0)
  83. return 0;
  84. BIO_puts(out, "\n");
  85. } else {
  86. BIO_printf(out, "%*sIssuer UID: <none>\n", indent, "");
  87. }
  88. return 1;
  89. }
  90. static int i2r_OBJECT_DIGEST_INFO(X509V3_EXT_METHOD *method,
  91. OSSL_OBJECT_DIGEST_INFO *odi,
  92. BIO *out, int indent)
  93. {
  94. int64_t dot = 0;
  95. int sig_nid;
  96. X509_ALGOR *digalg;
  97. ASN1_STRING *sig;
  98. if (odi == NULL) {
  99. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  100. return 0;
  101. }
  102. digalg = &odi->digestAlgorithm;
  103. sig = &odi->objectDigest;
  104. if (!ASN1_ENUMERATED_get_int64(&dot, &odi->digestedObjectType)) {
  105. return 0;
  106. }
  107. switch (dot) {
  108. case OSSL_ODI_TYPE_PUBLIC_KEY:
  109. BIO_printf(out, "%*sDigest Type: Public Key\n", indent, "");
  110. break;
  111. case OSSL_ODI_TYPE_PUBLIC_KEY_CERT:
  112. BIO_printf(out, "%*sDigest Type: Public Key Certificate\n", indent, "");
  113. break;
  114. case OSSL_ODI_TYPE_OTHER:
  115. BIO_printf(out, "%*sDigest Type: Other\n", indent, "");
  116. break;
  117. }
  118. if (odi->otherObjectTypeID != NULL) {
  119. BIO_printf(out, "%*sDigest Type Identifier: ", indent, "");
  120. i2a_ASN1_OBJECT(out, odi->otherObjectTypeID);
  121. BIO_puts(out, "\n");
  122. }
  123. if (BIO_printf(out, "%*sSignature Algorithm: ", indent, "") <= 0)
  124. return 0;
  125. if (i2a_ASN1_OBJECT(out, odi->digestAlgorithm.algorithm) <= 0)
  126. return 0;
  127. BIO_puts(out, "\n");
  128. if (BIO_printf(out, "\n%*sSignature Value: ", indent, "") <= 0)
  129. return 0;
  130. sig_nid = OBJ_obj2nid(odi->digestAlgorithm.algorithm);
  131. if (sig_nid != NID_undef) {
  132. int pkey_nid, dig_nid;
  133. const EVP_PKEY_ASN1_METHOD *ameth;
  134. if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
  135. ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
  136. if (ameth && ameth->sig_print)
  137. return ameth->sig_print(out, digalg, sig, indent + 4, 0);
  138. }
  139. }
  140. if (BIO_write(out, "\n", 1) != 1)
  141. return 0;
  142. if (sig)
  143. return X509_signature_dump(out, sig, indent + 4);
  144. return 1;
  145. }
  146. static int i2r_TARGET_CERT(X509V3_EXT_METHOD *method,
  147. OSSL_TARGET_CERT *tc,
  148. BIO *out, int indent)
  149. {
  150. BIO_printf(out, "%*s", indent, "");
  151. if (tc->targetCertificate != NULL) {
  152. BIO_puts(out, "Target Certificate:\n");
  153. i2r_ISSUER_SERIAL(method, tc->targetCertificate, out, indent + 2);
  154. }
  155. if (tc->targetName != NULL) {
  156. BIO_printf(out, "%*sTarget Name: ", indent, "");
  157. GENERAL_NAME_print(out, tc->targetName);
  158. BIO_puts(out, "\n");
  159. }
  160. if (tc->certDigestInfo != NULL) {
  161. BIO_printf(out, "%*sCertificate Digest Info:\n", indent, "");
  162. i2r_OBJECT_DIGEST_INFO(method, tc->certDigestInfo, out, indent + 2);
  163. }
  164. BIO_puts(out, "\n");
  165. return 1;
  166. }
  167. static int i2r_TARGET(X509V3_EXT_METHOD *method,
  168. OSSL_TARGET *target,
  169. BIO *out, int indent)
  170. {
  171. switch (target->type) {
  172. case OSSL_TGT_TARGET_NAME:
  173. BIO_printf(out, "%*sTarget Name: ", indent, "");
  174. GENERAL_NAME_print(out, target->choice.targetName);
  175. BIO_puts(out, "\n");
  176. break;
  177. case OSSL_TGT_TARGET_GROUP:
  178. BIO_printf(out, "%*sTarget Group: ", indent, "");
  179. GENERAL_NAME_print(out, target->choice.targetGroup);
  180. BIO_puts(out, "\n");
  181. break;
  182. case OSSL_TGT_TARGET_CERT:
  183. BIO_printf(out, "%*sTarget Cert:\n", indent, "");
  184. i2r_TARGET_CERT(method, target->choice.targetCert, out, indent + 2);
  185. break;
  186. }
  187. return 1;
  188. }
  189. static int i2r_TARGETS(X509V3_EXT_METHOD *method,
  190. OSSL_TARGETS *targets,
  191. BIO *out, int indent)
  192. {
  193. int i;
  194. OSSL_TARGET *target;
  195. for (i = 0; i < sk_OSSL_TARGET_num(targets); i++) {
  196. BIO_printf(out, "%*sTarget:\n", indent, "");
  197. target = sk_OSSL_TARGET_value(targets, i);
  198. i2r_TARGET(method, target, out, indent + 2);
  199. }
  200. return 1;
  201. }
  202. static int i2r_TARGETING_INFORMATION(X509V3_EXT_METHOD *method,
  203. OSSL_TARGETING_INFORMATION *tinfo,
  204. BIO *out, int indent)
  205. {
  206. int i;
  207. OSSL_TARGETS *targets;
  208. for (i = 0; i < sk_OSSL_TARGETS_num(tinfo); i++) {
  209. BIO_printf(out, "%*sTargets:\n", indent, "");
  210. targets = sk_OSSL_TARGETS_value(tinfo, i);
  211. i2r_TARGETS(method, targets, out, indent + 2);
  212. }
  213. return 1;
  214. }
  215. const X509V3_EXT_METHOD ossl_v3_targeting_information = {
  216. NID_target_information, 0, ASN1_ITEM_ref(OSSL_TARGETING_INFORMATION),
  217. 0, 0, 0, 0,
  218. 0,
  219. 0,
  220. 0, 0,
  221. (X509V3_EXT_I2R)i2r_TARGETING_INFORMATION,
  222. 0,
  223. NULL
  224. };