in src/main/java/software/amazon/awssdk/crt/utils/PemUtils.java [243:288]
private static int validatePemByRegexParser(String pem, String expectedPemTypeSubString, int maxChainDepth) {
int beginCount = 0;
int endCount = 0;
int objCount = 0;
Matcher beginMatcher = PEM_BEGIN_PATTERN.matcher(pem);
Matcher endMatcher = PEM_END_PATTERN.matcher(pem);
Matcher objMatcher = PEM_OBJECT_PATTERN.matcher(pem);
while (beginMatcher.find()) {
beginCount++;
}
while (endMatcher.find()) {
endCount++;
}
while (objMatcher.find()) {
String beginType = objMatcher.group(1);
String base64Contents = objMatcher.group(2);
String endType = objMatcher.group(3);
if (!beginType.contains(expectedPemTypeSubString) || !endType.contains(expectedPemTypeSubString)) {
throw new IllegalArgumentException(
"PEM Object does not have expected type. " + "Expected Type: " + expectedPemTypeSubString
+ ", Actual BEGIN Type: " + beginType + ", Actual END Type: " + endType);
}
if (base64Contents.length() == 0) {
throw new IllegalArgumentException("PEM Objet does not have any contents");
}
objCount++;
}
if (objCount == 0) {
throw new IllegalArgumentException("PEM contains no objects, or is not a PEM");
}
if (beginCount != endCount || beginCount != objCount) {
throw new IllegalArgumentException("PEM has mismatching BEGIN and END Delimiters. BeginCount: " + beginCount
+ ", EndCount: " + endCount + ", ObjCount: " + objCount);
}
if (beginCount > maxChainDepth) {
throw new IllegalArgumentException(
"PEM has greater than expected depth, ExpectedMax: " + maxChainDepth + ", Actual: " + beginCount);
}
return beginCount;
}