Contents · Forum Home · StreamSec Tools 4.1.5.364 documentation

Choose the encoding of a key file's password

A private-key file is encrypted with a key derived from the octets of its password, so the file opens only when the password is turned into the same octets as when the file was written. A key component takes the password in one of two ways, and they differ in that conversion.

The components on this page are normally placed on a form or a data module at design time and set up in the Object Inspector. The sample creates them in code instead, because code shows at a glance which properties are changed from their default values.

The default: PrivateKeyPassword

function OpenKeyFile(const aKeyFile: string; const aPassword: AnsiString): TstECDSAKey;
begin
  Result := TstECDSAKey.Create(nil);
  try
    Result.PrivateKeyFormat.SelectedOptionNames := 'PKCS8-Encrypted-PEM-DSS';
    { The password in the default encoding: the octets of the AnsiString,
      that is, in the code page of this computer's locale. }
    Result.PrivateKeyPassword := aPassword;
    Result.PrivateKeyFileName := aKeyFile;
    Result.Enabled := True;
    if not Result.Enabled then
      raise eKeyFormatError.CreateFmt('%s did not open: wrong password?', [aKeyFile]);
  except
    Result.Free;
    raise;
  end;
end;

PrivateKeyPassword is an AnsiString, and the octets of that string are the password: it is encoded in the code page of the computer's locale (DefaultSystemCodePage) - code page 1252 on a Western European Windows, 1251 on a Russian one.

A user types a password in the alphabet of their own locale, so for key files written and read on computers with the same locale this is all you need.

A key file from another locale: OnPassword

The octets differ when a key file crosses locales. Two cases are common:

For these, hand over the password yourself, in the encoding the file was written with:

type
  { Hands the key file's password to the key component through OnPassword,
    encoded in the code page aCodePage - UTF-8 unless another is given.
    Owned by the key component, so it lives as long as the key does. }
  tKeyPassword = class(TComponent)
  private
    fPassword: string;
    fCodePage: Integer;
    procedure Supply(Sender: TObject; aPassword: iSecretKey);
  public
    constructor Create(aKey: TstECDSAKey; const aPassword: string;
                       aCodePage: Integer = CP_UTF8); reintroduce;
  end;
constructor tKeyPassword.Create(aKey: TstECDSAKey; const aPassword: string;
  aCodePage: Integer);
begin
  inherited Create(aKey);
  fPassword := aPassword;
  fCodePage := aCodePage;
  aKey.OnPassword := Supply;
end;

procedure tKeyPassword.Supply(Sender: TObject; aPassword: iSecretKey);
var
  lEncoding: TEncoding;
  lOctets: TBytes;
begin
  lEncoding := TEncoding.GetEncoding(fCodePage);
  try
    lOctets := lEncoding.GetBytes(fPassword);
  finally
    lEncoding.Free;
  end;
  try
    aPassword.SetKey(lOctets, 0, Length(lOctets), 0);
  finally
    ProtectClear(lOctets);
  end;
end;

The component raises OnPassword each time it needs the password, with an iSecretKey to fill. The octets the handler puts into it are used exactly as they are, so the handler decides the encoding; leave PrivateKeyPassword empty. tKeyPassword takes the code page to encode in; for the Russian user's key file it is 1251:

{ A key file from a user whose computer uses code page 1251: the password is
  handed over in that code page, as PrivateKeyPassword did on that computer. }
function OpenOtherLocaleKey(const aKeyFile, aPassword: string): TstECDSAKey;
begin
  Result := TstECDSAKey.Create(nil);
  try
    Result.PrivateKeyFormat.SelectedOptionNames := 'PKCS8-Encrypted-PEM-DSS';
    tKeyPassword.Create(Result, aPassword, 1251);
    Result.PrivateKeyFileName := aKeyFile;
    Result.Enabled := True;
    if not Result.Enabled then
      raise eKeyFormatError.CreateFmt('%s did not open: wrong password?', [aKeyFile]);
  except
    Result.Free;
    raise;
  end;
end;

The octets are wiped as soon as they have been handed over. On a form, assign the handler to OnPassword in the Object Inspector.

Files your application writes

An application that writes key files of its own and reads them on other computers can avoid the question: hand the password over as UTF-8, which does not depend on the locale, as Create a key and a certificate signing request does with tKeyPassword and no code page given.

For a password of plain ASCII characters both ways give the same octets, so such a file also opens through PrivateKeyPassword. With other characters it does not: decide on one encoding for the files an application writes, and keep to it.

Reference

TstECDSAKey · TstRSAKey · StreamSec.DSI.ArithComp

Related tasks