v3_bitst.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/conf.h>
  12. #include <openssl/x509v3.h>
  13. #include "ext_dat.h"
  14. static BIT_STRING_BITNAME ns_cert_type_table[] = {
  15. {0, "SSL Client", "client"},
  16. {1, "SSL Server", "server"},
  17. {2, "S/MIME", "email"},
  18. {3, "Object Signing", "objsign"},
  19. {4, "Unused", "reserved"},
  20. {5, "SSL CA", "sslCA"},
  21. {6, "S/MIME CA", "emailCA"},
  22. {7, "Object Signing CA", "objCA"},
  23. {-1, NULL, NULL}
  24. };
  25. static BIT_STRING_BITNAME key_usage_type_table[] = {
  26. {0, "Digital Signature", "digitalSignature"},
  27. {1, "Non Repudiation", "nonRepudiation"},
  28. {2, "Key Encipherment", "keyEncipherment"},
  29. {3, "Data Encipherment", "dataEncipherment"},
  30. {4, "Key Agreement", "keyAgreement"},
  31. {5, "Certificate Sign", "keyCertSign"},
  32. {6, "CRL Sign", "cRLSign"},
  33. {7, "Encipher Only", "encipherOnly"},
  34. {8, "Decipher Only", "decipherOnly"},
  35. {-1, NULL, NULL}
  36. };
  37. const X509V3_EXT_METHOD v3_nscert =
  38. EXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
  39. const X509V3_EXT_METHOD v3_key_usage =
  40. EXT_BITSTRING(NID_key_usage, key_usage_type_table);
  41. STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
  42. ASN1_BIT_STRING *bits,
  43. STACK_OF(CONF_VALUE) *ret)
  44. {
  45. BIT_STRING_BITNAME *bnam;
  46. for (bnam = method->usr_data; bnam->lname; bnam++) {
  47. if (ASN1_BIT_STRING_get_bit(bits, bnam->bitnum))
  48. X509V3_add_value(bnam->lname, NULL, &ret);
  49. }
  50. return ret;
  51. }
  52. ASN1_BIT_STRING *v2i_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
  53. X509V3_CTX *ctx,
  54. STACK_OF(CONF_VALUE) *nval)
  55. {
  56. CONF_VALUE *val;
  57. ASN1_BIT_STRING *bs;
  58. int i;
  59. BIT_STRING_BITNAME *bnam;
  60. if ((bs = ASN1_BIT_STRING_new()) == NULL) {
  61. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  62. return NULL;
  63. }
  64. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  65. val = sk_CONF_VALUE_value(nval, i);
  66. for (bnam = method->usr_data; bnam->lname; bnam++) {
  67. if (strcmp(bnam->sname, val->name) == 0
  68. || strcmp(bnam->lname, val->name) == 0) {
  69. if (!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
  70. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  71. ASN1_BIT_STRING_free(bs);
  72. return NULL;
  73. }
  74. break;
  75. }
  76. }
  77. if (!bnam->lname) {
  78. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT,
  79. "%s", val->name);
  80. ASN1_BIT_STRING_free(bs);
  81. return NULL;
  82. }
  83. }
  84. return bs;
  85. }