001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1.x509;
017:
018: import org.apache.geronimo.crypto.asn1.ASN1Encodable;
019: import org.apache.geronimo.crypto.asn1.ASN1EncodableVector;
020: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
021: import org.apache.geronimo.crypto.asn1.ASN1TaggedObject;
022: import org.apache.geronimo.crypto.asn1.DEREncodable;
023: import org.apache.geronimo.crypto.asn1.DERObject;
024: import org.apache.geronimo.crypto.asn1.DERObjectIdentifier;
025: import org.apache.geronimo.crypto.asn1.DERSequence;
026:
027: public class AlgorithmIdentifier extends ASN1Encodable {
028: private DERObjectIdentifier objectId;
029: private DEREncodable parameters;
030: private boolean parametersDefined = false;
031:
032: public static AlgorithmIdentifier getInstance(ASN1TaggedObject obj,
033: boolean explicit) {
034: return getInstance(ASN1Sequence.getInstance(obj, explicit));
035: }
036:
037: public static AlgorithmIdentifier getInstance(Object obj) {
038: if (obj instanceof AlgorithmIdentifier) {
039: return (AlgorithmIdentifier) obj;
040: }
041:
042: if (obj instanceof DERObjectIdentifier) {
043: return new AlgorithmIdentifier((DERObjectIdentifier) obj);
044: }
045:
046: if (obj instanceof String) {
047: return new AlgorithmIdentifier((String) obj);
048: }
049:
050: if (obj instanceof ASN1Sequence) {
051: return new AlgorithmIdentifier((ASN1Sequence) obj);
052: }
053:
054: throw new IllegalArgumentException("unknown object in factory");
055: }
056:
057: public AlgorithmIdentifier(DERObjectIdentifier objectId) {
058: this .objectId = objectId;
059: }
060:
061: public AlgorithmIdentifier(String objectId) {
062: this .objectId = new DERObjectIdentifier(objectId);
063: }
064:
065: public AlgorithmIdentifier(DERObjectIdentifier objectId,
066: DEREncodable parameters) {
067: parametersDefined = true;
068: this .objectId = objectId;
069: this .parameters = parameters;
070: }
071:
072: public AlgorithmIdentifier(ASN1Sequence seq) {
073: objectId = (DERObjectIdentifier) seq.getObjectAt(0);
074:
075: if (seq.size() == 2) {
076: parametersDefined = true;
077: parameters = seq.getObjectAt(1);
078: } else {
079: parameters = null;
080: }
081: }
082:
083: public DERObjectIdentifier getObjectId() {
084: return objectId;
085: }
086:
087: public DEREncodable getParameters() {
088: return parameters;
089: }
090:
091: /**
092: * Produce an object suitable for an ASN1OutputStream.
093: * <pre>
094: * AlgorithmIdentifier ::= SEQUENCE {
095: * algorithm OBJECT IDENTIFIER,
096: * parameters ANY DEFINED BY algorithm OPTIONAL }
097: * </pre>
098: */
099: public DERObject toASN1Object() {
100: ASN1EncodableVector v = new ASN1EncodableVector();
101:
102: v.add(objectId);
103:
104: if (parametersDefined) {
105: v.add(parameters);
106: }
107:
108: return new DERSequence(v);
109: }
110: }
|