History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/x509/AlgorithmId.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/x509/AlgorithmId.java+943−0
@@ -0,0 +1,943 @@
1+/*
2+ * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package sun.security.x509;
27+
28+import java.io.*;
29+import java.util.*;
30+import java.security.*;
31+
32+import sun.security.util.*;
33+
34+
35+/**
36+ * This class identifies algorithms, such as cryptographic transforms, each
37+ * of which may be associated with parameters. Instances of this base class
38+ * are used when this runtime environment has no special knowledge of the
39+ * algorithm type, and may also be used in other cases. Equivalence is
40+ * defined according to OID and (where relevant) parameters.
41+ *
42+ * <P>Subclasses may be used, for example when when the algorithm ID has
43+ * associated parameters which some code (e.g. code using public keys) needs
44+ * to have parsed. Two examples of such algorithms are Diffie-Hellman key
45+ * exchange, and the Digital Signature Standard Algorithm (DSS/DSA).
46+ *
47+ * <P>The OID constants defined in this class correspond to some widely
48+ * used algorithms, for which conventional string names have been defined.
49+ * This class is not a general repository for OIDs, or for such string names.
50+ * Note that the mappings between algorithm IDs and algorithm names is
51+ * not one-to-one.
52+ *
53+ *
54+ * @author David Brownell
55+ * @author Amit Kapoor
56+ * @author Hemma Prafullchandra
57+ */
58+public class AlgorithmId implements Serializable, DerEncoder {
59+
60+ /** use serialVersionUID from JDK 1.1. for interoperability */
61+ private static final long serialVersionUID = 7205873507486557157L;
62+
63+ /**
64+ * The object identitifer being used for this algorithm.
65+ */
66+ private ObjectIdentifier algid;
67+
68+ // The (parsed) parameters
69+ private AlgorithmParameters algParams;
70+ private boolean constructedFromDer = true;
71+
72+ /**
73+ * Parameters for this algorithm. These are stored in unparsed
74+ * DER-encoded form; subclasses can be made to automaticaly parse
75+ * them so there is fast access to these parameters.
76+ */
77+ protected DerValue params;
78+
79+
80+ /**
81+ * Constructs an algorithm ID which will be initialized
82+ * separately, for example by deserialization.
83+ * @deprecated use one of the other constructors.
84+ */
85+ @Deprecated
86+ public AlgorithmId() { }
87+
88+ /**
89+ * Constructs a parameterless algorithm ID.
90+ *
91+ * @param oid the identifier for the algorithm
92+ */
93+ public AlgorithmId(ObjectIdentifier oid) {
94+ algid = oid;
95+ }
96+
97+ /**
98+ * Constructs an algorithm ID with algorithm parameters.
99+ *
100+ * @param oid the identifier for the algorithm.
101+ * @param algparams the associated algorithm parameters.
102+ */
103+ public AlgorithmId(ObjectIdentifier oid, AlgorithmParameters algparams) {
104+ algid = oid;
105+ algParams = algparams;
106+ constructedFromDer = false;
107+ }
108+
109+ private AlgorithmId(ObjectIdentifier oid, DerValue params)
110+ throws IOException {
111+ this.algid = oid;
112+ this.params = params;
113+ if (this.params != null) {
114+ decodeParams();
115+ }
116+ }
117+
118+ protected void decodeParams() throws IOException {
119+ String algidString = algid.toString();
120+ try {
121+ algParams = AlgorithmParameters.getInstance(algidString);
122+ } catch (NoSuchAlgorithmException e) {
123+ try {
124+ // Try the internal EC code so that we can fully parse EC
125+ // keys even if the provider is not registered.
126+ // This code can go away once we have EC in the SUN provider.
127+ algParams = AlgorithmParameters.getInstance(algidString,
128+ sun.security.ec.ECKeyFactory.ecInternalProvider);
129+ } catch (NoSuchAlgorithmException ee) {
130+ /*
131+ * This algorithm parameter type is not supported, so we cannot
132+ * parse the parameters.
133+ */
134+ algParams = null;
135+ return;
136+ }
137+ }
138+ // Decode (parse) the parameters
139+ algParams.init(params.toByteArray());
140+ }
141+
142+ /**
143+ * Marshal a DER-encoded "AlgorithmID" sequence on the DER stream.
144+ */
145+ public final void encode(DerOutputStream out) throws IOException {
146+ derEncode(out);
147+ }
148+
149+ /**
150+ * DER encode this object onto an output stream.
151+ * Implements the <code>DerEncoder</code> interface.
152+ *
153+ * @param out
154+ * the output stream on which to write the DER encoding.
155+ *
156+ * @exception IOException on encoding error.
157+ */
158+ public void derEncode (OutputStream out) throws IOException {
159+ DerOutputStream bytes = new DerOutputStream();
160+ DerOutputStream tmp = new DerOutputStream();
161+
162+ bytes.putOID(algid);
163+ // Setup params from algParams since no DER encoding is given
164+ if (constructedFromDer == false) {
165+ if (algParams != null) {
166+ params = new DerValue(algParams.getEncoded());
167+ } else {
168+ params = null;
169+ }
170+ }
171+ if (params == null) {
172+ // Changes backed out for compatibility with Solaris
173+
174+ // Several AlgorithmId should omit the whole parameter part when
175+ // it's NULL. They are ---
176+ // rfc3370 2.1: Implementations SHOULD generate SHA-1
177+ // AlgorithmIdentifiers with absent parameters.
178+ // rfc3447 C1: When id-sha1, id-sha256, id-sha384 and id-sha512
179+ // are used in an AlgorithmIdentifier the parameters (which are
180+ // optional) SHOULD be omitted.
181+ // rfc3279 2.3.2: The id-dsa algorithm syntax includes optional
182+ // domain parameters... When omitted, the parameters component
183+ // MUST be omitted entirely
184+ // rfc3370 3.1: When the id-dsa-with-sha1 algorithm identifier
185+ // is used, the AlgorithmIdentifier parameters field MUST be absent.
186+ /*if (
187+ algid.equals((Object)SHA_oid) ||
188+ algid.equals((Object)SHA256_oid) ||
189+ algid.equals((Object)SHA384_oid) ||
190+ algid.equals((Object)SHA512_oid) ||
191+ algid.equals((Object)DSA_oid) ||
192+ algid.equals((Object)sha1WithDSA_oid)) {
193+ ; // no parameter part encoded
194+ } else {
195+ bytes.putNull();
196+ }*/
197+ bytes.putNull();
198+ } else {
199+ bytes.putDerValue(params);
200+ }
201+ tmp.write(DerValue.tag_Sequence, bytes);
202+ out.write(tmp.toByteArray());
203+ }
204+
205+
206+ /**
207+ * Returns the DER-encoded X.509 AlgorithmId as a byte array.
208+ */
209+ public final byte[] encode() throws IOException {
210+ DerOutputStream out = new DerOutputStream();
211+ derEncode(out);
212+ return out.toByteArray();
213+ }
214+
215+ /**
216+ * Returns the ISO OID for this algorithm. This is usually converted
217+ * to a string and used as part of an algorithm name, for example
218+ * "OID.1.3.14.3.2.13" style notation. Use the <code>getName</code>
219+ * call when you do not need to ensure cross-system portability
220+ * of algorithm names, or need a user friendly name.
221+ */
222+ public final ObjectIdentifier getOID () {
223+ return algid;
224+ }
225+
226+ /**
227+ * Returns a name for the algorithm which may be more intelligible
228+ * to humans than the algorithm's OID, but which won't necessarily
229+ * be comprehensible on other systems. For example, this might
230+ * return a name such as "MD5withRSA" for a signature algorithm on
231+ * some systems. It also returns names like "OID.1.2.3.4", when
232+ * no particular name for the algorithm is known.
233+ */
234+ public String getName() {
235+ String algName = nameTable.get(algid);
236+ if (algName != null) {
237+ return algName;
238+ }
239+ if ((params != null) && algid.equals(specifiedWithECDSA_oid)) {
240+ try {
241+ AlgorithmId paramsId =
242+ AlgorithmId.parse(new DerValue(getEncodedParams()));
243+ String paramsName = paramsId.getName();
244+ if (paramsName.equals("SHA")) {
245+ paramsName = "SHA1";
246+ }
247+ algName = paramsName + "withECDSA";
248+ } catch (IOException e) {
249+ // ignore
250+ }
251+ }
252+ return (algName == null) ? algid.toString() : algName;
253+ }
254+
255+ public AlgorithmParameters getParameters() {
256+ return algParams;
257+ }
258+
259+ /**
260+ * Returns the DER encoded parameter, which can then be
261+ * used to initialize java.security.AlgorithmParamters.
262+ *
263+ * @return DER encoded parameters, or null not present.
264+ */
265+ public byte[] getEncodedParams() throws IOException {
266+ return (params == null) ? null : params.toByteArray();
267+ }
268+
269+ /**
270+ * Returns true iff the argument indicates the same algorithm
271+ * with the same parameters.
272+ */
273+ public boolean equals(AlgorithmId other) {
274+ boolean paramsEqual =
275+ (params == null ? other.params == null : params.equals(other.params));
276+ return (algid.equals(other.algid) && paramsEqual);
277+ }
278+
279+ /**
280+ * Compares this AlgorithmID to another. If algorithm parameters are
281+ * available, they are compared. Otherwise, just the object IDs
282+ * for the algorithm are compared.
283+ *
284+ * @param other preferably an AlgorithmId, else an ObjectIdentifier
285+ */
286+ public boolean equals(Object other) {
287+ if (this == other) {
288+ return true;
289+ }
290+ if (other instanceof AlgorithmId) {
291+ return equals((AlgorithmId) other);
292+ } else if (other instanceof ObjectIdentifier) {
293+ return equals((ObjectIdentifier) other);
294+ } else {
295+ return false;
296+ }
297+ }
298+
299+ /**
300+ * Compares two algorithm IDs for equality. Returns true iff
301+ * they are the same algorithm, ignoring algorithm parameters.
302+ */
303+ public final boolean equals(ObjectIdentifier id) {
304+ return algid.equals(id);
305+ }
306+
307+ /**
308+ * Returns a hashcode for this AlgorithmId.
309+ *
310+ * @return a hashcode for this AlgorithmId.
311+ */
312+ public int hashCode() {
313+ StringBuilder sbuf = new StringBuilder();
314+ sbuf.append(algid.toString());
315+ sbuf.append(paramsToString());
316+ return sbuf.toString().hashCode();
317+ }
318+
319+ /**
320+ * Provides a human-readable description of the algorithm parameters.
321+ * This may be redefined by subclasses which parse those parameters.
322+ */
323+ protected String paramsToString() {
324+ if (params == null) {
325+ return "";
326+ } else if (algParams != null) {
327+ return algParams.toString();
328+ } else {
329+ return ", params unparsed";
330+ }
331+ }
332+
333+ /**
334+ * Returns a string describing the algorithm and its parameters.
335+ */
336+ public String toString() {
337+ return getName() + paramsToString();
338+ }
339+
340+ /**
341+ * Parse (unmarshal) an ID from a DER sequence input value. This form
342+ * parsing might be used when expanding a value which has already been
343+ * partially unmarshaled as a set or sequence member.
344+ *
345+ * @exception IOException on error.
346+ * @param val the input value, which contains the algid and, if
347+ * there are any parameters, those parameters.
348+ * @return an ID for the algorithm. If the system is configured
349+ * appropriately, this may be an instance of a class
350+ * with some kind of special support for this algorithm.
351+ * In that case, you may "narrow" the type of the ID.
352+ */
353+ public static AlgorithmId parse(DerValue val) throws IOException {
354+ if (val.tag != DerValue.tag_Sequence) {
355+ throw new IOException("algid parse error, not a sequence");
356+ }
357+
358+ /*
359+ * Get the algorithm ID and any parameters.
360+ */
361+ ObjectIdentifier algid;
362+ DerValue params;
363+ DerInputStream in = val.toDerInputStream();
364+
365+ algid = in.getOID();
366+ if (in.available() == 0) {
367+ params = null;
368+ } else {
369+ params = in.getDerValue();
370+ if (params.tag == DerValue.tag_Null) {
371+ if (params.length() != 0) {
372+ throw new IOException("invalid NULL");
373+ }
374+ params = null;
375+ }
376+ if (in.available() != 0) {
377+ throw new IOException("Invalid AlgorithmIdentifier: extra data");
378+ }
379+ }
380+
381+ return new AlgorithmId(algid, params);
382+ }
383+
384+ /**
385+ * Returns one of the algorithm IDs most commonly associated
386+ * with this algorithm name.
387+ *
388+ * @param algname the name being used
389+ * @deprecated use the short get form of this method.
390+ * @exception NoSuchAlgorithmException on error.
391+ */
392+ @Deprecated
393+ public static AlgorithmId getAlgorithmId(String algname)
394+ throws NoSuchAlgorithmException {
395+ return get(algname);
396+ }
397+
398+ /**
399+ * Returns one of the algorithm IDs most commonly associated
400+ * with this algorithm name.
401+ *
402+ * @param algname the name being used
403+ * @exception NoSuchAlgorithmException on error.
404+ */
405+ public static AlgorithmId get(String algname)
406+ throws NoSuchAlgorithmException {
407+ ObjectIdentifier oid;
408+ try {
409+ oid = algOID(algname);
410+ } catch (IOException ioe) {
411+ throw new NoSuchAlgorithmException
412+ ("Invalid ObjectIdentifier " + algname);
413+ }
414+
415+ if (oid == null) {
416+ throw new NoSuchAlgorithmException
417+ ("unrecognized algorithm name: " + algname);
418+ }
419+ return new AlgorithmId(oid);
420+ }
421+
422+ /**
423+ * Returns one of the algorithm IDs most commonly associated
424+ * with this algorithm parameters.
425+ *
426+ * @param algparams the associated algorithm parameters.
427+ * @exception NoSuchAlgorithmException on error.
428+ */
429+ public static AlgorithmId get(AlgorithmParameters algparams)
430+ throws NoSuchAlgorithmException {
431+ ObjectIdentifier oid;
432+ String algname = algparams.getAlgorithm();
433+ try {
434+ oid = algOID(algname);
435+ } catch (IOException ioe) {
436+ throw new NoSuchAlgorithmException
437+ ("Invalid ObjectIdentifier " + algname);
438+ }
439+ if (oid == null) {
440+ throw new NoSuchAlgorithmException
441+ ("unrecognized algorithm name: " + algname);
442+ }
443+ return new AlgorithmId(oid, algparams);
444+ }
445+
446+ /*
447+ * Translates from some common algorithm names to the
448+ * OID with which they're usually associated ... this mapping
449+ * is the reverse of the one below, except in those cases
450+ * where synonyms are supported or where a given algorithm
451+ * is commonly associated with multiple OIDs.
452+ *
453+ * XXX This method needs to be enhanced so that we can also pass the
454+ * scope of the algorithm name to it, e.g., the algorithm name "DSA"
455+ * may have a different OID when used as a "Signature" algorithm than when
456+ * used as a "KeyPairGenerator" algorithm.
457+ */
458+ private static ObjectIdentifier algOID(String name) throws IOException {
459+ // See if algname is in printable OID ("dot-dot") notation
460+ if (name.indexOf('.') != -1) {
461+ if (name.startsWith("OID.")) {
462+ return new ObjectIdentifier(name.substring("OID.".length()));
463+ } else {
464+ return new ObjectIdentifier(name);
465+ }
466+ }
467+
468+ // Digesting algorithms
469+ if (name.equalsIgnoreCase("MD5")) {
470+ return AlgorithmId.MD5_oid;
471+ }
472+ if (name.equalsIgnoreCase("MD2")) {
473+ return AlgorithmId.MD2_oid;
474+ }
475+ if (name.equalsIgnoreCase("SHA") || name.equalsIgnoreCase("SHA1")
476+ || name.equalsIgnoreCase("SHA-1")) {
477+ return AlgorithmId.SHA_oid;
478+ }
479+ if (name.equalsIgnoreCase("SHA-256") ||
480+ name.equalsIgnoreCase("SHA256")) {
481+ return AlgorithmId.SHA256_oid;
482+ }
483+ if (name.equalsIgnoreCase("SHA-384") ||
484+ name.equalsIgnoreCase("SHA384")) {
485+ return AlgorithmId.SHA384_oid;
486+ }
487+ if (name.equalsIgnoreCase("SHA-512") ||
488+ name.equalsIgnoreCase("SHA512")) {
489+ return AlgorithmId.SHA512_oid;
490+ }
491+
492+
493+ // Various public key algorithms
494+ if (name.equalsIgnoreCase("RSA")) {
495+ return AlgorithmId.RSAEncryption_oid;
496+ }
497+ if (name.equalsIgnoreCase("Diffie-Hellman")
498+ || name.equalsIgnoreCase("DH")) {
499+ return AlgorithmId.DH_oid;
500+ }
501+ if (name.equalsIgnoreCase("DSA")) {
502+ return AlgorithmId.DSA_oid;
503+ }
504+ if (name.equalsIgnoreCase("EC")) {
505+ return EC_oid;
506+ }
507+
508+ // Common signature types
509+ if (name.equalsIgnoreCase("MD5withRSA")
510+ || name.equalsIgnoreCase("MD5/RSA")) {
511+ return AlgorithmId.md5WithRSAEncryption_oid;
512+ }
513+ if (name.equalsIgnoreCase("MD2withRSA")
514+ || name.equalsIgnoreCase("MD2/RSA")) {
515+ return AlgorithmId.md2WithRSAEncryption_oid;
516+ }
517+ if (name.equalsIgnoreCase("SHAwithDSA")
518+ || name.equalsIgnoreCase("SHA1withDSA")
519+ || name.equalsIgnoreCase("SHA/DSA")
520+ || name.equalsIgnoreCase("SHA1/DSA")
521+ || name.equalsIgnoreCase("DSAWithSHA1")
522+ || name.equalsIgnoreCase("DSS")
523+ || name.equalsIgnoreCase("SHA-1/DSA")) {
524+ return AlgorithmId.sha1WithDSA_oid;
525+ }
526+ if (name.equalsIgnoreCase("SHA1WithRSA")
527+ || name.equalsIgnoreCase("SHA1/RSA")) {
528+ return AlgorithmId.sha1WithRSAEncryption_oid;
529+ }
530+ if (name.equalsIgnoreCase("SHA1withECDSA")
531+ || name.equalsIgnoreCase("ECDSA")) {
532+ return AlgorithmId.sha1WithECDSA_oid;
533+ }
534+ if (name.equalsIgnoreCase("SHA224withECDSA")) {
535+ return AlgorithmId.sha224WithECDSA_oid;
536+ }
537+ if (name.equalsIgnoreCase("SHA256withECDSA")) {
538+ return AlgorithmId.sha256WithECDSA_oid;
539+ }
540+ if (name.equalsIgnoreCase("SHA384withECDSA")) {
541+ return AlgorithmId.sha384WithECDSA_oid;
542+ }
543+ if (name.equalsIgnoreCase("SHA512withECDSA")) {
544+ return AlgorithmId.sha512WithECDSA_oid;
545+ }
546+
547+ // See if any of the installed providers supply a mapping from
548+ // the given algorithm name to an OID string
549+ String oidString;
550+ if (!initOidTable) {
551+ Provider[] provs = Security.getProviders();
552+ for (int i=0; i<provs.length; i++) {
553+ for (Enumeration<Object> enum_ = provs[i].keys();
554+ enum_.hasMoreElements(); ) {
555+ String alias = (String)enum_.nextElement();
556+ String upperCaseAlias = alias.toUpperCase(Locale.ENGLISH);
557+ int index;
558+ if (upperCaseAlias.startsWith("ALG.ALIAS") &&
559+ (index=upperCaseAlias.indexOf("OID.", 0)) != -1) {
560+ index += "OID.".length();
561+ if (index == alias.length()) {
562+ // invalid alias entry
563+ break;
564+ }
565+ if (oidTable == null) {
566+ oidTable = new HashMap<String,ObjectIdentifier>();
567+ }
568+ oidString = alias.substring(index);
569+ String stdAlgName = provs[i].getProperty(alias);
570+ if (stdAlgName != null) {
571+ stdAlgName = stdAlgName.toUpperCase(Locale.ENGLISH);
572+ }
573+ if (stdAlgName != null &&
574+ oidTable.get(stdAlgName) == null) {
575+ oidTable.put(stdAlgName,
576+ new ObjectIdentifier(oidString));
577+ }
578+ }
579+ }
580+ }
581+
582+ if (oidTable == null) {
583+ oidTable = new HashMap<String,ObjectIdentifier>(1);
584+ }
585+ initOidTable = true;
586+ }
587+
588+ return oidTable.get(name.toUpperCase(Locale.ENGLISH));
589+ }
590+
591+ private static ObjectIdentifier oid(int ... values) {
592+ return ObjectIdentifier.newInternal(values);
593+ }
594+
595+ private static boolean initOidTable = false;
596+ private static Map<String,ObjectIdentifier> oidTable;
597+ private static final Map<ObjectIdentifier,String> nameTable;
598+
599+ /*****************************************************************/
600+
601+ /*
602+ * HASHING ALGORITHMS
603+ */
604+
605+ /**
606+ * Algorithm ID for the MD2 Message Digest Algorthm, from RFC 1319.
607+ * OID = 1.2.840.113549.2.2
608+ */
609+ public static final ObjectIdentifier MD2_oid =
610+ ObjectIdentifier.newInternal(new int[] {1, 2, 840, 113549, 2, 2});
611+
612+ /**
613+ * Algorithm ID for the MD5 Message Digest Algorthm, from RFC 1321.
614+ * OID = 1.2.840.113549.2.5
615+ */
616+ public static final ObjectIdentifier MD5_oid =
617+ ObjectIdentifier.newInternal(new int[] {1, 2, 840, 113549, 2, 5});
618+
619+ /**
620+ * Algorithm ID for the SHA1 Message Digest Algorithm, from FIPS 180-1.
621+ * This is sometimes called "SHA", though that is often confusing since
622+ * many people refer to FIPS 180 (which has an error) as defining SHA.
623+ * OID = 1.3.14.3.2.26. Old SHA-0 OID: 1.3.14.3.2.18.
624+ */
625+ public static final ObjectIdentifier SHA_oid =
626+ ObjectIdentifier.newInternal(new int[] {1, 3, 14, 3, 2, 26});
627+
628+ public static final ObjectIdentifier SHA256_oid =
629+ ObjectIdentifier.newInternal(new int[] {2, 16, 840, 1, 101, 3, 4, 2, 1});
630+
631+ public static final ObjectIdentifier SHA384_oid =
632+ ObjectIdentifier.newInternal(new int[] {2, 16, 840, 1, 101, 3, 4, 2, 2});
633+
634+ public static final ObjectIdentifier SHA512_oid =
635+ ObjectIdentifier.newInternal(new int[] {2, 16, 840, 1, 101, 3, 4, 2, 3});
636+
637+ /*
638+ * COMMON PUBLIC KEY TYPES
639+ */
640+ private static final int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 };
641+ private static final int DH_PKIX_data[] = { 1, 2, 840, 10046, 2, 1 };
642+ private static final int DSA_OIW_data[] = { 1, 3, 14, 3, 2, 12 };
643+ private static final int DSA_PKIX_data[] = { 1, 2, 840, 10040, 4, 1 };
644+ private static final int RSA_data[] = { 2, 5, 8, 1, 1 };
645+ private static final int RSAEncryption_data[] =
646+ { 1, 2, 840, 113549, 1, 1, 1 };
647+
648+ public static final ObjectIdentifier DH_oid;
649+ public static final ObjectIdentifier DH_PKIX_oid;
650+ public static final ObjectIdentifier DSA_oid;
651+ public static final ObjectIdentifier DSA_OIW_oid;
652+ public static final ObjectIdentifier EC_oid = oid(1, 2, 840, 10045, 2, 1);
653+ public static final ObjectIdentifier RSA_oid;
654+ public static final ObjectIdentifier RSAEncryption_oid;
655+
656+ /*
657+ * COMMON SIGNATURE ALGORITHMS
658+ */
659+ private static final int md2WithRSAEncryption_data[] =
660+ { 1, 2, 840, 113549, 1, 1, 2 };
661+ private static final int md5WithRSAEncryption_data[] =
662+ { 1, 2, 840, 113549, 1, 1, 4 };
663+ private static final int sha1WithRSAEncryption_data[] =
664+ { 1, 2, 840, 113549, 1, 1, 5 };
665+ private static final int sha1WithRSAEncryption_OIW_data[] =
666+ { 1, 3, 14, 3, 2, 29 };
667+ private static final int sha256WithRSAEncryption_data[] =
668+ { 1, 2, 840, 113549, 1, 1, 11 };
669+ private static final int sha384WithRSAEncryption_data[] =
670+ { 1, 2, 840, 113549, 1, 1, 12 };
671+ private static final int sha512WithRSAEncryption_data[] =
672+ { 1, 2, 840, 113549, 1, 1, 13 };
673+ private static final int shaWithDSA_OIW_data[] =
674+ { 1, 3, 14, 3, 2, 13 };
675+ private static final int sha1WithDSA_OIW_data[] =
676+ { 1, 3, 14, 3, 2, 27 };
677+ private static final int dsaWithSHA1_PKIX_data[] =
678+ { 1, 2, 840, 10040, 4, 3 };
679+
680+ public static final ObjectIdentifier md2WithRSAEncryption_oid;
681+ public static final ObjectIdentifier md5WithRSAEncryption_oid;
682+ public static final ObjectIdentifier sha1WithRSAEncryption_oid;
683+ public static final ObjectIdentifier sha1WithRSAEncryption_OIW_oid;
684+ public static final ObjectIdentifier sha256WithRSAEncryption_oid;
685+ public static final ObjectIdentifier sha384WithRSAEncryption_oid;
686+ public static final ObjectIdentifier sha512WithRSAEncryption_oid;
687+ public static final ObjectIdentifier shaWithDSA_OIW_oid;
688+ public static final ObjectIdentifier sha1WithDSA_OIW_oid;
689+ public static final ObjectIdentifier sha1WithDSA_oid;
690+
691+ public static final ObjectIdentifier sha1WithECDSA_oid =
692+ oid(1, 2, 840, 10045, 4, 1);
693+ public static final ObjectIdentifier sha224WithECDSA_oid =
694+ oid(1, 2, 840, 10045, 4, 3, 1);
695+ public static final ObjectIdentifier sha256WithECDSA_oid =
696+ oid(1, 2, 840, 10045, 4, 3, 2);
697+ public static final ObjectIdentifier sha384WithECDSA_oid =
698+ oid(1, 2, 840, 10045, 4, 3, 3);
699+ public static final ObjectIdentifier sha512WithECDSA_oid =
700+ oid(1, 2, 840, 10045, 4, 3, 4);
701+ public static final ObjectIdentifier specifiedWithECDSA_oid =
702+ oid(1, 2, 840, 10045, 4, 3);
703+
704+ /**
705+ * Algorithm ID for the PBE encryption algorithms from PKCS#5 and
706+ * PKCS#12.
707+ */
708+ public static final ObjectIdentifier pbeWithMD5AndDES_oid =
709+ ObjectIdentifier.newInternal(new int[]{1, 2, 840, 113549, 1, 5, 3});
710+ public static final ObjectIdentifier pbeWithMD5AndRC2_oid =
711+ ObjectIdentifier.newInternal(new int[] {1, 2, 840, 113549, 1, 5, 6});
712+ public static final ObjectIdentifier pbeWithSHA1AndDES_oid =
713+ ObjectIdentifier.newInternal(new int[] {1, 2, 840, 113549, 1, 5, 10});
714+ public static final ObjectIdentifier pbeWithSHA1AndRC2_oid =
715+ ObjectIdentifier.newInternal(new int[] {1, 2, 840, 113549, 1, 5, 11});
716+ public static ObjectIdentifier pbeWithSHA1AndDESede_oid =
717+ ObjectIdentifier.newInternal(new int[] {1, 2, 840, 113549, 1, 12, 1, 3});
718+ public static ObjectIdentifier pbeWithSHA1AndRC2_40_oid =
719+ ObjectIdentifier.newInternal(new int[] {1, 2, 840, 113549, 1, 12, 1, 6});
720+
721+
722+ static {
723+ /*
724+ * Note the preferred OIDs are named simply with no "OIW" or
725+ * "PKIX" in them, even though they may point to data from these
726+ * specs; e.g. SHA_oid, DH_oid, DSA_oid, SHA1WithDSA_oid...
727+ */
728+ /**
729+ * Algorithm ID for Diffie Hellman Key agreement, from PKCS #3.
730+ * Parameters include public values P and G, and may optionally specify
731+ * the length of the private key X. Alternatively, algorithm parameters
732+ * may be derived from another source such as a Certificate Authority's
733+ * certificate.
734+ * OID = 1.2.840.113549.1.3.1
735+ */
736+ DH_oid = ObjectIdentifier.newInternal(DH_data);
737+
738+ /**
739+ * Algorithm ID for the Diffie Hellman Key Agreement (DH), from RFC 3279.
740+ * Parameters may include public values P and G.
741+ * OID = 1.2.840.10046.2.1
742+ */
743+ DH_PKIX_oid = ObjectIdentifier.newInternal(DH_PKIX_data);
744+
745+ /**
746+ * Algorithm ID for the Digital Signing Algorithm (DSA), from the
747+ * NIST OIW Stable Agreements part 12.
748+ * Parameters may include public values P, Q, and G; or these may be
749+ * derived from
750+ * another source such as a Certificate Authority's certificate.
751+ * OID = 1.3.14.3.2.12
752+ */
753+ DSA_OIW_oid = ObjectIdentifier.newInternal(DSA_OIW_data);
754+
755+ /**
756+ * Algorithm ID for the Digital Signing Algorithm (DSA), from RFC 3279.
757+ * Parameters may include public values P, Q, and G; or these may be
758+ * derived from another source such as a Certificate Authority's
759+ * certificate.
760+ * OID = 1.2.840.10040.4.1
761+ */
762+ DSA_oid = ObjectIdentifier.newInternal(DSA_PKIX_data);
763+
764+ /**
765+ * Algorithm ID for RSA keys used for any purpose, as defined in X.509.
766+ * The algorithm parameter is a single value, the number of bits in the
767+ * public modulus.
768+ * OID = 2.5.8.1.1
769+ */
770+ RSA_oid = ObjectIdentifier.newInternal(RSA_data);
771+
772+ /**
773+ * Algorithm ID for RSA keys used with RSA encryption, as defined
774+ * in PKCS #1. There are no parameters associated with this algorithm.
775+ * OID = 1.2.840.113549.1.1.1
776+ */
777+ RSAEncryption_oid = ObjectIdentifier.newInternal(RSAEncryption_data);
778+
779+ /**
780+ * Identifies a signing algorithm where an MD2 digest is encrypted
781+ * using an RSA private key; defined in PKCS #1. Use of this
782+ * signing algorithm is discouraged due to MD2 vulnerabilities.
783+ * OID = 1.2.840.113549.1.1.2
784+ */
785+ md2WithRSAEncryption_oid =
786+ ObjectIdentifier.newInternal(md2WithRSAEncryption_data);
787+
788+ /**
789+ * Identifies a signing algorithm where an MD5 digest is
790+ * encrypted using an RSA private key; defined in PKCS #1.
791+ * OID = 1.2.840.113549.1.1.4
792+ */
793+ md5WithRSAEncryption_oid =
794+ ObjectIdentifier.newInternal(md5WithRSAEncryption_data);
795+
796+ /**
797+ * Identifies a signing algorithm where a SHA1 digest is
798+ * encrypted using an RSA private key; defined by RSA DSI.
799+ * OID = 1.2.840.113549.1.1.5
800+ */
801+ sha1WithRSAEncryption_oid =
802+ ObjectIdentifier.newInternal(sha1WithRSAEncryption_data);
803+
804+ /**
805+ * Identifies a signing algorithm where a SHA1 digest is
806+ * encrypted using an RSA private key; defined in NIST OIW.
807+ * OID = 1.3.14.3.2.29
808+ */
809+ sha1WithRSAEncryption_OIW_oid =
810+ ObjectIdentifier.newInternal(sha1WithRSAEncryption_OIW_data);
811+
812+ /**
813+ * Identifies a signing algorithm where a SHA256 digest is
814+ * encrypted using an RSA private key; defined by PKCS #1.
815+ * OID = 1.2.840.113549.1.1.11
816+ */
817+ sha256WithRSAEncryption_oid =
818+ ObjectIdentifier.newInternal(sha256WithRSAEncryption_data);
819+
820+ /**
821+ * Identifies a signing algorithm where a SHA384 digest is
822+ * encrypted using an RSA private key; defined by PKCS #1.
823+ * OID = 1.2.840.113549.1.1.12
824+ */
825+ sha384WithRSAEncryption_oid =
826+ ObjectIdentifier.newInternal(sha384WithRSAEncryption_data);
827+
828+ /**
829+ * Identifies a signing algorithm where a SHA512 digest is
830+ * encrypted using an RSA private key; defined by PKCS #1.
831+ * OID = 1.2.840.113549.1.1.13
832+ */
833+ sha512WithRSAEncryption_oid =
834+ ObjectIdentifier.newInternal(sha512WithRSAEncryption_data);
835+
836+ /**
837+ * Identifies the FIPS 186 "Digital Signature Standard" (DSS), where a
838+ * SHA digest is signed using the Digital Signing Algorithm (DSA).
839+ * This should not be used.
840+ * OID = 1.3.14.3.2.13
841+ */
842+ shaWithDSA_OIW_oid = ObjectIdentifier.newInternal(shaWithDSA_OIW_data);
843+
844+ /**
845+ * Identifies the FIPS 186 "Digital Signature Standard" (DSS), where a
846+ * SHA1 digest is signed using the Digital Signing Algorithm (DSA).
847+ * OID = 1.3.14.3.2.27
848+ */
849+ sha1WithDSA_OIW_oid = ObjectIdentifier.newInternal(sha1WithDSA_OIW_data);
850+
851+ /**
852+ * Identifies the FIPS 186 "Digital Signature Standard" (DSS), where a
853+ * SHA1 digest is signed using the Digital Signing Algorithm (DSA).
854+ * OID = 1.2.840.10040.4.3
855+ */
856+ sha1WithDSA_oid = ObjectIdentifier.newInternal(dsaWithSHA1_PKIX_data);
857+
858+ nameTable = new HashMap<ObjectIdentifier,String>();
859+ nameTable.put(MD5_oid, "MD5");
860+ nameTable.put(MD2_oid, "MD2");
861+ nameTable.put(SHA_oid, "SHA");
862+ nameTable.put(SHA256_oid, "SHA256");
863+ nameTable.put(SHA384_oid, "SHA384");
864+ nameTable.put(SHA512_oid, "SHA512");
865+ nameTable.put(RSAEncryption_oid, "RSA");
866+ nameTable.put(RSA_oid, "RSA");
867+ nameTable.put(DH_oid, "Diffie-Hellman");
868+ nameTable.put(DH_PKIX_oid, "Diffie-Hellman");
869+ nameTable.put(DSA_oid, "DSA");
870+ nameTable.put(DSA_OIW_oid, "DSA");
871+ nameTable.put(EC_oid, "EC");
872+ nameTable.put(sha1WithECDSA_oid, "SHA1withECDSA");
873+ nameTable.put(sha224WithECDSA_oid, "SHA224withECDSA");
874+ nameTable.put(sha256WithECDSA_oid, "SHA256withECDSA");
875+ nameTable.put(sha384WithECDSA_oid, "SHA384withECDSA");
876+ nameTable.put(sha512WithECDSA_oid, "SHA512withECDSA");
877+ nameTable.put(md5WithRSAEncryption_oid, "MD5withRSA");
878+ nameTable.put(md2WithRSAEncryption_oid, "MD2withRSA");
879+ nameTable.put(sha1WithDSA_oid, "SHA1withDSA");
880+ nameTable.put(sha1WithDSA_OIW_oid, "SHA1withDSA");
881+ nameTable.put(shaWithDSA_OIW_oid, "SHA1withDSA");
882+ nameTable.put(sha1WithRSAEncryption_oid, "SHA1withRSA");
883+ nameTable.put(sha1WithRSAEncryption_OIW_oid, "SHA1withRSA");
884+ nameTable.put(sha256WithRSAEncryption_oid, "SHA256withRSA");
885+ nameTable.put(sha384WithRSAEncryption_oid, "SHA384withRSA");
886+ nameTable.put(sha512WithRSAEncryption_oid, "SHA512withRSA");
887+ nameTable.put(pbeWithMD5AndDES_oid, "PBEWithMD5AndDES");
888+ nameTable.put(pbeWithMD5AndRC2_oid, "PBEWithMD5AndRC2");
889+ nameTable.put(pbeWithSHA1AndDES_oid, "PBEWithSHA1AndDES");
890+ nameTable.put(pbeWithSHA1AndRC2_oid, "PBEWithSHA1AndRC2");
891+ nameTable.put(pbeWithSHA1AndDESede_oid, "PBEWithSHA1AndDESede");
892+ nameTable.put(pbeWithSHA1AndRC2_40_oid, "PBEWithSHA1AndRC2_40");
893+ }
894+
895+ /**
896+ * Creates a signature algorithm name from a digest algorithm
897+ * name and a encryption algorithm name.
898+ */
899+ public static String makeSigAlg(String digAlg, String encAlg) {
900+ digAlg = digAlg.replace("-", "").toUpperCase(Locale.ENGLISH);
901+ if (digAlg.equalsIgnoreCase("SHA")) digAlg = "SHA1";
902+
903+ encAlg = encAlg.toUpperCase(Locale.ENGLISH);
904+ if (encAlg.equals("EC")) encAlg = "ECDSA";
905+
906+ return digAlg + "with" + encAlg;
907+ }
908+
909+ /**
910+ * Extracts the encryption algorithm name from a signature
911+ * algorithm name.
912+ */
913+ public static String getEncAlgFromSigAlg(String signatureAlgorithm) {
914+ signatureAlgorithm = signatureAlgorithm.toUpperCase(Locale.ENGLISH);
915+ int with = signatureAlgorithm.indexOf("WITH");
916+ String keyAlgorithm = null;
917+ if (with > 0) {
918+ int and = signatureAlgorithm.indexOf("AND", with + 4);
919+ if (and > 0) {
920+ keyAlgorithm = signatureAlgorithm.substring(with + 4, and);
921+ } else {
922+ keyAlgorithm = signatureAlgorithm.substring(with + 4);
923+ }
924+ if (keyAlgorithm.equalsIgnoreCase("ECDSA")) {
925+ keyAlgorithm = "EC";
926+ }
927+ }
928+ return keyAlgorithm;
929+ }
930+
931+ /**
932+ * Extracts the digest algorithm name from a signature
933+ * algorithm name.
934+ */
935+ public static String getDigAlgFromSigAlg(String signatureAlgorithm) {
936+ signatureAlgorithm = signatureAlgorithm.toUpperCase(Locale.ENGLISH);
937+ int with = signatureAlgorithm.indexOf("WITH");
938+ if (with > 0) {
939+ return signatureAlgorithm.substring(0, with);
940+ }
941+ return null;
942+ }
943+}