Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 89aa967d

Přidáno uživatelem Michal Seják před téměř 4 roky(ů)

Re #8705 - Review updates.

Zobrazit rozdíly:

src/controllers/certificates_controller.py
132 132
                                return E_WRONG_PASSWORD, C_BAD_REQUEST
133 133
                            key = self.key_service.wrap_custom_key(key_pem, passphrase=None)
134 134
                        else:
135
                            key = self.key_service.create_new_key()                 # if "key" exists but is empty
135
                            return E_WRONG_PARAMETERS, C_BAD_REQUEST                # if "key" exists but is empty
136 136
                else:
137 137
                    return E_WRONG_PARAMETERS, C_BAD_REQUEST
138 138
            else:
139 139
                key = self.key_service.create_new_key()                             # if "key" does not exist
140
                                                                                    # TODO Honza: line 134 / 138, should
141
                                                                                    #   they both be allowed? I say one
142
                                                                                    #   of those branches should return
143
                                                                                    #   wrong params bad request (mby).
144 140

  
145 141
            if CA not in body or body[CA] is None:                                  # if issuer omitted (legal) or none
146 142
                cert = self.certificate_service.create_root_ca(                     # create a root CA
......
167 163
                f = self.certificate_service.create_ca if CA_ID in usages_dict and usages_dict[CA_ID] else \
168 164
                    self.certificate_service.create_end_cert
169 165

  
170
                try:
171
                    # noinspection PyTypeChecker
172
                    cert = f(                                                           # create inter CA or end cert
173
                        key,                                                            # according to whether 'CA' is among
174
                        subject,                                                        # the usages' fields
175
                        issuer,
176
                        issuer_key,
177
                        usages=usages_dict,
178
                        days=body[VALIDITY_DAYS]
179
                    )
180
                except CryptographyException as e:
181
                    return {"success": False, "data": e.message}, C_BAD_REQUEST
166
                # noinspection PyTypeChecker
167
                cert = f(                                                           # create inter CA or end cert
168
                    key,                                                            # according to whether 'CA' is among
169
                    subject,                                                        # the usages' fields
170
                    issuer,
171
                    issuer_key,
172
                    usages=usages_dict,
173
                    days=body[VALIDITY_DAYS]
174
                )
182 175

  
183 176
            if cert is not None:
184 177
                return {"success": True,
tests/integration_tests/rest_api/certificates_test.py
910 910
    assert ret.json["success"]
911 911
    assert ret.json["data"]["status"] == "expired"
912 912

  
913
    def test_create_with_key_with_pass_valid(server, cryptography_service):
914
        key_pass = "123456Seven"
915
        key_pem = cryptography_service.create_private_key(key_pass)
916

  
917
        certificate = {
918
            "CA": 1,
919
            "subject": {
920
                "C": "CZ",
921
                "CN": "Rare Name",
922
                "L": "Legendary Name",
923
                "O": "Obscure Name",
924
                "OU": "Original Uniform"
925
            },
926
            "usage": ["CA", "digitalSignature"],
927
            "validityDays": 1,
928
            "key": {
929
                "password": key_pass,
930
                "key_pem": key_pem
931
            },
932
        }
933
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
934
        assert ret.status_code == 201
935

  
936
    def test_create_with_key_with_pass_invalid_1(server, cryptography_service):
937
        key_pass = "123456Seven"
938
        key_pem = cryptography_service.create_private_key(key_pass)
939

  
940
        certificate = {
941
            "CA": 1,
942
            "subject": {
943
                "C": "CZ",
944
                "CN": "Rare Name",
945
                "L": "Legendary Name",
946
                "O": "Obscure Name",
947
                "OU": "Original Uniform"
948
            },
949
            "usage": ["CA", "digitalSignature"],
950
            "validityDays": 1,
951
            "key": {
952
                "password": "whoopsiedaisy",
953
                "key_pem": key_pem
954
            },
955
        }
956
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
957
        assert ret.status_code == 400
958
        assert ret.json["data"] == 'The provided passphrase does not match the provided key.'
959
        assert not ret.json["success"]
913
def test_create_with_key_with_pass_valid(server, cryptography_service):
914
    key_pass = "123456Seven"
915
    key_pem = cryptography_service.create_private_key(key_pass)
960 916

  
961
    def test_create_with_key_with_pass_invalid_2(server, cryptography_service):
962
        key_pass = "123456Seven"
963

  
964
        certificate = {
965
            "CA": 1,
966
            "subject": {
967
                "C": "CZ",
968
                "CN": "Rare Name",
969
                "L": "Legendary Name",
970
                "O": "Obscure Name",
971
                "OU": "Original Uniform"
972
            },
973
            "usage": ["CA", "digitalSignature"],
974
            "validityDays": 1,
975
            "key": {
976
                "password": key_pass,
977
                "key_pem": "hi im garbage and you better pray `server` survives this"
978
            },
979
        }
980
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
981
        assert ret.status_code == 400
982
        assert ret.json["data"] == 'The provided passphrase does not match the provided key.'
983
        assert not ret.json["success"]
917
    certificate = {
918
        "CA": 1,
919
        "subject": {
920
            "C": "CZ",
921
            "CN": "Rare Name",
922
            "L": "Legendary Name",
923
            "O": "Obscure Name",
924
            "OU": "Original Uniform"
925
        },
926
        "usage": ["CA", "digitalSignature"],
927
        "validityDays": 1,
928
        "key": {
929
            "password": key_pass,
930
            "key_pem": key_pem
931
        },
932
    }
933
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
934
    assert ret.status_code == 201
984 935

  
985
    def test_create_with_key_no_pass(server, cryptography_service):
986
        key_pem = cryptography_service.create_private_key()
987

  
988
        certificate = {
989
            "CA": 1,
990
            "subject": {
991
                "C": "CZ",
992
                "CN": "Rare Name",
993
                "L": "Legendary Name",
994
                "O": "Obscure Name",
995
                "OU": "Original Uniform"
996
            },
997
            "usage": ["CA", "digitalSignature"],
998
            "validityDays": 1,
999
            "key": {
1000
                "key_pem": key_pem
1001
            },
1002
        }
1003
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1004
        assert ret.status_code == 201
1005

  
1006
    def test_create_with_key_no_pass_invalid_1(server, cryptography_service):
1007
        certificate = {
1008
            "CA": 1,
1009
            "subject": {
1010
                "C": "CZ",
1011
                "CN": "Rare Name",
1012
                "L": "Legendary Name",
1013
                "O": "Obscure Name",
1014
                "OU": "Original Uniform"
1015
            },
1016
            "usage": ["CA", "digitalSignature"],
1017
            "validityDays": 1,
1018
            "key": {
1019
                "key_pem": "hi im garbage and you better pray `server` survives this"
1020
            },
1021
        }
1022
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1023
        assert ret.status_code == 400
1024
        assert not ret.json["success"]
936
def test_create_with_key_with_pass_invalid_1(server, cryptography_service):
937
    key_pass = "123456Seven"
938
    key_pem = cryptography_service.create_private_key(key_pass)
1025 939

  
1026
    def test_create_no_key_with_pass(server, cryptography_service):
1027
        key_pass = "123456Seven"
1028

  
1029
        certificate = {
1030
            "CA": 1,
1031
            "subject": {
1032
                "C": "CZ",
1033
                "CN": "Rare Name",
1034
                "L": "Legendary Name",
1035
                "O": "Obscure Name",
1036
                "OU": "Original Uniform"
1037
            },
1038
            "usage": ["CA", "digitalSignature"],
1039
            "validityDays": 1,
1040
            "key": {
1041
                "password": key_pass,
1042
            },
1043
        }
1044
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1045
        assert ret.status_code == 201
1046
        assert ret.json["success"]
1047
        created_id = ret.json["data"]
1048
        ret = server.get(f"/api/certificates/{created_id}/privatekey")
1049
        assert ret.status_code == 200
1050
        assert ret.json["success"]
1051
        key_pem = ret.json["data"]
1052
        assert cryptography_service.verify_key(key_pem, key_pass)
1053

  
1054
    def test_create_no_key_with_pass_invalid_1(server, cryptography_service):
1055
        key_pass = "123456Seven"
1056

  
1057
        certificate = {
1058
            "CA": 1,
1059
            "subject": {
1060
                "C": "CZ",
1061
                "CN": "Rare Name",
1062
                "L": "Legendary Name",
1063
                "O": "Obscure Name",
1064
                "OU": "Original Uniform"
1065
            },
1066
            "usage": ["CA", "digitalSignature"],
1067
            "validityDays": 1,
1068
            "key": {
1069
                "password": key_pass,
1070
            },
1071
        }
1072
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1073
        assert ret.status_code == 201
1074
        assert ret.json["success"]
1075
        created_id = ret.json["data"]
1076
        ret = server.get(f"/api/certificates/{created_id}/privatekey")
1077
        assert ret.status_code == 200
1078
        assert ret.json["success"]
1079
        key_pem = ret.json["data"]
1080
        assert not cryptography_service.verify_key(key_pem, "iforgotthepassagainimsorryfrowningface")
1081

  
1082
    def test_create_no_key_no_pass_invalid(server, cryptography_service):
1083
        certificate = {
1084
            "CA": 1,
1085
            "subject": {
1086
                "C": "CZ",
1087
                "CN": "Rare Name",
1088
                "L": "Legendary Name",
1089
                "O": "Obscure Name",
1090
                "OU": "Original Uniform"
1091
            },
1092
            "usage": ["CA", "digitalSignature"],
1093
            "validityDays": 1,
1094
            "key": {
1095
            }
940
    certificate = {
941
        "CA": 1,
942
        "subject": {
943
            "C": "CZ",
944
            "CN": "Rare Name",
945
            "L": "Legendary Name",
946
            "O": "Obscure Name",
947
            "OU": "Original Uniform"
948
        },
949
        "usage": ["CA", "digitalSignature"],
950
        "validityDays": 1,
951
        "key": {
952
            "password": "whoopsiedaisy",
953
            "key_pem": key_pem
954
        },
955
    }
956
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
957
    assert ret.status_code == 400
958
    assert ret.json["data"] == 'The provided passphrase does not match the provided key.'
959
    assert not ret.json["success"]
960

  
961
def test_create_with_key_with_pass_invalid_2(server, cryptography_service):
962
    key_pass = "123456Seven"
963

  
964
    certificate = {
965
        "CA": 1,
966
        "subject": {
967
            "C": "CZ",
968
            "CN": "Rare Name",
969
            "L": "Legendary Name",
970
            "O": "Obscure Name",
971
            "OU": "Original Uniform"
972
        },
973
        "usage": ["CA", "digitalSignature"],
974
        "validityDays": 1,
975
        "key": {
976
            "password": key_pass,
977
            "key_pem": "hi im garbage and you better pray `server` survives this"
978
        },
979
    }
980
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
981
    assert ret.status_code == 400
982
    assert ret.json["data"] == 'The provided passphrase does not match the provided key.'
983
    assert not ret.json["success"]
984

  
985
def test_create_with_key_no_pass(server, cryptography_service):
986
    key_pem = cryptography_service.create_private_key()
987

  
988
    certificate = {
989
        "CA": 1,
990
        "subject": {
991
            "C": "CZ",
992
            "CN": "Rare Name",
993
            "L": "Legendary Name",
994
            "O": "Obscure Name",
995
            "OU": "Original Uniform"
996
        },
997
        "usage": ["CA", "digitalSignature"],
998
        "validityDays": 1,
999
        "key": {
1000
            "key_pem": key_pem
1001
        },
1002
    }
1003
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1004
    assert ret.status_code == 201
1005

  
1006
def test_create_with_key_no_pass_invalid_1(server, cryptography_service):
1007
    certificate = {
1008
        "CA": 1,
1009
        "subject": {
1010
            "C": "CZ",
1011
            "CN": "Rare Name",
1012
            "L": "Legendary Name",
1013
            "O": "Obscure Name",
1014
            "OU": "Original Uniform"
1015
        },
1016
        "usage": ["CA", "digitalSignature"],
1017
        "validityDays": 1,
1018
        "key": {
1019
            "key_pem": "hi im garbage and you better pray `server` survives this"
1020
        },
1021
    }
1022
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1023
    assert ret.status_code == 400
1024
    assert not ret.json["success"]
1025

  
1026
def test_create_no_key_with_pass(server, cryptography_service):
1027
    key_pass = "123456Seven"
1028

  
1029
    certificate = {
1030
        "CA": 1,
1031
        "subject": {
1032
            "C": "CZ",
1033
            "CN": "Rare Name",
1034
            "L": "Legendary Name",
1035
            "O": "Obscure Name",
1036
            "OU": "Original Uniform"
1037
        },
1038
        "usage": ["CA", "digitalSignature"],
1039
        "validityDays": 1,
1040
        "key": {
1041
            "password": key_pass,
1042
        },
1043
    }
1044
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1045
    assert ret.status_code == 201
1046
    assert ret.json["success"]
1047
    created_id = ret.json["data"]
1048
    ret = server.get(f"/api/certificates/{created_id}/privatekey")
1049
    assert ret.status_code == 200
1050
    assert ret.json["success"]
1051
    key_pem = ret.json["data"]
1052
    assert cryptography_service.verify_key(key_pem, key_pass)
1053

  
1054
def test_create_no_key_with_pass_invalid_1(server, cryptography_service):
1055
    key_pass = "123456Seven"
1056

  
1057
    certificate = {
1058
        "CA": 1,
1059
        "subject": {
1060
            "C": "CZ",
1061
            "CN": "Rare Name",
1062
            "L": "Legendary Name",
1063
            "O": "Obscure Name",
1064
            "OU": "Original Uniform"
1065
        },
1066
        "usage": ["CA", "digitalSignature"],
1067
        "validityDays": 1,
1068
        "key": {
1069
            "password": key_pass,
1070
        },
1071
    }
1072
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1073
    assert ret.status_code == 201
1074
    assert ret.json["success"]
1075
    created_id = ret.json["data"]
1076
    ret = server.get(f"/api/certificates/{created_id}/privatekey")
1077
    assert ret.status_code == 200
1078
    assert ret.json["success"]
1079
    key_pem = ret.json["data"]
1080
    assert not cryptography_service.verify_key(key_pem, "iforgotthepassagainimsorryfrowningface")
1081

  
1082
def test_create_no_key_no_pass_invalid(server, cryptography_service):
1083
    certificate = {
1084
        "CA": 1,
1085
        "subject": {
1086
            "C": "CZ",
1087
            "CN": "Rare Name",
1088
            "L": "Legendary Name",
1089
            "O": "Obscure Name",
1090
            "OU": "Original Uniform"
1091
        },
1092
        "usage": ["CA", "digitalSignature"],
1093
        "validityDays": 1,
1094
        "key": {
1096 1095
        }
1097
        ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1098
        assert ret.status_code == 201  # TODO Honza? CertificateController/create_cert
1099

  
1100
    def test_key_reusal(server, cryptography_service):
1101
        # key_pass = "1234567Eight"
1102
        # key_pem = cryptography_service.create_private_key(key_pass)
1103
        #
1104
        # certificate = {
1105
        #     "CA": 1,
1106
        #     "subject": {
1107
        #         "C": "CZ",
1108
        #         "CN": "Rare Name",
1109
        #         "L": "Legendary awd",
1110
        #         "O": "Obscure awd",
1111
        #         "OU": "Original Uniform"
1112
        #     },
1113
        #     "usage": ["CA", "digitalSignature"],
1114
        #     "validityDays": 1,
1115
        #     "key": {
1116
        #         "password": key_pass,
1117
        #         "key_pem": key_pem
1118
        #     },
1119
        # }
1120
        # ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1121
        # assert ret.staus_code == 201
1122
        # assert ret.json["success"]
1123
        # created_id = ret.json["data"]
1124
        # ret = server.get(f"/api/certificates/{created_id}/privatekey")
1125
        # assert ret.status_code == 200
1126
        # assert ret.json["success"]
1127
        # key_pem = ret.json["data"]
1128
        # TODO
1129
        pass
1096
    }
1097
    ret = server.post("/api/certificates", content_type="application/json", json=certificate)
1098
    assert ret.status_code == 201

Také k dispozici: Unified diff