Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_rsa_keys(key_file):
assert(key_file)
# Create and initialize keys manager, we use a simple list based
# keys manager, implement your own KeysStore klass if you need
# something more sophisticated
mngr = xmlsec.KeysMngr()
if mngr is None:
print "Error: failed to create keys manager."
return None
if xmlsec.cryptoAppDefaultKeysMngrInit(mngr) < 0:
print "Error: failed to initialize keys manager."
mngr.destroy()
return None
# Load private RSA key
if not check_filename(key_file):
mngr.destroy()
return None
key = xmlsec.cryptoAppKeyLoad(key_file, xmlsec.KeyDataFormatPem,
None, None, None);
if key is None:
print "Error: failed to load rsa key from file \"%s\"" % key_file
mngr.destroy()
node = xmlsec.findNode(doc.getRootElement(), xmlsec.NodeSignature,
xmlsec.DSigNs)
if not node:
raise SignatureError("couldn't find root node")
# load the private key
key = xmlsec.cryptoAppKeyLoad(self.key_file, xmlsec.KeyDataFormatPem,
self.key_pwd, None, None)
if not key:
raise SignatureError('failed to load the private key %s' % self.key_file)
if xmlsec.cryptoAppKeyCertLoad(key, self.cert_file, xmlsec.KeyDataFormatPem) < 0:
print "Error: failed to load pem certificate \"%s\"" % self.cert_file
return self.cleanup(doc, dsigctx)
keymngr = xmlsec.KeysMngr()
xmlsec.cryptoAppDefaultKeysMngrInit(keymngr)
xmlsec.cryptoAppDefaultKeysMngrAdoptKey(keymngr, key)
dsigctx = xmlsec.DSigCtx(keymngr)
if key.setName(self.key_file) < 0:
raise SignatureError('failed to set key name')
# sign
if dsigctx.sign(node) < 0:
raise SignatureError('signing failed')
signed_xml = doc.serialize()
finally:
if dsigctx:
dsigctx.destroy()
if doc:
def load_des_keys(files, files_size):
assert(files)
assert(files_size > 0)
# Create and initialize keys manager, we use a simple list based
# keys manager, implement your own KeysStore klass if you need
# something more sophisticated
mngr = xmlsec.KeysMngr()
if mngr is None:
print "Error: failed to create keys manager."
return None
if xmlsec.cryptoAppDefaultKeysMngrInit(mngr) < 0:
print "Error: failed to initialize keys manager."
mngr.destroy()
return None
for file in files:
if not check_filename(file):
mngr.destroy()
return None
# Load DES key
key = xmlsec.keyReadBinaryFile(xmlsec.keyDataDesId(), file)
if key is None:
print "Error: failed to load des key from binary file \"%s\"" % file
mngr.destroy()
def create_files_keys_mngr():
# Create files based keys store
storeId = xmlsec.KeyStoreId(0, 0, "files-based-keys-store",
None, None, files_keys_store_find_key)
keysStore = xmlsec.KeyStore(storeId)
if keysStore is None:
print "Error: failed to create keys store."
return None
# Create keys manager
mngr = xmlsec.KeysMngr()
if mngr is None:
print "Error: failed to create keys manager."
keysStore.destroy()
return None
# Add store to keys manager, from now on keys manager destroys the store
# if needed
if mngr.adoptKeysStore(keysStore) < 0:
print "Error: failed to add keys store to keys manager."
keysStore.destroy()
mngr.destroy()
return None
# Initialize crypto library specific data in keys manager
if xmlsec.cryptoKeysMngrInit(mngr) < 0:
print "Error: failed to initialize crypto data in keys manager."
def load_trusted_certs(files, files_size):
assert(files)
assert(files_size > 0)
# Create and initialize keys manager, we use a simple list based
# keys manager, implement your own KeysStore klass if you need
# something more sophisticated
mngr = xmlsec.KeysMngr()
if mngr is None:
print "Error: failed to create keys manager."
return None
if xmlsec.cryptoAppDefaultKeysMngrInit(mngr) < 0:
print "Error: failed to initialize keys manager."
mngr.destroy()
return None
for file in files:
if not check_filename(file):
mngr.destroy()
return None
# Load trusted cert
if mngr.certLoad(file, xmlsec.KeyDataFormatPem,
xmlsec.KeyDataTypeTrusted) < 0:
print "Error: failed to load pem certificate from \"%s\"", file
mngr.destroy()
def __getattr__(self, name):
if self.__isprivate(name):
return self.__dict__[name]
if name[:2] == "__" and name[-2:] == "__" and name != "__members__":
raise AttributeError, name
ret = xmlsecmod.keyInfoCtxGetAttr(self, name)
if ret is None:
raise AttributeError, name
if name == "keysMngr":
return KeysMngr(_obj=ret)
elif name == "enabledKeyData":
return PtrList(_obj=ret)
elif name == "retrievalMethodCtx":
return TransformCtx(_obj=ret)
elif name == "encCtx":
return EncCtx(_obj=ret)
elif name == "keyReq":
return KeyReq(_obj=ret)
else:
# flags, flags2, mode, base64LineSize, maxRetrievalMethodLevel
# maxEncryptedKeyLevel, certsVerificationTime, certsVerificationDepth
# curRetrievalMethodLevel, curEncryptedKeyLevel
return ret
def __setattr__(self, name, value):