1 #!/usr/bin/env python 2 # 3 # Read CMC certificate request with wrappers on stdin, parse each into 4 # plain text, then build substrate from it 5 # 6 import sys 7 8 from pyasn1.codec.der import decoder 9 from pyasn1.codec.der import encoder 10 11 from pyasn1_modules import pem 12 from pyasn1_modules import rfc5652 13 from pyasn1_modules import rfc6402 14 15 if len(sys.argv) != 1: 16 print("""Usage: 17 $ cat cmc_request.pem | %s""" % (sys.argv[0],)) 18 sys.exit(-1) 19 20 reqCnt = 0 21 22 substrate = pem.readBase64FromFile(sys.stdin) 23 24 _, rest = decoder.decode(substrate, asn1Spec=rfc5652.ContentInfo()) 25 assert not rest 26 27 next_layer = rfc5652.id_ct_contentInfo 28 data = substrate 29 while next_layer: 30 if next_layer == rfc5652.id_ct_contentInfo: 31 layer, rest = decoder.decode(data, asn1Spec=rfc5652.ContentInfo()) 32 assert encoder.encode(layer) == data, 'wrapper recode fails' 33 assert not rest 34 35 print(" * New layer (wrapper):") 36 print(layer.prettyPrint()) 37 38 next_layer = layer['contentType'] 39 data = layer['content'] 40 41 elif next_layer == rfc5652.id_signedData: 42 layer, rest = decoder.decode(data, asn1Spec=rfc5652.SignedData()) 43 assert encoder.encode(layer) == data, 'wrapper recode fails' 44 assert not rest 45 46 print(" * New layer (wrapper):") 47 print(layer.prettyPrint()) 48 49 next_layer = layer['encapContentInfo']['eContentType'] 50 data = layer['encapContentInfo']['eContent'] 51 52 elif next_layer == rfc6402.id_cct_PKIData: 53 layer, rest = decoder.decode(data, asn1Spec=rfc6402.PKIData()) 54 assert encoder.encode(layer) == data, 'pkidata recode fails' 55 assert not rest 56 57 print(" * New layer (pkidata):") 58 print(layer.prettyPrint()) 59 60 next_layer = None 61 data = None 62