예제 #1
0
 def asn1_to_ssh(self, pubkey):
     lines = pubkey.split("\n")
     lines = [x for x in lines if not x.startswith("----")]
     base64_encoded = "".join(lines)
     try:
         #TODO remove pyasn1 dependency
         from pyasn1.codec.der import decoder as der_decoder
         der_encoded = base64.b64decode(base64_encoded)
         der_encoded = der_decoder.decode(der_encoded)[0][1]
         key = der_decoder.decode(self.bits_to_bytes(der_encoded))[0]
         n=key[0]
         e=key[1]
         keydata = bytearray()
         keydata.extend(struct.pack('>I', len("ssh-rsa")))
         keydata.extend(b"ssh-rsa")
         keydata.extend(struct.pack('>I', len(self.num_to_bytes(e))))
         keydata.extend(self.num_to_bytes(e))
         keydata.extend(struct.pack('>I', len(self.num_to_bytes(n)) + 1))
         keydata.extend(b"\0")
         keydata.extend(self.num_to_bytes(n))
         keydata_base64 = base64.b64encode(bytebuffer(keydata))
         return ustr(b"ssh-rsa " +  keydata_base64 + b"\n", 
                     encoding='utf-8')
     except ImportError as e:
         raise CryptError("Failed to load pyasn1.codec.der")
예제 #2
0
    def asn1_to_ssh(self, pubkey):
        lines = pubkey.split("\n")
        lines = [x for x in lines if not x.startswith("----")]
        base64_encoded = "".join(lines)
        try:
            # TODO remove pyasn1 dependency
            from pyasn1.codec.der import decoder as der_decoder

            der_encoded = base64.b64decode(base64_encoded)
            der_encoded = der_decoder.decode(der_encoded)[0][1]
            key = der_decoder.decode(self.bits_to_bytes(der_encoded))[0]
            n = key[0]
            e = key[1]
            keydata = bytearray()
            keydata.extend(struct.pack(">I", len("ssh-rsa")))
            keydata.extend(b"ssh-rsa")
            keydata.extend(struct.pack(">I", len(self.num_to_bytes(e))))
            keydata.extend(self.num_to_bytes(e))
            keydata.extend(struct.pack(">I", len(self.num_to_bytes(n)) + 1))
            keydata.extend(b"\0")
            keydata.extend(self.num_to_bytes(n))
            keydata_base64 = base64.b64encode(bytebuffer(keydata))
            return ustr(b"ssh-rsa " + keydata_base64 + b"\n", encoding="utf-8")
        except ImportError as e:
            raise CryptError("Failed to load pyasn1.codec.der")
예제 #3
0
    def put_page_blob(self, url, data):
        logger.verbose("Replace old page blob")

        # Convert string into bytes
        data = bytearray(data, encoding='utf-8')
        timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())

        # Align to 512 bytes
        page_blob_size = int((len(data) + 511) / 512) * 512
        resp = self.client.call_storage_service(
            restutil.http_put,
            url,
            "",
            {
                "x-ms-date": timestamp,
                "x-ms-blob-type": "PageBlob",
                "Content-Length": "0",
                "x-ms-blob-content-length": ustr(page_blob_size),
                "x-ms-version": self.__class__.__storage_version__
            })
        if resp.status != httpclient.CREATED:
            raise UploadError(
                "Failed to clean up page blob: {0}".format(resp.status))

        if url.count("?") < 0:
            url = "{0}?comp=page".format(url)
        else:
            url = "{0}&comp=page".format(url)

        logger.verbose("Upload page blob")
        page_max = 4 * 1024 * 1024  # Max page size: 4MB
        start = 0
        end = 0
        while end < len(data):
            end = min(len(data), start + page_max)
            content_size = end - start
            # Align to 512 bytes
            page_end = int((end + 511) / 512) * 512
            buf_size = page_end - start
            buf = bytearray(buf_size)
            buf[0: content_size] = data[start: end]
            resp = self.client.call_storage_service(
                restutil.http_put,
                url,
                bytebuffer(buf),
                {
                    "x-ms-date": timestamp,
                    "x-ms-range": "bytes={0}-{1}".format(start, page_end - 1),
                    "x-ms-page-write": "update",
                    "x-ms-version": self.__class__.__storage_version__,
                    "Content-Length": ustr(page_end - start)
                })
            if resp is None or resp.status != httpclient.CREATED:
                raise UploadError(
                    "Failed to upload page blob: {0}".format(resp.status))
            start = end
예제 #4
0
    def put_page_blob(self, url, data):
        logger.verbose("Replace old page blob")

        # Convert string into bytes
        data = bytearray(data, encoding='utf-8')
        timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())

        # Align to 512 bytes
        page_blob_size = int((len(data) + 511) / 512) * 512
        resp = self.client.call_storage_service(
            restutil.http_put,
            url,
            "",
            {
                "x-ms-date": timestamp,
                "x-ms-blob-type": "PageBlob",
                "Content-Length": "0",
                "x-ms-blob-content-length": ustr(page_blob_size),
                "x-ms-version": self.__class__.__storage_version__
            })
        if resp.status != httpclient.CREATED:
            raise UploadError(
                "Failed to clean up page blob: {0}".format(resp.status))

        if url.count("?") < 0:
            url = "{0}?comp=page".format(url)
        else:
            url = "{0}&comp=page".format(url)

        logger.verbose("Upload page blob")
        page_max = 4 * 1024 * 1024  # Max page size: 4MB
        start = 0
        end = 0
        while end < len(data):
            end = min(len(data), start + page_max)
            content_size = end - start
            # Align to 512 bytes
            page_end = int((end + 511) / 512) * 512
            buf_size = page_end - start
            buf = bytearray(buf_size)
            buf[0: content_size] = data[start: end]
            resp = self.client.call_storage_service(
                restutil.http_put,
                url,
                bytebuffer(buf),
                {
                    "x-ms-date": timestamp,
                    "x-ms-range": "bytes={0}-{1}".format(start, page_end - 1),
                    "x-ms-page-write": "update",
                    "x-ms-version": self.__class__.__storage_version__,
                    "Content-Length": ustr(page_end - start)
                })
            if resp is None or resp.status != httpclient.CREATED:
                raise UploadError(
                    "Failed to upload page blob: {0}".format(resp.status))
            start = end
예제 #5
0
파일: gaia.py 프로젝트: Azure/WALinuxAgent
    def openssl_to_openssh(self, input_file, output_file):
        cryptutil = CryptUtil(conf.get_openssl_cmd())
        ret, out = shellutil.run_get_output(
            conf.get_openssl_cmd() +
            " rsa -pubin -noout -text -in '" + input_file + "'")
        if ret != 0:
            raise OSUtilError('openssl failed with {0}'.format(ret))

        modulus = []
        exponent = []
        buf = None
        for line in out.split('\n'):
            if line.startswith('Modulus:'):
                buf = modulus
                buf.append(line)
                continue
            if line.startswith('Exponent:'):
                buf = exponent
                buf.append(line)
                continue
            if buf and line:
                buf.append(line.strip().replace(':', ''))

        def text_to_num(buf):
            if len(buf) == 1:
                return int(buf[0].split()[1])
            return long(''.join(buf[1:]), 16)

        n = text_to_num(modulus)
        e = text_to_num(exponent)

        keydata = bytearray()
        keydata.extend(struct.pack('>I', len('ssh-rsa')))
        keydata.extend(b'ssh-rsa')
        keydata.extend(struct.pack('>I', len(cryptutil.num_to_bytes(e))))
        keydata.extend(cryptutil.num_to_bytes(e))
        keydata.extend(struct.pack('>I', len(cryptutil.num_to_bytes(n)) + 1))
        keydata.extend(b'\0')
        keydata.extend(cryptutil.num_to_bytes(n))
        keydata_base64 = base64.b64encode(bytebuffer(keydata))
        fileutil.write_file(output_file,
                            ustr(b'ssh-rsa ' + keydata_base64 + b'\n',
                                 encoding='utf-8'))
    def openssl_to_openssh(self, input_file, output_file):
        cryptutil = CryptUtil(conf.get_openssl_cmd())
        ret, out = shellutil.run_get_output(conf.get_openssl_cmd() +
                                            " rsa -pubin -noout -text -in '" +
                                            input_file + "'")
        if ret != 0:
            raise OSUtilError('openssl failed with {0}'.format(ret))

        modulus = []
        exponent = []
        buf = None
        for line in out.split('\n'):
            if line.startswith('Modulus:'):
                buf = modulus
                buf.append(line)
                continue
            if line.startswith('Exponent:'):
                buf = exponent
                buf.append(line)
                continue
            if buf and line:
                buf.append(line.strip().replace(':', ''))

        def text_to_num(buf):
            if len(buf) == 1:
                return int(buf[0].split()[1])
            return long(''.join(buf[1:]), 16)

        n = text_to_num(modulus)
        e = text_to_num(exponent)

        keydata = bytearray()
        keydata.extend(struct.pack('>I', len('ssh-rsa')))
        keydata.extend(b'ssh-rsa')
        keydata.extend(struct.pack('>I', len(cryptutil.num_to_bytes(e))))
        keydata.extend(cryptutil.num_to_bytes(e))
        keydata.extend(struct.pack('>I', len(cryptutil.num_to_bytes(n)) + 1))
        keydata.extend(b'\0')
        keydata.extend(cryptutil.num_to_bytes(n))
        keydata_base64 = base64.b64encode(bytebuffer(keydata))
        fileutil.write_file(
            output_file,
            ustr(b'ssh-rsa ' + keydata_base64 + b'\n', encoding='utf-8'))