Module: PkernelJce::Rfc3161::Response
- Included in:
- Pkernel::Rfc3161::Response, PkernelJce::Rfc3161ResponseEngine
- Defined in:
- lib/pkernel_jce/rfc3161.rb
Instance Method Summary collapse
-
#dump(resp, opts = { }) ⇒ Object
end parse().
-
#generate(opts = { }) ⇒ Object
generate rfc3161 timestamp token.
-
#parse(opts = {}) ⇒ Object
end generate().
Instance Method Details
#dump(resp, opts = { }) ⇒ Object
end parse()
266 267 268 269 270 271 272 |
# File 'lib/pkernel_jce/rfc3161.rb', line 266 def dump(resp, opts = { }) if resp.nil? raise PkernelJce::Error, "RFC3161 response to dump is nil." end resp.encode end |
#generate(opts = { }) ⇒ Object
generate rfc3161 timestamp token
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pkernel_jce/rfc3161.rb', line 18 def generate(opts = { }) bin = opts[:bin] file = opts[:file] if not (file.nil? or file.empty?) breq = IoUtils.file_to_memory_byte_array(file) elsif not bin.nil? breq = IoUtils.ensure_java_bytes(bin) else raise PkernelJce::Error, "No request file or memory is given to generate timestamp response" end id = opts[:identity] if id.nil? raise PkernelJce::Error, "Identity is not given to generate timestamping token" end req = org.bouncycastle.tsp.TimeStampRequest.new(breq) #p req.messageImprintAlgOID begin dgstEng = BcHelpers.find_digest_calculator(req.) PkernelJce::GConf.instance.glog.debug "Timestamp request using hash '#{dgstEng}'" rescue PkernelJce::Error => ex opts[:status] = Pkernel::Rfc3161::RESP_REJECTION opts[:reason] = Pkernel::Rfc3161::REASON_BAD_ALG opts[:reasonMsg] = "Digest not supported" end chain = id.chain list = java.util.ArrayList.new chain.each do |c| list.add(c) end store = org.bouncycastle.cert.jcajce.JcaCertStore.new(list) # this one seems useless since the actual algo shall be taken from request anyway... signHashAlgo = opts[:signHash] || "SHA256" policyId = opts[:policy_id] || "1.2.3.4.1" signInfo = org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoGeneratorBuilder.new.setProvider(id.provider).build(PkernelJce::KeyPair.derive_signing_algo(id.privKey,signHashAlgo), id.privKey, id.certificate) # policy 1.2.3.4.1 = tsa_policy1 gen = org.bouncycastle.tsp.TimeStampTokenGenerator.new(signInfo, BcHelpers.find_digest_calculator(signHashAlgo), org.bouncycastle.asn1.ASN1ObjectIdentifier.new(policyId)) # policy 1.2 = ISO Member body #gen = org.bouncycastle.tsp.TimeStampTokenGenerator.new(signInfo, SHA256DigestCalculator.new, org.bouncycastle.asn1.ASN1ObjectIdentifier.new("1.2")) gen.addCertificates(store) if req.cert_req? PkernelJce::GConf.instance.glog.debug "Client requested to include certificate for timestamping" # this option requires the request to reqCert set first if not error shall be thrown at the verification end gen.setTSA(org.bouncycastle.asn1.x509.GeneralName.new(PkernelJce::Certificate.ensure_bc_cert(id.certificate).subject_to_x500)) end status = opts[:status] || Pkernel::Rfc3161::RESP_GRANTED reason = opts[:reason] || Pkernel::Rfc3161::REASON_SYS_FAILURE reasonMsg = opts[:reasonMsg] || "" respGen = org.bouncycastle.tsp.TimeStampResponseGenerator.new(gen, org.bouncycastle.tsp.TSPAlgorithms::ALLOWED) if status == Pkernel::Rfc3161::RESP_GRANTED or status == Pkernel::Rfc3161::RESP_GRANTED_WITH_MODS # etsi want min 1 sec...:) accuracy = opts[:accuracy] || { seconds: 1 } if not accuracy[:seconds].nil? PkernelJce::GConf.instance.glog.debug "Timestamping setting accuracy #{accuracy[:seconds]} seconds" gen.setAccuracySeconds(accuracy[:seconds].to_i) end if not accuracy[:milis].nil? PkernelJce::GConf.instance.glog.debug "Timestamping setting accuracy #{accuracy[:milis]} mili seconds" gen.setAccuracyMillis(accuracy[:milis].to_i) end if not accuracy[:micros].nil? PkernelJce::GConf.instance.glog.debug "Timestamping setting accuracy #{accuracy[:micros]} micro seconds" gen.setAccuracyMicros(accuracy[:micros].to_i) end tsResp = respGen.generateGrantedResponse(req, java.math.BigInteger.new(SecureRandom.uuid.gsub("-",""),16), java.util.Date.new) else tsResp = respGen.generateFailResponse(status, reason, reasonMsg) end #resp = org.bouncycastle.tsp.TimeStampResponse.new(tsResp.getEncoded()) #resp tsResp end |
#parse(opts = {}) ⇒ Object
end generate()
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/pkernel_jce/rfc3161.rb', line 113 def parse(opts = {}) file = opts[:file] bin = opts[:bin] if not (file.nil? or file.empty?) bresp = IoUtils.file_to_memory_byte_array(file) elsif not bin.nil? bresp = IoUtils.ensure_java_bytes(bin) else raise PkernelJce::Error, "No file or memory is given to parse timestamp response" end result = { } verifyResp = opts[:verifyResp] || true resp = org.bouncycastle.tsp.TimeStampResponse.new(bresp) result[:status] = resp.status if resp.status == Pkernel::Rfc3161::RESP_GRANTED || resp.status == Pkernel::Rfc3161::RESP_GRANTED_WITH_MODS token = resp.getTimeStampToken if not token.nil? info = token.getTimeStampInfo else PkernelJce::GConf.instance.glog.warn "Timestamp does not contain token!" end if not token.nil? and verifyResp tsaCert = opts[:tsaCert] signingCert = nil token.getCertificates.to_a.each do |c| if c.subject_to_x500.equals(info.getTsa.name) signingCert = c break end end if not tsaCert.nil? # check if tsaCert and signingCert is same if not tsaCert.equals(signingCert) raise PkernelJce::Error, "Given TSA cert and signing cert obtained from the timestamp token is not the same" end end prov = PkernelJce::Provider.add_default begin token.validate(org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder.new.setProvider(prov).build(signingCert)) result[:signature_status] = :verified PkernelJce::GConf.instance.glog.debug "Timestamp verified against signing cert [#{signingCert.subject}]" result[:signer] = { } result[:signer][:cert] = signingCert result[:signer][:chain] = token.getCertificates.to_a rescue Exception => ex result[:signature_status] = :failed result[:signature_status_details] = ex. end end # end verifyResp req = opts[:request] if not token.nil? and not req.nil? source = opts[:data] if not source.nil? sfile = source[:file] sbin = source[:bin] end if not (sfile.nil? or sfile.empty?) or not sbin.nil? dgst = PkernelJce::BcHelpers.find_digest_calculator(req.getMessageImprintAlgOID) if not dgst.nil? if not (sfile.nil? or sfile.empty?) PkernelJce::GConf.instance.glog.debug "Calculating hash for source '#{sfile}'" b = Java::byte[10240].new fis = java.io.FileInputStream.new(sfile) while((read = fis.read(b,0,b.length)) != -1) dgst.getOutputStream.write(b,0,read) end dgst.getOutputStream.close elsif not sbin.nil? PkernelJce::GConf.instance.glog.debug "Calculating hash for source from memory" dgst.getOutputStream.write(sbin.to_java_bytes) dgst.getOutputStream.close end if not java.util.Arrays.equals(dgst.digest, req.getMessageImprintDigest) raise PkernelJce::Error, "Source digest is different from request! Source file is not the original data sent for timestamp" else PkernelJce::GConf.instance.glog.debug "Source file hash matched request digest." end else PkernelJce::GConf.instance.glog.warn "Failed to initialize the hashing algo for source verification. Source verification shall be skipped." end end # end not source.nil? begin resp.validate(req) result[:request_validation] = :verified result[:digest] = info.getMessageImprintDigest result[:digest_algo] = info.getMessageImprintAlgOID PkernelJce::GConf.instance.glog.debug "Timestamp response verified against timestamp request. Checked [Nonce, Status, Digest, Hash Engine, Policy, Signing Certificates]" rescue Exception => ex result[:request_validation] = :failed result[:request_validation_details] = ex. end end if not info.nil? acc = info.getAccuracy result[:accuracy] = {} result[:accuracy][:seconds] = acc.getSeconds.nil? ? 0 : acc.getSeconds.value result[:accuracy][:milis] = acc.getMillis.nil? ? 0 : acc.getMillis.value result[:accuracy][:micros] = acc.getMicros.nil? ? 0 : acc.getMicros.value time = info.getGenTime result[:time] = time nonce = info.getNonce result[:nonce] = nonce.to_s(16) serial = info.getSerialNumber result[:serial] = serial.to_s(16) policy = info.getPolicy result[:policy] = policy tsa = info.getTsa result[:tsa_name] = tsa.name.to_s ordered = info.isOrdered result[:ordered] = ordered end else # status is NOT granted or granted with mods! result[:reason] = resp.getFailInfo result[:reasonMsg] = resp.getStatusString end # if status is granted or granted with mods result end |