1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package org.apache.harmony.security.x509.tsp; 21 22 import java.math.BigInteger; 23 import org.apache.harmony.security.asn1.ASN1Boolean; 24 import org.apache.harmony.security.asn1.ASN1Implicit; 25 import org.apache.harmony.security.asn1.ASN1Integer; 26 import org.apache.harmony.security.asn1.ASN1Oid; 27 import org.apache.harmony.security.asn1.ASN1Sequence; 28 import org.apache.harmony.security.asn1.ASN1Type; 29 import org.apache.harmony.security.asn1.BerInputStream; 30 import org.apache.harmony.security.asn1.ObjectIdentifier; 31 import org.apache.harmony.security.x509.Extensions; 32 33 /** 34 * As defined in Time-Stamp Protocol (TSP) 35 * (http://www.ietf.org/rfc/rfc3161.txt) 36 * 37 * TimeStampReq ::= SEQUENCE { 38 * version INTEGER { v1(1) }, 39 * messageImprint MessageImprint, 40 * --a hash algorithm OID and the hash value of the data to be 41 * --time-stamped 42 * reqPolicy TSAPolicyId OPTIONAL, 43 * nonce INTEGER OPTIONAL, 44 * certReq BOOLEAN DEFAULT FALSE, 45 * extensions [0] IMPLICIT Extensions OPTIONAL 46 * } 47 * 48 * TSAPolicyId ::= OBJECT IDENTIFIER 49 */ 50 public class TimeStampReq { 51 private final int version; 52 53 private final MessageImprint messageImprint; 54 55 private final String reqPolicy; 56 57 private final BigInteger nonce; 58 59 private final Boolean certReq; 60 61 private final Extensions extensions; 62 63 private byte [] encoding; 64 65 public TimeStampReq(int version, MessageImprint messageImprint, 66 String reqPolicy, BigInteger nonce, Boolean certReq, 67 Extensions extensions) { 68 this.version = version; 69 this.messageImprint = messageImprint; 70 this.reqPolicy = reqPolicy; 71 this.nonce = nonce; 72 this.certReq = certReq; 73 this.extensions = extensions; 74 } 75 76 private TimeStampReq(int version, MessageImprint messageImprint, 77 String reqPolicy, BigInteger nonce, Boolean certReq, 78 Extensions extensions, byte [] encoding) { 79 this (version, messageImprint, reqPolicy, nonce, certReq, extensions); 80 this.encoding = encoding; 81 } 82 83 public String toString() { 84 StringBuilder res = new StringBuilder(); 85 res.append("-- TimeStampReq:"); 86 res.append("\nversion : "); 87 res.append(version); 88 res.append("\nmessageImprint: "); 89 res.append(messageImprint); 90 res.append("\nreqPolicy: "); 91 res.append(reqPolicy); 92 res.append("\nnonce: "); 93 res.append(nonce); 94 res.append("\ncertReq: "); 95 res.append(certReq); 96 res.append("\nextensions: "); 97 res.append(extensions); 98 res.append("\n-- TimeStampReq End\n"); 99 return res.toString(); 100 } 101 102 /** 103 * Returns ASN.1 encoded form of this TimeStampReq. 104 * @return a byte array containing ASN.1 encoded form. 105 */ 106 public byte [] getEncoded(){ 107 if (encoding == null) { 108 encoding = ASN1.encode(this); 109 } 110 return encoding; 111 } 112 113 /** 114 * @return Returns the certReq. 115 */ 116 public Boolean getCertReq() { 117 return certReq; 118 } 119 120 /** 121 * @return Returns the extensions. 122 */ 123 public Extensions getExtensions() { 124 return extensions; 125 } 126 127 /** 128 * @return Returns the messageImprint. 129 */ 130 public MessageImprint getMessageImprint() { 131 return messageImprint; 132 } 133 134 /** 135 * @return Returns the nonce. 136 */ 137 public BigInteger getNonce() { 138 return nonce; 139 } 140 141 /** 142 * @return Returns the reqPolicy. 143 */ 144 public String getReqPolicy() { 145 return reqPolicy; 146 } 147 148 /** 149 * @return Returns the version. 150 */ 151 public int getVersion() { 152 return version; 153 } 154 155 public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] { 156 ASN1Integer.getInstance(), // version 157 MessageImprint.ASN1, // messageImprint 158 ASN1Oid.getInstance(), // reqPolicy 159 ASN1Integer.getInstance(), // nonce 160 ASN1Boolean.getInstance(), // certReq 161 new ASN1Implicit(0, Extensions.ASN1)}) {// extensions 162 163 { 164 setDefault(Boolean.FALSE, 4); 165 setOptional(2); 166 setOptional(3); 167 setOptional(5); 168 } 169 170 protected Object getDecodedObject(BerInputStream in) { 171 Object[] values = (Object[]) in.content; 172 173 String objID = (values[2] == null) ? null : ObjectIdentifier 174 .toString((int[]) values[2]); 175 BigInteger nonce = (values[3] == null) ? null : new BigInteger( 176 (byte[]) values[3]); 177 178 if (values[5] == null) { 179 return new TimeStampReq( 180 ASN1Integer.toIntValue(values[0]), 181 (MessageImprint) values[1], 182 objID, 183 nonce, 184 (Boolean) values[4], 185 null, 186 in.getEncoded() 187 ); 188 } else { 189 return new TimeStampReq( 190 ASN1Integer.toIntValue(values[0]), 191 (MessageImprint) values[1], 192 objID, 193 nonce, 194 (Boolean) values[4], 195 (Extensions) values[5], 196 in.getEncoded() 197 ); 198 } 199 } 200 201 protected void getValues(Object object, Object[] values) { 202 TimeStampReq req = (TimeStampReq) object; 203 values[0] = ASN1Integer.fromIntValue(req.version); 204 values[1] = req.messageImprint; 205 values[2] = (req.reqPolicy == null) ? null : ObjectIdentifier 206 .toIntArray(req.reqPolicy); 207 values[3] = (req.nonce == null) ? null : req.nonce.toByteArray(); 208 values[4] = (req.certReq == null) ? Boolean.FALSE : req.certReq; 209 values[5] = req.extensions; 210 } 211 }; 212 213 } 214 215