我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用pyasn1.type.univ.Sequence()。
def setComponentByPosition(self, idx, value=None, verifyConstraints=True, exactTypes=False, matchTags=True, matchConstraints=True): if idx == 0: # controlType try: cls = KNOWN_CONTROLS[value] if self.__class__ is not cls: self.__class__ = cls except KeyError: pass return univ.Sequence.setComponentByPosition(self, idx, value=value, verifyConstraints=verifyConstraints, exactTypes=exactTypes, matchTags=matchTags, matchConstraints=matchConstraints)
def _save_pkcs1_der(self): '''Saves the private key in PKCS#1 DER format. @returns: the DER-encoded private key. ''' from pyasn1.type import univ, namedtype from pyasn1.codec.der import encoder class AsnPrivKey(univ.Sequence): componentType = namedtype.NamedTypes( namedtype.NamedType('version', univ.Integer()), namedtype.NamedType('modulus', univ.Integer()), namedtype.NamedType('publicExponent', univ.Integer()), namedtype.NamedType('privateExponent', univ.Integer()), namedtype.NamedType('prime1', univ.Integer()), namedtype.NamedType('prime2', univ.Integer()), namedtype.NamedType('exponent1', univ.Integer()), namedtype.NamedType('exponent2', univ.Integer()), namedtype.NamedType('coefficient', univ.Integer()), ) # Create the ASN object asn_key = AsnPrivKey() asn_key.setComponentByName('version', 0) asn_key.setComponentByName('modulus', self.n) asn_key.setComponentByName('publicExponent', self.e) asn_key.setComponentByName('privateExponent', self.d) asn_key.setComponentByName('prime1', self.p) asn_key.setComponentByName('prime2', self.q) asn_key.setComponentByName('exponent1', self.exp1) asn_key.setComponentByName('exponent2', self.exp2) asn_key.setComponentByName('coefficient', self.coef) return encoder.encode(asn_key)
def _save_pkcs1_der(self): """Saves the private key in PKCS#1 DER format. @returns: the DER-encoded private key. """ from pyasn1.type import univ, namedtype from pyasn1.codec.der import encoder class AsnPrivKey(univ.Sequence): componentType = namedtype.NamedTypes( namedtype.NamedType('version', univ.Integer()), namedtype.NamedType('modulus', univ.Integer()), namedtype.NamedType('publicExponent', univ.Integer()), namedtype.NamedType('privateExponent', univ.Integer()), namedtype.NamedType('prime1', univ.Integer()), namedtype.NamedType('prime2', univ.Integer()), namedtype.NamedType('exponent1', univ.Integer()), namedtype.NamedType('exponent2', univ.Integer()), namedtype.NamedType('coefficient', univ.Integer()), ) # Create the ASN object asn_key = AsnPrivKey() asn_key.setComponentByName('version', 0) asn_key.setComponentByName('modulus', self.n) asn_key.setComponentByName('publicExponent', self.e) asn_key.setComponentByName('privateExponent', self.d) asn_key.setComponentByName('prime1', self.p) asn_key.setComponentByName('prime2', self.q) asn_key.setComponentByName('exponent1', self.exp1) asn_key.setComponentByName('exponent2', self.exp2) asn_key.setComponentByName('coefficient', self.coef) return encoder.encode(asn_key)
def _application_tag(tag_value): return univ.Sequence.tagSet.tagExplicitly( tag.Tag(tag.tagClassApplication, tag.tagFormatConstructed, int(tag_value)))
def prettyPrint(self, scope=0): r = univ.Sequence.prettyPrint(self, scope) decodedControlValue = self.decodeControlValue() if decodedControlValue is not None: r = r[:r.rindex('=') + 1] + '%s\n' % decodedControlValue.prettyPrint(scope + 1) return r
def decodeMessageVersion(wholeMsg): try: seq, wholeMsg = decoder.decode( wholeMsg, asn1Spec=univ.Sequence(), recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c]) ) ver, wholeMsg = decoder.decode( wholeMsg, asn1Spec=univ.Integer(), recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c]) ) if eoo.endOfOctets.isSameTypeWith(ver): raise ProtocolError('EOO at SNMP version component') return ver except PyAsn1Error: raise ProtocolError('Invalid BER at SNMP version component')
def setComponentByPosition(self, idx, value=None, verifyConstraints=True, matchTags=True, matchConstraints=True): if idx == 0: # controlType try: cls = KNOWN_CONTROLS[value] if self.__class__ is not cls: self.__class__ = cls except KeyError: pass return univ.Sequence.setComponentByPosition(self, idx, value=value, verifyConstraints=verifyConstraints, matchTags=matchTags, matchConstraints=matchConstraints)
def decodeMessageVersion(wholeMsg): try: seq, wholeMsg = decoder.decode( wholeMsg, asn1Spec=univ.Sequence(), recursiveFlag=0 ) ver, wholeMsg = decoder.decode( wholeMsg, asn1Spec=univ.Integer(), recursiveFlag=0 ) if eoo.endOfOctets.isSameTypeWith(ver): raise ProtocolError('EOO at SNMP version component') return ver except PyAsn1Error: raise ProtocolError('Invalid BER at SNMP version component')
def is_ev_cert(ee_cert): '''Return True if ee_cert is an extended validation certificate, else False. Args: ee_cert (EndEntityCert) ''' oids = [] oid_certificate_policies = ObjectIdentifier('2.5.29.32') all_extensions = ee_cert.tbscert.pyasn1['extensions'] if all_extensions is not None: policy_extensions = [ext for ext in all_extensions if ext['extnID'] == oid_certificate_policies] if len(policy_extensions) > 0: policy_extension = policy_extensions[0] sequence_der = policy_extension['extnValue'] # type: Sequence() try: sequence, _ = der_decoder(sequence_der, Sequence()) except pyasn1.error.PyAsn1Error: sequence = [] # invalid encoded certificate policy extension for idx in range(len(sequence)): inner_sequence = sequence.getComponentByPosition(idx) oid = inner_sequence.getComponentByPosition(0) oids.append(str(oid)) intersection = list(set(oids) & set(EV_OIDs)) return intersection != []
def scts_from_ocsp_resp(ocsp_resp_der): '''Return list of SCTs of the OCSP status response. Args: ocsp_resp_der(bytes): DER encoded OCSP status response Return: [<ctutlz.rfc6962.SignedCertificateTimestamp>, ...] ''' if ocsp_resp_der: ocsp_resp, _ = der_decoder( ocsp_resp_der, asn1Spec=pyasn1_modules.rfc2560.OCSPResponse()) response_bytes = ocsp_resp.getComponentByName('responseBytes') if response_bytes is not None: # os: octet string response_os = response_bytes.getComponentByName('response') der_decoder.defaultErrorState = ber.decoder.stDumpRawValue response, _ = der_decoder(response_os, Sequence()) sctlist_os_hex = sctlist_hex_from_ocsp_pretty_print( response.prettyPrint()) if sctlist_os_hex: sctlist_os_der = binascii.unhexlify(sctlist_os_hex) sctlist_os, _ = der_decoder(sctlist_os_der, OctetString()) sctlist_hex = sctlist_os.prettyPrint().split('0x')[-1] sctlist_der = binascii.unhexlify(sctlist_hex) sctlist = SignedCertificateTimestampList(sctlist_der) return [SignedCertificateTimestamp(entry.sct_der) for entry in sctlist.sct_list] return []
def _save_pkcs1_der(self): """Saves the private key in PKCS#1 DER format. :returns: the DER-encoded private key. :rtype: bytes """ from pyasn1.type import univ, namedtype from pyasn1.codec.der import encoder class AsnPrivKey(univ.Sequence): componentType = namedtype.NamedTypes( namedtype.NamedType('version', univ.Integer()), namedtype.NamedType('modulus', univ.Integer()), namedtype.NamedType('publicExponent', univ.Integer()), namedtype.NamedType('privateExponent', univ.Integer()), namedtype.NamedType('prime1', univ.Integer()), namedtype.NamedType('prime2', univ.Integer()), namedtype.NamedType('exponent1', univ.Integer()), namedtype.NamedType('exponent2', univ.Integer()), namedtype.NamedType('coefficient', univ.Integer()), ) # Create the ASN object asn_key = AsnPrivKey() asn_key.setComponentByName('version', 0) asn_key.setComponentByName('modulus', self.n) asn_key.setComponentByName('publicExponent', self.e) asn_key.setComponentByName('privateExponent', self.d) asn_key.setComponentByName('prime1', self.p) asn_key.setComponentByName('prime2', self.q) asn_key.setComponentByName('exponent1', self.exp1) asn_key.setComponentByName('exponent2', self.exp2) asn_key.setComponentByName('coefficient', self.coef) return encoder.encode(asn_key)
def main(argv): try: infile = argv[0] except: print "usage: ./krb5-downgrade-preauth.py <infile>" sys.exit(0) fin = open(infile, 'r') data = fin.read() data_len = len(data) fin.close() krb_preauth_req, temp = decoder.decode(data[4:]) padata_seq, temp = decoder.decode(krb_preauth_req[7]) new_enctype_info = univ.Sequence() for padata in padata_seq: if padata[0] == 19: enctype_info, temp = decoder.decode(padata[1]) for enctype in enctype_info: if enctype[0] == 1: new_enctype_info.setComponentByPosition(0, enctype) padata[1] = univ.OctetString(encoder.encode(new_enctype_info)) krb_preauth_req[7] = univ.OctetString(encoder.encode(padata_seq)) payload_out = data[:4] payload_out += encoder.encode(krb_preauth_req) #payload_out = str(payload_out).ljust(data_len, '\0') # log what we're doing fout = open(infile +".in", "w") fout.write(data) fout.close() fout = open(infile +".out", "w") fout.write(payload_out) fout.close() sys.stdout.write(payload_out) os.remove(infile)