v3_tlsf.c 4.3 KB

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