1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.script

File ScriptingKeyStore.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

0
14
7
1
138
53
7
0.5
2
7
1

Classes

Class Line # Actions
ScriptingKeyStore 40 14 0% 7 21
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.script;
21   
22    import org.xwiki.crypto.pkix.params.CertifiedKeyPair;
23    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
24    import org.xwiki.crypto.script.internal.AbstractScriptingStore;
25    import org.xwiki.crypto.store.KeyStore;
26    import org.xwiki.crypto.store.KeyStoreException;
27    import org.xwiki.crypto.store.StoreReference;
28    import org.xwiki.security.authorization.AccessDeniedException;
29    import org.xwiki.security.authorization.ContextualAuthorizationManager;
30    import org.xwiki.security.authorization.Right;
31    import org.xwiki.stability.Unstable;
32   
33    /**
34    * Wrapper over {@link KeyStore} for scripting.
35    *
36    * @version $Id: 4d5040cff03c21d66ad04c5f38af069e3e020fe2 $
37    * @since 8.4RC1
38    */
39    @Unstable
 
40    public class ScriptingKeyStore extends AbstractScriptingStore
41    {
42    private KeyStore store;
43   
 
44  0 toggle ScriptingKeyStore(KeyStore store, StoreReference reference,
45    ContextualAuthorizationManager contextualAuthorizationManager)
46    {
47  0 super(reference, contextualAuthorizationManager);
48  0 this.store = store;
49    }
50   
51    /**
52    * Store a private key and its certificate into a given store.
53    *
54    * NOT VERY SECURE, since the key will be store AS IS without encryption.
55    *
56    * @param keyPair the key pair to be stored.
57    * @throws KeyStoreException on error.
58    * @throws AccessDeniedException if you do not have edit access rights to the store.
59    */
 
60  0 toggle public void store(CertifiedKeyPair keyPair) throws KeyStoreException, AccessDeniedException
61    {
62  0 checkAccess(Right.EDIT);
63  0 store.store(storeReference, keyPair);
64    }
65   
66    /**
67    * Store a private key and its certificate into a given store, encrypting the key with a password.
68    *
69    * @param keyPair the key pair to be stored.
70    * @param password the password to encrypt the private key.
71    * @throws KeyStoreException on error.
72    * @throws AccessDeniedException if you do not have edit access rights to the store.
73    */
 
74  0 toggle public void store(CertifiedKeyPair keyPair, String password) throws KeyStoreException, AccessDeniedException
75    {
76  0 checkAccess(Right.EDIT);
77  0 store.store(storeReference, keyPair, password.getBytes(UTF8));
78    }
79   
80    /**
81    * Retrieve a private key from a given store that may contains only a single key.
82    *
83    * @return the certified key pair, or null if none have been found.
84    * @throws KeyStoreException on error.
85    * @throws AccessDeniedException if you do not have edit access rights to the store.
86    */
 
87  0 toggle public CertifiedKeyPair retrieve() throws KeyStoreException, AccessDeniedException
88    {
89  0 checkAccess(Right.VIEW);
90  0 return store.retrieve(storeReference);
91    }
92   
93    /**
94    * Retrieve the certified key pair from a given store that may contains only a single key and decrypt it using
95    * the given password.
96    *
97    * @param password the password to decrypt the private key.
98    * @return the certified key pair, or null if none have been found.
99    * @throws KeyStoreException on error.
100    * @throws AccessDeniedException if you do not have edit access rights to the store.
101    */
 
102  0 toggle public CertifiedKeyPair retrieve(String password) throws KeyStoreException, AccessDeniedException
103    {
104  0 checkAccess(Right.VIEW);
105  0 return store.retrieve(storeReference, password.getBytes(UTF8));
106    }
107   
108    /**
109    * Retrieve the certified key pair from a given store that match the given certificate.
110    *
111    * @param publicKey for which the private key is requested.
112    * @return the certified key pair corresponding to the given certificate, or null if none have been found.
113    * @throws KeyStoreException on error.
114    * @throws AccessDeniedException if you do not have edit access rights to the store.
115    */
 
116  0 toggle public CertifiedKeyPair retrieve(CertifiedPublicKey publicKey) throws KeyStoreException, AccessDeniedException
117    {
118  0 checkAccess(Right.VIEW);
119  0 return store.retrieve(storeReference, publicKey);
120    }
121   
122    /**
123    * Retrieve the certified key pair from a given store that match the given certificate and decrypt it using
124    * the given password.
125    *
126    * @param publicKey for which the private key is requested.
127    * @param password the password to decrypt the private key.
128    * @return the certified key pair corresponding to the given certificate, or null if none have been found.
129    * @throws KeyStoreException on error.
130    * @throws AccessDeniedException if you do not have edit access rights to the store.
131    */
 
132  0 toggle public CertifiedKeyPair retrieve(CertifiedPublicKey publicKey, String password)
133    throws KeyStoreException, AccessDeniedException
134    {
135  0 checkAccess(Right.VIEW);
136  0 return store.retrieve(storeReference, publicKey, password.getBytes(UTF8));
137    }
138    }