x509v3.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * X.509v3 certificate parsing and processing
  3. * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #ifndef X509V3_H
  15. #define X509V3_H
  16. #include "asn1.h"
  17. struct x509_algorithm_identifier {
  18. struct asn1_oid oid;
  19. };
  20. struct x509_name {
  21. char *cn; /* commonName */
  22. char *c; /* countryName */
  23. char *l; /* localityName */
  24. char *st; /* stateOrProvinceName */
  25. char *o; /* organizationName */
  26. char *ou; /* organizationalUnitName */
  27. char *email; /* emailAddress */
  28. /* from alternative name extension */
  29. char *alt_email; /* rfc822Name */
  30. char *dns; /* dNSName */
  31. char *uri; /* uniformResourceIdentifier */
  32. u8 *ip; /* iPAddress */
  33. size_t ip_len; /* IPv4: 4, IPv6: 16 */
  34. struct asn1_oid rid; /* registeredID */
  35. };
  36. struct x509_certificate {
  37. struct x509_certificate *next;
  38. enum { X509_CERT_V1 = 0, X509_CERT_V2 = 1, X509_CERT_V3 = 2 } version;
  39. unsigned long serial_number;
  40. struct x509_algorithm_identifier signature;
  41. struct x509_name issuer;
  42. struct x509_name subject;
  43. os_time_t not_before;
  44. os_time_t not_after;
  45. struct x509_algorithm_identifier public_key_alg;
  46. u8 *public_key;
  47. size_t public_key_len;
  48. struct x509_algorithm_identifier signature_alg;
  49. u8 *sign_value;
  50. size_t sign_value_len;
  51. /* Extensions */
  52. unsigned int extensions_present;
  53. #define X509_EXT_BASIC_CONSTRAINTS (1 << 0)
  54. #define X509_EXT_PATH_LEN_CONSTRAINT (1 << 1)
  55. #define X509_EXT_KEY_USAGE (1 << 2)
  56. #define X509_EXT_SUBJECT_ALT_NAME (1 << 3)
  57. #define X509_EXT_ISSUER_ALT_NAME (1 << 4)
  58. /* BasicConstraints */
  59. int ca; /* cA */
  60. unsigned long path_len_constraint; /* pathLenConstraint */
  61. /* KeyUsage */
  62. unsigned long key_usage;
  63. #define X509_KEY_USAGE_DIGITAL_SIGNATURE (1 << 0)
  64. #define X509_KEY_USAGE_NON_REPUDIATION (1 << 1)
  65. #define X509_KEY_USAGE_KEY_ENCIPHERMENT (1 << 2)
  66. #define X509_KEY_USAGE_DATA_ENCIPHERMENT (1 << 3)
  67. #define X509_KEY_USAGE_KEY_AGREEMENT (1 << 4)
  68. #define X509_KEY_USAGE_KEY_CERT_SIGN (1 << 5)
  69. #define X509_KEY_USAGE_CRL_SIGN (1 << 6)
  70. #define X509_KEY_USAGE_ENCIPHER_ONLY (1 << 7)
  71. #define X509_KEY_USAGE_DECIPHER_ONLY (1 << 8)
  72. /*
  73. * The DER format certificate follows struct x509_certificate. These
  74. * pointers point to that buffer.
  75. */
  76. const u8 *cert_start;
  77. size_t cert_len;
  78. const u8 *tbs_cert_start;
  79. size_t tbs_cert_len;
  80. };
  81. enum {
  82. X509_VALIDATE_OK,
  83. X509_VALIDATE_BAD_CERTIFICATE,
  84. X509_VALIDATE_UNSUPPORTED_CERTIFICATE,
  85. X509_VALIDATE_CERTIFICATE_REVOKED,
  86. X509_VALIDATE_CERTIFICATE_EXPIRED,
  87. X509_VALIDATE_CERTIFICATE_UNKNOWN,
  88. X509_VALIDATE_UNKNOWN_CA
  89. };
  90. #ifdef CONFIG_INTERNAL_X509
  91. void x509_certificate_free(struct x509_certificate *cert);
  92. struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len);
  93. void x509_name_string(struct x509_name *name, char *buf, size_t len);
  94. int x509_name_compare(struct x509_name *a, struct x509_name *b);
  95. void x509_certificate_chain_free(struct x509_certificate *cert);
  96. int x509_certificate_check_signature(struct x509_certificate *issuer,
  97. struct x509_certificate *cert);
  98. int x509_certificate_chain_validate(struct x509_certificate *trusted,
  99. struct x509_certificate *chain,
  100. int *reason);
  101. struct x509_certificate *
  102. x509_certificate_get_subject(struct x509_certificate *chain,
  103. struct x509_name *name);
  104. int x509_certificate_self_signed(struct x509_certificate *cert);
  105. #else /* CONFIG_INTERNAL_X509 */
  106. static inline void x509_certificate_free(struct x509_certificate *cert)
  107. {
  108. }
  109. static inline struct x509_certificate *
  110. x509_certificate_parse(const u8 *buf, size_t len)
  111. {
  112. return NULL;
  113. }
  114. static inline void x509_name_string(struct x509_name *name, char *buf,
  115. size_t len)
  116. {
  117. if (len)
  118. buf[0] = '\0';
  119. }
  120. static inline void x509_certificate_chain_free(struct x509_certificate *cert)
  121. {
  122. }
  123. static inline int
  124. x509_certificate_chain_validate(struct x509_certificate *trusted,
  125. struct x509_certificate *chain,
  126. int *reason)
  127. {
  128. return -1;
  129. }
  130. static inline struct x509_certificate *
  131. x509_certificate_get_subject(struct x509_certificate *chain,
  132. struct x509_name *name)
  133. {
  134. return NULL;
  135. }
  136. static inline int x509_certificate_self_signed(struct x509_certificate *cert)
  137. {
  138. return -1;
  139. }
  140. #endif /* CONFIG_INTERNAL_X509 */
  141. #endif /* X509V3_H */