Advertisement
Guest User

Untitled

a guest
Feb 29th, 2012
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. zdroj:
  2. http://web.archive.org/web/20100206030825/http://denum.bloguje.cz/819199-datove-schranky-zpracovani-souboru-zfo.php
  3.  
  4. Datové schránky - zpracování souboru ZFO
  5.  
  6. Napsáno dne 30.10.2009, 12:14:00 | denum
  7. Tolik vztekání co mě povedený formát ZFO od lidí z 602 stál, se vám teď pokusím aspoň trochu ušetřit. Vy, kdož jste už měli co dočinění s webovými službami datových schránek jistě víte, že pokud chcete stáhnout přílohu z odeslané zprávy, operace MessageDownload je vám k ničemu, protože umí stáhnout pouze zprávu doručenou. Tzn. pokud se zprávami chcete pracovat jednotně je třeba využít metod SignedMessageDownload a SignedSentMessageDownload, které vcací zprávy ve formátu ZFO. Jeho čtení není nikterak složité, ale hledáním způsobu, jak z XML odstranit PKCS#7 obálku, jsem strávil dva dny.
  8.  
  9.  
  10. Zde je jednoduchý způsob:
  11.  
  12.  
  13.  
  14.  
  15. import java.io.*;
  16.  
  17. import java.util.*;
  18.  
  19. import java.security.*;
  20.  
  21. import java.security.Security;
  22.  
  23. import java.security.cert.*;
  24.  
  25. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  26.  
  27. import org.bouncycastle.cms.*;
  28.  
  29.  
  30.  
  31.  
  32.  
  33. /* Verify INCLUDED CMS signature CMS/pkcs #7 signature using BC provider.
  34.  
  35. Verify with either the included signer certificate, or a specified separate signer
  36.  
  37. certificate file.
  38.  
  39. Output signed content to binary file
  40.  
  41. M. Gallant 04/01/2005 */
  42.  
  43.  
  44.  
  45. class BCVerifyISig {
  46.  
  47. static final boolean DEBUG =true;
  48.  
  49.  
  50.  
  51. public static void main(String args[]) {
  52.  
  53. System.out.println("");
  54.  
  55.  
  56.  
  57. if (args.length != 2 && args.length !=3)
  58.  
  59. usage();
  60.  
  61.  
  62.  
  63. Security.addProvider(new BouncyCastleProvider());
  64.  
  65.  
  66.  
  67. X509Certificate signercert = null;
  68.  
  69.  
  70.  
  71. String INFILE = args[0]; // Input CMS/PKCS#7 included signed content to verify
  72.  
  73. String OUTFILE = args[1]; //output file containing recovered signed-content
  74.  
  75. if(args.length == 3) {
  76.  
  77. try{
  78.  
  79. InputStream inStream = new FileInputStream(args[2]);
  80.  
  81. CertificateFactory cf = CertificateFactory.getInstance("X.509");
  82.  
  83. signercert = (X509Certificate)cf.generateCertificate(inStream);
  84.  
  85. inStream.close();
  86.  
  87. if(DEBUG)
  88.  
  89. System.out.println("Got certificate from file " + args[2]) ;
  90.  
  91. }
  92.  
  93. catch(Exception cerexc) {
  94.  
  95. System.out.println("Failed to create certificate from file " + args[2]) ;
  96.  
  97. System.exit(1) ;
  98.  
  99. }
  100.  
  101. }
  102.  
  103.  
  104.  
  105. boolean INCLUDED = true; // included (true) or detached (false) content
  106.  
  107.  
  108.  
  109. FileInputStream freader = null;
  110.  
  111. File f = null;
  112.  
  113.  
  114.  
  115. //------ Get the included data signature from file -------------
  116.  
  117. f = new File(INFILE) ;
  118.  
  119. int sizecontent = ((int) f.length());
  120.  
  121. byte[] sigbytes = new byte[sizecontent];
  122.  
  123.  
  124.  
  125. try {
  126.  
  127. freader = new FileInputStream(f);
  128.  
  129. System.out.println("nSignature Bytes: " + freader.read(sigbytes, 0, sizecontent));
  130.  
  131. freader.close();
  132.  
  133. }
  134.  
  135. catch(IOException ioe) {
  136.  
  137. System.out.println(ioe.toString());
  138.  
  139. return;
  140.  
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. if(isBase64Encoded(sigbytes)){
  152.  
  153. try{
  154.  
  155. sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder() ;
  156.  
  157. sigbytes = dec.decodeBuffer(new String(sigbytes));
  158.  
  159. System.out.println("Signature file is BASE64 encoded") ;
  160.  
  161. }
  162.  
  163. catch(IOException ioe) {System.out.println("Problem decoding from b64") ; }
  164.  
  165. }
  166.  
  167.  
  168.  
  169. // --- Use Bouncy Castle provider to verify included-content CSM/PKCS#7 signature ---
  170.  
  171. try{
  172.  
  173. CMSSignedData s = new CMSSignedData(sigbytes) ;
  174.  
  175. CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");
  176.  
  177. SignerInformationStore signers = s.getSignerInfos();
  178.  
  179. Collection c = signers.getSigners();
  180.  
  181. Iterator it = c.iterator();
  182.  
  183. int verified = 0;
  184.  
  185.  
  186.  
  187. while (it.hasNext())
  188.  
  189. {
  190.  
  191. X509Certificate cert =null;
  192.  
  193. SignerInformation signer = (SignerInformation)it.next();
  194.  
  195. Collection certCollection = certs.getCertificates(signer.getSID());
  196.  
  197. if (certCollection.isEmpty() && signercert==null)
  198.  
  199. continue;
  200.  
  201. else if (signercert !=null) // use a signer cert file for verification, if it was provided
  202.  
  203. cert = signercert;
  204.  
  205. else { // use the certificates included in the signature for verification
  206.  
  207. Iterator certIt = certCollection.iterator();
  208.  
  209. cert = (X509Certificate)certIt.next();
  210.  
  211. }
  212.  
  213.  
  214.  
  215. if(DEBUG)
  216.  
  217. System.out.println("Current certificate " + cert.toString()) ;
  218.  
  219. System.out.println("") ;
  220.  
  221.  
  222.  
  223. if (signer.verify(cert.getPublicKey(), "BC"))
  224.  
  225. verified++;
  226.  
  227. }
  228.  
  229.  
  230.  
  231. if(verified == 0)
  232.  
  233. System.out.println("WARNING: No signers' signatures could be verified !") ;
  234.  
  235. else if(signercert !=null)
  236.  
  237. System.out.println("Verified a signature using signer certificate file '" + args[2] + "'") ;
  238.  
  239. else
  240.  
  241. System.out.println("Verified a signature using a certificate in the signature file '" + INFILE + "'") ;
  242.  
  243.  
  244.  
  245.  
  246.  
  247. CMSProcessableByteArray cpb = (CMSProcessableByteArray) s.getSignedContent() ;
  248.  
  249. byte[] rawcontent = (byte[]) cpb.getContent() ;
  250.  
  251. System.out.println("nWriting content (" + rawcontent.length + " bytes) to file " + OUTFILE + " ... ") ;
  252.  
  253. FileOutputStream fcontent = new FileOutputStream(OUTFILE);
  254.  
  255. fcontent.write(rawcontent);
  256.  
  257. fcontent.close();
  258.  
  259.  
  260.  
  261. }
  262.  
  263. catch(Exception ex){
  264.  
  265. System.out.println("Couldn't verify included-content CMS signaturen" + ex.toString()) ;
  266.  
  267. }
  268.  
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275. private static final boolean isBase64Encoded(byte[] data) {
  276.  
  277. Arrays.sort(Base64Map);
  278.  
  279. for (int i=0; i
  280. //System.out.println("data[" + i + "] " + (char)data[i]) ;
  281.  
  282. if( Arrays.binarySearch(Base64Map, (char)data[i])<0
  283.  
  284. && !Character.isWhitespace((char)data[i]) )
  285.  
  286. return false;
  287.  
  288. }
  289.  
  290. return true;
  291.  
  292. }
  293.  
  294.  
  295.  
  296. private static char[] Base64Map =
  297.  
  298. { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  299.  
  300. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  301.  
  302. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  303.  
  304. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  305.  
  306. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  307.  
  308. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  309.  
  310. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  311.  
  312. '4', '5', '6', '7', '8', '9', '+', '/', '='
  313.  
  314. };
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324. private static void usage() {
  325.  
  326. System.out.println("Usage:n java BCVerifyISig [signercertFile]") ;
  327.  
  328. System.exit(1);
  329.  
  330. }
  331.  
  332. }
  333.  
  334.  
  335.  
  336. Zdroj: Java Cryptography Samples od (Michel I. Gallant)
  337.  
  338.  
  339.  
  340. Tímto panu/paní Gallant tisíceré díky. Doufám, že i vám přijde vhod.
  341.  
  342. linkuj.cz vybrali.sme.sk
  343.  
  344. Komentáře (3) | Rubrika: Programování | Ohodnotit | Průměrná známka: 0
  345. < Funkce MIN pro tři čísla |
  346.  
  347. Komentáře
  348.  
  349.  
  350.  
  351. C#
  352. Přidal Ex_animo (WWW) dne 16.11.2009, 20:46:35
  353.  
  354. bylo by mozne toto prepsat i do C#? rozhodne je to velice uzitecna zalezitost .-)
  355.  
  356.  
  357. Přidal Tim (WWW) dne 22.01.2010, 08:19:11
  358.  
  359. Jojo, chtelo by to prepsat do .Netu :) Ale diky. No nic ja to jdu prepisovat, snad se mi to nejak povede.
  360.  
  361.  
  362. Přidal Tim (WWW) dne 22.01.2010, 08:52:41
  363.  
  364. Tak v .NETu staci pouzit knihovnu
  365.  
  366. System.Security.Cryptography.Pkcs
  367.  
  368. ta obsahuje tridy pro praci s PKCS.
  369.  
  370. Takze pak uz jen staci neco jako
  371.  
  372. SignedCms verifyCms = new SignedCms();
  373. File.WriteAllBytes(path , signature);
  374. verifyCms.Decode(signature);
  375.  
  376. a mate z PKCS hezke XMLko :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement