v3_bitst.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. X509V3err(X509V3_F_V2I_ASN1_BIT_STRING, 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. X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
  71. ERR_R_MALLOC_FAILURE);
  72. ASN1_BIT_STRING_free(bs);
  73. return NULL;
  74. }
  75. break;
  76. }
  77. }
  78. if (!bnam->lname) {
  79. X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
  80. X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT);
  81. X509V3_conf_err(val);
  82. ASN1_BIT_STRING_free(bs);
  83. return NULL;
  84. }
  85. }
  86. return bs;
  87. }