def AddVersion(self, status, size=None): """ Adds a new key version with given status to key set. Generates a new key of same type (repeated until hash identifier is unique) for this version. Uses supplied key size (if provided) in lieu of the default key size. If this is an unacceptable key size, raises an error. Uses next available version number. @param status: the status of the new key to be added @type status: L{keyinfo.KeyStatus} @param size: size of key in bits, uses default size if not provided. @type size: integer @raise KeyczarError: if either key type or key size is unsupported. """ if size is None: size = self.default_size if not self.metadata.type.IsValidSize(size): raise errors.KeyczarError("Unsupported key size %d bits." % size) max_version_number = 0 for version in self.versions: if max_version_number < version.version_number: max_version_number = version.version_number # Make the new version number the max of the existing versions plus one version = keydata.KeyVersion(max_version_number + 1, status, False) if status == keyinfo.PRIMARY: if self.primary_version is not None: self.primary_version.status = keyinfo.ACTIVE self.primary_version = version if size < self.default_size: print( "WARNING: %d-bit key size is less than recommended default key" "size of %d bits for %s keys." % (size, self.default_size, str(self.metadata.type))) # Make sure no keys collide on their identifiers while True: key = keys.GenKey(self.metadata.type, size) if self._keys.get(key.hash_id) is None: break self._AddKey(version, key)
def AddKey(self, version_number, status, size=None): """Utility method for testing.""" key = keys.GenKey(self.kmd.type, size) self.keys[version_number] = key return self.kmd.AddVersion( keydata.KeyVersion(version_number, status, False))