v3_tlsf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2015-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 "e_os.h"
  10. #include "internal/cryptlib.h"
  11. #include <stdio.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/conf.h>
  14. #include <openssl/x509v3.h>
  15. #include "ext_dat.h"
  16. DEFINE_STACK_OF(ASN1_INTEGER)
  17. DEFINE_STACK_OF(CONF_VALUE)
  18. static STACK_OF(CONF_VALUE) *i2v_TLS_FEATURE(const X509V3_EXT_METHOD *method,
  19. TLS_FEATURE *tls_feature,
  20. STACK_OF(CONF_VALUE) *ext_list);
  21. static TLS_FEATURE *v2i_TLS_FEATURE(const X509V3_EXT_METHOD *method,
  22. X509V3_CTX *ctx,
  23. STACK_OF(CONF_VALUE) *nval);
  24. ASN1_ITEM_TEMPLATE(TLS_FEATURE) =
  25. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, TLS_FEATURE, ASN1_INTEGER)
  26. static_ASN1_ITEM_TEMPLATE_END(TLS_FEATURE)
  27. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(TLS_FEATURE)
  28. const X509V3_EXT_METHOD v3_tls_feature = {
  29. NID_tlsfeature, 0,
  30. ASN1_ITEM_ref(TLS_FEATURE),
  31. 0, 0, 0, 0,
  32. 0, 0,
  33. (X509V3_EXT_I2V)i2v_TLS_FEATURE,
  34. (X509V3_EXT_V2I)v2i_TLS_FEATURE,
  35. 0, 0,
  36. NULL
  37. };
  38. typedef struct {
  39. long num;
  40. const char *name;
  41. } TLS_FEATURE_NAME;
  42. static TLS_FEATURE_NAME tls_feature_tbl[] = {
  43. { 5, "status_request" },
  44. { 17, "status_request_v2" }
  45. };
  46. /*
  47. * i2v_TLS_FEATURE converts the TLS_FEATURE structure tls_feature into the
  48. * STACK_OF(CONF_VALUE) structure ext_list. STACK_OF(CONF_VALUE) is the format
  49. * used by the CONF library to represent a multi-valued extension. ext_list is
  50. * returned.
  51. */
  52. static STACK_OF(CONF_VALUE) *i2v_TLS_FEATURE(const X509V3_EXT_METHOD *method,
  53. TLS_FEATURE *tls_feature,
  54. STACK_OF(CONF_VALUE) *ext_list)
  55. {
  56. int i;
  57. size_t j;
  58. ASN1_INTEGER *ai;
  59. long tlsextid;
  60. for (i = 0; i < sk_ASN1_INTEGER_num(tls_feature); i++) {
  61. ai = sk_ASN1_INTEGER_value(tls_feature, i);
  62. tlsextid = ASN1_INTEGER_get(ai);
  63. for (j = 0; j < OSSL_NELEM(tls_feature_tbl); j++)
  64. if (tlsextid == tls_feature_tbl[j].num)
  65. break;
  66. if (j < OSSL_NELEM(tls_feature_tbl))
  67. X509V3_add_value(NULL, tls_feature_tbl[j].name, &ext_list);
  68. else
  69. X509V3_add_value_int(NULL, ai, &ext_list);
  70. }
  71. return ext_list;
  72. }
  73. /*
  74. * v2i_TLS_FEATURE converts the multi-valued extension nval into a TLS_FEATURE
  75. * structure, which is returned if the conversion is successful. In case of
  76. * error, NULL is returned.
  77. */
  78. static TLS_FEATURE *v2i_TLS_FEATURE(const X509V3_EXT_METHOD *method,
  79. X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
  80. {
  81. TLS_FEATURE *tlsf;
  82. char *extval, *endptr;
  83. ASN1_INTEGER *ai = NULL;
  84. CONF_VALUE *val;
  85. int i;
  86. size_t j;
  87. long tlsextid;
  88. if ((tlsf = sk_ASN1_INTEGER_new_null()) == NULL) {
  89. X509V3err(X509V3_F_V2I_TLS_FEATURE, ERR_R_MALLOC_FAILURE);
  90. return NULL;
  91. }
  92. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  93. val = sk_CONF_VALUE_value(nval, i);
  94. if (val->value)
  95. extval = val->value;
  96. else
  97. extval = val->name;
  98. for (j = 0; j < OSSL_NELEM(tls_feature_tbl); j++)
  99. if (strcasecmp(extval, tls_feature_tbl[j].name) == 0)
  100. break;
  101. if (j < OSSL_NELEM(tls_feature_tbl))
  102. tlsextid = tls_feature_tbl[j].num;
  103. else {
  104. tlsextid = strtol(extval, &endptr, 10);
  105. if (((*endptr) != '\0') || (extval == endptr) || (tlsextid < 0) ||
  106. (tlsextid > 65535)) {
  107. X509V3err(X509V3_F_V2I_TLS_FEATURE, X509V3_R_INVALID_SYNTAX);
  108. X509V3_conf_err(val);
  109. goto err;
  110. }
  111. }
  112. if ((ai = ASN1_INTEGER_new()) == NULL
  113. || !ASN1_INTEGER_set(ai, tlsextid)
  114. || sk_ASN1_INTEGER_push(tlsf, ai) <= 0) {
  115. X509V3err(X509V3_F_V2I_TLS_FEATURE, ERR_R_MALLOC_FAILURE);
  116. goto err;
  117. }
  118. /* So it doesn't get purged if an error occurs next time around */
  119. ai = NULL;
  120. }
  121. return tlsf;
  122. err:
  123. sk_ASN1_INTEGER_pop_free(tlsf, ASN1_INTEGER_free);
  124. ASN1_INTEGER_free(ai);
  125. return NULL;
  126. }