Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private String doEncryptInternal(String hashAlgorithm, String asymmetricEncrypti
return doEncrypt(algorithm, hash, padding, asymKey, plainText);
} catch (InvalidCipherTextException e) {
this.error.setError("AE036", "Algoritmo inválido" + algorithm);
e.printStackTrace();
//e.printStackTrace();
return "";
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ private String doDecryptInternal(String hashAlgorithm, String asymmetricEncrypti
return doDecyrpt(algorithm, hash, padding, asymKey, encryptedInput);
} catch (InvalidCipherTextException | UnsupportedEncodingException e) {
this.error.setError("AE039", "Algoritmo inválido" + algorithm);
e.printStackTrace();
//e.printStackTrace();
return "";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public PrivateKey() {
}
public abstract boolean load(String path);
public abstract boolean loadPKCS12(String path, String alias, String password);
public abstract boolean fromBase64(String base64);
public abstract String toBase64();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import javax.crypto.EncryptedPrivateKeyInfo;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.cert.X509CertificateHolder;
Expand All @@ -26,8 +28,8 @@
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
import org.bouncycastle.util.encoders.Base64;

import com.genexus.securityapicommons.config.EncodingUtil;
import com.genexus.securityapicommons.utils.SecurityUtils;

/**
Expand Down Expand Up @@ -67,8 +69,56 @@ public boolean loadPKCS12(String privateKeyPath, String alias, String password)
}
return true;
}

@Override
public boolean fromBase64(String base64)
{
boolean res;
try {
res = readBase64(base64);
} catch (IOException e) {
this.error.setError("PK0015", e.getMessage());
return false;
}
this.hasPrivateKey = res;
return res;
}

@Override
public String toBase64()
{
if(this.hasPrivateKey) {
String encoded = "";
try {
encoded = Base64.toBase64String(this.privateKeyInfo.getEncoded());
} catch (IOException e) {
this.error.setError("PK0017", e.getMessage());
return "";
}
return encoded;
}
this.error.setError("PK0016", "No private key loaded");
return "";
}

/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
/******** EXTERNAL OBJECT PUBLIC METHODS - END
* @throws IOException ********/

private boolean readBase64(String base64) throws IOException
{
byte[] keybytes = Base64.decode(base64);
ASN1InputStream istream = new ASN1InputStream(keybytes);
ASN1Sequence seq = (ASN1Sequence) istream.readObject();
this.privateKeyInfo = PrivateKeyInfo.getInstance(seq);
istream.close();
if (this.privateKeyInfo == null)
{
this.error.setError("PK015", "Could not read private key from base64 string");
return false;
}
this.privateKeyAlgorithm = this.privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm().getId(); // 1.2.840.113549.1.1.1
return true;
}

/**
* @return PrivateKey type for the key type
Expand Down