rfc2511.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #
  2. # X.509 certificate Request Message Format (CRMF) syntax
  3. #
  4. # ASN.1 source from:
  5. # http://tools.ietf.org/html/rfc2511
  6. #
  7. # Sample captures could be obtained with OpenSSL
  8. #
  9. from pyasn1.type import tag, namedtype, namedval, univ, constraint, char,useful
  10. from pyasn1_modules.rfc2459 import *
  11. from pyasn1_modules import rfc2315
  12. MAX=16
  13. id_pkix = univ.ObjectIdentifier('1.3.6.1.5.5.7')
  14. id_pkip = univ.ObjectIdentifier('1.3.6.1.5.5.7.5')
  15. id_regCtrl = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.1')
  16. id_regCtrl_regToken = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.1.1')
  17. id_regCtrl_authenticator = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.1.2')
  18. id_regCtrl_pkiPublicationInfo = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.1.3')
  19. id_regCtrl_pkiArchiveOptions = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.1.4')
  20. id_regCtrl_oldCertID = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.1.5')
  21. id_regCtrl_protocolEncrKey = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.1.6')
  22. id_regInfo = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.2')
  23. id_regInfo_utf8Pairs = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.2.1')
  24. id_regInfo_certReq = univ.ObjectIdentifier('1.3.6.1.5.5.7.5.2.2')
  25. # This should be in PKIX Certificate Extensions module
  26. class GeneralName(univ.OctetString): pass
  27. # end of PKIX Certificate Extensions module
  28. class UTF8Pairs(char.UTF8String): pass
  29. class ProtocolEncrKey(SubjectPublicKeyInfo): pass
  30. class CertId(univ.Sequence):
  31. componentType = namedtype.NamedTypes(
  32. namedtype.NamedType('issuer', GeneralName()),
  33. namedtype.NamedType('serialNumber', univ.Integer())
  34. )
  35. class OldCertId(CertId): pass
  36. class KeyGenParameters(univ.OctetString): pass
  37. class EncryptedValue(univ.Sequence):
  38. componentType = namedtype.NamedTypes(
  39. namedtype.OptionalNamedType('intendedAlg', AlgorithmIdentifier().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
  40. namedtype.OptionalNamedType('symmAlg', AlgorithmIdentifier().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1))),
  41. namedtype.OptionalNamedType('encSymmKey', univ.BitString().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2))),
  42. namedtype.OptionalNamedType('keyAlg', AlgorithmIdentifier().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))),
  43. namedtype.OptionalNamedType('valueHint', univ.OctetString().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4))),
  44. namedtype.NamedType('encValue', univ.BitString())
  45. )
  46. class EncryptedKey(univ.Choice):
  47. componentType = namedtype.NamedTypes(
  48. namedtype.NamedType('encryptedValue', EncryptedValue()),
  49. namedtype.NamedType('envelopedData', rfc2315.EnvelopedData().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)))
  50. )
  51. class PKIArchiveOptions(univ.Choice):
  52. componentType = namedtype.NamedTypes(
  53. namedtype.NamedType('encryptedPrivKey', EncryptedKey().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
  54. namedtype.NamedType('keyGenParameters', KeyGenParameters().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  55. namedtype.NamedType('archiveRemGenPrivKey', univ.Boolean().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
  56. )
  57. class SinglePubInfo(univ.Sequence):
  58. componentType = namedtype.NamedTypes(
  59. namedtype.NamedType('pubMethod', univ.Integer(namedValues=namedval.NamedValues(('dontCare', 0), ('x500', 1), ('web', 2), ('ldap', 3)))),
  60. namedtype.OptionalNamedType('pubLocation', GeneralName())
  61. )
  62. class PKIPublicationInfo(univ.Sequence):
  63. componentType = namedtype.NamedTypes(
  64. namedtype.NamedType('action', univ.Integer(namedValues=namedval.NamedValues(('dontPublish', 0), ('pleasePublish', 1)))),
  65. namedtype.OptionalNamedType('pubInfos', univ.SequenceOf(componentType=SinglePubInfo()).subtype(subtypeSpec=constraint.ValueSizeConstraint(1, MAX)))
  66. )
  67. class Authenticator(char.UTF8String): pass
  68. class RegToken(char.UTF8String): pass
  69. class SubsequentMessage(univ.Integer):
  70. namedValues = namedval.NamedValues(
  71. ('encrCert', 0),
  72. ('challengeResp', 1)
  73. )
  74. class POPOPrivKey(univ.Choice):
  75. componentType = namedtype.NamedTypes(
  76. namedtype.NamedType('thisMessage', univ.BitString().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  77. namedtype.NamedType('subsequentMessage', SubsequentMessage().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  78. namedtype.NamedType('dhMAC', univ.BitString().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
  79. )
  80. class PBMParameter(univ.Sequence):
  81. componentType = namedtype.NamedTypes(
  82. namedtype.NamedType('salt', univ.OctetString()),
  83. namedtype.NamedType('owf', AlgorithmIdentifier()),
  84. namedtype.NamedType('iterationCount', univ.Integer()),
  85. namedtype.NamedType('mac', AlgorithmIdentifier())
  86. )
  87. class PKMACValue(univ.Sequence):
  88. componentType = namedtype.NamedTypes(
  89. namedtype.NamedType('algId', AlgorithmIdentifier()),
  90. namedtype.NamedType('value', univ.BitString())
  91. )
  92. class POPOSigningKeyInput(univ.Sequence):
  93. componentType = namedtype.NamedTypes(
  94. namedtype.NamedType('authInfo', univ.Choice(componentType=namedtype.NamedTypes(namedtype.NamedType('sender', GeneralName().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))), namedtype.NamedType('publicKeyMAC', PKMACValue())))),
  95. namedtype.NamedType('publicKey', SubjectPublicKeyInfo())
  96. )
  97. class POPOSigningKey(univ.Sequence):
  98. componentType = namedtype.NamedTypes(
  99. namedtype.OptionalNamedType('poposkInput', POPOSigningKeyInput().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
  100. namedtype.NamedType('algorithmIdentifier', AlgorithmIdentifier()),
  101. namedtype.NamedType('signature', univ.BitString())
  102. )
  103. class ProofOfPossession(univ.Choice):
  104. componentType = namedtype.NamedTypes(
  105. namedtype.NamedType('raVerified', univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  106. namedtype.NamedType('signature', POPOSigningKey().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1))),
  107. namedtype.NamedType('keyEncipherment', POPOPrivKey().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2))),
  108. namedtype.NamedType('keyAgreement', POPOPrivKey().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3)))
  109. )
  110. class Controls(univ.SequenceOf):
  111. componentType = AttributeTypeAndValue()
  112. subtypeSpec = univ.SequenceOf.subtypeSpec + constraint.ValueSizeConstraint(1, MAX)
  113. class OptionalValidity(univ.Sequence):
  114. componentType = namedtype.NamedTypes(
  115. namedtype.OptionalNamedType('notBefore', Time().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  116. namedtype.OptionalNamedType('notAfter', Time().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
  117. )
  118. class CertTemplate(univ.Sequence):
  119. componentType = namedtype.NamedTypes(
  120. namedtype.OptionalNamedType('version', Version().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  121. namedtype.OptionalNamedType('serialNumber', univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  122. namedtype.OptionalNamedType('signingAlg', AlgorithmIdentifier().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2))),
  123. namedtype.OptionalNamedType('issuer', Name().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))),
  124. namedtype.OptionalNamedType('validity', OptionalValidity().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4))),
  125. namedtype.OptionalNamedType('subject', Name().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 5))),
  126. namedtype.OptionalNamedType('publicKey', SubjectPublicKeyInfo().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 6))),
  127. namedtype.OptionalNamedType('issuerUID', UniqueIdentifier().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 7))),
  128. namedtype.OptionalNamedType('subjectUID', UniqueIdentifier().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 8))),
  129. namedtype.OptionalNamedType('extensions', Extensions().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 9)))
  130. )
  131. class CertRequest(univ.Sequence):
  132. componentType = namedtype.NamedTypes(
  133. namedtype.NamedType('certReqId', univ.Integer()),
  134. namedtype.NamedType('certTemplate', CertTemplate()),
  135. namedtype.OptionalNamedType('controls', Controls())
  136. )
  137. class CertReq(CertRequest): pass
  138. class CertReqMsg(univ.Sequence):
  139. componentType = namedtype.NamedTypes(
  140. namedtype.NamedType('certReq', CertRequest()),
  141. namedtype.OptionalNamedType('pop', ProofOfPossession()),
  142. namedtype.OptionalNamedType('regInfo', univ.SequenceOf(componentType=AttributeTypeAndValue).subtype(subtypeSpec=constraint.ValueSizeConstraint(1, MAX)))
  143. )
  144. class CertReqMessages(univ.SequenceOf):
  145. componentType = CertReqMsg()
  146. subtypeSpec = univ.SequenceOf.subtypeSpec + constraint.ValueSizeConstraint(1, MAX)