| 215 | } |
| 216 | |
| 217 | void cKeyFile::WriteFile(const TCHAR* filename) const // throw eKeyFile() |
| 218 | { |
| 219 | ASSERT(KeysLoaded()); |
| 220 | |
| 221 | try |
| 222 | { |
| 223 | cFileArchive outFile; |
| 224 | cFileHeader fileHeader; |
| 225 | |
| 226 | outFile.OpenReadWrite(filename); |
| 227 | |
| 228 | fileHeader.SetID(cKeyFile::GetFileHeaderID()); |
| 229 | |
| 230 | // Set file version. |
| 231 | // If we in the future we wish to support reading keys different versions, |
| 232 | // we will have to move this |
| 233 | fileHeader.SetVersion(CURRENT_FIXED_VERSION); |
| 234 | |
| 235 | fileHeader.SetEncoding(cFileHeader::NO_ENCODING); |
| 236 | |
| 237 | { |
| 238 | cSerializerImpl fhSer(outFile, cSerializerImpl::S_WRITE, filename); |
| 239 | fileHeader.Write(&fhSer); |
| 240 | } |
| 241 | |
| 242 | // save public key |
| 243 | int16 len = mpPublicKey->GetWriteLen(); |
| 244 | int8* publicMem = new int8[len]; |
| 245 | mpPublicKey->Write(publicMem); |
| 246 | |
| 247 | outFile.WriteInt16(len); |
| 248 | outFile.WriteBlob(publicMem, len); |
| 249 | |
| 250 | delete [] publicMem; |
| 251 | |
| 252 | // save private key |
| 253 | len = mPrivateKeyMemLen; |
| 254 | outFile.WriteInt16(len); |
| 255 | outFile.WriteBlob(mpPrivateKeyMem, mPrivateKeyMemLen); |
| 256 | } |
| 257 | catch (eArchive&) |
| 258 | { |
| 259 | throw eKeyFileArchive(filename); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Functions to read and write the key to memory. GetWriteLen() will throw an |
| 264 | // exception if keys are not currently loaded. |