Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add revoke method to User #1584

Merged
merged 1 commit into from
Jan 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/key/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import enums from '../enums';
import util from '../util';
import { PacketList } from '../packet';
import { mergeSignatures, isDataRevoked, createSignaturePacket } from './helper';
import defaultConfig from '../config';

/**
* Class that represents an user ID or attribute packet and the relevant signatures.
Expand Down Expand Up @@ -94,7 +95,7 @@ class User {
* @returns {Promise<Boolean>} True if the certificate is revoked.
* @async
*/
async isRevoked(certificate, keyPacket, date = new Date(), config) {
async isRevoked(certificate, keyPacket, date = new Date(), config = defaultConfig) {
const primaryKey = this.mainKey.keyPacket;
return isDataRevoked(primaryKey, enums.signature.certRevocation, {
key: primaryKey,
Expand Down Expand Up @@ -233,6 +234,41 @@ class User {
return isDataRevoked(primaryKey, enums.signature.certRevocation, dataToVerify, [srcRevSig], undefined, undefined, date, config);
});
}

/**
* Revokes the user
* @param {SecretKeyPacket} primaryKey - decrypted private primary key for revocation
* @param {Object} reasonForRevocation - optional, object indicating the reason for revocation
* @param {module:enums.reasonForRevocation} reasonForRevocation.flag optional, flag indicating the reason for revocation
* @param {String} reasonForRevocation.string optional, string explaining the reason for revocation
* @param {Date} date - optional, override the creationtime of the revocation signature
* @param {Object} [config] - Full configuration, defaults to openpgp.config
* @returns {Promise<User>} New user with revocation signature.
* @async
*/
async revoke(
primaryKey,
{
flag: reasonForRevocationFlag = enums.reasonForRevocation.noReason,
string: reasonForRevocationString = ''
} = {},
date = new Date(),
config = defaultConfig
) {
const dataToSign = {
userID: this.userID,
userAttribute: this.userAttribute,
key: primaryKey
};
const user = new User(dataToSign.userID || dataToSign.userAttribute, this.mainKey);
user.revocationSignatures.push(await createSignaturePacket(dataToSign, null, primaryKey, {
signatureType: enums.signature.certRevocation,
reasonForRevocationFlag: enums.write(enums.reasonForRevocation, reasonForRevocationFlag),
reasonForRevocationString
}, date, undefined, false, config));
await user.update(this);
return user;
}
}

export default User;
22 changes: 22 additions & 0 deletions test/general/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -3277,6 +3277,28 @@ module.exports = () => describe('Key', function() {
});
});

it('revoke() - user', async function() {
const pubKey = await openpgp.readKey({ armoredKey: pub_key_arm2 });
const privKey = await openpgp.decryptKey({
privateKey: await openpgp.readKey({ armoredKey: priv_key_arm2 }),
passphrase: 'hello world'
});

const user = pubKey.users[0];
await user.revoke(privKey.keyPacket, {
flag: openpgp.enums.reasonForRevocation.userIDInvalid
}).then(async revUser => {
expect(user.userID.equals(revUser.userID)).to.be.true;
expect(revUser.revocationSignatures).to.exist.and.have.length(1);
expect(revUser.revocationSignatures[0].signatureType).to.equal(openpgp.enums.signature.certRevocation);
expect(revUser.revocationSignatures[0].reasonForRevocationFlag).to.equal(openpgp.enums.reasonForRevocation.userIDInvalid);
expect(revUser.revocationSignatures[0].reasonForRevocationString).to.equal('');

await user.verify();
await expect(revUser.verify()).to.be.rejectedWith('Self-certification is revoked');
});
});

it('applyRevocationCertificate() should produce the same revoked key as GnuPG', async function() {
const pubKey = await openpgp.readKey({ armoredKey: pub_key_arm4 });

Expand Down