rfc2560.py 7.6 KB

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