Skip to content

Commit 3eea5b1

Browse files
committed
Add test for key archival and retrieval
The basic KRA test has been modified to perform a cert enrollment with key archival against CA, retrieve the archive key from KRA, then verify that the archived key belongs to the cert. The pki kra-key-retrieve has been modified to store the PKCS #12 data returned by KRA into the output file if specified.
1 parent 9d26689 commit 3eea5b1

File tree

2 files changed

+135
-9
lines changed

2 files changed

+135
-9
lines changed

.github/workflows/kra-basic-test.yml

+125-3
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,131 @@ jobs:
206206
echo pki.example.com > expected
207207
diff expected actual
208208
209-
- name: Verify cert key archival
209+
- name: Check cert enrollment with key archival
210210
run: |
211-
docker exec pki /usr/share/pki/tests/kra/bin/test-cert-key-archival.sh
211+
# generate key and submit cert request
212+
# https://github.com/dogtagpki/pki/wiki/Submitting-Certificate-Request-with-Key-Archival
213+
docker exec pki pki \
214+
-U http://pki.example.com:8080 \
215+
client-cert-request \
216+
--profile caUserCert \
217+
--type crmf \
218+
--algorithm rsa \
219+
--permanent \
220+
--transport kra_transport.crt \
221+
UID=testuser | tee output
222+
223+
REQUEST_ID=$(sed -n "s/^\s*Request ID:\s*\(\S*\)$/\1/p" output)
224+
echo "Request ID: $REQUEST_ID"
225+
226+
# issue cert
227+
docker exec pki pki \
228+
-u caadmin \
229+
-w Secret.123 \
230+
ca-cert-request-approve \
231+
--force \
232+
$REQUEST_ID | tee output
233+
234+
CERT_ID=$(sed -n "s/^\s*Certificate ID:\s*\(\S*\)$/\1/p" output)
235+
echo "Cert ID: $CERT_ID"
236+
237+
# import cert into NSS database
238+
docker exec pki pki ca-cert-export --output-file testuser.crt $CERT_ID
239+
docker exec pki pki nss-cert-import --cert testuser.crt testuser
240+
241+
# the cert should match the key (trust flags must be u,u,u)
242+
echo "u,u,u" > expected
243+
docker exec pki pki nss-cert-show testuser | tee output
244+
sed -n "s/^\s*Trust Flags:\s*\(\S*\)$/\1/p" output > actual
245+
diff expected actual
246+
247+
- name: Check archived key
248+
run: |
249+
# find archived key by owner
250+
docker exec pki pki \
251+
-u kraadmin \
252+
-w Secret.123 \
253+
kra-key-find --owner UID=testuser | tee output
254+
255+
KEY_ID=$(sed -n "s/^\s*Key ID:\s*\(\S*\)$/\1/p" output)
256+
echo "Key ID: $KEY_ID"
257+
echo $KEY_ID > key.id
258+
259+
DEC_KEY_ID=$(python -c "print(int('$KEY_ID', 16))")
260+
echo "Dec Key ID: $DEC_KEY_ID"
261+
262+
# get key record
263+
docker exec ds ldapsearch \
264+
-H ldap://ds.example.com:3389 \
265+
-x \
266+
-D "cn=Directory Manager" \
267+
-w Secret.123 \
268+
-b "cn=$DEC_KEY_ID,ou=keyRepository,ou=kra,dc=kra,dc=pki,dc=example,dc=com" \
269+
-o ldif_wrap=no \
270+
-LLL | tee output
271+
272+
# encryption mode should be "false" by default
273+
echo "false" > expected
274+
sed -n 's/^metaInfo:\s*payloadEncrypted:\(.*\)$/\1/p' output > actual
275+
diff expected actual
276+
277+
# key wrap algorithm should be "AES KeyWrap/Padding" by default
278+
echo "AES KeyWrap/Padding" > expected
279+
sed -n 's/^metaInfo:\s*payloadWrapAlgorithm:\(.*\)$/\1/p' output > actual
280+
diff expected actual
281+
282+
- name: Check key retrieval
283+
run: |
284+
KEY_ID=$(cat key.id)
285+
echo "Key ID: $KEY_ID"
286+
287+
BASE64_CERT=$(docker exec pki pki nss-cert-export --format DER testuser | base64 --wrap=0)
288+
echo "Cert: $BASE64_CERT"
289+
290+
cat > request.json <<EOF
291+
{
292+
"ClassName" : "com.netscape.certsrv.key.KeyRecoveryRequest",
293+
"Attributes" : {
294+
"Attribute" : [ {
295+
"name" : "keyId",
296+
"value" : "$KEY_ID"
297+
}, {
298+
"name" : "certificate",
299+
"value" : "$BASE64_CERT"
300+
}, {
301+
"name" : "passphrase",
302+
"value" : "Secret.123"
303+
} ]
304+
}
305+
}
306+
EOF
307+
308+
# retrieve archived cert and key into PKCS #12 file
309+
# https://github.com/dogtagpki/pki/wiki/Retrieving-Archived-Key
310+
docker exec pki pki \
311+
-n caadmin \
312+
kra-key-retrieve \
313+
--input $SHARED/request.json \
314+
--output-data archived.p12
315+
316+
# import PKCS #12 file into NSS database
317+
docker exec pki pki \
318+
-d nssdb \
319+
pkcs12-import \
320+
--pkcs12 archived.p12 \
321+
--password Secret.123
322+
323+
# remove archived cert from NSS database
324+
docker exec pki pki -d nssdb nss-cert-del UID=testuser
325+
326+
# import original cert into NSS database
327+
docker exec pki pki -d nssdb nss-cert-import --cert testuser.crt testuser
328+
329+
# the original cert should match the archived key (trust flags must be u,u,u)
330+
echo "u,u,u" > expected
331+
docker exec pki pki -d nssdb nss-cert-show testuser | tee output
332+
sed -n "s/^\s*Trust Flags:\s*\(\S*\)$/\1/p" output > actual
333+
diff expected actual
212334
213335
- name: Remove KRA
214336
run: docker exec pki pkidestroy -i pki-tomcat -s KRA -v
@@ -243,4 +365,4 @@ jobs:
243365
uses: actions/upload-artifact@v4
244366
with:
245367
name: kra-basic
246-
path: /tmp/artifacts/pki
368+
path: /tmp/artifacts

base/tools/src/main/java/com/netscape/cmstools/kra/KRAKeyRetrieveCLI.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,12 @@ public void execute(CommandLine cmd) throws Exception {
179179
if (outputDataFile != null) {
180180

181181
byte[] data;
182-
if (clientEncryption) { // store encrypted data
182+
183+
String base64pkcs12Data = key.getP12Data();
184+
if (base64pkcs12Data != null) {
185+
data = Utils.base64decode(base64pkcs12Data);
186+
187+
} else if (clientEncryption) { // store encrypted data
183188
data = key.getEncryptedData();
184189

185190
} else { // store unencrypted data
@@ -228,16 +233,15 @@ public void printKeyInfo(Key key) {
228233
}
229234

230235
public void printKeyData(Key key) {
231-
if (clientEncryption) {
236+
String base64pkcs12Data = key.getP12Data();
237+
if (base64pkcs12Data != null) {
238+
System.out.println(" Key data in PKCS12 format: " + base64pkcs12Data);
239+
} else if (clientEncryption) {
232240
if (key.getEncryptedData() != null)
233241
System.out.println(" Encrypted Data:" + Utils.base64encode(key.getEncryptedData(), false));
234242
} else {
235243
if (key.getData() != null)
236244
System.out.println(" Actual archived data: " + Utils.base64encode(key.getData(), false));
237245
}
238-
239-
if (key.getP12Data() != null) {
240-
System.out.println(" Key data in PKCS12 format: " + key.getP12Data());
241-
}
242246
}
243247
}

0 commit comments

Comments
 (0)