You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ocsp.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef _IPXE_OCSP_H
  2. #define _IPXE_OCSP_H
  3. /** @file
  4. *
  5. * Online Certificate Status Protocol
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdarg.h>
  10. #include <time.h>
  11. #include <ipxe/asn1.h>
  12. #include <ipxe/x509.h>
  13. #include <ipxe/refcnt.h>
  14. #include <config/crypto.h>
  15. /* Allow OCSP to be disabled completely */
  16. #ifdef OCSP_CHECK
  17. #define OCSP_ENABLED 1
  18. #else
  19. #define OCSP_ENABLED 0
  20. #endif
  21. /** OCSP algorithm identifier */
  22. #define OCSP_ALGORITHM_IDENTIFIER( ... ) \
  23. ASN1_OID, VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__, \
  24. ASN1_NULL, 0x00
  25. /* OCSP response statuses */
  26. #define OCSP_STATUS_SUCCESSFUL 0x00
  27. #define OCSP_STATUS_MALFORMED_REQUEST 0x01
  28. #define OCSP_STATUS_INTERNAL_ERROR 0x02
  29. #define OCSP_STATUS_TRY_LATER 0x03
  30. #define OCSP_STATUS_SIG_REQUIRED 0x05
  31. #define OCSP_STATUS_UNAUTHORIZED 0x06
  32. struct ocsp_check;
  33. /** An OCSP request */
  34. struct ocsp_request {
  35. /** Request builder */
  36. struct asn1_builder builder;
  37. /** Certificate ID (excluding hashAlgorithm) */
  38. struct asn1_cursor cert_id_tail;
  39. };
  40. /** An OCSP responder */
  41. struct ocsp_responder {
  42. /**
  43. * Check if certificate is the responder's certificate
  44. *
  45. * @v ocsp OCSP check
  46. * @v cert Certificate
  47. * @ret difference Difference as returned by memcmp()
  48. */
  49. int ( * compare ) ( struct ocsp_check *ocsp,
  50. struct x509_certificate *cert );
  51. /** Responder ID */
  52. struct asn1_cursor id;
  53. };
  54. /** An OCSP response */
  55. struct ocsp_response {
  56. /** Raw response */
  57. void *data;
  58. /** Raw tbsResponseData */
  59. struct asn1_cursor tbs;
  60. /** Responder */
  61. struct ocsp_responder responder;
  62. /** Time at which status is known to be correct */
  63. time_t this_update;
  64. /** Time at which newer status information will be available */
  65. time_t next_update;
  66. /** Signature algorithm */
  67. struct asn1_algorithm *algorithm;
  68. /** Signature value */
  69. struct asn1_bit_string signature;
  70. /** Signing certificate */
  71. struct x509_certificate *signer;
  72. };
  73. /** An OCSP check */
  74. struct ocsp_check {
  75. /** Reference count */
  76. struct refcnt refcnt;
  77. /** Certificate being checked */
  78. struct x509_certificate *cert;
  79. /** Issuing certificate */
  80. struct x509_certificate *issuer;
  81. /** URI string */
  82. char *uri_string;
  83. /** Request */
  84. struct ocsp_request request;
  85. /** Response */
  86. struct ocsp_response response;
  87. };
  88. /**
  89. * Get reference to OCSP check
  90. *
  91. * @v ocsp OCSP check
  92. * @ret ocsp OCSP check
  93. */
  94. static inline __attribute__ (( always_inline )) struct ocsp_check *
  95. ocsp_get ( struct ocsp_check *ocsp ) {
  96. ref_get ( &ocsp->refcnt );
  97. return ocsp;
  98. }
  99. /**
  100. * Drop reference to OCSP check
  101. *
  102. * @v ocsp OCSP check
  103. */
  104. static inline __attribute__ (( always_inline )) void
  105. ocsp_put ( struct ocsp_check *ocsp ) {
  106. ref_put ( &ocsp->refcnt );
  107. }
  108. /**
  109. * Check if X.509 certificate requires an OCSP check
  110. *
  111. * @v cert X.509 certificate
  112. * @ret ocsp_required An OCSP check is required
  113. */
  114. static inline int ocsp_required ( struct x509_certificate *cert ) {
  115. /* An OCSP check is never required if OCSP checks are disabled */
  116. if ( ! OCSP_ENABLED )
  117. return 0;
  118. /* An OCSP check is required if an OCSP URI exists but the
  119. * OCSP status is not (yet) good.
  120. */
  121. return ( cert->extensions.auth_info.ocsp.uri.len &&
  122. ( ! cert->extensions.auth_info.ocsp.good ) );
  123. }
  124. extern int ocsp_check ( struct x509_certificate *cert,
  125. struct x509_certificate *issuer,
  126. struct ocsp_check **ocsp );
  127. extern int ocsp_response ( struct ocsp_check *ocsp, const void *data,
  128. size_t len );
  129. extern int ocsp_validate ( struct ocsp_check *check, time_t time );
  130. #endif /* _IPXE_OCSP_H */