Advertisement
Guest User

rfc 4210

a guest
May 2nd, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1.  
  2. #PKIMessage data structure used in CMP requests, defined in RFC4210
  3. #http://www.ietf.org/rfc/rfc4210.txt
  4.  
  5. from pyasn1.type import tag,namedtype,namedval,univ,constraint,char,useful
  6. from pyasn1.codec.der import decoder, encoder
  7. from pyasn1 import error
  8. from rfc2986 import GeneralName, AlgorithmIdentifier
  9.  
  10.  
  11. MAX = 64
  12.  
  13. ##################
  14. ###small things used in the more complex structures
  15. class pvno(univ.Integer):
  16.     """Defined in PKIHeader as:
  17.         pvno INTEGER     { cmp1999(1), cmp2000(2) }
  18.     """
  19.     namedValues = namedval.NamedValues(
  20.         ('cmp1999', 1),
  21.         ('cmp2000', 2) 
  22.         )
  23.  
  24. class KeyIdentifier(univ.OctetString): pass
  25.  
  26.  
  27. class InfoTypeAndValue(univ.Sequence):
  28.     """
  29.     InfoTypeAndValue ::= SEQUENCE {
  30.      infoType               OBJECT IDENTIFIER,
  31.      infoValue              ANY DEFINED BY infoType  OPTIONAL
  32.     }"""
  33.     componentType = namedtype.NamedTypes(
  34.         namedtype.NamedType( 'infoType', univ.ObjectIdentifier() ),
  35.         namedtype.OptionalNamedType( 'infoValue', univ.Any() )
  36.         )
  37.  
  38. class PKIProtection(univ.BitString): pass
  39.  
  40.  
  41. class CMPCertificate(univ.Choice):
  42.     """CMPCertificate ::= CHOICE {
  43.         x509v3PKCert        Certificate
  44.      }
  45.     """
  46.     componentType = namedtype.NamedTypes(
  47.         'x509v3PKCert', univ.Null() ) #TODO NULL?
  48.  
  49.  
  50. class PKIFreeText(univ.SequenceOf):
  51.     """PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String"""
  52.     componentType = char.UTF8String()
  53.  
  54. class PKIHeader(univ.Sequence):
  55.     """
  56.     PKIHeader ::= SEQUENCE {
  57.     pvno                INTEGER     { cmp1999(1), cmp2000(2) },
  58.     sender              GeneralName,
  59.     recipient           GeneralName,
  60.     messageTime     [0] GeneralizedTime         OPTIONAL,
  61.     protectionAlg   [1] AlgorithmIdentifier     OPTIONAL,
  62.     senderKID       [2] KeyIdentifier           OPTIONAL,
  63.     recipKID        [3] KeyIdentifier           OPTIONAL,
  64.     transactionID   [4] OCTET STRING            OPTIONAL,
  65.     senderNonce     [5] OCTET STRING            OPTIONAL,
  66.     recipNonce      [6] OCTET STRING            OPTIONAL,
  67.     freeText        [7] PKIFreeText             OPTIONAL,
  68.     generalInfo     [8] SEQUENCE SIZE (1..MAX) OF
  69.                      InfoTypeAndValue     OPTIONAL
  70.     }
  71.  
  72.     """
  73.     componentType = namedtype.NamedTypes(
  74.         namedtype.NamedType( 'pvno', pvno() ),
  75.         namedtype.NamedType( 'sender', GeneralName() ),
  76.         namedtype.NamedType( 'recipient', GeneralName() ),
  77.         namedtype.OptionalNamedType( 'messageTime', useful.GeneralizedTime().subtype(
  78.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  79.         namedtype.OptionalNamedType( 'protectionAlg', AlgorithmIdentifier().subtype(
  80.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  81.         namedtype.OptionalNamedType( 'senderKID', KeyIdentifier().subtype(
  82.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))),
  83.         namedtype.OptionalNamedType( 'recipKID', KeyIdentifier().subtype(
  84.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),
  85.         namedtype.OptionalNamedType( 'transactionID', univ.OctetString().subtype(
  86.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))),
  87.         namedtype.OptionalNamedType( 'senderNonce', univ.OctetString().subtype(
  88.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 5))),
  89.         namedtype.OptionalNamedType( 'recipNonce', univ.OctetString().subtype(
  90.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 6))),
  91.         namedtype.OptionalNamedType( 'freeText', PKIFreeText().subtype(
  92.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 7))),
  93.         namedtype.OptionalNamedType('generalInfo',
  94.             univ.SequenceOf(
  95.                 InfoTypeAndValue().subtype(
  96.                     subtypeSpec=constraint.ValueSizeConstraint(1, MAX),
  97.                     implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 8)
  98.                     )
  99.                 )
  100.             )
  101.         )
  102.  
  103.  
  104.  
  105.  
  106.  
  107. #class PKIBody(univ.Any): pass
  108. class PKIBody(univ.Null): pass  #TODO
  109.  
  110.  
  111.  
  112. class PKIMessage(univ.Sequence):
  113.     """
  114.     PKIMessage ::= SEQUENCE {
  115.     header           PKIHeader,
  116.     body             PKIBody,
  117.     protection   [0] PKIProtection OPTIONAL,
  118.     extraCerts   [1] SEQUENCE SIZE (1..MAX) OF CMPCertificate
  119.                   OPTIONAL
  120.     }"""
  121.     componentType = namedtype.NamedTypes(
  122.         namedtype.NamedType( 'header', PKIHeader()),
  123.         """
  124.         namedtype.NamedType( 'protection', univ.Null() ),
  125.         namedtype.NamedType( 'extracerts', univ.Null() )
  126.         """
  127.         namedtype.NamedType( 'body', PKIBody()),
  128.         namedtype.OptionalNamedType( 'protection', PKIProtection().subtype(
  129.             implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  130.         namedtype.OptionalNamedType( 'extraCerts',
  131.             univ.SequenceOf(
  132.                 CMPCertificate().subtype( subtypeSpec=constraint.ValueSizeConstraint(1, MAX))
  133.                 )
  134.             )
  135.         )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement