v3_info.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 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 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. goto err;
  67. tret = tmp;
  68. vtmp = sk_CONF_VALUE_value(tret, i);
  69. i2t_ASN1_OBJECT(objtmp, sizeof(objtmp), desc->method);
  70. nlen = strlen(objtmp) + 3 + strlen(vtmp->name) + 1;
  71. ntmp = OPENSSL_malloc(nlen);
  72. if (ntmp == NULL)
  73. goto err;
  74. BIO_snprintf(ntmp, nlen, "%s - %s", objtmp, vtmp->name);
  75. OPENSSL_free(vtmp->name);
  76. vtmp->name = ntmp;
  77. }
  78. if (ret == NULL && tret == NULL)
  79. return sk_CONF_VALUE_new_null();
  80. return tret;
  81. err:
  82. X509V3err(X509V3_F_I2V_AUTHORITY_INFO_ACCESS, ERR_R_MALLOC_FAILURE);
  83. if (ret == NULL && tret != NULL)
  84. sk_CONF_VALUE_pop_free(tret, X509V3_conf_free);
  85. return NULL;
  86. }
  87. static AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
  88. *method,
  89. X509V3_CTX *ctx,
  90. STACK_OF(CONF_VALUE)
  91. *nval)
  92. {
  93. AUTHORITY_INFO_ACCESS *ainfo = NULL;
  94. CONF_VALUE *cnf, ctmp;
  95. ACCESS_DESCRIPTION *acc;
  96. int i, objlen;
  97. const int num = sk_CONF_VALUE_num(nval);
  98. char *objtmp, *ptmp;
  99. if ((ainfo = sk_ACCESS_DESCRIPTION_new_reserve(NULL, num)) == NULL) {
  100. X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS, ERR_R_MALLOC_FAILURE);
  101. return NULL;
  102. }
  103. for (i = 0; i < num; i++) {
  104. cnf = sk_CONF_VALUE_value(nval, i);
  105. if ((acc = ACCESS_DESCRIPTION_new()) == NULL) {
  106. X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
  107. ERR_R_MALLOC_FAILURE);
  108. goto err;
  109. }
  110. sk_ACCESS_DESCRIPTION_push(ainfo, acc); /* Cannot fail due to reserve */
  111. ptmp = strchr(cnf->name, ';');
  112. if (!ptmp) {
  113. X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
  114. X509V3_R_INVALID_SYNTAX);
  115. goto err;
  116. }
  117. objlen = ptmp - cnf->name;
  118. ctmp.name = ptmp + 1;
  119. ctmp.value = cnf->value;
  120. if (!v2i_GENERAL_NAME_ex(acc->location, method, ctx, &ctmp, 0))
  121. goto err;
  122. if ((objtmp = OPENSSL_strndup(cnf->name, objlen)) == NULL) {
  123. X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
  124. ERR_R_MALLOC_FAILURE);
  125. goto err;
  126. }
  127. acc->method = OBJ_txt2obj(objtmp, 0);
  128. if (!acc->method) {
  129. X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
  130. X509V3_R_BAD_OBJECT);
  131. ERR_add_error_data(2, "value=", objtmp);
  132. OPENSSL_free(objtmp);
  133. goto err;
  134. }
  135. OPENSSL_free(objtmp);
  136. }
  137. return ainfo;
  138. err:
  139. sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free);
  140. return NULL;
  141. }
  142. int i2a_ACCESS_DESCRIPTION(BIO *bp, const ACCESS_DESCRIPTION *a)
  143. {
  144. i2a_ASN1_OBJECT(bp, a->method);
  145. return 2;
  146. }