Repository: pyca/cryptography

Issue:
Spaces in ssh public key comment header cause serialization to fail
When serializing an ssh public key that contains spaces in the comment a ValueError is raised. There are several examples in RFC4716 of comments with spaces.

steps to reproduce:
1. create a public key with spaces in comment

```
 $ ssh-keygen -t rsa -f test_key -C "foo bar baz"
```
1. attempt to serialize through python repl

```
>>> from cryptography.hazmat import backends
>>> from cryptography.hazmat.primitives import serialization
>>> key=open('test_key.pub')
>>> keyval = key.read()
>>> serialization.load_ssh_public_key(keyval, backends.default_backend())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mike/.venvs/openstack/lib/python2.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 40, in load_ssh_public_key
    'Key is not in the proper format or contains extra data.')
ValueError: Key is not in the proper format or contains extra data.
>>> 
```



Discussion:

Resolving patch:
diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py
--- a/src/cryptography/hazmat/primitives/serialization.py
+++ b/src/cryptography/hazmat/primitives/serialization.py
@@ -33,9 +33,9 @@ def load_der_public_key(data, backend):
 
 
 def load_ssh_public_key(data, backend):
-    key_parts = data.split(b' ')
+    key_parts = data.split(b' ', 2)
 
-    if len(key_parts) != 2 and len(key_parts) != 3:
+    if len(key_parts) < 2:
         raise ValueError(
             'Key is not in the proper format or contains extra data.')
 


Test patch:
diff --git a/tests/hazmat/primitives/test_serialization.py b/tests/hazmat/primitives/test_serialization.py
--- a/tests/hazmat/primitives/test_serialization.py
+++ b/tests/hazmat/primitives/test_serialization.py
@@ -854,7 +854,7 @@ def test_load_ssh_public_key_rsa_too_short(self, backend):
         with pytest.raises(ValueError):
             load_ssh_public_key(ssh_key, backend)
 
-    def test_load_ssh_public_key_rsa_extra_string_after_comment(self, backend):
+    def test_load_ssh_public_key_rsa_comment_with_spaces(self, backend):
         ssh_key = (
             b"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDu/XRP1kyK6Cgt36gts9XAk"
             b"FiiuJLW6RU0j3KKVZSs1I7Z3UmU9/9aVh/rZV43WQG8jaR6kkcP4stOR0DEtll"
@@ -866,8 +866,7 @@ def test_load_ssh_public_key_rsa_extra_string_after_comment(self, backend):
             b"2MzHvnbv testkey@localhost extra"
         )
 
-        with pytest.raises(ValueError):
-            load_ssh_public_key(ssh_key, backend)
+        load_ssh_public_key(ssh_key, backend)
 
     def test_load_ssh_public_key_rsa_extra_data_after_modulo(self, backend):
         ssh_key = (
@@ -943,7 +942,7 @@ def test_load_ssh_public_key_dss_too_short(self, backend):
         with pytest.raises(ValueError):
             load_ssh_public_key(ssh_key, backend)
 
-    def test_load_ssh_public_key_dss_extra_string_after_comment(self, backend):
+    def test_load_ssh_public_key_dss_comment_with_spaces(self, backend):
         ssh_key = (
             b"ssh-dss AAAAB3NzaC1kc3MAAACBALmwUtfwdjAUjU2Dixd5DvT0NDcjjr69UD"
             b"LqSD/Xt5Al7D3GXr1WOrWGpjO0NE9qzRCvMTU7zykRH6XjuNXB6Hvv48Zfm4vm"
@@ -957,8 +956,7 @@ def test_load_ssh_public_key_dss_extra_string_after_comment(self, backend):
             b"z53N7tPF/IhHTjBHb1Ol7IFu9p9A== testkey@localhost extra"
         )
 
-        with pytest.raises(ValueError):
-            load_ssh_public_key(ssh_key, backend)
+        load_ssh_public_key(ssh_key, backend)
 
     def test_load_ssh_public_key_dss_extra_data_after_modulo(self, backend):
         ssh_key = (