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: */
017:
018: /**
019: * @author Boris Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.security.pkcs7;
022:
023: import java.io.IOException;
024: import java.math.BigInteger;
025: import java.util.List;
026:
027: import javax.security.auth.x500.X500Principal;
028:
029: import org.apache.harmony.security.asn1.ASN1Implicit;
030: import org.apache.harmony.security.asn1.ASN1Integer;
031: import org.apache.harmony.security.asn1.ASN1OctetString;
032: import org.apache.harmony.security.asn1.ASN1Sequence;
033: import org.apache.harmony.security.asn1.ASN1SetOf;
034: import org.apache.harmony.security.asn1.ASN1Type;
035: import org.apache.harmony.security.asn1.BerInputStream;
036: import org.apache.harmony.security.internal.nls.Messages;
037: import org.apache.harmony.security.x501.AttributeTypeAndValue;
038: import org.apache.harmony.security.x501.Name;
039: import org.apache.harmony.security.x509.AlgorithmIdentifier;
040:
041: /**
042: * As defined in PKCS #7: Cryptographic Message Syntax Standard
043: * (http://www.ietf.org/rfc/rfc2315.txt)
044: *
045: * SignerInfo ::= SEQUENCE {
046: * version Version,
047: * issuerAndSerialNumber IssuerAndSerialNumber,
048: * digestAlgorithm DigestAlgorithmIdentifier,
049: * authenticatedAttributes
050: * [0] IMPLICIT Attributes OPTIONAL,
051: * digestEncryptionAlgorithm
052: * DigestEncryptionAlgorithmIdentifier,
053: * encryptedDigest EncryptedDigest,
054: * unauthenticatedAttributes
055: * [1] IMPLICIT Attributes OPTIONAL
056: * }
057: *
058: */
059: public class SignerInfo {
060:
061: private int version;
062: private X500Principal issuer;
063: private BigInteger serialNumber;
064:
065: private AlgorithmIdentifier digestAlgorithm;
066: private AuthenticatedAttributes authenticatedAttributes;
067: private AlgorithmIdentifier digestEncryptionAlgorithm;
068: private byte[] encryptedDigest;
069: private List unauthenticatedAttributes;
070:
071: public SignerInfo(int version, Object[] issuerAndSerialNumber,
072: AlgorithmIdentifier digestAlgorithm,
073: AuthenticatedAttributes authenticatedAttributes,
074: AlgorithmIdentifier digestEncryptionAlgorithm,
075: byte[] encryptedDigest, List unauthenticatedAttributes) {
076: this .version = version;
077: this .issuer = ((Name) issuerAndSerialNumber[0])
078: .getX500Principal();
079: this .serialNumber = BigInteger.valueOf(ASN1Integer
080: .toIntValue(issuerAndSerialNumber[1]));
081: this .digestAlgorithm = digestAlgorithm;
082: this .authenticatedAttributes = authenticatedAttributes;
083: this .digestEncryptionAlgorithm = digestEncryptionAlgorithm;
084: this .encryptedDigest = encryptedDigest;
085: this .unauthenticatedAttributes = unauthenticatedAttributes;
086: }
087:
088: public X500Principal getIssuer() {
089: return issuer;
090: }
091:
092: public BigInteger getSerialNumber() {
093: return serialNumber;
094: }
095:
096: public String getDigestAlgorithm() {
097: return digestAlgorithm.getAlgorithm();
098: }
099:
100: public String getdigestAlgorithm() {
101: return digestAlgorithm.getAlgorithm();
102: }
103:
104: public String getDigestEncryptionAlgorithm() {
105: return digestEncryptionAlgorithm.getAlgorithm();
106: }
107:
108: public List getAuthenticatedAttributes() {
109: if (authenticatedAttributes == null) {
110: return null;
111: }
112: return authenticatedAttributes.getAttributes();
113: }
114:
115: public byte[] getEncodedAuthenticatedAttributes() {
116: if (authenticatedAttributes == null) {
117: return null;
118: }
119: return authenticatedAttributes.getEncoded();
120: }
121:
122: public byte[] getEncryptedDigest() {
123: return encryptedDigest;
124: }
125:
126: public String toString() {
127: StringBuffer res = new StringBuffer();
128: res.append("-- SignerInfo:"); //$NON-NLS-1$
129: res.append("\n version : "); //$NON-NLS-1$
130: res.append(version);
131: res.append("\nissuerAndSerialNumber: "); //$NON-NLS-1$
132: res.append(issuer);
133: res.append(" "); //$NON-NLS-1$
134: res.append(serialNumber);
135: res.append("\ndigestAlgorithm: "); //$NON-NLS-1$
136: res.append(digestAlgorithm.toString());
137: res.append("\nauthenticatedAttributes: "); //$NON-NLS-1$
138: if (authenticatedAttributes != null) {
139: res.append(authenticatedAttributes.toString());
140: }
141: res.append("\ndigestEncryptionAlgorithm: "); //$NON-NLS-1$
142: res.append(digestEncryptionAlgorithm.toString());
143: res.append("\nunauthenticatedAttributes: "); //$NON-NLS-1$
144: if (unauthenticatedAttributes != null) {
145: res.append(unauthenticatedAttributes.toString());
146: }
147: res.append("\n-- SignerInfo End\n"); //$NON-NLS-1$
148: return res.toString();
149: }
150:
151: public static final ASN1Sequence ISSUER_AND_SERIAL_NUMBER = new ASN1Sequence(
152: new ASN1Type[] { Name.ASN1, // issuer
153: ASN1Integer.getInstance(), // serialNumber
154: }) {
155: // method to encode
156: public void getValues(Object object, Object[] values) {
157: Object[] issAndSerial = (Object[]) object;
158: values[0] = issAndSerial[0];
159: values[1] = issAndSerial[1];
160: }
161: };
162:
163: public static final ASN1Sequence ASN1 = new ASN1Sequence(
164: new ASN1Type[] {
165: ASN1Integer.getInstance(), //version
166: ISSUER_AND_SERIAL_NUMBER,
167: AlgorithmIdentifier.ASN1, //digestAlgorithm
168: new ASN1Implicit(0, AuthenticatedAttributes.ASN1),//authenticatedAttributes
169: AlgorithmIdentifier.ASN1, //digestEncryptionAlgorithm
170: ASN1OctetString.getInstance(), //encryptedDigest
171: new ASN1Implicit(1, new ASN1SetOf(
172: AttributeTypeAndValue.ASN1)),//unauthenticatedAttributes
173: }) {
174: {
175: setOptional(3); // authenticatedAttributes is optional
176: setOptional(6); // unauthenticatedAttributes is optional
177: }
178:
179: protected void getValues(Object object, Object[] values) {
180: SignerInfo si = (SignerInfo) object;
181: values[0] = new byte[] { (byte) si.version };
182: try {
183: values[1] = new Object[] {
184: new Name(si.issuer.getName()),
185: si.serialNumber.toByteArray() };
186: } catch (IOException e) {
187: // The exception is never thrown, because si.issuer
188: // is created using Name.getX500Principal().
189: // Throw a RuntimeException just to be safe.
190: throw new RuntimeException(
191: // Msg: "Failed to encode issuer name
192: Messages.getString("security.1A2"), e);
193: }
194: values[2] = si.digestAlgorithm;
195: values[3] = si.authenticatedAttributes;
196: values[4] = si.digestEncryptionAlgorithm;
197: values[5] = si.encryptedDigest;
198: values[6] = si.unauthenticatedAttributes;
199: }
200:
201: protected Object getDecodedObject(BerInputStream in) {
202: Object[] values = (Object[]) in.content;
203: return new SignerInfo(ASN1Integer.toIntValue(values[0]),
204: (Object[]) values[1],
205: (AlgorithmIdentifier) values[2],
206: (AuthenticatedAttributes) values[3],
207: (AlgorithmIdentifier) values[4],
208: (byte[]) values[5], (List) values[6]);
209: }
210: };
211: }
|