01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.crypto.asn1;
17:
18: import java.io.ByteArrayOutputStream;
19: import java.io.IOException;
20:
21: /**
22: * DER TaggedObject - in ASN.1 nottation this is any object proceeded by
23: * a [n] where n is some number - these are assume to follow the construction
24: * rules (as with sequences).
25: */
26: public class DERTaggedObject extends ASN1TaggedObject {
27: /**
28: * @param tagNo the tag number for this object.
29: * @param obj the tagged object.
30: */
31: public DERTaggedObject(int tagNo, DEREncodable obj) {
32: super (tagNo, obj);
33: }
34:
35: /**
36: * @param explicit true if an explicitly tagged object.
37: * @param tagNo the tag number for this object.
38: * @param obj the tagged object.
39: */
40: public DERTaggedObject(boolean explicit, int tagNo, DEREncodable obj) {
41: super (explicit, tagNo, obj);
42: }
43:
44: /**
45: * create an implicitly tagged object that contains a zero
46: * length sequence.
47: */
48: public DERTaggedObject(int tagNo) {
49: super (false, tagNo, new DERSequence());
50: }
51:
52: void encode(DEROutputStream out) throws IOException {
53: if (!empty) {
54: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
55: DEROutputStream dOut = new DEROutputStream(bOut);
56:
57: dOut.writeObject(obj);
58: dOut.close();
59:
60: byte[] bytes = bOut.toByteArray();
61:
62: if (explicit) {
63: out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bytes);
64: } else {
65: //
66: // need to mark constructed types...
67: //
68: if ((bytes[0] & CONSTRUCTED) != 0) {
69: bytes[0] = (byte) (CONSTRUCTED | TAGGED | tagNo);
70: } else {
71: bytes[0] = (byte) (TAGGED | tagNo);
72: }
73:
74: out.write(bytes);
75: }
76: } else {
77: out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
78: }
79: }
80: }
|