| OLD | NEW |
| 1 # Copyright 2015 Google Inc. All rights reserved. | 1 # Copyright 2015 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 key_pem: string, public key in PEM format. | 61 key_pem: string, public key in PEM format. |
| 62 is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it | 62 is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it |
| 63 is expected to be an RSA key in PEM format. | 63 is expected to be an RSA key in PEM format. |
| 64 | 64 |
| 65 Returns: | 65 Returns: |
| 66 Verifier instance. | 66 Verifier instance. |
| 67 | 67 |
| 68 Raises: | 68 Raises: |
| 69 OpenSSL.crypto.Error: if the key_pem can't be parsed. | 69 OpenSSL.crypto.Error: if the key_pem can't be parsed. |
| 70 """ | 70 """ |
| 71 key_pem = _to_bytes(key_pem) |
| 71 if is_x509_cert: | 72 if is_x509_cert: |
| 72 pubkey = crypto.load_certificate(crypto.FILETYPE_PEM, key_pem) | 73 pubkey = crypto.load_certificate(crypto.FILETYPE_PEM, key_pem) |
| 73 else: | 74 else: |
| 74 pubkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key_pem) | 75 pubkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key_pem) |
| 75 return OpenSSLVerifier(pubkey) | 76 return OpenSSLVerifier(pubkey) |
| 76 | 77 |
| 77 | 78 |
| 78 class OpenSSLSigner(object): | 79 class OpenSSLSigner(object): |
| 79 """Signs messages with a private key.""" | 80 """Signs messages with a private key.""" |
| 80 | 81 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 105 Args: | 106 Args: |
| 106 key: string, private key in PKCS12 or PEM format. | 107 key: string, private key in PKCS12 or PEM format. |
| 107 password: string, password for the private key file. | 108 password: string, password for the private key file. |
| 108 | 109 |
| 109 Returns: | 110 Returns: |
| 110 Signer instance. | 111 Signer instance. |
| 111 | 112 |
| 112 Raises: | 113 Raises: |
| 113 OpenSSL.crypto.Error if the key can't be parsed. | 114 OpenSSL.crypto.Error if the key can't be parsed. |
| 114 """ | 115 """ |
| 116 key = _to_bytes(key) |
| 115 parsed_pem_key = _parse_pem_key(key) | 117 parsed_pem_key = _parse_pem_key(key) |
| 116 if parsed_pem_key: | 118 if parsed_pem_key: |
| 117 pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, parsed_pem_key) | 119 pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, parsed_pem_key) |
| 118 else: | 120 else: |
| 119 password = _to_bytes(password, encoding='utf-8') | 121 password = _to_bytes(password, encoding='utf-8') |
| 120 pkey = crypto.load_pkcs12(key, password).get_privatekey() | 122 pkey = crypto.load_pkcs12(key, password).get_privatekey() |
| 121 return OpenSSLSigner(pkey) | 123 return OpenSSLSigner(pkey) |
| 122 | 124 |
| 123 | 125 |
| 124 def pkcs12_key_as_pem(private_key_text, private_key_password): | 126 def pkcs12_key_as_pem(private_key_bytes, private_key_password): |
| 125 """Convert the contents of a PKCS12 key to PEM using OpenSSL. | 127 """Convert the contents of a PKCS#12 key to PEM using pyOpenSSL. |
| 126 | 128 |
| 127 Args: | 129 Args: |
| 128 private_key_text: String. Private key. | 130 private_key_bytes: Bytes. PKCS#12 key in DER format. |
| 129 private_key_password: String. Password for PKCS12. | 131 private_key_password: String. Password for PKCS#12 key. |
| 130 | 132 |
| 131 Returns: | 133 Returns: |
| 132 String. PEM contents of ``private_key_text``. | 134 String. PEM contents of ``private_key_bytes``. |
| 133 """ | 135 """ |
| 134 decoded_body = base64.b64decode(private_key_text) | |
| 135 private_key_password = _to_bytes(private_key_password) | 136 private_key_password = _to_bytes(private_key_password) |
| 136 | 137 pkcs12 = crypto.load_pkcs12(private_key_bytes, private_key_password) |
| 137 pkcs12 = crypto.load_pkcs12(decoded_body, private_key_password) | |
| 138 return crypto.dump_privatekey(crypto.FILETYPE_PEM, | 138 return crypto.dump_privatekey(crypto.FILETYPE_PEM, |
| 139 pkcs12.get_privatekey()) | 139 pkcs12.get_privatekey()) |
| OLD | NEW |