2016-05-26 19 views
1

公開鍵/秘密鍵のペアを使用してxml文書を生成して署名するasp.netアプリケーションのdjangoポートに取り組んでいます。Python signxml - 公開/秘密鍵だけでXML文書に署名する

私は署名の側面を除いてXML生成のあらゆる面を複製することができました。私はこれを行うことができるように思われるsignxmlライブラリを見つけましたが、動作させる方法を理解できません。ここでは(例hereをモデルにした)私が持っているコードです:

# store keys as strings 
cert = open(signprivatepath).read() 
key = open(signpublicpath).read() 

data = ET.fromstring(docstring) 
xmldsig_stuff = xmldsig(data, 'sha1') 
signed_root = xmldsig_stuff.sign(
    key=key, 
    cert=cert, 
    algorithm='rsa-sha1', 
    c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315' 
    ) 
verified_data = xmldsig(signed_root).verify() 
return verified_data 

signprivatepathとsignpublicpathはPEMフォーマットされたキーに両方のパスです。

Environment: 


Request Method: GET 
Request URL: http://127.0.0.1:8000/site/245/download-site-license 

Django Version: 1.9.5 
Python Version: 3.5.1 
Installed Applications: 
['licenses.apps.LicensesConfig', 
'simple_history', 
'django.contrib.admindocs', 
'django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles'] 
Installed Middleware: 
['simple_history.middleware.HistoryRequestMiddleware', 
'django.middleware.security.SecurityMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware'] 



Traceback: 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response 
    149.      response = self.process_exception_by_middleware(e, request) 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response 
    147.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "C:\projects\django\swlicensing\licenses\views\site.py" in downloadSiteLicense 
    206.   signedXMLTree = signXML(treestring) 

File "C:\projects\django\swlicensing\licenses\views\site.py" in signXML 
    144.   c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315' 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\signxml-1.0.0-py3.5.egg\signxml\__init__.py" in sign 
    414.     key = load_pem_private_key(self.key, password=passphrase, backend=default_backend()) 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cryptography\hazmat\primitives\serialization.py" in load_pem_private_key 
    20.  return backend.load_pem_private_key(data, password) 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cryptography\hazmat\backends\multibackend.py" in load_pem_private_key 
    282.    return b.load_pem_private_key(data, password) 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py" in load_pem_private_key 
    1606.    password, 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py" in _load_key 
    1784.   mem_bio = self._bytes_to_bio(data) 

File "C:\Users\servant\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py" in _bytes_to_bio 
    1058.   data_char_p = self._ffi.new("char[]", data) 

Exception Type: TypeError at /site/245/download-site-license 
Exception Value: initializer for ctype 'char[]' must be a bytes or list or tuple, not str 

これを行う方法があります:私は、コードを実行すると

は、それが次のエラーを返しますか?私がコピーしているコードは、秘密鍵そのものを使用するだけで証明書を使用していないようです。または私は何かを逃していますか?

答えて

0

certkey変数は、その後、あなたがすでに

signed_root = xmldsig_stuff.sign(
    key=key, 
    cert=cert, 
    algorithm='rsa-sha1', 
    c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315' 
    ) 
を行ったよう sign関数に渡すバイト配列はそう下記の

cert = open(signprivatepath, "rb").read() 
key = open(signpublicpath, "rb").read() 

によってバイト配列としてそれを読むことが必要です

関連する問題