|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 | # |
| 16 | +import base64 |
16 | 17 | import logging as log |
17 | 18 | import re |
18 | 19 | import time |
| 20 | +from pprint import pprint |
| 21 | + |
19 | 22 | from cryptography import x509 |
20 | 23 | from cryptography.hazmat.backends import default_backend |
21 | 24 | from cryptography.x509 import SignatureAlgorithmOID as AlgOID |
22 | | -from pprint import pprint |
23 | 25 |
|
| 26 | +from vcert.pem import parse_pem |
24 | 27 | from vcert.common import CertField, CommonConnection, CertificateRequest, CSR_ORIGIN_LOCAL, CSR_ORIGIN_PROVIDED, \ |
25 | | - CSR_ORIGIN_SERVICE, KeyType |
| 28 | + CSR_ORIGIN_SERVICE, KeyType, CHAIN_OPTION_LAST, CHAIN_OPTION_FIRST, CHAIN_OPTION_IGNORE |
26 | 29 | from vcert.errors import VenafiError, ServerUnexptedBehavior, ClientBadData, RetrieveCertificateTimeoutError, \ |
27 | 30 | CertificateRequestError, CertificateRenewError |
28 | 31 | from vcert.http import HTTPStatus |
@@ -126,6 +129,56 @@ def request_cert(self, request, zone): |
126 | 129 | log.error("Request status is not %s. %s." % HTTPStatus.OK, status) |
127 | 130 | raise CertificateRequestError |
128 | 131 |
|
| 132 | + def retrieve_cert(self, cert_request): |
| 133 | + log.debug("Getting certificate status for id %s" % cert_request.id) |
| 134 | + |
| 135 | + retrieve_request = dict(CertificateDN=cert_request.id, |
| 136 | + Format="base64", |
| 137 | + IncludeChain=True) |
| 138 | + |
| 139 | + if cert_request.csr_origin == CSR_ORIGIN_SERVICE: |
| 140 | + retrieve_request['IncludePrivateKey'] = cert_request.include_private_key |
| 141 | + if cert_request.key_password: |
| 142 | + # The password is encoded when assigned (for local use, I suppose). |
| 143 | + # decode is needed to send a raw string |
| 144 | + retrieve_request['Password'] = cert_request.key_password.decode() |
| 145 | + |
| 146 | + if cert_request.chain_option == CHAIN_OPTION_LAST: |
| 147 | + retrieve_request['RootFirstOrder'] = 'false' |
| 148 | + retrieve_request['IncludeChain'] = 'true' |
| 149 | + elif cert_request.chain_option == CHAIN_OPTION_FIRST: |
| 150 | + retrieve_request['RootFirstOrder'] = 'true' |
| 151 | + retrieve_request['IncludeChain'] = 'true' |
| 152 | + elif cert_request.chain_option == CHAIN_OPTION_IGNORE: |
| 153 | + retrieve_request['IncludeChain'] = 'false' |
| 154 | + else: |
| 155 | + log.error("chain option %s is not valid" % cert_request.chain_option) |
| 156 | + raise ClientBadData |
| 157 | + |
| 158 | + time_start = time.time() |
| 159 | + while True: |
| 160 | + try: |
| 161 | + status, data = self._post(URLS.CERTIFICATE_RETRIEVE, data=retrieve_request) |
| 162 | + except VenafiError: |
| 163 | + log.debug("Certificate with id %s not found." % cert_request.id) |
| 164 | + status = 0 |
| 165 | + |
| 166 | + if status == HTTPStatus.OK: |
| 167 | + pem64 = data['CertificateData'] |
| 168 | + pem = base64.b64decode(pem64) |
| 169 | + cert_response = parse_pem(pem.decode(), cert_request.chain_option) |
| 170 | + if cert_response.key is None and cert_request.private_key is not None: |
| 171 | + log.debug("Adding private key to response...") |
| 172 | + cert_response.key = cert_request.private_key_pem |
| 173 | + return cert_response |
| 174 | + elif (time.time() - time_start) < cert_request.timeout: |
| 175 | + log.debug("Waiting for certificate...") |
| 176 | + time.sleep(2) |
| 177 | + else: |
| 178 | + raise RetrieveCertificateTimeoutError( |
| 179 | + 'Operation timed out at %d seconds while retrieving certificate with id %s' |
| 180 | + % (cert_request.timeout, cert_request.id)) |
| 181 | + |
129 | 182 | def renew_cert(self, request, reuse_key=False): |
130 | 183 | if not request.id and not request.thumbprint: |
131 | 184 | log.debug("Request id or thumbprint must be specified for TPP") |
|
0 commit comments