doipjs/src/signatures.js

139 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-01-07 08:15:50 -07:00
/*
2021-01-13 05:20:33 -07:00
Copyright 2021 Yarmo Mackenbach
2021-01-07 08:15:50 -07:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const openpgp = require('openpgp')
2021-04-19 03:06:29 -06:00
const Claim = require('./claim')
2021-01-07 08:15:50 -07:00
const keys = require('./keys')
2021-04-19 03:06:29 -06:00
const process = (signature) => {
2021-01-07 08:15:50 -07:00
return new Promise(async (resolve, reject) => {
2021-04-19 03:06:29 -06:00
let sigData,
result = {
fingerprint: null,
2021-04-19 03:44:30 -06:00
users: [
{
userData: {},
claims: [],
},
],
2021-04-19 03:06:29 -06:00
primaryUserIndex: null,
key: {
data: null,
fetchMethod: null,
uri: null,
},
}
2021-01-07 08:15:50 -07:00
try {
sigData = await openpgp.cleartext.readArmored(signature)
} catch (error) {
2021-04-19 03:06:29 -06:00
reject('invalid_signature')
return
2021-01-07 08:15:50 -07:00
}
const issuerKeyId = sigData.signature.packets[0].issuerKeyId.toHex()
const signersUserId = sigData.signature.packets[0].signersUserId
const preferredKeyServer =
sigData.signature.packets[0].preferredKeyServer ||
2021-01-10 09:56:59 -07:00
'https://keys.openpgp.org/'
2021-01-07 08:15:50 -07:00
const text = sigData.getText()
let sigKeys = []
2021-01-07 08:15:50 -07:00
text.split('\n').forEach((line, i) => {
2021-01-10 04:26:18 -07:00
const match = line.match(/^([a-zA-Z0-9]*)\=(.*)$/i)
2021-01-07 08:15:50 -07:00
if (!match) {
return
}
switch (match[1].toLowerCase()) {
case 'key':
sigKeys.push(match[2])
break
case 'proof':
2021-04-19 03:06:29 -06:00
result.users[0].claims.push(new Claim(match[2]))
2021-01-07 08:15:50 -07:00
break
2021-01-07 08:17:24 -07:00
2021-01-07 08:15:50 -07:00
default:
break
}
})
// Try overruling key
if (sigKeys.length > 0) {
try {
2021-04-19 03:06:29 -06:00
result.key.uri = sigKeys[0]
result.key.data = await keys.fetch.uri(result.key.uri)
result.key.fetchMethod = result.key.uri.split(':')[0]
2021-03-01 10:27:29 -07:00
} catch (e) {}
}
// Try WKD
2021-04-19 03:06:29 -06:00
if (!result.key.data && signersUserId) {
try {
2021-04-19 03:06:29 -06:00
result.key.uri = `wkd:${signersUserId}`
result.key.data = await keys.fetch.uri(result.key.uri)
result.key.fetchMethod = 'wkd'
2021-03-01 10:27:29 -07:00
} catch (e) {}
}
// Try HKP
2021-04-19 03:06:29 -06:00
if (!result.key.data) {
try {
const match = preferredKeyServer.match(/^(.*\:\/\/)?([^/]*)(?:\/)?$/i)
2021-04-19 03:44:30 -06:00
result.key.uri = `hkp:${match[2]}:${
issuerKeyId ? issuerKeyId : signersUserId
}`
2021-04-19 03:06:29 -06:00
result.key.data = await keys.fetch.uri(result.key.uri)
result.key.fetchMethod = 'hkp'
2021-03-01 10:27:29 -07:00
} catch (e) {
2021-04-19 03:06:29 -06:00
reject('key_not_found')
return
}
2021-01-07 08:15:50 -07:00
}
2021-01-07 08:17:24 -07:00
2021-04-19 03:06:29 -06:00
result.fingerprint = result.key.data.keyPacket.getFingerprint()
2021-01-07 08:17:24 -07:00
2021-04-19 03:44:30 -06:00
result.users[0].claims.forEach((claim) => {
2021-04-19 03:06:29 -06:00
claim.fingerprint = result.fingerprint
})
const primaryUserData = await result.key.data.getPrimaryUser()
let userData
if (signersUserId) {
2021-04-19 03:44:30 -06:00
result.key.data.users.forEach((user) => {
2021-04-19 03:06:29 -06:00
if (user.userId.email == signersUserId) {
userData = user
}
})
2021-04-19 03:44:30 -06:00
}
2021-04-19 03:06:29 -06:00
if (!userData) {
userData = primaryUserData.user
2021-01-07 08:15:50 -07:00
}
2021-04-19 03:06:29 -06:00
result.users[0].userData = {
id: userData.userId ? userData.userId.userid : null,
name: userData.userId ? userData.userId.name : null,
email: userData.userId ? userData.userId.email : null,
comment: userData.userId ? userData.userId.comment : null,
isPrimary: primaryUserData.user.userId.userid === userData.userId.userid,
}
result.primaryUserIndex = result.users[0].userData.isPrimary ? 0 : null
resolve(result)
2021-01-07 08:15:50 -07:00
})
}
2021-04-19 03:06:29 -06:00
exports.process = process