v3_info.c 5.6 KB

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