rfc2511.py 10 KB

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