v3_extku.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/asn1t.h>
  12. #include <openssl/conf.h>
  13. #include <openssl/x509v3.h>
  14. #include "ext_dat.h"
  15. DEFINE_STACK_OF(ASN1_OBJECT)
  16. DEFINE_STACK_OF(CONF_VALUE)
  17. static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method,
  18. X509V3_CTX *ctx,
  19. STACK_OF(CONF_VALUE) *nval);
  20. static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD
  21. *method, void *eku, STACK_OF(CONF_VALUE)
  22. *extlist);
  23. const X509V3_EXT_METHOD v3_ext_ku = {
  24. NID_ext_key_usage, 0,
  25. ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
  26. 0, 0, 0, 0,
  27. 0, 0,
  28. i2v_EXTENDED_KEY_USAGE,
  29. v2i_EXTENDED_KEY_USAGE,
  30. 0, 0,
  31. NULL
  32. };
  33. /* NB OCSP acceptable responses also is a SEQUENCE OF OBJECT */
  34. const X509V3_EXT_METHOD v3_ocsp_accresp = {
  35. NID_id_pkix_OCSP_acceptableResponses, 0,
  36. ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
  37. 0, 0, 0, 0,
  38. 0, 0,
  39. i2v_EXTENDED_KEY_USAGE,
  40. v2i_EXTENDED_KEY_USAGE,
  41. 0, 0,
  42. NULL
  43. };
  44. ASN1_ITEM_TEMPLATE(EXTENDED_KEY_USAGE) =
  45. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, EXTENDED_KEY_USAGE, ASN1_OBJECT)
  46. ASN1_ITEM_TEMPLATE_END(EXTENDED_KEY_USAGE)
  47. IMPLEMENT_ASN1_FUNCTIONS(EXTENDED_KEY_USAGE)
  48. static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD
  49. *method, void *a, STACK_OF(CONF_VALUE)
  50. *ext_list)
  51. {
  52. EXTENDED_KEY_USAGE *eku = a;
  53. int i;
  54. ASN1_OBJECT *obj;
  55. char obj_tmp[80];
  56. for (i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
  57. obj = sk_ASN1_OBJECT_value(eku, i);
  58. i2t_ASN1_OBJECT(obj_tmp, 80, obj);
  59. X509V3_add_value(NULL, obj_tmp, &ext_list);
  60. }
  61. return ext_list;
  62. }
  63. static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method,
  64. X509V3_CTX *ctx,
  65. STACK_OF(CONF_VALUE) *nval)
  66. {
  67. EXTENDED_KEY_USAGE *extku;
  68. char *extval;
  69. ASN1_OBJECT *objtmp;
  70. CONF_VALUE *val;
  71. const int num = sk_CONF_VALUE_num(nval);
  72. int i;
  73. extku = sk_ASN1_OBJECT_new_reserve(NULL, num);
  74. if (extku == NULL) {
  75. X509V3err(X509V3_F_V2I_EXTENDED_KEY_USAGE, ERR_R_MALLOC_FAILURE);
  76. sk_ASN1_OBJECT_free(extku);
  77. return NULL;
  78. }
  79. for (i = 0; i < num; i++) {
  80. val = sk_CONF_VALUE_value(nval, i);
  81. if (val->value)
  82. extval = val->value;
  83. else
  84. extval = val->name;
  85. if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
  86. sk_ASN1_OBJECT_pop_free(extku, ASN1_OBJECT_free);
  87. X509V3err(X509V3_F_V2I_EXTENDED_KEY_USAGE,
  88. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  89. X509V3_conf_err(val);
  90. return NULL;
  91. }
  92. sk_ASN1_OBJECT_push(extku, objtmp); /* no failure as it was reserved */
  93. }
  94. return extku;
  95. }