Example #1
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("signature_name")
    parser.add_argument("input_json", nargs="?", type=argparse.FileType('r'),
                        default=sys.stdin)

    args = parser.parse_args()
    logging.basicConfig()

    server_name = args.signature_name
    keys = {}
    for target, port in get_targets(server_name):
        try:
            keys = get_server_keys(server_name, target, port)
            print "Using keys from https://%s:%s/_matrix/key/v1" % (target, port)
            write_signing_keys(sys.stdout, keys.values())
            break
        except:
            logging.exception("Error talking to %s:%s", target, port)

    json_to_check = json.load(args.input_json)
    print "Checking JSON:"
    for key_id in json_to_check["signatures"][args.signature_name]:
        try:
            key = keys[key_id]
            verify_signed_json(json_to_check, args.signature_name, key)
            print "PASS %s" % (key_id,)
        except:
            logging.exception("Check for key %s failed" % (key_id,))
            print "FAIL %s" % (key_id,)
Example #2
0
    def generate_files(self, config: Dict[str, Any],
                       config_dir_path: str) -> None:
        if "signing_key" in config:
            return

        signing_key_path = config.get("signing_key_path")
        if signing_key_path is None:
            signing_key_path = os.path.join(
                config_dir_path, config["server_name"] + ".signing.key")

        if not self.path_exists(signing_key_path):
            print("Generating signing key file %s" % (signing_key_path, ))
            with open(signing_key_path, "w") as signing_key_file:
                key_id = "a_" + random_string(4)
                write_signing_keys(signing_key_file,
                                   (generate_signing_key(key_id), ))
        else:
            signing_keys = self.read_file(signing_key_path, "signing_key")
            if len(signing_keys.split("\n")[0].split()) == 1:
                # handle keys in the old format.
                key_id = "a_" + random_string(4)
                key = decode_signing_key_base64(NACL_ED25519, key_id,
                                                signing_keys.split("\n")[0])
                with open(signing_key_path, "w") as signing_key_file:
                    write_signing_keys(signing_key_file, (key, ))
Example #3
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("signature_name")
    parser.add_argument("input_json",
                        nargs="?",
                        type=argparse.FileType('r'),
                        default=sys.stdin)

    args = parser.parse_args()
    logging.basicConfig()

    server_name = args.signature_name
    keys = {}
    for target, port in get_targets(server_name):
        try:
            keys = get_server_keys(server_name, target, port)
            print("Using keys from https://%s:%s/_matrix/key/v1" %
                  (target, port))
            write_signing_keys(sys.stdout, keys.values())
            break
        except Exception:
            logging.exception("Error talking to %s:%s", target, port)

    json_to_check = json.load(args.input_json)
    print("Checking JSON:")
    for key_id in json_to_check["signatures"][args.signature_name]:
        try:
            key = keys[key_id]
            verify_signed_json(json_to_check, args.signature_name, key)
            print("PASS %s" % (key_id, ))
        except Exception:
            logging.exception("Check for key %s failed" % (key_id, ))
            print("FAIL %s" % (key_id, ))
Example #4
0
 def get_synapse_signing_key(self):
     """Return the path of the synapse signing key, generating it if missing."""
     if not path.exists(self.synapse_signing_key_file):
         key_id = "a_" + self.random_string(4)
         key_content = generate_signing_key(key_id)
         with open(self.synapse_signing_key_file, "w+") as key_file:
             write_signing_keys(key_file, (key_content, ))
     return self.synapse_signing_key_file
Example #5
0
def main() -> None:
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-o",
        "--output_file",
        type=argparse.FileType("w"),
        default=sys.stdout,
        help="Where to write the output to",
    )
    args = parser.parse_args()

    key_id = "a_" + random_string(4)
    key = (generate_signing_key(key_id),)
    write_signing_keys(args.output_file, key)
Example #6
0
    def generate_files(self, config):
        signing_key_path = config["signing_key_path"]

        if not self.path_exists(signing_key_path):
            with open(signing_key_path, "w") as signing_key_file:
                key_id = "a_" + random_string(4)
                write_signing_keys(signing_key_file,
                                   (generate_signing_key(key_id), ))
        else:
            signing_keys = self.read_file(signing_key_path, "signing_key")
            if len(signing_keys.split("\n")[0].split()) == 1:
                # handle keys in the old format.
                key_id = "a_" + random_string(4)
                key = decode_signing_key_base64(NACL_ED25519, key_id,
                                                signing_keys.split("\n")[0])
                with open(signing_key_path, "w") as signing_key_file:
                    write_signing_keys(signing_key_file, (key, ))
Example #7
0
 def generate_files(self, config):
     signing_key_path = config["signing_key_path"]
     if not os.path.exists(signing_key_path):
         with open(signing_key_path, "w") as signing_key_file:
             key_id = "a_" + random_string(4)
             write_signing_keys(
                 signing_key_file, (generate_signing_key(key_id),),
             )
     else:
         signing_keys = self.read_file(signing_key_path, "signing_key")
         if len(signing_keys.split("\n")[0].split()) == 1:
             # handle keys in the old format.
             key_id = "a_" + random_string(4)
             key = decode_signing_key_base64(
                 NACL_ED25519, key_id, signing_keys.split("\n")[0]
             )
             with open(signing_key_path, "w") as signing_key_file:
                 write_signing_keys(
                     signing_key_file, (key,),
                 )
Example #8
0
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import sys

from signedjson.key import write_signing_keys, generate_signing_key

from synapse.util.stringutils import random_string

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-o",
        "--output_file",
        type=argparse.FileType('w'),
        default=sys.stdout,
        help="Where to write the output to",
    )
    args = parser.parse_args()

    key_id = "a_" + random_string(4)
    key = generate_signing_key(key_id),
    write_signing_keys(args.output_file, key)
Example #9
0
    def test_write_signing_keys(self):
        class MockStream(object):
            def write(self, data):
                pass

        write_signing_keys(MockStream(), [self.key])
Example #10
0
 def test_write_signing_keys(self):
     class MockStream(object):
         def write(self, data):
             pass
     write_signing_keys(MockStream(), [self.key])
Example #11
0
def write_signing_key(path):
    with open(path, 'w') as f:
        key.write_signing_keys(f, [key.generate_signing_key('first')])