v3_info.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 1999-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/conf.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509v3.h>
  15. #include "ext_dat.h"
  16. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
  17. *method, AUTHORITY_INFO_ACCESS
  18. *ainfo, STACK_OF(CONF_VALUE)
  19. *ret);
  20. static AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
  21. *method,
  22. X509V3_CTX *ctx,
  23. STACK_OF(CONF_VALUE)
  24. *nval);
  25. const X509V3_EXT_METHOD ossl_v3_info = { NID_info_access, X509V3_EXT_MULTILINE,
  26. ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
  27. 0, 0, 0, 0,
  28. 0, 0,
  29. (X509V3_EXT_I2V) i2v_AUTHORITY_INFO_ACCESS,
  30. (X509V3_EXT_V2I)v2i_AUTHORITY_INFO_ACCESS,
  31. 0, 0,
  32. NULL
  33. };
  34. const X509V3_EXT_METHOD ossl_v3_sinfo = { NID_sinfo_access, X509V3_EXT_MULTILINE,
  35. ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
  36. 0, 0, 0, 0,
  37. 0, 0,
  38. (X509V3_EXT_I2V) i2v_AUTHORITY_INFO_ACCESS,
  39. (X509V3_EXT_V2I)v2i_AUTHORITY_INFO_ACCESS,
  40. 0, 0,
  41. NULL
  42. };
  43. ASN1_SEQUENCE(ACCESS_DESCRIPTION) = {
  44. ASN1_SIMPLE(ACCESS_DESCRIPTION, method, ASN1_OBJECT),
  45. ASN1_SIMPLE(ACCESS_DESCRIPTION, location, GENERAL_NAME)
  46. } ASN1_SEQUENCE_END(ACCESS_DESCRIPTION)
  47. IMPLEMENT_ASN1_FUNCTIONS(ACCESS_DESCRIPTION)
  48. ASN1_ITEM_TEMPLATE(AUTHORITY_INFO_ACCESS) =
  49. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, ACCESS_DESCRIPTION)
  50. ASN1_ITEM_TEMPLATE_END(AUTHORITY_INFO_ACCESS)
  51. IMPLEMENT_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS)
  52. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(
  53. X509V3_EXT_METHOD *method, AUTHORITY_INFO_ACCESS *ainfo,
  54. STACK_OF(CONF_VALUE) *ret)
  55. {
  56. ACCESS_DESCRIPTION *desc;
  57. int i, nlen;
  58. char objtmp[80], *ntmp;
  59. CONF_VALUE *vtmp;
  60. STACK_OF(CONF_VALUE) *tret = ret;
  61. for (i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) {
  62. STACK_OF(CONF_VALUE) *tmp;
  63. desc = sk_ACCESS_DESCRIPTION_value(ainfo, i);
  64. tmp = i2v_GENERAL_NAME(method, desc->location, tret);
  65. if (tmp == NULL) {
  66. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  67. goto err;
  68. }
  69. tret = tmp;
  70. vtmp = sk_CONF_VALUE_value(tret, i);
  71. i2t_ASN1_OBJECT(objtmp, sizeof(objtmp), desc->method);
  72. nlen = strlen(objtmp) + 3 + strlen(vtmp->name) + 1;
  73. ntmp = OPENSSL_malloc(nlen);
  74. if (ntmp == NULL)
  75. goto err;
  76. BIO_snprintf(ntmp, nlen, "%s - %s", objtmp, vtmp->name);
  77. OPENSSL_free(vtmp->name);
  78. vtmp->name = ntmp;
  79. }
  80. if (ret == NULL && tret == NULL)
  81. return sk_CONF_VALUE_new_null();
  82. return tret;
  83. err:
  84. if (ret == NULL && tret != NULL)
  85. sk_CONF_VALUE_pop_free(tret, X509V3_conf_free);
  86. return NULL;
  87. }
  88. static AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
  89. *method,
  90. X509V3_CTX *ctx,
  91. STACK_OF(CONF_VALUE)
  92. *nval)
  93. {
  94. AUTHORITY_INFO_ACCESS *ainfo = NULL;
  95. CONF_VALUE *cnf, ctmp;
  96. ACCESS_DESCRIPTION *acc;
  97. int i;
  98. const int num = sk_CONF_VALUE_num(nval);
  99. char *objtmp, *ptmp;
  100. if ((ainfo = sk_ACCESS_DESCRIPTION_new_reserve(NULL, num)) == NULL) {
  101. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  102. return NULL;
  103. }
  104. for (i = 0; i < num; i++) {
  105. cnf = sk_CONF_VALUE_value(nval, i);
  106. if ((acc = ACCESS_DESCRIPTION_new()) == NULL) {
  107. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  108. goto err;
  109. }
  110. sk_ACCESS_DESCRIPTION_push(ainfo, acc); /* Cannot fail due to reserve */
  111. ptmp = strchr(cnf->name, ';');
  112. if (ptmp == NULL) {
  113. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SYNTAX);
  114. goto err;
  115. }
  116. ctmp.name = ptmp + 1;
  117. ctmp.value = cnf->value;
  118. if (!v2i_GENERAL_NAME_ex(acc->location, method, ctx, &ctmp, 0))
  119. goto err;
  120. if ((objtmp = OPENSSL_strndup(cnf->name, ptmp - cnf->name)) == NULL)
  121. goto err;
  122. acc->method = OBJ_txt2obj(objtmp, 0);
  123. if (!acc->method) {
  124. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT,
  125. "value=%s", objtmp);
  126. OPENSSL_free(objtmp);
  127. goto err;
  128. }
  129. OPENSSL_free(objtmp);
  130. }
  131. return ainfo;
  132. err:
  133. sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free);
  134. return NULL;
  135. }
  136. int i2a_ACCESS_DESCRIPTION(BIO *bp, const ACCESS_DESCRIPTION *a)
  137. {
  138. i2a_ASN1_OBJECT(bp, a->method);
  139. return 2;
  140. }