Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/stores/ResourceTrustAnchor.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15
16 package org.globus.gsi.stores;
17
18 import org.globus.gsi.util.CertificateIOUtil;
19 import org.globus.gsi.util.CertificateLoadUtil;
20 import org.globus.util.GlobusResource;
21
22 import java.io.BufferedInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.security.GeneralSecurityException;
26 import java.security.cert.CertificateEncodingException;
27 import java.security.cert.TrustAnchor;
28 import java.security.cert.X509Certificate;
29
30
31 /**
32 * Created by IntelliJ IDEA.
33 * User: turtlebender
34 * Date: Dec 29, 2009
35 * Time: 11:37:52 AM
36 * To change this template use File | Settings | File Templates.
37 */
38 public class ResourceTrustAnchor extends AbstractResourceSecurityWrapper<TrustAnchor> {
39
40
41 public ResourceTrustAnchor(String fileName) throws ResourceStoreException {
42 super(false);
43 init(globusResolver.getResource(fileName));
44 }
45
46 public ResourceTrustAnchor(boolean inMemory, GlobusResource globusResource) throws ResourceStoreException {
47 super(inMemory);
48 init(globusResource);
49 }
50
51 public ResourceTrustAnchor(String fileName, TrustAnchor cachedAnchor) throws ResourceStoreException {
52 super(false);
53 init(globusResolver.getResource(fileName), cachedAnchor);
54 }
55
56 public ResourceTrustAnchor(boolean inMemory, GlobusResource globusResource, TrustAnchor cachedAnchor) throws ResourceStoreException {
57 super(inMemory);
58 init(globusResource, cachedAnchor);
59 }
60
61 public TrustAnchor getTrustAnchor() throws ResourceStoreException {
62 return super.getSecurityObject();
63 }
64
65 @Override
66 protected TrustAnchor create(GlobusResource globusResource) throws ResourceStoreException {
67 X509Certificate certificate;
68 try {
69 InputStream inputStream = globusResource.getInputStream();
70 try {
71 certificate = CertificateLoadUtil.loadCertificate(new BufferedInputStream(inputStream));
72 } finally {
73 try {
74 inputStream.close();
75 } catch (IOException ignored) {
76 }
77 }
78 } catch (IOException e) {
79 throw new ResourceStoreException(e);
80 } catch (GeneralSecurityException e) {
81 throw new ResourceStoreException(e);
82 }
83
84 return new TrustAnchor(certificate, null);
85 }
86
87 public void store() throws ResourceStoreException {
88 try {
89 CertificateIOUtil.writeCertificate(this.getTrustAnchor().getTrustedCert(), globusResource.getFile());
90 } catch (CertificateEncodingException e) {
91 throw new ResourceStoreException(e);
92 } catch (IOException e) {
93 throw new ResourceStoreException(e);
94 }
95 }
96 }