x509v3.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * X.509v3 certificate parsing and processing
  3. * Copyright (c) 2006-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #ifndef X509V3_H
  9. #define X509V3_H
  10. #include "asn1.h"
  11. struct x509_algorithm_identifier {
  12. struct asn1_oid oid;
  13. };
  14. struct x509_name_attr {
  15. enum x509_name_attr_type {
  16. X509_NAME_ATTR_NOT_USED,
  17. X509_NAME_ATTR_DC,
  18. X509_NAME_ATTR_CN,
  19. X509_NAME_ATTR_C,
  20. X509_NAME_ATTR_L,
  21. X509_NAME_ATTR_ST,
  22. X509_NAME_ATTR_O,
  23. X509_NAME_ATTR_OU
  24. } type;
  25. char *value;
  26. };
  27. #define X509_MAX_NAME_ATTRIBUTES 20
  28. struct x509_name {
  29. struct x509_name_attr attr[X509_MAX_NAME_ATTRIBUTES];
  30. size_t num_attr;
  31. char *email; /* emailAddress */
  32. /* from alternative name extension */
  33. char *alt_email; /* rfc822Name */
  34. char *dns; /* dNSName */
  35. char *uri; /* uniformResourceIdentifier */
  36. u8 *ip; /* iPAddress */
  37. size_t ip_len; /* IPv4: 4, IPv6: 16 */
  38. struct asn1_oid rid; /* registeredID */
  39. };
  40. struct x509_certificate {
  41. struct x509_certificate *next;
  42. enum { X509_CERT_V1 = 0, X509_CERT_V2 = 1, X509_CERT_V3 = 2 } version;
  43. unsigned long serial_number;
  44. struct x509_algorithm_identifier signature;
  45. struct x509_name issuer;
  46. struct x509_name subject;
  47. os_time_t not_before;
  48. os_time_t not_after;
  49. struct x509_algorithm_identifier public_key_alg;
  50. u8 *public_key;
  51. size_t public_key_len;
  52. struct x509_algorithm_identifier signature_alg;
  53. u8 *sign_value;
  54. size_t sign_value_len;
  55. /* Extensions */
  56. unsigned int extensions_present;
  57. #define X509_EXT_BASIC_CONSTRAINTS (1 << 0)
  58. #define X509_EXT_PATH_LEN_CONSTRAINT (1 << 1)
  59. #define X509_EXT_KEY_USAGE (1 << 2)
  60. #define X509_EXT_SUBJECT_ALT_NAME (1 << 3)
  61. #define X509_EXT_ISSUER_ALT_NAME (1 << 4)
  62. /* BasicConstraints */
  63. int ca; /* cA */
  64. unsigned long path_len_constraint; /* pathLenConstraint */
  65. /* KeyUsage */
  66. unsigned long key_usage;
  67. #define X509_KEY_USAGE_DIGITAL_SIGNATURE (1 << 0)
  68. #define X509_KEY_USAGE_NON_REPUDIATION (1 << 1)
  69. #define X509_KEY_USAGE_KEY_ENCIPHERMENT (1 << 2)
  70. #define X509_KEY_USAGE_DATA_ENCIPHERMENT (1 << 3)
  71. #define X509_KEY_USAGE_KEY_AGREEMENT (1 << 4)
  72. #define X509_KEY_USAGE_KEY_CERT_SIGN (1 << 5)
  73. #define X509_KEY_USAGE_CRL_SIGN (1 << 6)
  74. #define X509_KEY_USAGE_ENCIPHER_ONLY (1 << 7)
  75. #define X509_KEY_USAGE_DECIPHER_ONLY (1 << 8)
  76. /*
  77. * The DER format certificate follows struct x509_certificate. These
  78. * pointers point to that buffer.
  79. */
  80. const u8 *cert_start;
  81. size_t cert_len;
  82. const u8 *tbs_cert_start;
  83. size_t tbs_cert_len;
  84. };
  85. enum {
  86. X509_VALIDATE_OK,
  87. X509_VALIDATE_BAD_CERTIFICATE,
  88. X509_VALIDATE_UNSUPPORTED_CERTIFICATE,
  89. X509_VALIDATE_CERTIFICATE_REVOKED,
  90. X509_VALIDATE_CERTIFICATE_EXPIRED,
  91. X509_VALIDATE_CERTIFICATE_UNKNOWN,
  92. X509_VALIDATE_UNKNOWN_CA
  93. };
  94. void x509_certificate_free(struct x509_certificate *cert);
  95. struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len);
  96. void x509_name_string(struct x509_name *name, char *buf, size_t len);
  97. int x509_name_compare(struct x509_name *a, struct x509_name *b);
  98. void x509_certificate_chain_free(struct x509_certificate *cert);
  99. int x509_certificate_check_signature(struct x509_certificate *issuer,
  100. struct x509_certificate *cert);
  101. int x509_certificate_chain_validate(struct x509_certificate *trusted,
  102. struct x509_certificate *chain,
  103. int *reason, int disable_time_checks);
  104. struct x509_certificate *
  105. x509_certificate_get_subject(struct x509_certificate *chain,
  106. struct x509_name *name);
  107. int x509_certificate_self_signed(struct x509_certificate *cert);
  108. #endif /* X509V3_H */