Exemplo n.º 1
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, ))
Exemplo n.º 2
0
def generate_self_id_key() -> SigningKey:
    """generate a signing key whose version is its public key

    ... as used by the cross-signing-keys.
    """
    k = key.generate_signing_key("x")
    k.version = encode_pubkey(k)
    return k
Exemplo n.º 3
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
Exemplo n.º 4
0
    def generate_and_upload_device_signing_key(self, user_id: str,
                                               device_id: str) -> SigningKey:
        """Generate a signing keypair for the given device, and upload it"""
        sk = key.generate_signing_key(device_id)

        device_dict = build_device_dict(user_id, device_id, sk)

        self.get_success(self.hs.get_e2e_keys_handler().upload_keys_for_user(
            user_id,
            device_id,
            {"device_keys": device_dict},
        ))
        return sk
Exemplo n.º 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)
Exemplo n.º 6
0
    def prepare(self, reactor, clock, hs):
        self.federation_sender = hs.get_federation_sender()
        self.event_builder_factory = hs.get_event_builder_factory()
        self.federation_handler = hs.get_federation_handler()
        self.presence_handler = hs.get_presence_handler()

        # self.event_builder_for_2 = EventBuilderFactory(hs)
        # self.event_builder_for_2.hostname = "test2"

        self.store = hs.get_datastore()
        self.state = hs.get_state_handler()
        self.auth = hs.get_auth()

        # We don't actually check signatures in tests, so lets just create a
        # random key to use.
        self.random_signing_key = generate_signing_key("ver")
Exemplo n.º 7
0
    def prepare(self, reactor, clock, hs):
        self.federation_sender = hs.get_federation_sender()
        self.event_builder_factory = hs.get_event_builder_factory()
        self.federation_handler = hs.get_handlers().federation_handler
        self.presence_handler = hs.get_presence_handler()

        # self.event_builder_for_2 = EventBuilderFactory(hs)
        # self.event_builder_for_2.hostname = "test2"

        self.store = hs.get_datastore()
        self.state = hs.get_state_handler()
        self.auth = hs.get_auth()

        # We don't actually check signatures in tests, so lets just create a
        # random key to use.
        self.random_signing_key = generate_signing_key("ver")
Exemplo n.º 8
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, ))
Exemplo n.º 9
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,),
                 )
Exemplo n.º 10
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)
Exemplo n.º 11
0
 def setUp(self):
     self.version = "my_version"
     self.key = generate_signing_key(self.version)
     self.key_base64 = encode_signing_key_base64(self.key)
     self.verify_key = get_verify_key(self.key)
     self.verify_key_base64 = encode_verify_key_base64(self.verify_key)
Exemplo n.º 12
0
 def test_generate_key(self):
     my_version = "my_version"
     my_key = generate_signing_key(my_version)
     self.assertEquals(my_key.alg, "ed25519")
     self.assertEquals(my_key.version, my_version)
Exemplo n.º 13
0
        y = arr
      else:
        return sum(arr) 


print(summer_69([1, 3, 5]))
print(summer_69([4, 5, 6, 7, 8, 9]))
print(summer_69([2, 1, 6, 9, 11]))
print(summer_69([]))
'''
print("\n")
from signedjson.key import generate_signing_key, get_verify_key, encode_signing_key_base64
from signedjson.sign import (
    sign_json, verify_signed_json, SignatureVerifyException
)

signing_key = generate_signing_key('zxcvb')
base_64_signing_key = encode_signing_key_base64(signing_key)
print(base_64_signing_key)
signed_json = sign_json({'my_key': 'my_data'},'Drew', signing_key)
print(signed_json)
verify_key = get_verify_key(signing_key)
base_64_verify_key = encode_signing_key_base64(verify_key)
print(base_64_verify_key)
#check = get_verify_key(base_64_signing_key)

try:
    verify_signed_json(signed_json, 'Drew', verify_key)
    print('Signature is valid')
except SignatureVerifyException:
    print('Signature is invalid')
Exemplo n.º 14
0
 def setUp(self):
     self.version = "my_version"
     self.key = generate_signing_key(self.version)
     self.key_base64 = encode_signing_key_base64(self.key)
     self.verify_key = get_verify_key(self.key)
     self.verify_key_base64 = encode_verify_key_base64(self.verify_key)
Exemplo n.º 15
0
 def test_generate_key(self):
     my_version = "my_version"
     my_key = generate_signing_key(my_version)
     self.assertEquals(my_key.alg, "ed25519")
     self.assertEquals(my_key.version, my_version)
Exemplo n.º 16
0
def write_signing_key(path):
    with open(path, 'w') as f:
        key.write_signing_keys(f, [key.generate_signing_key('first')])