22// This software is released under the BSD License (see LICENSE.BSD)
33
44using System ;
5- using System . Collections . Generic ;
65using System . IO ;
7- using System . Linq ;
8- using System . Text ;
6+ using System . Security . Cryptography . X509Certificates ;
97using digidoc ;
108
119namespace DigiDocCSharp
@@ -21,21 +19,13 @@ static void Main(string[] args)
2119 return ;
2220 }
2321
24- int pos = args [ 0 ] . IndexOf ( "=" ) ;
25- switch ( pos == - 1 ? args [ 0 ] : args [ 0 ] . Substring ( 0 , pos ) )
22+ switch ( args [ 0 ] )
2623 {
27- case "-extract" :
28- if ( pos < 0 )
29- {
30- Console . WriteLine ( "Index must be 0 or greater" ) ;
31- help ( ) ;
32- }
33- else
34- extract ( Convert . ToInt32 ( args [ 0 ] . Substring ( pos + 1 ) ) , args [ 1 ] ) ;
35- return ;
36- case "-verify" : verify ( args [ 1 ] ) ; return ;
37- case "-version" : version ( ) ; return ;
38- case "-help" :
24+ case "extract" : extract ( Convert . ToInt32 ( args [ 1 ] ) , args [ 2 ] ) ; return ;
25+ case "sign" : sign ( args ) ; return ;
26+ case "verify" : verify ( args [ 1 ] ) ; return ;
27+ case "version" : version ( ) ; return ;
28+ case "help" :
3929 default : help ( ) ; return ;
4030 }
4131 }
@@ -69,15 +59,38 @@ static void extract(int index, string file)
6959
7060 static void help ( )
7161 {
72- Console . WriteLine ( "DigiDocCSharpt [[ command] file] " ) ;
62+ Console . WriteLine ( "DigiDocCSharpt command" ) ;
7363 Console . WriteLine ( "Command:" ) ;
74- Console . WriteLine ( " -extract=[num]\t Extracts files from document" ) ;
75- Console . WriteLine ( " -help\t \t Prints utility commands" ) ;
76- Console . WriteLine ( " -verify\t Verifies document signature and shows info" ) ;
77- Console . WriteLine ( " -version\t Prints utility version" ) ;
64+ Console . WriteLine ( " extract\t Extracts files from document" ) ;
65+ Console . WriteLine ( " num" ) ;
66+ Console . WriteLine ( " file" ) ;
67+ Console . WriteLine ( " help\t \t Prints utility commands" ) ;
68+ Console . WriteLine ( " sign\t \t Signs file" ) ;
69+ Console . WriteLine ( " verify\t \t Verifies document signature and shows info" ) ;
70+ Console . WriteLine ( " file" ) ;
71+ Console . WriteLine ( " version\t Prints utility version" ) ;
7872 version ( ) ;
7973 }
8074
75+ static void sign ( string [ ] args )
76+ {
77+ digidoc . digidoc . initialize ( ) ;
78+ try
79+ {
80+ Console . WriteLine ( "Creating file: " + args [ args . Length - 1 ] ) ;
81+ Container b = new Container ( Container . DocumentType . BDocType ) ;
82+ 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 ] ) ;
86+ }
87+ catch ( Exception e )
88+ {
89+ Console . WriteLine ( e . Message ) ;
90+ }
91+ digidoc . digidoc . terminate ( ) ;
92+ }
93+
8194 static void verify ( string file )
8295 {
8396 digidoc . digidoc . initialize ( ) ;
@@ -88,9 +101,7 @@ static void verify(string file)
88101
89102 Console . WriteLine ( "Files:" ) ;
90103 foreach ( DataFile d in b . dataFiles ( ) )
91- {
92104 Console . WriteLine ( " {0} - {1}" , d . fileName ( ) , d . mediaType ( ) ) ;
93- }
94105 Console . WriteLine ( ) ;
95106
96107 Console . WriteLine ( "Signatures:" ) ;
@@ -104,32 +115,28 @@ static void verify(string file)
104115 Console . WriteLine ( ) ;
105116
106117 Console . WriteLine ( "Time: " + s . signingTime ( ) ) ;
107-
108- System . Security . Cryptography . X509Certificates . X509Certificate2 c =
109- new System . Security . Cryptography . X509Certificates . X509Certificate2 ( s . signingCert ( ) ) ;
118+ X509Certificate2 c = new X509Certificate2 ( s . signingCert ( ) ) ;
110119 Console . WriteLine ( "Cert: " + c . Subject ) ;
111- try
112- {
113- s . validate ( ) ;
114- Console . WriteLine ( "Signature is valid" ) ;
115- }
116- catch ( Exception e )
117- {
118- Console . WriteLine ( "Signature is invalid" ) ;
119- Console . WriteLine ( e . Message ) ;
120- }
120+
121+ Console . WriteLine ( "ProducedAt: " + s . producedAt ( ) ) ;
122+ c = new X509Certificate2 ( s . OCSPCert ( ) ) ;
123+ Console . WriteLine ( "OCSP Cert: " + c . Subject ) ;
124+
125+ s . validate ( ) ;
126+ Console . WriteLine ( "Signature is valid" ) ;
121127 }
122128 }
123129 catch ( Exception e )
124130 {
131+ Console . WriteLine ( "Signature is invalid" ) ;
125132 Console . WriteLine ( e . Message ) ;
126133 }
127134 digidoc . digidoc . terminate ( ) ;
128135 }
129136
130137 static void version ( )
131138 {
132- Console . WriteLine ( "DigiDocCSharp 0.1" ) ;
139+ Console . WriteLine ( "DigiDocCSharp 0.1 libdigidocpp " + digidoc . digidoc . version ( ) ) ;
133140 }
134141 }
135142}
0 commit comments