Esempio n. 1
0
  @Override
  public KeySelectorResult select(
      final KeyInfo ki, final Purpose p, final AlgorithmMethod m, final XMLCryptoContext c)
      throws KeySelectorException {

    if (ki == null) throw new KeySelectorException("KeyInfo is null");

    final SignatureMethod sm = (SignatureMethod) m;
    @SuppressWarnings("unchecked")
    final List<Object> list = ki.getContent();

    for (final Object l : list) {
      final XMLStructure s = (XMLStructure) l;
      PublicKey pk = null;
      if (s instanceof KeyValue) {
        try {
          pk = ((KeyValue) s).getPublicKey();
        } catch (final KeyException ke) {
          throw new KeySelectorException(ke);
        }

      } else if (s instanceof X509Data) {
        for (final Object d : ((X509Data) s).getContent()) {
          if (d instanceof X509Certificate) {
            pk = ((Certificate) d).getPublicKey();
          }
        }
      }

      if (pk != null) {
        final String sa = sm.getAlgorithm();
        final String ka = pk.getAlgorithm();
        if ("DSA".equalsIgnoreCase(ka) && "http://www.w3.org/2000/09/xmldsig#dsa-sha1".equals(sa)
            || "RSA".equalsIgnoreCase(ka)
                && "http://www.w3.org/2000/09/xmldsig#rsa-sha1".equals(sa)) {
          return new MyKeySelectorResult(pk);
        }
      }
    }

    throw new KeySelectorException("No KeyValue element found");
  }
Esempio n. 2
0
 @Override
 public BitcoinSigner findSignerByPublicKey(PublicKey publicKey) {
   Address address = publicKey.toAddress(_network);
   InMemoryPrivateKey privateKey;
   try {
     privateKey = getPrivateKeyForAddress(address, _cipher);
   } catch (InvalidKeyCipher e) {
     throw new RuntimeException(
         "Unable to decrypt private key for address " + address.toString());
   }
   if (privateKey != null) {
     return privateKey;
   }
   throw new RuntimeException("Unable to find private key for address " + address.toString());
 }