----------------------------------------------------------------------------- Purpose: Test parsing and re-writing of keys in OpenSSH formats -----------------------------------------------------------------------------
| 539 | // Purpose: Test parsing and re-writing of keys in OpenSSH formats |
| 540 | //----------------------------------------------------------------------------- |
| 541 | void TestOpenSSHEd25519() |
| 542 | { |
| 543 | char buf[ 2048 ]; |
| 544 | |
| 545 | // Generate some keys, and make sure we can round trip them |
| 546 | { |
| 547 | CECSigningPublicKey pubKey; |
| 548 | CECSigningPrivateKey privKey; |
| 549 | CCrypto::GenerateSigningKeyPair( &pubKey, &privKey ); |
| 550 | |
| 551 | uint32 cbBuf; |
| 552 | |
| 553 | // Get public key as authorized_keys format. |
| 554 | // Should fail if we tell it the buffer is too small |
| 555 | CHECK( !pubKey.GetAsOpenSSHAuthorizedKeys( buf, 16, &cbBuf, "" ) ); |
| 556 | CHECK( pubKey.GetAsOpenSSHAuthorizedKeys( buf, sizeof(buf), &cbBuf, "" ) ); |
| 557 | CHECK( (int)cbBuf == V_strlen( buf )+1 ); |
| 558 | CHECK( 75 <= cbBuf && cbBuf <= 85 ); // typical size (assuming no password or key comment). Not necessarily a bug if this assert fires, but maybe something suspicious |
| 559 | |
| 560 | // Parse it back out, make sure it matches |
| 561 | CECSigningPublicKey pubKey2; |
| 562 | CHECK( pubKey2.LoadFromAndWipeBuffer( buf, cbBuf ) ); |
| 563 | CHECK( pubKey2 == pubKey ); |
| 564 | |
| 565 | // Get private key in openSSH PEM-ish format. |
| 566 | // Should fail if we tell it the buffer is too small |
| 567 | CHECK( !privKey.GetAsPEM( buf, 64, &cbBuf ) ); |
| 568 | CHECK( privKey.GetAsPEM( buf, sizeof(buf), &cbBuf ) ); |
| 569 | CHECK( (int)cbBuf == V_strlen( buf )+1 ); |
| 570 | CHECK( 370 <= cbBuf && cbBuf <= 390 ); // typical size (assuming no password or key comment). Not necessarily a bug if this assert fires, but maybe something suspicious |
| 571 | |
| 572 | // Parse it back out, make sure it matches |
| 573 | CECSigningPrivateKey privKey2; |
| 574 | CHECK( privKey2.LoadFromAndWipeBuffer( buf, cbBuf ) ); |
| 575 | CHECK( privKey2 == privKey ); |
| 576 | |
| 577 | CHECK( privKey2.MatchesPublicKey( pubKey2 ) ); |
| 578 | } |
| 579 | |
| 580 | // Parse some known keys |
| 581 | { |
| 582 | CUtlBuffer bufPrivKeyPEMA; |
| 583 | bufPrivKeyPEMA.PutString( |
| 584 | R"PEM( |
| 585 | -----BEGIN OPENSSH PRIVATE KEY----- |
| 586 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW |
| 587 | QyNTUxOQAAACC3vdN6llE0by4d7aFur0nBXdu5hXJb7LLkiC5UCEPFDgAAAJgJaJG1CWiR |
| 588 | tQAAAAtzc2gtZWQyNTUxOQAAACC3vdN6llE0by4d7aFur0nBXdu5hXJb7LLkiC5UCEPFDg |
| 589 | AAAECpUfg4C0BkgsCO+GlFAbcTQZUeFFQcamXzDA1tx7aNWre903qWUTRvLh3toW6vScFd |
| 590 | 27mFclvssuSILlQIQ8UOAAAAEmZsZXRjaGVyZEBzcmNkczAwMwECAw== |
| 591 | -----END OPENSSH PRIVATE KEY----- |
| 592 | )PEM" ); |
| 593 | CECSigningPrivateKey privKeyA; |
| 594 | CHECK( privKeyA.LoadFromAndWipeBuffer( bufPrivKeyPEMA.Base(), bufPrivKeyPEMA.TellPut() ) ); |
| 595 | |
| 596 | CUtlBuffer bufPubKeyA; |
| 597 | bufPubKeyA.PutString( "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILe903qWUTRvLh3toW6vScFd27mFclvssuSILlQIQ8UO" ); |
| 598 | CECSigningPublicKey pubKeyA; |
no test coverage detected