33
44using System ;
55using System . IO ;
6+ using System . Linq ;
67using System . Security . Cryptography . X509Certificates ;
78using digidoc ;
89
@@ -23,6 +24,7 @@ static void Main(string[] args)
2324 {
2425 case "extract" : extract ( Convert . ToInt32 ( args [ 1 ] ) , args [ 2 ] ) ; return ;
2526 case "sign" : sign ( args ) ; return ;
27+ case "websign" : websign ( args ) ; return ;
2628 case "verify" : verify ( args [ 1 ] ) ; return ;
2729 case "version" : version ( ) ; return ;
2830 case "help" :
@@ -36,7 +38,7 @@ static void extract(int index, string file)
3638 try
3739 {
3840 Console . WriteLine ( "Opening file: " + file ) ;
39- Container b = new Container ( file ) ;
41+ Container b = Container . open ( file ) ;
4042 DataFile d = b . dataFiles ( ) [ index ] ;
4143 string dest = Path . Combine ( Directory . GetCurrentDirectory ( ) , d . fileName ( ) ) ;
4244 Console . WriteLine ( "Extracting file {0} to {1}" , d . fileName ( ) , dest ) ;
@@ -59,13 +61,19 @@ static void extract(int index, string file)
5961
6062 static void help ( )
6163 {
62- Console . WriteLine ( "DigiDocCSharpt command" ) ;
64+ Console . WriteLine ( "DigiDocCSharp command [params] " ) ;
6365 Console . WriteLine ( "Command:" ) ;
6466 Console . WriteLine ( " extract\t Extracts files from document" ) ;
6567 Console . WriteLine ( " num" ) ;
6668 Console . WriteLine ( " file" ) ;
6769 Console . WriteLine ( " help\t \t Prints utility commands" ) ;
6870 Console . WriteLine ( " sign\t \t Signs file" ) ;
71+ Console . WriteLine ( " datafile1 datafile2 ..." ) ;
72+ Console . WriteLine ( " file" ) ;
73+ Console . WriteLine ( " websign\t \t Signs file" ) ;
74+ Console . WriteLine ( " datafile1 datafile2 ..." ) ;
75+ Console . WriteLine ( " cert" ) ;
76+ Console . WriteLine ( " file" ) ;
6977 Console . WriteLine ( " verify\t \t Verifies document signature and shows info" ) ;
7078 Console . WriteLine ( " file" ) ;
7179 Console . WriteLine ( " version\t Prints utility version" ) ;
@@ -78,11 +86,46 @@ static void sign(string[] args)
7886 try
7987 {
8088 Console . WriteLine ( "Creating file: " + args [ args . Length - 1 ] ) ;
81- Container b = new Container ( Container . DocumentType . BDocType ) ;
89+ Container b = Container . create ( args [ args . Length - 1 ] ) ;
8290 for ( int i = 1 ; i < args . Length - 1 ; ++ i )
83- b . addDataFile ( args [ i ] , "" ) ;
84- b . sign ( "" , "" , "" , "" , new StringVector { } , "" ) ;
85- b . save ( args [ args . Length - 1 ] ) ;
91+ b . addDataFile ( args [ i ] , "application/octet-stream" ) ;
92+ using ( WinSigner signer = new WinSigner ( ) )
93+ b . sign ( signer ) ;
94+ b . save ( ) ;
95+ }
96+ catch ( Exception e )
97+ {
98+ Console . WriteLine ( e . Message ) ;
99+ }
100+ digidoc . digidoc . terminate ( ) ;
101+ }
102+
103+ static void websign ( string [ ] args )
104+ {
105+ digidoc . digidoc . initialize ( ) ;
106+ try
107+ {
108+ Console . WriteLine ( "Creating file: " + args [ args . Length - 1 ] ) ;
109+ Container b = Container . create ( args [ args . Length - 1 ] ) ;
110+ for ( int i = 1 ; i < args . Length - 2 ; ++ i )
111+ b . addDataFile ( args [ i ] , "application/octet-stream" ) ;
112+
113+ X509Certificate cert = new X509Certificate ( ) ;
114+ cert . Import ( args [ args . Length - 2 ] ) ;
115+ Signature c = b . prepareWebSignature ( cert . Export ( X509ContentType . Cert ) , "BES/time-stamp" ) ;
116+ Console . WriteLine ( "Signature method: " + c . signatureMethod ( ) ) ;
117+ Console . WriteLine ( "Digest to sign: " + BitConverter . ToString ( c . dataToSign ( ) ) . Replace ( "-" , string . Empty ) ) ;
118+ Console . WriteLine ( "Please enter signed digest in hex: " ) ;
119+
120+ byte [ ] inputBuffer = new byte [ 1024 ] ;
121+ Stream inputStream = Console . OpenStandardInput ( inputBuffer . Length ) ;
122+ Console . SetIn ( new StreamReader ( inputStream , Console . InputEncoding , false , inputBuffer . Length ) ) ;
123+ String hex = Console . ReadLine ( ) ;
124+
125+ byte [ ] signature = Enumerable . Range ( 0 , hex . Length / 2 ) . Select ( x => Convert . ToByte ( hex . Substring ( x * 2 , 2 ) , 16 ) ) . ToArray ( ) ;
126+ c . setSignatureValue ( signature ) ;
127+ c . extendSignatureProfile ( "BES/time-stamp" ) ;
128+ b . save ( ) ;
86129 }
87130 catch ( Exception e )
88131 {
@@ -97,7 +140,7 @@ static void verify(string file)
97140 try
98141 {
99142 Console . WriteLine ( "Opening file: " + file ) ;
100- Container b = new Container ( file ) ;
143+ Container b = Container . open ( file ) ;
101144
102145 Console . WriteLine ( "Files:" ) ;
103146 foreach ( DataFile d in b . dataFiles ( ) )
@@ -114,13 +157,8 @@ static void verify(string file)
114157 Console . Write ( " " + role ) ;
115158 Console . WriteLine ( ) ;
116159
117- Console . WriteLine ( "Time: " + s . signingTime ( ) ) ;
118- X509Certificate2 c = new X509Certificate2 ( s . signingCert ( ) ) ;
119- Console . WriteLine ( "Cert: " + c . Subject ) ;
120-
121- Console . WriteLine ( "ProducedAt: " + s . producedAt ( ) ) ;
122- c = new X509Certificate2 ( s . OCSPCert ( ) ) ;
123- Console . WriteLine ( "OCSP Cert: " + c . Subject ) ;
160+ Console . WriteLine ( "Time: " + s . trustedSigningTime ( ) ) ;
161+ Console . WriteLine ( "Cert: " + new X509Certificate2 ( s . signingCertificateDer ( ) ) . Subject ) ;
124162
125163 s . validate ( ) ;
126164 Console . WriteLine ( "Signature is valid" ) ;
@@ -136,7 +174,7 @@ static void verify(string file)
136174
137175 static void version ( )
138176 {
139- Console . WriteLine ( "DigiDocCSharp 0.1 libdigidocpp " + digidoc . digidoc . version ( ) ) ;
177+ Console . WriteLine ( "DigiDocCSharp 0.2 libdigidocpp " + digidoc . digidoc . version ( ) ) ;
140178 }
141179 }
142180}
0 commit comments