rfc2560.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
  5. # License: http://pyasn1.sf.net/license.html
  6. #
  7. # OCSP request/response syntax
  8. #
  9. # Derived from a minimal OCSP library (RFC2560) code written by
  10. # Bud P. Bruegger <bud@ancitel.it>
  11. # Copyright: Ancitel, S.p.a, Rome, Italy
  12. # License: BSD
  13. #
  14. #
  15. # current limitations:
  16. # * request and response works only for a single certificate
  17. # * only some values are parsed out of the response
  18. # * the request does't set a nonce nor signature
  19. # * there is no signature validation of the response
  20. # * dates are left as strings in GeneralizedTime format -- datetime.datetime
  21. # would be nicer
  22. #
  23. from pyasn1.type import tag, namedtype, namedval, univ, useful
  24. from pyasn1_modules import rfc2459
  25. # Start of OCSP module definitions
  26. # This should be in directory Authentication Framework (X.509) module
  27. class CRLReason(univ.Enumerated):
  28. namedValues = namedval.NamedValues(
  29. ('unspecified', 0),
  30. ('keyCompromise', 1),
  31. ('cACompromise', 2),
  32. ('affiliationChanged', 3),
  33. ('superseded', 4),
  34. ('cessationOfOperation', 5),
  35. ('certificateHold', 6),
  36. ('removeFromCRL', 8),
  37. ('privilegeWithdrawn', 9),
  38. ('aACompromise', 10)
  39. )
  40. # end of directory Authentication Framework (X.509) module
  41. # This should be in PKIX Certificate Extensions module
  42. class GeneralName(univ.OctetString):
  43. pass
  44. # end of PKIX Certificate Extensions module
  45. id_kp_OCSPSigning = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 3, 9))
  46. id_pkix_ocsp = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1))
  47. id_pkix_ocsp_basic = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1, 1))
  48. id_pkix_ocsp_nonce = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1, 2))
  49. id_pkix_ocsp_crl = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1, 3))
  50. id_pkix_ocsp_response = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1, 4))
  51. id_pkix_ocsp_nocheck = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1, 5))
  52. id_pkix_ocsp_archive_cutoff = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1, 6))
  53. id_pkix_ocsp_service_locator = univ.ObjectIdentifier((1, 3, 6, 1, 5, 5, 7, 48, 1, 7))
  54. class AcceptableResponses(univ.SequenceOf):
  55. componentType = univ.ObjectIdentifier()
  56. class ArchiveCutoff(useful.GeneralizedTime):
  57. pass
  58. class UnknownInfo(univ.Null):
  59. pass
  60. class RevokedInfo(univ.Sequence):
  61. componentType = namedtype.NamedTypes(
  62. namedtype.NamedType('revocationTime', useful.GeneralizedTime()),
  63. namedtype.OptionalNamedType('revocationReason', CRLReason().subtype(
  64. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))
  65. )
  66. class CertID(univ.Sequence):
  67. componentType = namedtype.NamedTypes(
  68. namedtype.NamedType('hashAlgorithm', rfc2459.AlgorithmIdentifier()),
  69. namedtype.NamedType('issuerNameHash', univ.OctetString()),
  70. namedtype.NamedType('issuerKeyHash', univ.OctetString()),
  71. namedtype.NamedType('serialNumber', rfc2459.CertificateSerialNumber())
  72. )
  73. class CertStatus(univ.Choice):
  74. componentType = namedtype.NamedTypes(
  75. namedtype.NamedType('good',
  76. univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  77. namedtype.NamedType('revoked',
  78. RevokedInfo().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  79. namedtype.NamedType('unknown',
  80. UnknownInfo().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
  81. )
  82. class SingleResponse(univ.Sequence):
  83. componentType = namedtype.NamedTypes(
  84. namedtype.NamedType('certID', CertID()),
  85. namedtype.NamedType('certStatus', CertStatus()),
  86. namedtype.NamedType('thisUpdate', useful.GeneralizedTime()),
  87. namedtype.OptionalNamedType('nextUpdate', useful.GeneralizedTime().subtype(
  88. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  89. namedtype.OptionalNamedType('singleExtensions', rfc2459.Extensions().subtype(
  90. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
  91. )
  92. class KeyHash(univ.OctetString):
  93. pass
  94. class ResponderID(univ.Choice):
  95. componentType = namedtype.NamedTypes(
  96. namedtype.NamedType('byName',
  97. rfc2459.Name().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  98. namedtype.NamedType('byKey',
  99. KeyHash().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
  100. )
  101. class Version(univ.Integer):
  102. namedValues = namedval.NamedValues(('v1', 0))
  103. class ResponseData(univ.Sequence):
  104. componentType = namedtype.NamedTypes(
  105. namedtype.DefaultedNamedType('version', Version('v1').subtype(
  106. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  107. namedtype.NamedType('responderID', ResponderID()),
  108. namedtype.NamedType('producedAt', useful.GeneralizedTime()),
  109. namedtype.NamedType('responses', univ.SequenceOf(SingleResponse())),
  110. namedtype.OptionalNamedType('responseExtensions', rfc2459.Extensions().subtype(
  111. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
  112. )
  113. class BasicOCSPResponse(univ.Sequence):
  114. componentType = namedtype.NamedTypes(
  115. namedtype.NamedType('tbsResponseData', ResponseData()),
  116. namedtype.NamedType('signatureAlgorithm', rfc2459.AlgorithmIdentifier()),
  117. namedtype.NamedType('signature', univ.BitString()),
  118. namedtype.OptionalNamedType('certs', univ.SequenceOf(rfc2459.Certificate()).subtype(
  119. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))
  120. )
  121. class ResponseBytes(univ.Sequence):
  122. componentType = namedtype.NamedTypes(
  123. namedtype.NamedType('responseType', univ.ObjectIdentifier()),
  124. namedtype.NamedType('response', univ.OctetString())
  125. )
  126. class OCSPResponseStatus(univ.Enumerated):
  127. namedValues = namedval.NamedValues(
  128. ('successful', 0),
  129. ('malformedRequest', 1),
  130. ('internalError', 2),
  131. ('tryLater', 3),
  132. ('undefinedStatus', 4), # should never occur
  133. ('sigRequired', 5),
  134. ('unauthorized', 6)
  135. )
  136. class OCSPResponse(univ.Sequence):
  137. componentType = namedtype.NamedTypes(
  138. namedtype.NamedType('responseStatus', OCSPResponseStatus()),
  139. namedtype.OptionalNamedType('responseBytes', ResponseBytes().subtype(
  140. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))
  141. )
  142. class Request(univ.Sequence):
  143. componentType = namedtype.NamedTypes(
  144. namedtype.NamedType('reqCert', CertID()),
  145. namedtype.OptionalNamedType('singleRequestExtensions', rfc2459.Extensions().subtype(
  146. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))
  147. )
  148. class Signature(univ.Sequence):
  149. componentType = namedtype.NamedTypes(
  150. namedtype.NamedType('signatureAlgorithm', rfc2459.AlgorithmIdentifier()),
  151. namedtype.NamedType('signature', univ.BitString()),
  152. namedtype.OptionalNamedType('certs', univ.SequenceOf(rfc2459.Certificate()).subtype(
  153. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))
  154. )
  155. class TBSRequest(univ.Sequence):
  156. componentType = namedtype.NamedTypes(
  157. namedtype.DefaultedNamedType('version', Version('v1').subtype(
  158. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  159. namedtype.OptionalNamedType('requestorName', GeneralName().subtype(
  160. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  161. namedtype.NamedType('requestList', univ.SequenceOf(Request())),
  162. namedtype.OptionalNamedType('requestExtensions', rfc2459.Extensions().subtype(
  163. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
  164. )
  165. class OCSPRequest(univ.Sequence):
  166. componentType = namedtype.NamedTypes(
  167. namedtype.NamedType('tbsRequest', TBSRequest()),
  168. namedtype.OptionalNamedType('optionalSignature', Signature().subtype(
  169. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))
  170. )