v3_tlsf.c 4.3 KB

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