rfc6402.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. # coding: utf-8
  2. #
  3. # This file is part of pyasn1-modules software.
  4. #
  5. # Created by Stanisław Pitucha with asn1ate tool.
  6. # Modified by Russ Housley to add a maps for CMC Control Attributes
  7. # and CMC Content Types for use with opentypes.
  8. #
  9. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  10. # License: http://snmplabs.com/pyasn1/license.html
  11. #
  12. # Certificate Management over CMS (CMC) Updates
  13. #
  14. # ASN.1 source from:
  15. # https://www.rfc-editor.org/rfc/rfc6402.txt
  16. #
  17. from pyasn1.type import char
  18. from pyasn1.type import constraint
  19. from pyasn1.type import namedtype
  20. from pyasn1.type import namedval
  21. from pyasn1.type import opentype
  22. from pyasn1.type import tag
  23. from pyasn1.type import univ
  24. from pyasn1.type import useful
  25. from pyasn1_modules import rfc4211
  26. from pyasn1_modules import rfc5280
  27. from pyasn1_modules import rfc5652
  28. MAX = float('inf')
  29. def _buildOid(*components):
  30. output = []
  31. for x in tuple(components):
  32. if isinstance(x, univ.ObjectIdentifier):
  33. output.extend(list(x))
  34. else:
  35. output.append(int(x))
  36. return univ.ObjectIdentifier(output)
  37. # Since CMS Attributes and CMC Controls both use 'attrType', one map is used
  38. cmcControlAttributesMap = rfc5652.cmsAttributesMap
  39. class ChangeSubjectName(univ.Sequence):
  40. pass
  41. ChangeSubjectName.componentType = namedtype.NamedTypes(
  42. namedtype.OptionalNamedType('subject', rfc5280.Name()),
  43. namedtype.OptionalNamedType('subjectAlt', rfc5280.GeneralNames())
  44. )
  45. class AttributeValue(univ.Any):
  46. pass
  47. class CMCStatus(univ.Integer):
  48. pass
  49. CMCStatus.namedValues = namedval.NamedValues(
  50. ('success', 0),
  51. ('failed', 2),
  52. ('pending', 3),
  53. ('noSupport', 4),
  54. ('confirmRequired', 5),
  55. ('popRequired', 6),
  56. ('partial', 7)
  57. )
  58. class PendInfo(univ.Sequence):
  59. pass
  60. PendInfo.componentType = namedtype.NamedTypes(
  61. namedtype.NamedType('pendToken', univ.OctetString()),
  62. namedtype.NamedType('pendTime', useful.GeneralizedTime())
  63. )
  64. bodyIdMax = univ.Integer(4294967295)
  65. class BodyPartID(univ.Integer):
  66. pass
  67. BodyPartID.subtypeSpec = constraint.ValueRangeConstraint(0, bodyIdMax)
  68. class BodyPartPath(univ.SequenceOf):
  69. pass
  70. BodyPartPath.componentType = BodyPartID()
  71. BodyPartPath.sizeSpec = constraint.ValueSizeConstraint(1, MAX)
  72. class BodyPartReference(univ.Choice):
  73. pass
  74. BodyPartReference.componentType = namedtype.NamedTypes(
  75. namedtype.NamedType('bodyPartID', BodyPartID()),
  76. namedtype.NamedType('bodyPartPath', BodyPartPath())
  77. )
  78. class CMCFailInfo(univ.Integer):
  79. pass
  80. CMCFailInfo.namedValues = namedval.NamedValues(
  81. ('badAlg', 0),
  82. ('badMessageCheck', 1),
  83. ('badRequest', 2),
  84. ('badTime', 3),
  85. ('badCertId', 4),
  86. ('unsupportedExt', 5),
  87. ('mustArchiveKeys', 6),
  88. ('badIdentity', 7),
  89. ('popRequired', 8),
  90. ('popFailed', 9),
  91. ('noKeyReuse', 10),
  92. ('internalCAError', 11),
  93. ('tryLater', 12),
  94. ('authDataFail', 13)
  95. )
  96. class CMCStatusInfoV2(univ.Sequence):
  97. pass
  98. CMCStatusInfoV2.componentType = namedtype.NamedTypes(
  99. namedtype.NamedType('cMCStatus', CMCStatus()),
  100. namedtype.NamedType('bodyList', univ.SequenceOf(componentType=BodyPartReference())),
  101. namedtype.OptionalNamedType('statusString', char.UTF8String()),
  102. namedtype.OptionalNamedType(
  103. 'otherInfo', univ.Choice(
  104. componentType=namedtype.NamedTypes(
  105. namedtype.NamedType('failInfo', CMCFailInfo()),
  106. namedtype.NamedType('pendInfo', PendInfo()),
  107. namedtype.NamedType(
  108. 'extendedFailInfo', univ.Sequence(
  109. componentType=namedtype.NamedTypes(
  110. namedtype.NamedType('failInfoOID', univ.ObjectIdentifier()),
  111. namedtype.NamedType('failInfoValue', AttributeValue()))
  112. )
  113. )
  114. )
  115. )
  116. )
  117. )
  118. class GetCRL(univ.Sequence):
  119. pass
  120. GetCRL.componentType = namedtype.NamedTypes(
  121. namedtype.NamedType('issuerName', rfc5280.Name()),
  122. namedtype.OptionalNamedType('cRLName', rfc5280.GeneralName()),
  123. namedtype.OptionalNamedType('time', useful.GeneralizedTime()),
  124. namedtype.OptionalNamedType('reasons', rfc5280.ReasonFlags())
  125. )
  126. id_pkix = _buildOid(1, 3, 6, 1, 5, 5, 7)
  127. id_cmc = _buildOid(id_pkix, 7)
  128. id_cmc_batchResponses = _buildOid(id_cmc, 29)
  129. id_cmc_popLinkWitness = _buildOid(id_cmc, 23)
  130. class PopLinkWitnessV2(univ.Sequence):
  131. pass
  132. PopLinkWitnessV2.componentType = namedtype.NamedTypes(
  133. namedtype.NamedType('keyGenAlgorithm', rfc5280.AlgorithmIdentifier()),
  134. namedtype.NamedType('macAlgorithm', rfc5280.AlgorithmIdentifier()),
  135. namedtype.NamedType('witness', univ.OctetString())
  136. )
  137. id_cmc_popLinkWitnessV2 = _buildOid(id_cmc, 33)
  138. id_cmc_identityProofV2 = _buildOid(id_cmc, 34)
  139. id_cmc_revokeRequest = _buildOid(id_cmc, 17)
  140. id_cmc_recipientNonce = _buildOid(id_cmc, 7)
  141. class ControlsProcessed(univ.Sequence):
  142. pass
  143. ControlsProcessed.componentType = namedtype.NamedTypes(
  144. namedtype.NamedType('bodyList', univ.SequenceOf(componentType=BodyPartReference()))
  145. )
  146. class CertificationRequest(univ.Sequence):
  147. pass
  148. CertificationRequest.componentType = namedtype.NamedTypes(
  149. namedtype.NamedType(
  150. 'certificationRequestInfo', univ.Sequence(
  151. componentType=namedtype.NamedTypes(
  152. namedtype.NamedType('version', univ.Integer()),
  153. namedtype.NamedType('subject', rfc5280.Name()),
  154. namedtype.NamedType(
  155. 'subjectPublicKeyInfo', univ.Sequence(
  156. componentType=namedtype.NamedTypes(
  157. namedtype.NamedType('algorithm', rfc5280.AlgorithmIdentifier()),
  158. namedtype.NamedType('subjectPublicKey', univ.BitString())
  159. )
  160. )
  161. ),
  162. namedtype.NamedType(
  163. 'attributes', univ.SetOf(
  164. componentType=rfc5652.Attribute()).subtype(
  165. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))
  166. )
  167. )
  168. )
  169. ),
  170. namedtype.NamedType('signatureAlgorithm', rfc5280.AlgorithmIdentifier()),
  171. namedtype.NamedType('signature', univ.BitString())
  172. )
  173. class TaggedCertificationRequest(univ.Sequence):
  174. pass
  175. TaggedCertificationRequest.componentType = namedtype.NamedTypes(
  176. namedtype.NamedType('bodyPartID', BodyPartID()),
  177. namedtype.NamedType('certificationRequest', CertificationRequest())
  178. )
  179. class TaggedRequest(univ.Choice):
  180. pass
  181. TaggedRequest.componentType = namedtype.NamedTypes(
  182. namedtype.NamedType('tcr', TaggedCertificationRequest().subtype(
  183. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
  184. namedtype.NamedType('crm',
  185. rfc4211.CertReqMsg().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  186. namedtype.NamedType('orm', univ.Sequence(componentType=namedtype.NamedTypes(
  187. namedtype.NamedType('bodyPartID', BodyPartID()),
  188. namedtype.NamedType('requestMessageType', univ.ObjectIdentifier()),
  189. namedtype.NamedType('requestMessageValue', univ.Any())
  190. ))
  191. .subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2)))
  192. )
  193. id_cmc_popLinkRandom = _buildOid(id_cmc, 22)
  194. id_cmc_statusInfo = _buildOid(id_cmc, 1)
  195. id_cmc_trustedAnchors = _buildOid(id_cmc, 26)
  196. id_cmc_transactionId = _buildOid(id_cmc, 5)
  197. id_cmc_encryptedPOP = _buildOid(id_cmc, 9)
  198. class PublishTrustAnchors(univ.Sequence):
  199. pass
  200. PublishTrustAnchors.componentType = namedtype.NamedTypes(
  201. namedtype.NamedType('seqNumber', univ.Integer()),
  202. namedtype.NamedType('hashAlgorithm', rfc5280.AlgorithmIdentifier()),
  203. namedtype.NamedType('anchorHashes', univ.SequenceOf(componentType=univ.OctetString()))
  204. )
  205. class RevokeRequest(univ.Sequence):
  206. pass
  207. RevokeRequest.componentType = namedtype.NamedTypes(
  208. namedtype.NamedType('issuerName', rfc5280.Name()),
  209. namedtype.NamedType('serialNumber', univ.Integer()),
  210. namedtype.NamedType('reason', rfc5280.CRLReason()),
  211. namedtype.OptionalNamedType('invalidityDate', useful.GeneralizedTime()),
  212. namedtype.OptionalNamedType('passphrase', univ.OctetString()),
  213. namedtype.OptionalNamedType('comment', char.UTF8String())
  214. )
  215. id_cmc_senderNonce = _buildOid(id_cmc, 6)
  216. id_cmc_authData = _buildOid(id_cmc, 27)
  217. class TaggedContentInfo(univ.Sequence):
  218. pass
  219. TaggedContentInfo.componentType = namedtype.NamedTypes(
  220. namedtype.NamedType('bodyPartID', BodyPartID()),
  221. namedtype.NamedType('contentInfo', rfc5652.ContentInfo())
  222. )
  223. class IdentifyProofV2(univ.Sequence):
  224. pass
  225. IdentifyProofV2.componentType = namedtype.NamedTypes(
  226. namedtype.NamedType('proofAlgID', rfc5280.AlgorithmIdentifier()),
  227. namedtype.NamedType('macAlgId', rfc5280.AlgorithmIdentifier()),
  228. namedtype.NamedType('witness', univ.OctetString())
  229. )
  230. class CMCPublicationInfo(univ.Sequence):
  231. pass
  232. CMCPublicationInfo.componentType = namedtype.NamedTypes(
  233. namedtype.NamedType('hashAlg', rfc5280.AlgorithmIdentifier()),
  234. namedtype.NamedType('certHashes', univ.SequenceOf(componentType=univ.OctetString())),
  235. namedtype.NamedType('pubInfo', rfc4211.PKIPublicationInfo())
  236. )
  237. id_kp_cmcCA = _buildOid(rfc5280.id_kp, 27)
  238. id_cmc_confirmCertAcceptance = _buildOid(id_cmc, 24)
  239. id_cmc_raIdentityWitness = _buildOid(id_cmc, 35)
  240. id_ExtensionReq = _buildOid(1, 2, 840, 113549, 1, 9, 14)
  241. id_cct = _buildOid(id_pkix, 12)
  242. id_cct_PKIData = _buildOid(id_cct, 2)
  243. id_kp_cmcRA = _buildOid(rfc5280.id_kp, 28)
  244. class CMCStatusInfo(univ.Sequence):
  245. pass
  246. CMCStatusInfo.componentType = namedtype.NamedTypes(
  247. namedtype.NamedType('cMCStatus', CMCStatus()),
  248. namedtype.NamedType('bodyList', univ.SequenceOf(componentType=BodyPartID())),
  249. namedtype.OptionalNamedType('statusString', char.UTF8String()),
  250. namedtype.OptionalNamedType(
  251. 'otherInfo', univ.Choice(
  252. componentType=namedtype.NamedTypes(
  253. namedtype.NamedType('failInfo', CMCFailInfo()),
  254. namedtype.NamedType('pendInfo', PendInfo())
  255. )
  256. )
  257. )
  258. )
  259. class DecryptedPOP(univ.Sequence):
  260. pass
  261. DecryptedPOP.componentType = namedtype.NamedTypes(
  262. namedtype.NamedType('bodyPartID', BodyPartID()),
  263. namedtype.NamedType('thePOPAlgID', rfc5280.AlgorithmIdentifier()),
  264. namedtype.NamedType('thePOP', univ.OctetString())
  265. )
  266. id_cmc_addExtensions = _buildOid(id_cmc, 8)
  267. id_cmc_modCertTemplate = _buildOid(id_cmc, 31)
  268. class TaggedAttribute(univ.Sequence):
  269. pass
  270. TaggedAttribute.componentType = namedtype.NamedTypes(
  271. namedtype.NamedType('bodyPartID', BodyPartID()),
  272. namedtype.NamedType('attrType', univ.ObjectIdentifier()),
  273. namedtype.NamedType('attrValues', univ.SetOf(componentType=AttributeValue()),
  274. openType=opentype.OpenType('attrType', cmcControlAttributesMap)
  275. )
  276. )
  277. class OtherMsg(univ.Sequence):
  278. pass
  279. OtherMsg.componentType = namedtype.NamedTypes(
  280. namedtype.NamedType('bodyPartID', BodyPartID()),
  281. namedtype.NamedType('otherMsgType', univ.ObjectIdentifier()),
  282. namedtype.NamedType('otherMsgValue', univ.Any())
  283. )
  284. class PKIData(univ.Sequence):
  285. pass
  286. PKIData.componentType = namedtype.NamedTypes(
  287. namedtype.NamedType('controlSequence', univ.SequenceOf(componentType=TaggedAttribute())),
  288. namedtype.NamedType('reqSequence', univ.SequenceOf(componentType=TaggedRequest())),
  289. namedtype.NamedType('cmsSequence', univ.SequenceOf(componentType=TaggedContentInfo())),
  290. namedtype.NamedType('otherMsgSequence', univ.SequenceOf(componentType=OtherMsg()))
  291. )
  292. class BodyPartList(univ.SequenceOf):
  293. pass
  294. BodyPartList.componentType = BodyPartID()
  295. BodyPartList.sizeSpec = constraint.ValueSizeConstraint(1, MAX)
  296. id_cmc_responseBody = _buildOid(id_cmc, 37)
  297. class AuthPublish(BodyPartID):
  298. pass
  299. class CMCUnsignedData(univ.Sequence):
  300. pass
  301. CMCUnsignedData.componentType = namedtype.NamedTypes(
  302. namedtype.NamedType('bodyPartPath', BodyPartPath()),
  303. namedtype.NamedType('identifier', univ.ObjectIdentifier()),
  304. namedtype.NamedType('content', univ.Any())
  305. )
  306. class CMCCertId(rfc5652.IssuerAndSerialNumber):
  307. pass
  308. class PKIResponse(univ.Sequence):
  309. pass
  310. PKIResponse.componentType = namedtype.NamedTypes(
  311. namedtype.NamedType('controlSequence', univ.SequenceOf(componentType=TaggedAttribute())),
  312. namedtype.NamedType('cmsSequence', univ.SequenceOf(componentType=TaggedContentInfo())),
  313. namedtype.NamedType('otherMsgSequence', univ.SequenceOf(componentType=OtherMsg()))
  314. )
  315. class ResponseBody(PKIResponse):
  316. pass
  317. id_cmc_statusInfoV2 = _buildOid(id_cmc, 25)
  318. id_cmc_lraPOPWitness = _buildOid(id_cmc, 11)
  319. class ModCertTemplate(univ.Sequence):
  320. pass
  321. ModCertTemplate.componentType = namedtype.NamedTypes(
  322. namedtype.NamedType('pkiDataReference', BodyPartPath()),
  323. namedtype.NamedType('certReferences', BodyPartList()),
  324. namedtype.DefaultedNamedType('replace', univ.Boolean().subtype(value=1)),
  325. namedtype.NamedType('certTemplate', rfc4211.CertTemplate())
  326. )
  327. id_cmc_regInfo = _buildOid(id_cmc, 18)
  328. id_cmc_identityProof = _buildOid(id_cmc, 3)
  329. class ExtensionReq(univ.SequenceOf):
  330. pass
  331. ExtensionReq.componentType = rfc5280.Extension()
  332. ExtensionReq.sizeSpec = constraint.ValueSizeConstraint(1, MAX)
  333. id_kp_cmcArchive = _buildOid(rfc5280.id_kp, 28)
  334. id_cmc_publishCert = _buildOid(id_cmc, 30)
  335. id_cmc_dataReturn = _buildOid(id_cmc, 4)
  336. class LraPopWitness(univ.Sequence):
  337. pass
  338. LraPopWitness.componentType = namedtype.NamedTypes(
  339. namedtype.NamedType('pkiDataBodyid', BodyPartID()),
  340. namedtype.NamedType('bodyIds', univ.SequenceOf(componentType=BodyPartID()))
  341. )
  342. id_aa = _buildOid(1, 2, 840, 113549, 1, 9, 16, 2)
  343. id_aa_cmc_unsignedData = _buildOid(id_aa, 34)
  344. id_cmc_getCert = _buildOid(id_cmc, 15)
  345. id_cmc_batchRequests = _buildOid(id_cmc, 28)
  346. id_cmc_decryptedPOP = _buildOid(id_cmc, 10)
  347. id_cmc_responseInfo = _buildOid(id_cmc, 19)
  348. id_cmc_changeSubjectName = _buildOid(id_cmc, 36)
  349. class GetCert(univ.Sequence):
  350. pass
  351. GetCert.componentType = namedtype.NamedTypes(
  352. namedtype.NamedType('issuerName', rfc5280.GeneralName()),
  353. namedtype.NamedType('serialNumber', univ.Integer())
  354. )
  355. id_cmc_identification = _buildOid(id_cmc, 2)
  356. id_cmc_queryPending = _buildOid(id_cmc, 21)
  357. class AddExtensions(univ.Sequence):
  358. pass
  359. AddExtensions.componentType = namedtype.NamedTypes(
  360. namedtype.NamedType('pkiDataReference', BodyPartID()),
  361. namedtype.NamedType('certReferences', univ.SequenceOf(componentType=BodyPartID())),
  362. namedtype.NamedType('extensions', univ.SequenceOf(componentType=rfc5280.Extension()))
  363. )
  364. class EncryptedPOP(univ.Sequence):
  365. pass
  366. EncryptedPOP.componentType = namedtype.NamedTypes(
  367. namedtype.NamedType('request', TaggedRequest()),
  368. namedtype.NamedType('cms', rfc5652.ContentInfo()),
  369. namedtype.NamedType('thePOPAlgID', rfc5280.AlgorithmIdentifier()),
  370. namedtype.NamedType('witnessAlgID', rfc5280.AlgorithmIdentifier()),
  371. namedtype.NamedType('witness', univ.OctetString())
  372. )
  373. id_cmc_getCRL = _buildOid(id_cmc, 16)
  374. id_cct_PKIResponse = _buildOid(id_cct, 3)
  375. id_cmc_controlProcessed = _buildOid(id_cmc, 32)
  376. class NoSignatureValue(univ.OctetString):
  377. pass
  378. id_ad_cmc = _buildOid(rfc5280.id_ad, 12)
  379. id_alg_noSignature = _buildOid(id_pkix, 6, 2)
  380. # Map of CMC Control OIDs to CMC Control Attributes
  381. _cmcControlAttributesMapUpdate = {
  382. id_cmc_statusInfo: CMCStatusInfo(),
  383. id_cmc_statusInfoV2: CMCStatusInfoV2(),
  384. id_cmc_identification: char.UTF8String(),
  385. id_cmc_identityProof: univ.OctetString(),
  386. id_cmc_identityProofV2: IdentifyProofV2(),
  387. id_cmc_dataReturn: univ.OctetString(),
  388. id_cmc_transactionId: univ.Integer(),
  389. id_cmc_senderNonce: univ.OctetString(),
  390. id_cmc_recipientNonce: univ.OctetString(),
  391. id_cmc_addExtensions: AddExtensions(),
  392. id_cmc_encryptedPOP: EncryptedPOP(),
  393. id_cmc_decryptedPOP: DecryptedPOP(),
  394. id_cmc_lraPOPWitness: LraPopWitness(),
  395. id_cmc_getCert: GetCert(),
  396. id_cmc_getCRL: GetCRL(),
  397. id_cmc_revokeRequest: RevokeRequest(),
  398. id_cmc_regInfo: univ.OctetString(),
  399. id_cmc_responseInfo: univ.OctetString(),
  400. id_cmc_queryPending: univ.OctetString(),
  401. id_cmc_popLinkRandom: univ.OctetString(),
  402. id_cmc_popLinkWitness: univ.OctetString(),
  403. id_cmc_popLinkWitnessV2: PopLinkWitnessV2(),
  404. id_cmc_confirmCertAcceptance: CMCCertId(),
  405. id_cmc_trustedAnchors: PublishTrustAnchors(),
  406. id_cmc_authData: AuthPublish(),
  407. id_cmc_batchRequests: BodyPartList(),
  408. id_cmc_batchResponses: BodyPartList(),
  409. id_cmc_publishCert: CMCPublicationInfo(),
  410. id_cmc_modCertTemplate: ModCertTemplate(),
  411. id_cmc_controlProcessed: ControlsProcessed(),
  412. id_ExtensionReq: ExtensionReq(),
  413. }
  414. cmcControlAttributesMap.update(_cmcControlAttributesMapUpdate)
  415. # Map of CMC Content Type OIDs to CMC Content Types are added to
  416. # the ones that are in rfc5652.py
  417. _cmsContentTypesMapUpdate = {
  418. id_cct_PKIData: PKIData(),
  419. id_cct_PKIResponse: PKIResponse(),
  420. }
  421. rfc5652.cmsContentTypesMap.update(_cmsContentTypesMapUpdate)