/Users/lyon/j4p/src/security/SignatureFile.java
|
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4 // Source File Name: SignatureFile.java
5
6 package security;
7
8
9 import sun.security.pkcs.ContentInfo;
10 import sun.security.pkcs.PKCS7;
11 import sun.security.pkcs.SignerInfo;
12 import sun.security.x509.AlgorithmId;
13 import sun.security.x509.X500Name;
14 import sun.security.x509.X509CertInfo;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.security.*;
20 import java.security.cert.CertificateException;
21 import java.security.cert.X509Certificate;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.jar.Attributes;
25 import java.util.jar.Manifest;
26
27 // Referenced classes of package sun.security.util:
28 // ManifestDigester
29
30 public class SignatureFile {
31 public static class Block {
32
33 public String getMetaName() {
34 return blockFileName;
35 }
36
37 public void write(OutputStream out)
38 throws IOException {
39 block.encodeSignedData(out);
40 }
41
42 private PKCS7 block;
43 private String blockFileName;
44
45 Block(SignatureFile sfg,
46 PrivateKey privateKey,
47 X509Certificate certChain[],
48 boolean externalSF)
49 throws NoSuchAlgorithmException,
50 InvalidKeyException,
51 IOException,
52 SignatureException,
53 CertificateException {
54 Principal issuerName = certChain[0].getIssuerDN();
55 if (!(issuerName instanceof X500Name)) {
56 X509CertInfo tbsCert = new X509CertInfo(
57 certChain[0].getTBSCertificate());
58 issuerName =
59 (Principal) tbsCert.get(
60 "issuer.dname");
61 }
62 java.math.BigInteger serial = certChain[0].getSerialNumber();
63 String keyAlgorithm = privateKey.getAlgorithm();
64 String digestAlgorithm;
65 if (keyAlgorithm.equalsIgnoreCase(
66 "DSA"))
67 digestAlgorithm = "SHA1";
68 else if (keyAlgorithm.equalsIgnoreCase(
69 "RSA"))
70 digestAlgorithm = "MD5";
71 else
72 throw new RuntimeException(
73 "private key is not a DSA or RSA key");
74 String signatureAlgorithm = digestAlgorithm +
75 "with" +
76 keyAlgorithm;
77 blockFileName = "META-INF/" +
78 sfg.getBaseName() +
79 "." +
80 keyAlgorithm;
81 AlgorithmId digestAlg = AlgorithmId.get(
82 digestAlgorithm);
83 AlgorithmId sigAlg = AlgorithmId.get(
84 signatureAlgorithm);
85 AlgorithmId digEncrAlg = AlgorithmId.get(
86 keyAlgorithm);
87 Signature sig = Signature.getInstance(
88 signatureAlgorithm);
89 sig.initSign(privateKey);
90 ByteArrayOutputStream baos = new ByteArrayOutputStream();
91 sfg.write(baos);
92 byte bytes[] = baos.toByteArray();
93 ContentInfo contentInfo;
94 if (externalSF)
95 contentInfo =
96 new ContentInfo(
97 ContentInfo.DATA_OID,
98 null);
99 else
100 contentInfo =
101 new ContentInfo(bytes);
102 sig.update(bytes);
103 byte signature[] = sig.sign();
104 SignerInfo signerInfo = new SignerInfo(
105 (X500Name) issuerName,
106 serial,
107 digestAlg,
108 digEncrAlg,
109 signature);
110 AlgorithmId algs[] = {
111 digestAlg
112 };
113 SignerInfo infos[] = {
114 signerInfo
115 };
116 block =
117 new PKCS7(algs,
118 contentInfo,
119 certChain,
120 infos);
121 }
122 }
123
124
125 public SignatureFile(MessageDigest digests[],
126 Manifest mf,
127 ManifestDigester md,
128 String baseName,
129 boolean signManifest) {
130 this.baseName = baseName;
131 String version = System.getProperty(
132 "java.version");
133 String javaVendor = System.getProperty(
134 "java.vendor");
135 sf = new Manifest();
136 Attributes mattr = sf.getMainAttributes();
137 BASE64Encoder encoder = new BASE64Encoder();
138 mattr.putValue(
139 java.util.jar.Attributes.Name.SIGNATURE_VERSION.toString(),
140 "1.0");
141 mattr.putValue("Created-By",
142 version + " (" +
143 javaVendor +
144 ")");
145 if (signManifest) {
146 for (int i = 0; i < digests.length; i++)
147 mattr.putValue(
148 digests[i].getAlgorithm() +
149 "-Digest-Manifest",
150 encoder.encode(
151 md.manifestDigest(
152 digests[i])));
153
154 }
155 Map entries = sf.getEntries();
156 Iterator mit = mf.getEntries()
157 .entrySet()
158 .iterator();
159 do {
160 if (!mit.hasNext())
161 break;
162 java.util.Map.Entry e = (java.util.Map.Entry) mit.next();
163 String name = (String) e.getKey();
164 ManifestDigester.Entry mde = md.get(
165 name, false);
166 if (mde != null) {
167 Attributes attr = new Attributes();
168 for (int i = 0; i <
169 digests.length; i++)
170 attr.putValue(
171 digests[i].getAlgorithm() +
172 "-Digest",
173 encoder.encode(
174 mde.digest(
175 digests[i])));
176
177 entries.put(name, attr);
178 }
179 } while (true);
180 }
181
182 public void write(OutputStream out)
183 throws IOException {
184 sf.write(out);
185 }
186
187 public String getMetaName() {
188 return "META-INF/" + baseName + ".SF";
189 }
190
191 public String getBaseName() {
192 return baseName;
193 }
194
195 public Block generateBlock(
196 PrivateKey privateKey,
197 X509Certificate certChain[],
198 boolean externalSF)
199 throws NoSuchAlgorithmException,
200 InvalidKeyException,
201 IOException,
202 SignatureException,
203 CertificateException {
204 return new Block(this,
205 privateKey,
206 certChain,
207 externalSF);
208 }
209
210 Manifest sf;
211 String baseName;
212 }
213