Skip to content

Commit

Permalink
Trim ecdh key (#485)
Browse files Browse the repository at this point in the history
* VAULT-3067 Limits for steps, time in Eval

* Trim the EDCH key

* Revert "VAULT-3067 Limits for steps, time in Eval"

This reverts commit c08831b.
  • Loading branch information
Iapetus999 authored Apr 16, 2024
1 parent 0d168c7 commit eed2534
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.security.*;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.stream.Collectors;
import javax.crypto.KeyAgreement;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.*;
import net.starlark.java.eval.StarlarkBytes;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

import javax.crypto.KeyAgreement;

@StarlarkBuiltin(
name = "ECDH",
category = "BUILTIN",
Expand Down Expand Up @@ -107,6 +115,11 @@ private static PrivateKey loadPrivateKeyPKCS8(StarlarkBytes privKey) throws Exce
}

private static PrivateKey loadPrivateKeySEC1(String pem) throws Exception {
// Trim each line
pem = Arrays.stream(pem.split("\n"))
.map(String::trim)
.collect(Collectors.joining("\n"));

final PEMParser pemParser = new PEMParser(new StringReader(pem));
final Object parsedPem = pemParser.readObject();
if (!(parsedPem instanceof PEMKeyPair)) {
Expand Down

0 comments on commit eed2534

Please sign in to comment.