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AwCyz9cuyQHu2jZh3apErYeZPQi+MOqtZeaazZitsBGNB1BXV4eIbavAluR
oiMsvUx2ifGnMws+EEjfWIbHSZ+BXgFS1lo8TViVHUmtW0z6jFUz491rdpaEOpeusG06Uu
GAE+GAQBXvCHOloGfosUCsO+MA/cs5QnkqGNMBmi1XNOmQElSHt8Tphkwm5JngIDdCjFA6
nOEpbEg/sp9CCeVJxpgSxyoFYixGLW85Ss+QrMub+nXIOYXyQksx6PJDzCQ0OR1N1WuH25
vuA2jstQZQOQfgqFdWlqFNWIG13kDahuNyun1RZiU3Io3i6rNkcgUfiG2UBd7TWZYmuE43
TAYnB9YyPCGphG
-----END OPENSSH PRIVATE KEY-----"""

        self._run_dsa_priv_test(pem_decode(dsa_key), b'bad password')
Ejemplo n.º 2
0
    def test_vec0(self):
        enc_rsa = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABC3OI9y/7
XkcWYsp75ZQNg+AAAAEAAAAAEAAAEXAAAAB3NzaC1yc2EAAAADAQABAAABAQDEkJ8gu8Ka
OrlokS0bVSsuUQ5m+mDvllZKc0rni6uGDn6njzg43U+Rco077M0y3CGnD9ESrsaHwI5ltS
fx9zceRlinwDma0rVPajfDq7Yh/xk3N2WlBio4nRNF4bPtZIA0vR842PisyKSn9RrF/iR8
9KUzaabEQpGkWGWJrHiGg/h3H4yJBGX9xm+tPU3nFoTj2gjHZTLvEBuSWfYtLnbqmW8Ar9
+zbCs3eUge7T7tCUPmqFMTKXN6N5rFtkoS2WGP2to7OLEXqWoSnMame3Z4UriBj4gbf13x
NVzVUnshwVqkEVz1pDJva0nSMVs1wU4xIm2rzku6hTOcuALF9nElAAAD0JUm6Sr05geQlR
n/VIJ95PW3PUh1qHVcF7TLoSfIkhfcuaS7PtcXyHmQJ+YWmCpGpGTZC/pzY79LXDPGIbcr
K0AjgX6XVKUfgA6H0zZhBHN8H2rNn3I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z2Cjv02tPvQx+G+WJBDQ56RRMCAfmbfdLX+Ks5u
5tshY6hFICd/aoQUh1GChuBy4ME88WEL/9vpNj6KGZ8RR8IB9QfDCFRNqvXEXyHGexgRCA
7s6/e1jqadOkrWct67dkl+P+28IR0=
-----END OPENSSH PRIVATE KEY-----"""

        # Watch out for the spelling mistake in the key
        self._run_rsa_priv_test(pem_decode(enc_rsa),
                                b'super secret passhphrase')
Ejemplo n.º 3
0
    def test_vec1(self):
        enc_rsa = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABAgZYxVX3
wNr/2tmsTgRd25AAAAEAAAAAEAAAEXAAAAB3NzaC1yc2EAAAADAQABAAABAQCcvbb8PXGg
Ohb/T8qHun2JcBjWVJnHepDyQnlfjjqrwpsjqJQ8a0qQ8+dxXuO+B3fYc22Gt1Nr4t3fNS
U21sRmxrR/em1WRh6Wc+7Nj/tFv9nsFh0tvYP5pDPsI+sFRt1mnxGaHy5EXwuxkPgDr/Ak
dvCaicwzuxXpsdwQQ+6om0NXiCjfp9qhd80TnQdmlrATJTXdYyFaysIf8HWQRxGUSQzwhe
J6nU+eOmTqpMVlfQ03LM676OZAztDiqbONg3xQb07n/BK2UY3OZtmebqZgsUw4Ajg+LMxI
O1fCTSVFI+GEvhW0ZmaXtFw2N8NveRKJ3gfM9ugVY7YvXdVdNYfJAAAD0FDG2FqPhIkK+0
W94eEM8+OjIi8C+VV677Ft+1x9BJL4rybawpOoMvTTKPdQB0RwxAUaglPVSyDUL/gRvnv3
gAeAW2PXToOKbxAm2GcfvZbaLl4re9pGrNiUPP92iw9F20w8/KcQ3r5sy1xdSN1mb1W/76
7c2OhTliCbWQS5s1WjDuZ3J9+ppVwwOTzWwqyWT3G1zNoE1pDquM2yiR11iulWD1sJN6n9
4L+2NO52ckuT76WpPUO8Qi1BKhjmY+UyVQNOUOdi0GujRCtA91ww+SIH7dRZIy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self._run_rsa_priv_test(pem_decode(enc_rsa), b'qwertyuiop')
Ejemplo n.º 4
0
    def import_key(buffer: bytes, passphrase: bytes = None):
        from samson.core.encodable_pki import EncodablePKI, ORDER
        from samson.encoding.pem import pem_decode

        subclasses = [EncodablePKI]

        for subclass in subclasses:
            subclasses.extend(subclass.__subclasses__())

        if buffer.startswith(b'----'):
            buffer = pem_decode(buffer, passphrase)

        for encoding in ORDER:
            for subclass in subclasses:
                for encoding_type in [
                        subclass.PRIV_ENCODINGS, subclass.PUB_ENCODINGS
                ]:
                    if encoding in encoding_type:
                        encoder = encoding_type[encoding]

                        if encoder.check(buffer, passphrase=passphrase):
                            return encoder.decode(buffer,
                                                  passphrase=passphrase)

        raise ValueError(f"Unable to parse provided key.")
Ejemplo n.º 5
0
    def test_vec8(self):
        eddsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBy8UrwPjpDfsvoXEaW9bXPqokbsMeRg+aVpCrQpYYDngAAAJhY0rPxWNKz
8QAAAAtzc2gtZWQyNTUxOQAAACBy8UrwPjpDfsvoXEaW9bXPqokbsMeRg+aVpCrQpYYDng
AAAEAsbUCaK45d4/w/FEkq70q8cCH1Azd9cM/5ndnuAQ4Sx3LxSvA+OkN+y+hcRpb1tc+q
iRuwx5GD5pWkKtClhgOeAAAAEWRvbmFsZEBEb25hbGQtTUJQAQIDBA==
-----END OPENSSH PRIVATE KEY-----"""

        self._run_eddsa_priv_test(pem_decode(eddsa_key))
Ejemplo n.º 6
0
    def test_vec7(self):
        eddsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAZQYey20n5vfzDpBfgpREtBbgm3nausKMUPWxGJOvLygAAAJh8qQ7jfKkO
4wAAAAtzc2gtZWQyNTUxOQAAACAZQYey20n5vfzDpBfgpREtBbgm3nausKMUPWxGJOvLyg
AAAEA9dyAbkQ9SYMduePMZX2oDsNxrAp8eqL4b2LZ5k33q7xlBh7LbSfm9/MOkF+ClES0F
uCbedq6woxQ9bEYk68vKAAAAEWRvbmFsZEBEb25hbGQtTUJQAQIDBA==
-----END OPENSSH PRIVATE KEY-----"""

        self._run_eddsa_priv_test(pem_decode(eddsa_key))
Ejemplo n.º 7
0
    def test_vec11(self):
        ecdsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDbKkOvUD
+n9Wd6aD/Di+kPAAAAEAAAAAEAAABoAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlz
dHAyNTYAAABBBKVtI+4WuK2ADIne2450En/Rx0kvYTsYa1/Pxv4fWWbMclGUDcM5GCUDj3
MOswiPcHjIvI4tTEEATVznwVO+IlgAAACw2IsDbaNLemAvulO0Rghu174rE2UtGPMAk/4y
Cv6E3BAccLLe6XkCC3Tplb0/bF0Xlb46jSiWBdXoFF8DU8o6sUVP925noq2PyU5z3oKbgG
AxTLP7peowIUVuhYTxEm+UjbHKffmZqpcl7NjmXZne3mBQ0ER2leJYbCMSOIFCjfvHcUYS
QkIhI9KAdhuATMcnJAGE+AZWb7kc5e5DCjzcmXwiwgyziX87684XLJkabl4=
-----END OPENSSH PRIVATE KEY-----"""

        self._run_ecdsa_priv_test(pem_decode(ecdsa_key), b'1234567890')
Ejemplo n.º 8
0
    def test_vec10(self):
        ecdsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDeR5QLZu
OJR5459267526tAAAAEAAAAAEAAABoAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlz
dHAyNTYAAABBBExcYjymvITWMgT+46+Q2HW32mGDiGHh04Db0dQBGPGIMiTUaQjTc2ypy4
mYClvKWK6cWyJpS2c+sxJsjNvVgO0AAACwC4nQ9eI7nwZaccUcNvPnLdEKdDJVkqowQqkV
6ELOXek4CxM/Zjcn/zFOJ4CzsOOkbyxMM8b3++WDvo0rl2Mh5GBNYNzTlGI3xyWG9aIymN
dKe54rrd6Vthc8FWa+QJGMM7iW40wH3X8I0Bma3/QeepSjk7d4i6yqyGUdOJUw/6LY81c8
POk1CjRmYABH3Nx4ZOyM0DQUr60xJ3p+DK0r2JuJag6NlfHah6RlmwsJKLQ=
-----END OPENSSH PRIVATE KEY-----"""

        self._run_ecdsa_priv_test(pem_decode(ecdsa_key), b'ab62b682b7e52bd2')
Ejemplo n.º 9
0
    def test_vec6(self):
        ecdsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS
1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQTEvVHDHpdd5FO9kDK3NGvYrELv/xz8
TkyilsxSun696P6JGfALwGekYR3vZCVbx8jpadYepm43qCo3A/K+f41cAAAAsA50iCsOdI
grAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMS9UcMel13kU72Q
Mrc0a9isQu//HPxOTKKWzFK6fr3o/okZ8AvAZ6RhHe9kJVvHyOlp1h6mbjeoKjcD8r5/jV
wAAAAgGGwMBSyO8je/vgMA03iw0Ss+H6bbWJ5yArOBP/cauq8AAAARZG9uYWxkQERvbmFs
ZC1NQlABAgMEBQYH
-----END OPENSSH PRIVATE KEY-----"""

        self._run_ecdsa_priv_test(pem_decode(ecdsa_key))
Ejemplo n.º 10
0
    def test_vec5(self):
        ecdsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS
1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQSg72tZM8/K9MdojLaDTbvkoYVXpKOy
1uApmrsxCZvlCUSY5oTbO6dLX4CfHBRBIyYozxBfgcfmFt/t4XI/lLLYAAAAsL4zYeO+M2
HjAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKDva1kzz8r0x2iM
toNNu+ShhVeko7LW4CmauzEJm+UJRJjmhNs7p0tfgJ8cFEEjJijPEF+Bx+YW3+3hcj+Ust
gAAAAhAIlhbJgLqbWvSMoc8DLRfjFxwMD+IsVUJVJh9d/dyIlaAAAAEWRvbmFsZEBEb25h
bGQtTUJQAQIDBAUG
-----END OPENSSH PRIVATE KEY-----"""

        self._run_ecdsa_priv_test(pem_decode(ecdsa_key))
Ejemplo n.º 11
0
    def test_import_pkcs8(self):
        from samson.encoding.pem import pem_decode
        rsa_priv = RSA.import_key(TEST_PKCS8_PRIV)

        priv_out = rsa_priv.export_private_key(encoding=PKIEncoding.PKCS8)
        print(pem_decode(priv_out))

        self.assertEqual((rsa_priv.p, rsa_priv.q, rsa_priv.d, rsa_priv.e), (
            166139291584298120902443024169830845883823889075313984939917653208853637890905161126081148766441591539160401657770377684418968678267217917397922110840150169955546544901086157269951344598822644183037291254655299911780341794815716113854851191488541745191877611504429644948688088285889033976663223284567224856833,
            150119113690962013024882886767787358736438702874024877639987005001513857060716647340813407248335373915813194132699511163987634021103271207272747869072785907538172094131387346081221837644849184613482205496553768651031911865532714182752679665482800511607578006666579667465077298743858640016483407400610361062117,
            261443907406365378354663085808874736386503755521058985081882965637470767771213025297791271476414073951003560905113869260605140573834934363441563582910410964978725003553295056860481290987047183978890184230384420847820829849525721267527923689830443383138481525900699205548045140667247267145170931395629543661831064542899777508230897484324075615967481549633013603167452463475949339014097601443520652166671659780291518107163852587423275987949452420808289251299490423174099687524419810127240076379136338103470684200785820699599792642573784713209902559428358302232211377555678297104998775430839041504293459045414977936385,
            65537))
        self.assertEqual(priv_out.replace(b'\n', b''),
                         TEST_PKCS8_PRIV.replace(b'\n', b''))
Ejemplo n.º 12
0
    def test_vec12(self):
        eddsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABBWby+4bc
EsmEWj6fq6SQUPAAAAEAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIHP1W2usseyTiIU1
XYSZmJpLud3rQIaIK4Sz+kvD4htlAAAAoMltuZETjKT60Af4ZafuGcyGOmRFZxeww2Q8cW
pR+EL8sH4Xv59v/xSbfdtQ//b/nKe3l2O9ctzyxKHplQtuhxnZyKieLrevtm0WYB04Y6CM
no7OAzxRjL6EEP1tjS+BODjLzSl65RA/xs+in7isGuKdoIRNsjuTFkv5dpSPNxnt7XTCoD
MjVf9IZeisieJJYf/NF2ttJ+JRkUlWZhx7xHM=
-----END OPENSSH PRIVATE KEY-----"""

        self._run_eddsa_priv_test(
            pem_decode(eddsa_key),
            b'49a4b3b86a7a51758ec37e3a0a503add1dc867a6ebace796134a8078687c607b982cca76e488a7010067e52c2c2adcf3f18a129f6e47b80b1b0aea6a73810e76'
        )
Ejemplo n.º 13
0
    def verify(buffer: bytes, verification_key: object) -> bool:
        if buffer.startswith(b'----'):
            buffer = pem_decode(buffer)

        # Decode the full cert and get the encoded TBSCertificate
        cert, _left_over = decoder.decode(buffer,
                                          asn1Spec=rfc2459.Certificate())

        sig_value = cert['signatureValue']
        signature_alg = INVERSE_SIGNING_ALG_OIDS[str(
            cert['signatureAlgorithm']['algorithm'])].replace('-', '_')
        tbs_cert = cert['tbsCertificate']
        encoded_tbs = encoder.encode(tbs_cert,
                                     asn1Spec=rfc2459.TBSCertificate())

        alg = verification_key.X509_SIGNING_ALGORITHMS[signature_alg].value
        return alg.verify(verification_key, encoded_tbs, sig_value)
Ejemplo n.º 14
0
def bytes_to_der_sequence(buffer: bytes, passphrase: bytes = None) -> Sequence:
    """
    Attempts to PEM-decode `buffer` then decodes the result to a DER sequence.

    Parameters:
        buffer     (bytes): The bytes to DER-decode.
        passphrase (bytes): Passphrase to decrypt DER-bytes (if applicable).
    
    Returns:
        Sequence: DER sequence.
    """
    from samson.encoding.pem import pem_decode
    try:
        buffer = pem_decode(buffer, passphrase)
    except ValueError as e:
        if 'passphrase not specified' in str(e):
            raise e

    seq = decoder.decode(buffer)
    items = seq[0]

    return items
Ejemplo n.º 15
0
    def import_key(cls, buffer: bytes, passphrase: bytes = None) -> object:
        """
        Builds a PKI instance from DER and/or PEM-encoded bytes.

        Parameters:
            buffer     (bytes): DER and/or PEM-encoded bytes.
            passphrase (bytes): Passphrase to decrypt DER-bytes (if applicable).

        Returns:
            object: PKI instance.
        """
        if buffer.startswith(b'----'):
            buffer = pem_decode(buffer, passphrase)

        for encoding in ORDER:
            for encoding_type in [cls.PRIV_ENCODINGS, cls.PUB_ENCODINGS]:
                if encoding in encoding_type:
                    encoder = encoding_type[encoding]
                    if encoder.check(buffer, passphrase=passphrase):
                        return encoder.decode(buffer, passphrase=passphrase)

        raise ValueError(f"Unable to parse provided {cls} key.")
Ejemplo n.º 16
0
    def test_vec4(self):
        dsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABsQAAAAdzc2gtZH
NzAAAAgQCEeZFwyJ4M1QEL251l+mo/dmUo9GXmPpfr8EA44uUuM4m6Gi2KnLugNCIaFanT
356VvWIetrFf38rKs8GpoBbdA1S9oilhs7pmL4TQf+zLv/a/pVo7xPmSNj0y9wEEaaPC/9
xu+tqxaCAt+lV3QtRrbjotEscT1WWyABJtyiZtPQAAABUAp/CKxPlh1CJ+wflQMNMX1nZ9
RwcAAACAUs5bKq4/BKjwPBNsjKpReBYohjdUb6PimWmPjsoAMrKTy1rMXHq+lX8wmxwEIU
XB1Con0QrSN3TuqUih0uGfC9mV1T2bBCrJRmW/qlyqqg5R0JnJb3wnZQR9QBsAac8w8vcZ
TYD8k7pKuvaz/UvDyfjeryshmwqkcDXUdtcTgzgAAACAMsp9fPlf+ePJ1Zn+DgUKHlGZqJ
rhKV7cnGNN69bptFwKntPTdxBZxPiYcuJC9NqdIYf1K/v53ovpIiHqwLB0Jq0HpAhwX3q0
+gjhsNWrCXvGXUrL3/Yg///dlLp3legH/4qmU+sql5I8jwdXr2ODdkzSdy0mI7MWv2TfA7
S5vtQAAAHojUae2o1GntoAAAAHc3NoLWRzcwAAAIEAhHmRcMieDNUBC9udZfpqP3ZlKPRl
5j6X6/BAOOLlLjOJuhotipy7oDQiGhWp09+elb1iHraxX9/KyrPBqaAW3QNUvaIpYbO6Zi
+E0H/sy7/2v6VaO8T5kjY9MvcBBGmjwv/cbvrasWggLfpVd0LUa246LRLHE9VlsgASbcom
bT0AAAAVAKfwisT5YdQifsH5UDDTF9Z2fUcHAAAAgFLOWyquPwSo8DwTbIyqUXgWKIY3VG
+j4plpj47KADKyk8tazFx6vpV/MJscBCFFwdQqJ9EK0jd07qlIodLhnwvZldU9mwQqyUZl
v6pcqqoOUdCZyW98J2UEfUAbAGnPMPL3GU2A/JO6Srr2s/1Lw8n43q8rIZsKpHA11HbXE4
M4AAAAgDLKfXz5X/njydWZ/g4FCh5Rmaia4Sle3JxjTevW6bRcCp7T03cQWcT4mHLiQvTa
nSGH9Sv7+d6L6SIh6sCwdCatB6QIcF96tPoI4bDVqwl7xl1Ky9/2IP//3ZS6d5XoB/+Kpl
PrKpeSPI8HV69jg3ZM0nctJiOzFr9k3wO0ub7UAAAAFEEnPct/4v9ju0UrGrltlvYMHcfv
AAAAEWRvbmFsZEBEb25hbGQtTUJQAQI=
-----END OPENSSH PRIVATE KEY-----"""

        self._run_dsa_priv_test(pem_decode(dsa_key))
Ejemplo n.º 17
0
    def test_vec3(self):
        dsa_key = b"""-----BEGIN OPENSSH PRIVATE KEY-----
    b3Bl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njMjDKhoVsWa
    HkRD8AAACAMoTYXybQMSQnsM/21z89KqJ3uOQT/0KK6ckqDT+VuiEir+iHBFWzd8WP9uwy
    Qb7t+FlzR2xFnZuMoC8sXaNLZaQdzPR80aRk6Uqf2srkEQG0oamOh6cjjpI/lXSP/HIAqA
    lyq1051yrZeYzd3VzApYn/sEeQ1pfoUsQlEyX7i7MAAAAUPPaYXG06+mjRlqnkrbQO9Toe
    V60AAAARZG9uYWxkQERvbmFsZC1NQlAB
    -----END OPENSSH PRIVATE KEY-----"""

        self._run_dsa_priv_test(pem_decode(dsa_key))