v3_bcons.c 2.9 KB

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