You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KeyInfoKeySelector.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. /*
  16. * Based on the eID Applet Project code.
  17. * Original Copyright (C) 2008-2009 FedICT.
  18. */
  19. package org.apache.poi.ooxml.signature.service.signer;
  20. import java.security.Key;
  21. import java.security.cert.X509Certificate;
  22. import java.util.List;
  23. import javax.xml.crypto.AlgorithmMethod;
  24. import javax.xml.crypto.KeySelector;
  25. import javax.xml.crypto.KeySelectorException;
  26. import javax.xml.crypto.KeySelectorResult;
  27. import javax.xml.crypto.XMLCryptoContext;
  28. import javax.xml.crypto.XMLStructure;
  29. import javax.xml.crypto.dsig.keyinfo.KeyInfo;
  30. import javax.xml.crypto.dsig.keyinfo.X509Data;
  31. import org.apache.commons.logging.Log;
  32. import org.apache.commons.logging.LogFactory;
  33. /**
  34. * JSR105 key selector implementation using the ds:KeyInfo data of the signature
  35. * itself.
  36. */
  37. public class KeyInfoKeySelector extends KeySelector implements KeySelectorResult {
  38. private static final Log LOG = LogFactory.getLog(KeyInfoKeySelector.class);
  39. private X509Certificate certificate;
  40. @Override
  41. public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException {
  42. LOG.debug("select key");
  43. if (null == keyInfo) {
  44. throw new KeySelectorException("no ds:KeyInfo present");
  45. }
  46. List<XMLStructure> keyInfoContent = keyInfo.getContent();
  47. this.certificate = null;
  48. for (XMLStructure keyInfoStructure : keyInfoContent) {
  49. if (false == (keyInfoStructure instanceof X509Data)) {
  50. continue;
  51. }
  52. X509Data x509Data = (X509Data) keyInfoStructure;
  53. List<Object> x509DataList = x509Data.getContent();
  54. for (Object x509DataObject : x509DataList) {
  55. if (false == (x509DataObject instanceof X509Certificate)) {
  56. continue;
  57. }
  58. X509Certificate certificate = (X509Certificate) x509DataObject;
  59. LOG.debug("certificate: " + certificate.getSubjectX500Principal());
  60. if (null == this.certificate) {
  61. /*
  62. * The first certificate is presumably the signer.
  63. */
  64. this.certificate = certificate;
  65. }
  66. }
  67. if (null != this.certificate) {
  68. return this;
  69. }
  70. }
  71. throw new KeySelectorException("No key found!");
  72. }
  73. public Key getKey() {
  74. return this.certificate.getPublicKey();
  75. }
  76. /**
  77. * Gives back the X509 certificate used during the last signature
  78. * verification operation.
  79. *
  80. * @return
  81. */
  82. public X509Certificate getCertificate() {
  83. return this.certificate;
  84. }
  85. }