1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 109 110 111 112 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 263 264 265 266 267 268 269 270 271
| @GetMapping("/doc-sign/sign") public ResponseEntity<Resource> getHashForSign() throws IOException { log.debug("start to generate merged PDF in java web start program"); synchronized (eventPublisher) { eventPublisher.publishEvent(new OnDocMergeEvent(getUserId(), getXxxFormMaster().getXxxFormMasterId(), getXxxFormMaster().getFormType())); } Integer xxxFormMasterId = getXxxFormMaster().getXxxFormMasterId(); String userId = getUserId();
List<SuppDoc> suppDocs = suppDocMapper.selectSuppDocByXxxFormMasterIdAndDocTypeAndUserId(xxxFormMasterId, userId, Constants.SUPP_DOC_TYPE_SYS_GEN_MERGE); if (suppDocs == null || suppDocs.isEmpty()) { suppDocs = suppDocMapper.selectSuppDocByXxxFormMasterIdAndDocTypeAndUserId(xxxFormMasterId, userId, Constants.SUPP_DOC_TYPE_SYS_SIGN); if (suppDocs != null && !suppDocs.isEmpty()) { log.info("signed doc exists !!"); return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(null); } return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); }
SuppDoc suppDoc = suppDocs.get(0); Integer suppDocId = suppDoc.getSuppDocId();
Resource pdfFile = storageService.loadAsResource(xxxFormMasterId, suppDocId);
File tempPdfFile = File.createTempFile("tmp", ".pdf"); log.debug("Prepare pdf file for sign: {}", tempPdfFile.getPath());
log.info("get hash for sign, userId {} form master id {}", userId, xxxFormMasterId);
InputStream is = null; try { is = emptySignature(pdfFile.getInputStream(), tempPdfFile); } catch (DocumentException | GeneralSecurityException e) { log.error("error occurrs on calculate signature", e); }
byte[] encryptedEmptySig = encrypt(StreamUtils.copyToByteArray(is)); String emptySig = Base64.getEncoder().encodeToString(encryptedEmptySig);
ByteArrayResource resource = new ByteArrayResource(emptySig.getBytes(StandardCharsets.ISO_8859_1));
HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Pragma", "no-cache"); headers.add("Expires", "0");
request.getSession(false).setAttribute(TEMP_SIGN_PATH, tempPdfFile.getPath());
return ResponseEntity.ok() .headers(headers) .contentLength(resource.contentLength()) .contentType(MediaType.TEXT_PLAIN) .body(resource); }
private byte[] encrypt(byte[] emptySig) {
String type = "X.509";
String encodedCertificate = request.getParameter("certificate"); try { CertificateFactory certificateFactory = CertificateFactory.getInstance(type); Certificate certificate = certificateFactory.generateCertificate( new ByteArrayInputStream(HashUtils.base64Decode(encodedCertificate))); PublicKey publicKey = certificate.getPublicKey();
Cipher rsaCipher; try { rsaCipher = Cipher.getInstance(RSA_ALGORITHM); rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream output = new DataOutputStream(baos);
KeyGenerator generator = KeyGenerator.getInstance("AES"); generator.init(128); log.debug("Generating session key..."); SecretKey aesKey = generator.generateKey(); log.debug("Done generating key.");
log.debug("aesKey.getEncoded() = " + Base64.getEncoder().encodeToString(aesKey.getEncoded())); byte[] encodedKeyBytes = rsaCipher.doFinal(aesKey.getEncoded());
log.debug("encrypted AES key length = " + encodedKeyBytes.length); output.writeInt(encodedKeyBytes.length); output.write(encodedKeyBytes);
Cipher symmetricCipher = Cipher.getInstance(AES_ALGORITHM); symmetricCipher.init(Cipher.ENCRYPT_MODE, aesKey);
output.write(symmetricCipher.doFinal(emptySig)); output.flush();
log.debug("length of encrypted data = " + baos.toByteArray().length); return baos.toByteArray(); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException e) { log.error("encrypt empty signature", e); } } catch (CertificateException e) { log.error("encrypt", e); }
return null; }
private InputStream emptySignature(InputStream src, File dest) throws IOException, DocumentException, GeneralSecurityException { PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
log.info("create empty signature"); String position = (String) WebUtils.getSession().getAttribute(WebAttributeNames.SESSION_PSCT_DOC_SIGNATURE_POSITION); String[] p = position.split("\\|"); appearance.setVisibleSignature(new Rectangle(Float.valueOf(p[0]), Float.valueOf(p[1]), Float.valueOf(p[2]), Float.valueOf(p[3])), Integer.valueOf(p[4]), Constants.SIG_FIELD_NAME);
Font font = new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD); appearance.setLayer2Font(font); appearance.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
appearance.setLayer2Text("Signed By Digital Certificate");
ClassPathResource pinImage = new ClassPathResource("static/images/Cert-sign.png"); appearance.setImage(Image.getInstance(pinImage.getURL()));
ExternalSignatureContainer external = new ExternalBlankSignatureContainer(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED); MakeSignature.signExternalContainer(appearance, external, 8192); os.close(); return appearance.getRangeStream(); }
@PostMapping(value = "/doc-sign/sign", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public CertSignResult attachSignedHash() { CertSignResult certSignResult = new CertSignResult(); certSignResult.setStatus(CertSignResult.STATUS_NOT_SIGNED);
try { if (!WebUtils.validateCert(pkiClient)) { return certSignResult; } } catch (Exception e) { log.error("signHash", e); return certSignResult; }
String tempPath = (String) request.getSession(false).getAttribute(TEMP_SIGN_PATH);
Integer xxxFormMasterId = getXxxFormMaster().getXxxFormMasterId(); String userId = getUserId();
log.info("sign hash, userId {} form master id {}", userId, xxxFormMasterId);
File tempSignedFile = null;
try { tempSignedFile = File.createTempFile("signed-" + xxxFormMasterId + "-", ".pdf");
String hash = request.getParameter("signed");
byte[] signedhash = Base64.getDecoder().decode(hash);
createSignature(tempPath, tempSignedFile, signedhash); } catch (DocumentException | GeneralSecurityException | IOException e) { log.error("Fail to sign document " + e); return certSignResult; }
SimpleSuppDoc doc = new SimpleSuppDoc(); doc.setXxxFormMasterId(xxxFormMasterId); doc.setLocalFile(tempSignedFile); doc.setSuppDocType(Constants.SUPP_DOC_TYPE_SYS_SIGN);
try { Integer suppDocId = storageService.store(doc, userId);
if (doc.isUploadSucess()) { insertSigningLog(userId, xxxFormMasterId, suppDocId);
storageService.deleteDocByUserIdAndType(xxxFormMasterId, userId, Constants.SUPP_DOC_TYPE_SYS_GEN_MERGE);
certSignResult.setStatus(CertSignResult.STATUS_SIGNED); request.getSession(false).setAttribute(WebAttributeNames.SESSION_PSCT_DOC_SIGN_STATUS, certSignResult);
createZipFileForSignedDoc(userId, tempSignedFile); return certSignResult; } } catch (IOException e) { log.error("sign pdf file", e); } finally { if (tempSignedFile != null) tempSignedFile.delete(); }
return certSignResult; }
private void createSignature(String src, File dest, byte[] signedHash) throws IOException, DocumentException, GeneralSecurityException { PdfReader reader = new PdfReader(src); FileOutputStream os = new FileOutputStream(dest); ExternalSignatureContainer external = new MyExternalSignatureContainer(signedHash); MakeSignature.signDeferred(reader, Constants.SIG_FIELD_NAME, os, external); os.close(); }
class MyExternalSignatureContainer implements ExternalSignatureContainer { protected byte[] sig;
public MyExternalSignatureContainer(byte[] sig) { this.sig = sig; }
@Override public void modifySigningDictionary(PdfDictionary signDic) { }
@Override public byte[] sign(InputStream arg0) throws GeneralSecurityException { return sig; } }
|