v3_bcons.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 1999-2021 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/asn1.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/conf.h>
  14. #include <openssl/x509v3.h>
  15. #include "ext_dat.h"
  16. #include "x509_local.h"
  17. static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
  18. BASIC_CONSTRAINTS *bcons,
  19. STACK_OF(CONF_VALUE)
  20. *extlist);
  21. static BASIC_CONSTRAINTS *v2i_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
  22. X509V3_CTX *ctx,
  23. STACK_OF(CONF_VALUE) *values);
  24. const X509V3_EXT_METHOD ossl_v3_bcons = {
  25. NID_basic_constraints, 0,
  26. ASN1_ITEM_ref(BASIC_CONSTRAINTS),
  27. 0, 0, 0, 0,
  28. 0, 0,
  29. (X509V3_EXT_I2V) i2v_BASIC_CONSTRAINTS,
  30. (X509V3_EXT_V2I)v2i_BASIC_CONSTRAINTS,
  31. NULL, NULL,
  32. NULL
  33. };
  34. ASN1_SEQUENCE(BASIC_CONSTRAINTS) = {
  35. ASN1_OPT(BASIC_CONSTRAINTS, ca, ASN1_FBOOLEAN),
  36. ASN1_OPT(BASIC_CONSTRAINTS, pathlen, ASN1_INTEGER)
  37. } ASN1_SEQUENCE_END(BASIC_CONSTRAINTS)
  38. IMPLEMENT_ASN1_FUNCTIONS(BASIC_CONSTRAINTS)
  39. static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
  40. BASIC_CONSTRAINTS *bcons,
  41. STACK_OF(CONF_VALUE)
  42. *extlist)
  43. {
  44. X509V3_add_value_bool("CA", bcons->ca, &extlist);
  45. X509V3_add_value_int("pathlen", bcons->pathlen, &extlist);
  46. return extlist;
  47. }
  48. static BASIC_CONSTRAINTS *v2i_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
  49. X509V3_CTX *ctx,
  50. STACK_OF(CONF_VALUE) *values)
  51. {
  52. BASIC_CONSTRAINTS *bcons = NULL;
  53. CONF_VALUE *val;
  54. int i;
  55. if ((bcons = BASIC_CONSTRAINTS_new()) == NULL) {
  56. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  57. return NULL;
  58. }
  59. for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
  60. val = sk_CONF_VALUE_value(values, i);
  61. if (strcmp(val->name, "CA") == 0) {
  62. if (!X509V3_get_value_bool(val, &bcons->ca))
  63. goto err;
  64. } else if (strcmp(val->name, "pathlen") == 0) {
  65. if (!X509V3_get_value_int(val, &bcons->pathlen))
  66. goto err;
  67. } else {
  68. ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NAME);
  69. X509V3_conf_add_error_name_value(val);
  70. goto err;
  71. }
  72. }
  73. return bcons;
  74. err:
  75. BASIC_CONSTRAINTS_free(bcons);
  76. return NULL;
  77. }