forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebid-tls-certificate.js
More file actions
184 lines (164 loc) · 4.88 KB
/
webid-tls-certificate.js
File metadata and controls
184 lines (164 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict'
/* eslint-disable node/no-deprecated-api */
const webidTls = require('../webid')('tls')
const forge = require('node-forge')
const utils = require('../utils')
/**
* Models a WebID-TLS crypto certificate, as generated at signup from a browser's
* `<keygen>` element.
*
* @class WebIdTlsCertificate
*/
class WebIdTlsCertificate {
/**
* @param [options={}] {Object}
* @param [options.spkac] {Buffer<string>}
* @param [options.date] {Date}
* @param [options.webId] {string}
* @param [options.commonName] {string} Certificate name
* @param [options.issuerName] {string}
*/
constructor (options = {}) {
this.spkac = options.spkac
this.date = options.date || new Date()
this.webId = options.webId
this.commonName = options.commonName
this.issuer = { commonName: options.issuerName }
this.certificate = null // gets initialized in `generateCertificate()`
}
/**
* Factory method, used to construct a certificate instance from a browser-
* based signup.
*
* @param spkac {string} Signed Public Key and Challenge from a browser's
* `<keygen>` element.
* @param userAccount {UserAccount}
* @param host {SolidHost}
*
* @throws {TypeError} If no `spkac` param provided (http 400)
*
* @return {WebIdTlsCertificate}
*/
static fromSpkacPost (spkac, userAccount, host) {
if (!spkac) {
const error = new TypeError('Missing spkac parameter')
error.status = 400
throw error
}
const date = new Date()
const commonName = `${userAccount.displayName} [on ${host.serverUri}, created ${date}]`
const options = {
spkac: WebIdTlsCertificate.prepPublicKey(spkac),
webId: userAccount.webId,
date,
commonName,
issuerName: host.serverUri
}
return new WebIdTlsCertificate(options)
}
/**
* Formats a `<keygen>`-generated public key and converts it into a Buffer,
* to use in TLS certificate generation.
*
* @param spkac {string} Signed Public Key and Challenge from a browser's
* `<keygen>` element.
*
* @return {Buffer<string>|null} UTF-8 string buffer of the public key, if one
* was passed in.
*/
static prepPublicKey (spkac) {
if (!spkac) { return null }
spkac = utils.stripLineEndings(spkac)
spkac = new Buffer(spkac, 'utf-8')
return spkac
}
/**
* Generates an X509Certificate from the passed-in `spkac` value.
*
* @throws {Error} See `webid` module's `generate()` function.
*
* @return {Promise<WebIdTlsCertificate>} Resolves to self (chainable), with
* the `.certificate` property initialized.
*/
generateCertificate () {
const certOptions = {
spkac: this.spkac,
agent: this.webId,
commonName: this.commonName,
issuer: this.issuer
}
return new Promise((resolve, reject) => {
webidTls.generate(certOptions, (err, certificate) => {
if (err) {
reject(err)
} else {
this.certificate = certificate
resolve(this)
}
})
})
}
/**
* Returns the URI (with hash fragment) for this certificate's public key,
* to be used as a subject of RDF triples in a user's WebID Profile.
*
* @throws {TypeError} HTTP 400 error if no `webId` has been set.
*
* @return {string}
*/
get keyUri () {
if (!this.webId) {
const error = new TypeError('Cannot construct key URI, WebID is missing')
error.status = 400
throw error
}
const profileUri = this.webId.split('#')[0]
return profileUri + '#key-' + this.date.getTime()
}
/**
* Returns the public key exponent (for adding to a user's WebID Profile)
*
* @throws {TypeError} HTTP 400 error if no certificate has been generated.
*
* @return {string}
*/
get exponent () {
if (!this.certificate) {
const error = new TypeError('Cannot return exponent, certificate has not been generated')
error.status = 400
throw error
}
return this.certificate.publicKey.e.toString()
}
/**
* Returns the public key modulus (for adding to a user's WebID Profile)
*
* @throws {TypeError} HTTP 400 error if no certificate has been generated.
*
* @return {string}
*/
get modulus () {
if (!this.certificate) {
const error = new TypeError('Cannot return modulus, certificate has not been generated')
error.status = 400
throw error
}
return this.certificate.publicKey.n.toString(16).toUpperCase()
}
/**
* Converts the generated cert to DER format and returns it.
*
* @return {X509Certificate|null} In DER format
*/
toDER () {
if (!this.certificate) {
return null
}
const certificateAsn = forge.pki.certificateToAsn1(this.certificate)
// Convert to DER
const certificateDer = forge.asn1.toDer(certificateAsn).getBytes()
// new Buffer(der, 'binary')
return certificateDer
}
}
module.exports = WebIdTlsCertificate