1+ using System ;
2+ using System . IO ;
3+ using System . Text ;
4+ using Renci . SshNet . Common ;
5+ using Renci . SshNet . Security . Cryptography ;
6+
7+ namespace Renci . SshNet . Security
8+ {
9+ /// <summary>
10+ /// Contains OpenSSH private and public key
11+ /// </summary>
12+ public class OpenSSHKey : Key , IDisposable
13+ {
14+ /// <summary>
15+ /// Gets the Key String.
16+ /// </summary>
17+ public override string ToString ( )
18+ {
19+ // ToDo: return extraced Key String
20+ return base . ToString ( ) ;
21+ }
22+
23+ /// <summary>
24+ /// Gets the length of the key.
25+ /// </summary>
26+ /// <value>
27+ /// The length of the key.
28+ /// </value>
29+ public override int KeyLength
30+ {
31+ get
32+ {
33+ // ToDo: actual KeySize;
34+ return 0 ;
35+ }
36+ }
37+
38+ /// <summary>
39+ /// Gets the digital signature.
40+ /// </summary>
41+ protected override DigitalSignature DigitalSignature
42+ {
43+ get
44+ {
45+ throw new NotImplementedException ( ) ;
46+ }
47+ }
48+
49+ /// <summary>
50+ /// Gets or sets the public.
51+ /// </summary>
52+ /// <value>
53+ /// The public.
54+ /// </value>
55+ public override BigInteger [ ] Public
56+ {
57+ get
58+ {
59+ throw new NotImplementedException ( ) ;
60+ }
61+ set
62+ {
63+ throw new NotImplementedException ( ) ;
64+ }
65+ }
66+
67+ const string AUTH_MAGIC = "openssh-key-v1" ;
68+
69+ /// <summary>
70+ /// Initializes a new instance of the <see cref="OpenSSHKey"/> class.
71+ /// </summary>
72+ public OpenSSHKey ( )
73+ {
74+ }
75+
76+ /// <summary>
77+ /// Initializes a new instance of the <see cref="OpenSSHKey"/> class.
78+ /// </summary>
79+ /// <param name="data">DER encoded private key data.</param>
80+ public OpenSSHKey ( byte [ ] data )
81+ {
82+ //throw new SshException("Unsupported Format: " + BitConverter.ToString(data));
83+
84+ // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key
85+ using ( var br = new BinaryReader ( new MemoryStream ( data ) , Encoding . ASCII ) )
86+ {
87+ var magic = Encoding . ASCII . GetString ( br . ReadBytes ( AUTH_MAGIC . Length ) ) ;
88+ if ( magic != AUTH_MAGIC )
89+ throw new SshException ( "Unsupported Format: " + magic ) ;
90+ br . ReadByte ( ) ; // AUTH_MAGIC is 0 terminated string
91+ Console . WriteLine ( "Len: " + BitConverter . ToString ( br . ReadBytes ( 4 ) ) ) ;
92+ Console . WriteLine ( "String: " + Encoding . ASCII . GetString ( br . ReadBytes ( 4 ) ) ) ;
93+ }
94+ }
95+
96+ #region IDisposable Members
97+
98+ private bool _isDisposed ;
99+
100+ /// <summary>
101+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
102+ /// </summary>
103+ public void Dispose ( )
104+ {
105+ Dispose ( true ) ;
106+ GC . SuppressFinalize ( this ) ;
107+ }
108+
109+ /// <summary>
110+ /// Releases unmanaged and - optionally - managed resources
111+ /// </summary>
112+ /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
113+ protected virtual void Dispose ( bool disposing )
114+ {
115+ if ( _isDisposed )
116+ return ;
117+
118+ if ( disposing )
119+ {
120+ _isDisposed = true ;
121+ }
122+ }
123+
124+ /// <summary>
125+ /// Releases unmanaged resources and performs other cleanup operations before the
126+ /// <see cref="DsaKey"/> is reclaimed by garbage collection.
127+ /// </summary>
128+ ~ OpenSSHKey ( )
129+ {
130+ Dispose ( false ) ;
131+ }
132+
133+ #endregion
134+ }
135+ }
0 commit comments