View Javadoc
1   package civitas.bboard.server.controllers;
2   
3   import java.io.IOException;
4   import java.security.KeyStoreException;
5   import java.security.NoSuchAlgorithmException;
6   import java.security.PrivateKey;
7   import java.security.PublicKey;
8   import java.security.UnrecoverableKeyException;
9   import java.security.cert.CertificateException;
10  
11  import org.bouncycastle.crypto.CryptoException;
12  import org.springframework.beans.factory.annotation.Autowired;
13  import org.springframework.stereotype.Controller;
14  import org.springframework.web.bind.annotation.GetMapping;
15  import org.springframework.web.bind.annotation.PathVariable;
16  
17  import civitas.bboard.common.BBPost;
18  import civitas.bboard.common.BBPostRepository;
19  import civitas.bboard.server.GetBoardForId;
20  import civitas.common.Configuration;
21  import civitas.common.GetPrivateKey;
22  import civitas.common.GetPublicKey;
23  import civitas.crypto.messagedigest.CryptoHash;
24  import civitas.crypto.signature.SignWithPublicKey;
25  import civitas.crypto.signature.Signature;
26  
27  @Controller
28  public class RetrieveHashController {
29  	@Autowired
30  	GetBoardForId getBoardForId;
31  
32  	@Autowired
33  	BBPostRepository bBPostRepository;
34  
35  	@Autowired
36  	CryptoHash cryptoHash;
37  
38  	@Autowired
39  	SignWithPublicKey signWithPublicKey;
40  
41  	@Autowired
42  	GetPrivateKey getPrivateKey;
43  
44  	@Autowired
45  	Configuration configuration;
46  
47  	@Autowired
48  	GetPublicKey getServerPublicKey;
49  
50  	@GetMapping("/boards/{bbid}/signature-{fromTime}-{toTime}-{metaCriteria}")
51  	private Signature apply(
52  			@PathVariable("bbid") final String bbid,
53  			@PathVariable("fromTime") final Long fromTime,
54  			@PathVariable("toTime") final Long toTime,
55  			@PathVariable("metaCriteria") final String metaCriteria)
56  			throws CommunicableException {
57  
58  		getBoardForId.apply(bbid, true);
59  
60  		Iterable<BBPost> posts =
61  				bBPostRepository.findByBbidAndTimestampBetweenAndMeta(bbid, fromTime, toTime, metaCriteria);
62  
63  		byte[] hash = null;
64  		for (BBPost post : posts) { // FIXME probably this should be something else
65  			hash = cryptoHash.apply(post.sig.signatureBytes, hash);
66  		}
67  
68  		Signature signature;
69  		try {
70  			PrivateKey privKey;
71  			privKey = getPrivateKey.apply(
72  					configuration.storePassword, configuration.storeFile, configuration.serverKeyEntry);
73  			PublicKey pubKey = getServerPublicKey.apply(
74  					configuration.storePassword, configuration.storeFile, configuration.serverKeyEntry);
75  			signature = signWithPublicKey.apply(privKey, pubKey, hash);
76  		} catch (UnrecoverableKeyException
77  				| KeyStoreException
78  				| NoSuchAlgorithmException
79  				| CertificateException
80  				| IOException
81  				| CryptoException e) {
82  			throw new CommunicableException("internal error");
83  		}
84  
85  		return signature;
86  	}
87  }