summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/crypto/certs.py
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2013-09-18 12:34:31 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2013-09-18 12:34:31 -0300
commit9f0968da51cc9a16577c563887ae336066ddc0eb (patch)
tree7f0e744a12e0b67fab2818216a353b54bc8ba730 /src/leap/bitmask/crypto/certs.py
parent735111cee1c28ccc07c87d806bc5412aaffcedf7 (diff)
parent13f5d8fcee038f441dd91ef16dfdb254e1f0dd3f (diff)
Merge remote-tracking branch 'kali/bug/start_smtp_without_eip' into develop
Diffstat (limited to 'src/leap/bitmask/crypto/certs.py')
-rw-r--r--src/leap/bitmask/crypto/certs.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/leap/bitmask/crypto/certs.py b/src/leap/bitmask/crypto/certs.py
new file mode 100644
index 00000000..244decfd
--- /dev/null
+++ b/src/leap/bitmask/crypto/certs.py
@@ -0,0 +1,80 @@
+# -*- coding: utf-8 -*-
+# certs.py
+# Copyright (C) 2013 LEAP
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+Utilities for dealing with client certs
+"""
+import logging
+import os
+
+from leap.bitmask.crypto.srpauth import SRPAuth
+from leap.bitmask.util.constants import REQUEST_TIMEOUT
+from leap.common.files import check_and_fix_urw_only
+from leap.common.files import mkdir_p
+
+from leap.common import certs as leap_certs
+
+logger = logging.getLogger(__name__)
+
+
+def download_client_cert(provider_config, path, session):
+ """
+ Downloads the client certificate for each service.
+
+ :param provider_config: instance of a ProviderConfig
+ :type provider_config: ProviderConfig
+ :param path: the path to download the cert to.
+ :type path: str
+ :param session: a fetcher.session instance. For the moment we only
+ support requests.sessions
+ :type session: requests.sessions.Session
+ """
+ # TODO we should implement the @with_srp_auth decorator
+ # again.
+ srp_auth = SRPAuth(provider_config)
+ session_id = srp_auth.get_session_id()
+ cookies = None
+ if session_id:
+ cookies = {"_session_id": session_id}
+ cert_uri = "%s/%s/cert" % (
+ provider_config.get_api_uri(),
+ provider_config.get_api_version())
+ logger.debug('getting cert from uri: %s' % cert_uri)
+
+ res = session.get(cert_uri,
+ verify=provider_config
+ .get_ca_cert_path(),
+ cookies=cookies,
+ timeout=REQUEST_TIMEOUT)
+ res.raise_for_status()
+ client_cert = res.content
+
+ if not leap_certs.is_valid_pemfile(client_cert):
+ # XXX raise more specific exception.
+ raise Exception("The downloaded certificate is not a "
+ "valid PEM file")
+
+ mkdir_p(os.path.dirname(path))
+
+ try:
+ with open(path, "w") as f:
+ f.write(client_cert)
+ except IOError as exc:
+ logger.error(
+ "Error saving client cert: %r" % (exc,))
+ raise
+
+ check_and_fix_urw_only(path)