Class: Oydid

Inherits:
Object
  • Object
show all
Defined in:
lib/oydid.rb,
lib/oydid/vc.rb,
lib/oydid/log.rb,
lib/oydid/basic.rb,
lib/oydid/didcomm.rb

Constant Summary collapse

LOCATION_PREFIX =
"@"
DEFAULT_LOCATION =
"https://oydid.ownyourdata.eu"
DEFAULT_DIGEST =
"sha2-256"
SUPPORTED_DIGESTS =

BLAKE2b digests are named by digest size in BYTES here (house convention, e.g. "blake2b-16" is a 16 byte / 128 bit digest). This deliberately deviates from the multicodec registry, where blake2b-N counts bits.

["sha2-256", "sha2-512", "sha3-224", "sha3-256", "sha3-384", "sha3-512",
"blake2b-16", "blake2b-17", "blake2b-18", "blake2b-19", "blake2b-20",
"blake2b-21", "blake2b-22", "blake2b-23", "blake2b-32", "blake2b-64"]
DEFAULT_ENCODING =
"base58btc"
SUPPORTED_ENCODINGS =
["base16", "base32", "base58btc", "base64"]
LOG_HASH_OPTIONS =
{:digest => "sha2-256", :encode => "base58btc"}
ED25519_SECURITY_SUITE =
"https://w3id.org/security/suites/ed25519-2020/v1"
JWS_SECURITY_SUITE =
"https://w3id.org/security/suites/jws-2020/v1"
DEFAULT_PUBLIC_RESOLVER =
"https://dev.uniresolver.io/1.0/identifiers/"
BLAKE2B_EXTRA_CODES =

Single-byte multicodec codes for the intermediate BLAKE2b digest sizes (17-23 bytes), needed to keep a did:oyd identifier short enough for the 50 character URL limit of the EU Digital Product Passport registry.

These sizes cannot take their code from Multicodecs: multi_hash packs the code into a single byte, which effectively truncates the registry's two-byte blake2b codes to their low byte - and those collide with the SHA codes already in use (0xb212 -> 0x12 = sha2-256, 0xb214 -> 0x14 = sha3-512, 0xb216 -> 0x16 = sha3-256, 0xb217 -> 0x17 = sha3-224).

Instead the free, contiguous range 0x0B-0x11 is used (code = size - 6). Codes are kept low and single-byte on purpose: the value of the leading byte feeds into the base58btc length, and a low code saves a character.

{
    17 => 0x0B, 18 => 0x0C, 19 => 0x0D, 20 => 0x0E,
    21 => 0x0F, 22 => 0x10, 23 => 0x11
}.freeze
REVOKED_ERROR_CODE =

a DID is revoked when the REVOCATION record referenced by the current TERMINATE record has been published and no UPDATE record follows it

410

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_hash(log) ⇒ Object

log functions -----------------------------



6
7
8
9
10
11
12
13
14
# File 'lib/oydid/log.rb', line 6

def self.add_hash(log)
    log.map do |item|
        item["entry-hash"] = multi_hash(canonical(item.slice("ts","op","doc","sig","previous")), LOG_HASH_OPTIONS).first
        if item.transform_keys(&:to_s)["op"] == 1 # REVOKE
            item["sub-entry-hash"] = multi_hash(canonical(item.slice("ts","op","doc","sig")), LOG_HASH_OPTIONS).first
        end
        item
    end
end

.base64_url_decode(str) ⇒ Object



1004
1005
1006
# File 'lib/oydid/basic.rb', line 1004

def self.base64_url_decode(str)
    Base64.urlsafe_decode64(str + '=' * (4 - str.length % 4))
end

.build_jwks(content, input_did, options) ⇒ Object

other helpers -----------------------------



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
# File 'lib/oydid/didcomm.rb', line 145

def self.build_jwks(content, input_did, options)

    tmp_did_hash = input_did.delete_prefix("did:oyd:") rescue ""
    tmp_did10 = tmp_did_hash[0,10] + "_private_key.enc" rescue ""
    privateKey, msg = getPrivateKey(options[:doc_enc], options[:doc_pwd], options[:doc_key], tmp_did10, options)
    if privateKey.nil?
        return [nil, "private document key not available: " + msg.to_s]
    end

    code, length, digest = multi_decode(privateKey).first.unpack('SCa*')
    case Multicodecs[code].name
    when 'ed25519-priv'        
        signing_key = Ed25519::SigningKey.new(digest)
    else
        return [nil, "unsupported key codec: " + Multicodecs[code].name.to_s]
    end

    jwk = content
    jwk['iss'] = input_did
    jwk['sub'] = input_did
    jwk['iat'] = Time.now.to_i
    jwk['exp'] = Time.now.to_i + 120
    jwk['jti'] = SecureRandom.uuid

    algorithm = 'EdDSA'
    headers = {
        alg: algorithm,
        typ: 'JWT',
        kid: input_did + '#key-doc' }

    jwks = JWT.encode(jwk, signing_key, algorithm, headers)
    return [jwks, nil]
end

.canonical(message) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/oydid/basic.rb', line 116

def self.canonical(message)
    if message.is_a? String
        message = JSON.parse(message) rescue message
    else
        message = JSON.parse(message.to_json) rescue message
    end
    message.to_json_c14n
end

.check_cmsm(session, options) ⇒ Object



861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'lib/oydid.rb', line 861

def self.check_cmsm(session, options)
    # in-process callers can supply a store object (responding to
    # #get(session)) to read CMSM data from a local database
    if options[:cmsm_store]
        payload = options[:cmsm_store].get(session)
        if payload.nil?
            return [nil, "unknown or expired CMSM session"]
        end
        payload = payload.transform_keys(&:to_s) if payload.is_a?(Hash)
        return [payload, ""]
    end

    doc_location = options[:doc_location]
    if doc_location.to_s == ""
        doc_location = DEFAULT_LOCATION
    end
    doc_location = doc_location.sub("%3A%2F%2F","://").sub("%3A", ":")

    case doc_location.to_s
    when /^http/
        retVal = HTTParty.get(doc_location + "/cmsm/" + session.to_s)
        if retVal.code != 200
            msg = retVal.parsed_response["error"].to_s rescue ""
            if msg.to_s == ""
                msg = "invalid response from " + doc_location.to_s + "/cmsm/" + session.to_s
            end
            return [nil, msg]
        end
        return [retVal.parsed_response.transform_keys(&:to_s), ""]
    else
        return [nil, "location not supported for querying data in cmsm-flow"]
    end
    return [payload, ""]
end

.clone(did, options) ⇒ Object



1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
# File 'lib/oydid.rb', line 1289

def self.clone(did, options)
    # check if locations differ
    target_location = options[:doc_location]
    if target_location.to_s == ""
        target_location = DEFAULT_LOCATION
    end
    if did.include?(LOCATION_PREFIX)
        tmp = did.split(LOCATION_PREFIX)
        did = tmp[0]
        source_location = tmp[1]
    end
    if did.include?(CGI.escape LOCATION_PREFIX)
        tmp = did.split(CGI.escape LOCATION_PREFIX)
        did = tmp[0] 
        source_location = tmp[1]
    end

    if source_location.to_s == ""
        source_location = DEFAULT_LOCATION
    end
    if target_location == source_location
        return [nil, "cannot clone to same location (" + target_location.to_s + ")"]
    end

    # get original did info
    options[:doc_location] = source_location
    options[:log_location] = source_location
    source_did, msg = read(did, options)
    if source_did.nil?
        return [nil, "cannot resolve DID (on cloning DID)"]
    end
    if source_did["error"] != 0
        return [nil, source_did["message"].to_s]
    end
    if source_did["doc_log_id"].nil?
        return [nil, "cannot parse DID log"]
    end        
    source_log = source_did["log"].first(source_did["doc_log_id"] + 1).last.to_json

    # write did to new location
    options[:doc_location] = target_location
    options[:log_location] = target_location
    options[:previous_clone] = multi_hash(canonical(source_log), LOG_HASH_OPTIONS).first + LOCATION_PREFIX + source_location
    options[:source_location] = source_location
    options[:source_did] = source_did["did"]
    retVal, msg = write(source_did["doc"]["doc"], nil, "clone", options)
    return [retVal, msg]
end

.cmsm_challenge(value, with, state, options) ⇒ Object

persist the state of a CMSM flow and return the next challenge: the value the client has to sign, and which of its keys it has to sign it with



801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/oydid.rb', line 801

def self.cmsm_challenge(value, with, state, options)
    session, msg = persist_cmsm(options[:cmsm_session], state, options)
    if session.nil?
        return [nil, nil, nil, msg]
    end
    collected = [state[:sig_rev], state[:sig_doc], state[:sig_create]].count { |sig| sig.to_s != "" }
    cmsm_doc = {
        cmsm: true,
        session: session,
        phase: collected + 1,
        pk: state[:publicKey],
        sign: value,
        with: with
    }
    return [cmsm_doc, nil, nil, "cmsm"]
end

.cmsm_state(publicKey, pubRevoKey, revocationKey, did_doc, ts, doc_location, sig_rev, sig_doc, sig_create, did_old = nil, log_revoke_old = nil) ⇒ Object

state of a CMSM flow that has to survive between phases. Deliberately only inputs and collected signatures - no derived hashes: every phase recomputes r1 / l2_doc / the DID from these values, so there is nothing to keep in sync.



779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/oydid.rb', line 779

def self.cmsm_state(publicKey, pubRevoKey, revocationKey, did_doc, ts, doc_location, sig_rev, sig_doc, sig_create, did_old = nil, log_revoke_old = nil)
    {
        publicKey: publicKey,
        pubRevoKey: pubRevoKey,
        revocationKey: revocationKey,
        did_doc: did_doc,
        ts: ts,
        doc_location: doc_location,
        sig_rev: sig_rev,
        sig_doc: sig_doc,
        # signature over l1_doc: the CREATE entry on create, the UPDATE entry
        # on update - the latter is signed with the OLD document key
        sig_create: sig_create,
        # update only: which DID is being replaced, and the revocation record
        # of its current document (supplied by the client in phase 1)
        did_old: did_old,
        log_revoke_old: log_revoke_old
    }
end

.create(content, options) ⇒ Object



198
199
200
# File 'lib/oydid.rb', line 198

def self.create(content, options)
    return write(content, nil, "create", options)
end

.create_vc(content, options) ⇒ Object



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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/oydid/vc.rb', line 136

def self.create_vc(content, options)
    if options[:issuer_privateKey].to_s == ""
        return [nil, "missing issuer private key"]
    end
    code, length, digest = multi_decode(options[:issuer_privateKey]).first.unpack('SCa*')
    case options[:vc_type].to_s
    when 'Ed25519Signature2020'
        if Multicodecs[code].name != 'ed25519-priv'
            return [nil, "combination of credential type '" + options[:vc_type].to_s + "' and key type '" + Multicodecs[code].name.to_s + "' not supported"]
        end
    when 'JsonWebSignature2020'
        if Multicodecs[code].name != 'p256-priv'
            return [nil, "combination of credential type '" + options[:vc_type].to_s + "' and key type '" + Multicodecs[code].name.to_s + "' not supported"]
        end
    else
        return [nil, "unsupported credential type '" + options[:vc_type].to_s + "'"]
    end
    vercred = content
    # set the context, which establishes the special terms used
    if content["@context"].nil?
        case options[:vc_type].to_s
        when "Ed25519Signature2020"
            vercred["@context"] = ["https://www.w3.org/ns/credentials/v2"]
        when "JsonWebSignature2020"
            vercred["@context"] = ["https://www.w3.org/2018/credentials/v1"]
        else
            return [nil, "invalid credential type '" + options[:vc_type].to_s + "'"]
        end
    else
        vercred["@context"] = content["@context"]
    end
    if vercred["@context"].to_s == "" || vercred["@context"].to_s == "{}" || vercred["@context"].to_s == "[]"
        return [nil, "invalid '@context'"]
    end
    if content["type"].nil?
        vercred["type"] = ["VerifiableCredential"]
    else
        vercred["type"] = content["type"]
    end
    if vercred["type"].to_s == "" || vercred["type"].to_s == "{}" || vercred["type"].to_s == "[]"
        return [nil, "invalid 'type'"]
    end
    if content["issuer"].nil?
        vercred["issuer"] = options[:issuer]
    else
        vercred["issuer"] = content["issuer"]
    end
    if vercred["issuer"].to_s == "" || vercred["issuer"].to_s == "{}" || vercred["issuer"].to_s == "[]"
        return [nil, "invalid 'issuer'"]
    end
    if options[:ts].nil?
        vercred["issuanceDate"] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
    else
        vercred["issuanceDate"] = Time.at(options[:ts]).utc.iso8601
    end
    if content["credentialSubject"].nil?
        vercred["credentialSubject"] = {"id": options[:holder]}.merge(content)
    else
        vercred["credentialSubject"] = content["credentialSubject"]
        if vercred["credentialSubject"]["id"].nil?
            if options[:holder].nil?
                return [nil, "missing 'id' (of holder) in 'credentialSubject'"]
            end
            vercred["credentialSubject"]["id"] = options[:holder]
        end
    end
    if vercred["credentialSubject"].to_s == "" || vercred["credentialSubject"].to_s == "{}" || vercred["credentialSubject"].to_s == "[]"
        return [nil, "invalid 'credentialSubject'"]
    end

    case options[:vc_type].to_s
    when 'Ed25519Signature2020'
        if content["proof"].nil?
            proof = {}
            proof["type"] = "Ed25519Signature2020"
            proof["verificationMethod"] = options[:issuer].to_s + "#key-doc"
            proof["proofPurpose"] = "assertionMethod"
            id_vc = vercred.dup
            id_vc["proof"] = proof
            identifier_str = multi_hash(canonical(id_vc), options).first
            if options[:vc_location].nil?
                vercred["identifier"] = identifier_str
            else
                vc_location = options[:vc_location].to_s
                if !vc_location.start_with?("http")
                    vc_location = "https://" + token_url
                end
                if !vc_location.end_with?('/')
                    vc_location += '/'
                end
                if !vc_location.end_with?('credentials/')
                    vc_location += 'credentials/'
                end
                vercred["id"] = vc_location + identifier_str
            end

            vercred, errmsg = vc_proof(vercred, proof, options[:issuer_privateKey], options)
            if vercred.nil?
                return [nil, errmsg]
            end
            # proof["proofValue"] = sign(vercred["credentialSubject"].transform_keys(&:to_s).to_json_c14n, options[:issuer_privateKey], []).first
        else
            id_vc = vercred.dup
            content["proof"].delete("proofValue")
            id_vc["proof"] = content["proof"]
            identifier_str = multi_hash(canonical(id_vc), options).first
            if options[:vc_location].nil?
                vercred["identifier"] = identifier_str
            else
                vercred["id"] = options[:vc_location].to_s + identifier_str
            end

            vercred, errmsg = vc_proof(vercred, content["proof"], options[:issuer_privateKey], options)
            if vercred.nil?
                return [nil, errmsg]
            end
        end
        if vercred["proof"].to_s == "" || vercred["proof"].to_s == "{}" || vercred["proof"].to_s == "[]"
            return [nil, "invalid 'proof'"]
        end

    when 'JsonWebSignature2020'
        jwt_vc = {}
        jwt_vc["vc"] = vercred.dup
        jwt_vc["exp"] = (Time.now + (3 * 30 * 24 * 60 * 60)).to_i
        jwt_vc["iss"] = vercred["issuer"]
        jwt_vc["nbf"] = 
        if options[:ts].nil?
            jwt_vc["nbf"] = Time.now.utc.to_i
        else
            jwt_vc["nbf"] = options[:ts].to_i
        end
        identifier_str = multi_hash(canonical(vercred), options).first
        jwt_vc["jti"] = identifier_str
        jwt_vc["sub"] = options[:holder]

        vercred = jwt_vc.dup
    else
        return [nil, "unsupported credential type '" + options[:vc_type].to_s + "'"]
    end

    return [vercred, ""]
end

.create_vc_proof(content, options) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/oydid/vc.rb', line 280

def self.create_vc_proof(content, options)
    if content["id"].nil?
        content["id"] = options[:issuer]
    end
    proof = {}
    proof["type"] = "Ed25519Signature2020"
    proof["verificationMethod"] = options[:issuer].to_s
    proof["proofPurpose"] = "assertionMethod"

    content, errmsg = vc_proof(content, proof, options[:issuer_privateKey], options)
    if content.nil?
        return [nil, errmsg]
    end
    # proof["proofValue"] = sign(content.to_json_c14n, options[:issuer_privateKey], []).first

    return [content["proof"], ""]
end

.create_vp(content, options) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/oydid/vc.rb', line 364

def self.create_vp(content, options)
    verpres = {}
    # set the context, which establishes the special terms used
    if !content["@context"].nil?
        verpres["@context"] = content["@context"].dup
    else
        verpres["@context"] = ["https://www.w3.org/ns/credentials/v2", ED25519_SECURITY_SUITE]
    end
    verpres["type"] = ["VerifiablePresentation"]
    verpres["verifiableCredential"] = [content].flatten

    proof = {}
    case options[:vc_type].to_s
    when 'Ed25519Signature2020'
        proof['type'] = 'Ed25519Signature2020'
            if !options[:ts].nil?
                proof["created"] = Time.at(options[:ts]).utc.strftime("%Y-%m-%dT%H:%M:%SZ")
            end
            proof["verificationMethod"] = options[:holder].to_s
            proof["proofPurpose"] = "authentication"
            verpres, errmsg = vc_proof(verpres, proof, options[:holder_privateKey], options)
    when 'JsonWebSignature2020'
        verpres["holder"] = options[:holder].to_s
        proof['type'] = 'JsonWebSignature2020'
        if options[:ts].nil?
            proof['created'] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
        else
            proof["created"] = Time.at(options[:ts]).utc.strftime("%Y-%m-%dT%H:%M:%SZ")
        end
        proof["proofPurpose"] = "authentication"
        proof["verificationMethod"] = options[:holder].to_s + '#key-doc'

        verpres["proof"] = proof

        options[:issuer] = options[:holder]
        options[:issuer_privateKey] = options[:holder_privateKey]
        jwt, msg = Oydid.jwt_from_vc(verpres, options)
        parts = jwt.split('.')
        detached_jws = "#{parts[0]}..#{parts[2]}"

        proof['jws'] = detached_jws
        verpres["proof"] = proof
    else
        return [nil, "unsupported credential type '" + options[:vc_type].to_s + "'"]
    end

    # private_key = generate_private_key(options[:issuer_privateKey], "ed25519-priv", []).first
    # proof["proofValue"] = sign([content].flatten.to_json_c14n, options[:holder_privateKey], []).first
    # verpres["proof"] = proof

    # specify the identifier of the credential
    verpres["identifier"] = hash(canonical(verpres.to_json))
    return [verpres, ""]
end

.dag2array(dag, log_array, index, result, options) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/oydid/log.rb', line 222

def self.dag2array(dag, log_array, index, result, options)
    if options.transform_keys(&:to_s)["trace"]
        if options[:silent].nil? || !options[:silent]
            puts "    vertex " + index.to_s + " at " + log_array[index]["ts"].to_s + " op: " + log_array[index]["op"].to_s + " doc: " + log_array[index]["doc"].to_s
        end
    end
    result << log_array[index]
    dag.vertices[index].successors.each do |s|
        # check if successor has predecessor that is not self (i.e. REVOKE with TERMINATE)
        s.predecessors.each do |p|
            if p[:id] != index
                if options.transform_keys(&:to_s)["trace"]
                    if options[:silent].nil? || !options[:silent]
                        puts "    vertex " + p[:id].to_s + " at " + log_array[p[:id]]["ts"].to_s + " op: " + log_array[p[:id]]["op"].to_s + " doc: " + log_array[p[:id]]["doc"].to_s
                    end
                end
                result << log_array[p[:id]]
            end
        end unless s.predecessors.length < 2
        dag2array(dag, log_array, s[:id], result, options)
    end unless dag.vertices[index].successors.count == 0
    result.uniq
end

.dag2array_terminate(dag, log_array, index, result, options) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/oydid/log.rb', line 246

def self.dag2array_terminate(dag, log_array, index, result, options)
    if options.transform_keys(&:to_s)["trace"]
        if options[:silent].nil? || !options[:silent]
            puts "    vertex " + index.to_s + " at " + log_array[index]["ts"].to_s + " op: " + log_array[index]["op"].to_s + " doc: " + log_array[index]["doc"].to_s
        end
    end
    dag.vertices[index].predecessors.each do |p|
        if p[:id] != index
            if options.transform_keys(&:to_s)["trace"]
                if options[:silent].nil? || !options[:silent]
                    puts "    vertex " + p[:id].to_s + " at " + log_array[p[:id]]["ts"].to_s + " op: " + log_array[p[:id]]["op"].to_s + " doc: " + log_array[p[:id]]["doc"].to_s
                end
            end
            result << log_array[p[:id]]
        end
    end unless dag.vertices[index].nil?
    result << log_array[index]
    result.uniq
end

.dag_did(logs, options) ⇒ Object



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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/oydid/log.rb', line 98

def self.dag_did(logs, options)
    dag = DAG.new
    dag_log = []
    log_hash = []

    # calculate hash values for each entry and build vertices
    i = 0
    create_entries = 0
    create_index = nil
    terminate_indices = []
    logs.each do |el|
        case el["op"].to_i
        when 0 # TERMINATE
            terminate_indices << i
        when 2 # CREATE
            create_entries += 1
            create_index = i
        end
        log_hash << Oydid.multi_hash(Oydid.canonical(el.slice("ts","op","doc","sig","previous")), LOG_HASH_OPTIONS).first
        dag_log << dag.add_vertex(id: i)
        i += 1
    end unless logs.nil?
    if create_entries != 1
        return [nil, nil, nil, "wrong number of CREATE entries (" + create_entries.to_s + ") in log" ]
    end
    if terminate_indices.length == 0
        return [nil, nil, nil, "missing TERMINATE entries" ]
    end 

    # create provisional edges between vertices
    i = 0
    logs.each do |el|
        el["previous"].each do |p|
            position = log_hash.find_index(p)
            if !position.nil?
                dag.add_edge from: dag_log[position], to: dag_log[i]
            end
        end unless el["previous"] == []
        i += 1
    end unless logs.nil?

    # identify tangling TERMINATE entry
    i = 0
    terminate_entries = 0
    terminate_overall = 0
    terminate_index = nil
    revoked_terminate_found = false
    logs.each do |el|
        if el["op"].to_i == 0 # TERMINATE
            if dag.vertices[i].successors.length == 0
                terminate_entries += 1
                terminate_index = i
            end
            terminate_overall += 1
        elsif el["op"].to_i == 1 # REVOKE
            # get terminate_index for revoked DIDs
            if dag.vertices[i].successors.length == 0
                dag.vertices[i].predecessors.each do |l|
                    if logs[l[:id]]["op"].to_i == 0 # TERMINATE
                        terminate_index = l[:id]
                        revoked_terminate_found = true
                    end
                end
            end
        end
        i += 1
    end unless logs.nil?

    # NOTE: this is a structural check on the log and must not depend on how
    # verbose the caller wants to be. Suppressing it under :silent returned a
    # DID document for logs that cannot be resolved.
    if terminate_entries != 1 && !options[:log_complete] && !options[:followAlsoKnownAs]
        # a revoked DID has no tangling TERMINATE entry (its TERMINATE is
        # followed by the REVOKE record). That is not a broken log: continue
        # so that dag_update can report the revocation - or apply DID
        # Rotation - instead of the unspecific "cannot resolve DID".
        unless terminate_entries == 0 && revoked_terminate_found
            return [nil, nil, nil, "cannot resolve DID" ]
        end
    end

    # create actual edges between vertices (but only use last terminate index for delegates)
    dag = DAG.new
    dag_log = []
    log_hash = []

    # calculate hash values for each entry and build vertices
    i = 0
    create_entries = 0
    create_index = nil
    terminate_indices = []
    logs.each do |el|
        case el["op"].to_i
        when 0 # TERMINATE
            terminate_indices << i
        when 2 # CREATE
            create_entries += 1
            create_index = i
        end
        log_hash << Oydid.multi_hash(Oydid.canonical(el.slice("ts","op","doc","sig","previous")), LOG_HASH_OPTIONS).first
        dag_log << dag.add_vertex(id: i)
        i += 1
    end unless logs.nil?
    i = 0
    logs.each do |el|
        el["previous"].each do |p|
            position = log_hash.find_index(p)
            if !position.nil?
                if logs[position]["op"].to_i == 5 # DELEGATE
                    if i == terminate_index
                        # only delegates in the last terminate index are relevant
                        dag.add_edge from: dag_log[position], to: dag_log[i]
                    end
                else
                    dag.add_edge from: dag_log[position], to: dag_log[i]
                end
            end
        end unless el["previous"] == []
        i += 1
    end unless logs.nil?

    return [dag, create_index, terminate_index, ""]
end

.dag_update(currentDID, options) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/oydid/log.rb', line 270

def self.dag_update(currentDID, options)
    i = 0
    revoked = false
    rotation_performed = false
    doc_location = options[:doc_location].to_s
    initial_did = currentDID["did"].to_s.dup
    initial_did = initial_did.delete_prefix("did:oyd:")
    if initial_did.include?(LOCATION_PREFIX)
        tmp = initial_did.split(LOCATION_PREFIX)
        initial_did = tmp[0] 
        doc_location = tmp[1]
    end
    if initial_did.include?(CGI.escape LOCATION_PREFIX)
        tmp = initial_did.split(CGI.escape LOCATION_PREFIX)
        initial_did = tmp[0] 
        doc_location = tmp[1]
    end
    doc_location = doc_location.gsub("%3A",":")
    doc_location = doc_location.gsub("%2F%2F","//")
    current_public_doc_key = ""
    verification_output = false
    currentDID["log"].each do |el|
        case el["op"]
        when 2,3 # CREATE, UPDATE
            currentDID["doc_log_id"] = i
            doc_did = el["doc"]
            did_hash = doc_did.delete_prefix("did:oyd:")
            did_hash = did_hash.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
            did10 = did_hash[0,10]

            # pass options through: without them options[:local_store] is lost
            # and the lookup falls back to HTTP against DEFAULT_LOCATION, which
            # breaks every in-process caller that keeps its DIDs locally
            doc = retrieve_document_raw(doc_did, did10 + ".doc", doc_location, options)
            if doc.first.nil?
                currentDID["error"] = 2
                msg = doc.last.to_s
                if msg == ""
                    msg = "cannot retrieve " + doc_did.to_s
                end
                currentDID["message"] = msg
                return currentDID
            end
            doc = doc.first["doc"]
            if el["op"] == 2 # CREATE
                # The CREATE signature used to be unobtainable in the
                # Client-Managed-Secret-Mode: its payload l1_doc is the hash
                # of a DID document that already contains the TERMINATE
                # signature, so it can only be collected in a phase of its
                # own. CMSM has that phase now and new DIDs always carry the
                # signature - but DIDs created before it do not, and
                # rejecting them would make them unresolvable.
                #
                # The check is therefore tolerant by default and strict on
                # request: a relying party that only accepts DIDs created
                # with a signed CREATE entry sets options[:strict_create_sig].
                if el["sig"].nil?
                    if options[:strict_create_sig]
                        currentDID["error"] = 1
                        currentDID["message"] = "missing signature in CREATE log entry"
                        return currentDID
                    end
                elsif !match_log_did?(el, doc)
                    currentDID["error"] = 1
                    currentDID["message"] = "Signatures in log don't match"
                    return currentDID
                end
            end
            currentDID["did"] = doc_did
            currentDID["doc"] = doc
            # since hash is guaranteed during retrieve_document this check is not necessary
            # if hash(canonical(doc)) != did_hash
            #     currentDID["error"] = 1
            #     currentDID["message"] = "DID identifier and DID document don't match"
            #     if did_hash == initial_did
            #         verification_output = true
            #     end
            #     if verification_output
            #         currentDID["verification"] += "identifier: " + did_hash.to_s + "\n"
            #         currentDID["verification"] += "⛔ does not match DID Document:" + "\n"
            #         currentDID["verification"] += JSON.pretty_generate(doc) + "\n"
            #         currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#calculate_hash)" + "\n\n"
            #     end
            #     return currentDID
            # end
            if did_hash == initial_did
                verification_output = true
            end
            if verification_output
                currentDID["verification"] += "identifier: " + did_hash.to_s + "\n"
                currentDID["verification"] += "✅ is hash of DID Document:" + "\n"
                currentDID["verification"] += JSON.pretty_generate(doc) + "\n"
                currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#calculate_hash)" + "\n\n"
            end
            current_public_doc_key = currentDID["doc"]["key"].split(":").first rescue ""

        when 0 # TERMINATE
            currentDID["termination_log_id"] = i

            doc_did = currentDID["did"]
            did_hash = doc_did.delete_prefix("did:oyd:")
            did_hash = did_hash.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
            did10 = did_hash[0,10]
            # pass options through: without them options[:local_store] is lost
            # and the lookup falls back to HTTP against DEFAULT_LOCATION, which
            # breaks every in-process caller that keeps its DIDs locally
            doc = retrieve_document_raw(doc_did, did10 + ".doc", doc_location, options)
            doc = doc.first["doc"]

            if !Oydid.match_log_did?(el, doc)
                currentDID["error"] = 1
                currentDID["message"] = "Signatures in log don't match"
                return currentDID
            end

            # since it retrieves a DID that previously existed, this test is not necessary
            # if doc.first.nil?
            #     currentDID["error"] = 2
            #     currentDID["message"] = doc.last.to_s
            #     return currentDID
            # end
            term = doc["log"]
            log_location = term.split(LOCATION_PREFIX).last.split(CGI.escape LOCATION_PREFIX).last rescue ""
            if log_location.to_s == "" || log_location == term
                log_location = DEFAULT_LOCATION
            end
            term = term.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
            log_options = options.dup
            el_hash = el["doc"].split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
            log_options[:digest] = get_digest(el_hash).first
            log_options[:encode] = get_encoding(el_hash).first
            if multi_hash(canonical(el.slice("ts","op","doc","sig","previous")), log_options).first != term
                currentDID["error"] = 1
                currentDID["message"] = "Log reference and record don't match"
                if verification_output
                    currentDID["verification"] += "'log' reference in DID Document: " + term.to_s + "\n"
                    currentDID["verification"] += "⛔ does not match TERMINATE log record:" + "\n"
                    currentDID["verification"] += JSON.pretty_generate(el) + "\n"
                    currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#calculate_hash)" + "\n\n"
                end
                return currentDID
            end
            if verification_output
                currentDID["verification"] += "'log' reference in DID Document: " + term.to_s + "\n"
                currentDID["verification"] += "✅ is hash of TERMINATE log record:" + "\n"
                currentDID["verification"] += JSON.pretty_generate(el) + "\n"
                currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#calculate_hash)" + "\n\n"
            end

            # check if there is a revocation entry
            revocation_record = {}
            revoc_term = el["doc"]
            revoc_term = revoc_term.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
            revoc_term_found = false
            log_array, msg = retrieve_log(did_hash, did10 + ".log", log_location, options)
            log_array.each do |log_el|
                log_el_structure = log_el.dup
                if log_el["op"].to_i == 1 # TERMINATE
                    log_el_structure.delete("previous")
                end
                if multi_hash(canonical(log_el_structure.slice("ts","op","doc","sig","previous")), log_options).first == revoc_term
                    revoc_term_found = true
                    revocation_record = log_el.dup
                    if verification_output
                        currentDID["verification"] += "'doc' reference in TERMINATE log record: " + revoc_term.to_s + "\n"
                        currentDID["verification"] += "✅ is hash of REVOCATION log record (without 'previous' attribute):" + "\n"
                        currentDID["verification"] += JSON.pretty_generate(log_el) + "\n"
                        currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#calculate_hash)" + "\n\n"
                    end
                    break
                end
            end unless log_array.nil?
            # this should actually be covered by retrieve_log in the block above
            # (actually I wasn't able to craft a test case covering this part...)
            # if !options.transform_keys(&:to_s)["log_location"].nil?
            #     log_array, msg = retrieve_log(revoc_term, did10 + ".log", options.transform_keys(&:to_s)["log_location"], options)
            #     log_array.each do |log_el|
            #         if log_el["op"] == 1 # REVOKE
            #             log_el_structure = log_el.delete("previous")
            #         else
            #             log_el_structure = log_el
            #         end
            #         if hash(canonical(log_el_structure)) == revoc_term
            #             revoc_term_found = true
            #             revocation_record = log_el.dup
            #             if verification_output
            #                 currentDID["verification"] += "'doc' reference in TERMINATE log record: " + revoc_term.to_s + "\n"
            #                 currentDID["verification"] += "✅ is hash of REVOCATION log record (without 'previous' attribute):" + "\n"
            #                 currentDID["verification"] += JSON.pretty_generate(log_el) + "\n"
            #                 currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#calculate_hash)" + "\n\n"
            #             end
            #             break
            #         end
            #     end
            # end

            if revoc_term_found
                update_term_found = false
                log_array.each do |log_el|
                    if log_el["op"].to_i == 3
                        if log_el["previous"].include?(multi_hash(canonical(revocation_record), LOG_HASH_OPTIONS).first)
                            update_term_found = true
                            message = log_el["doc"].to_s
                            signature = log_el["sig"]
                            # public_key = current_public_doc_key.to_s
                            extend_currentDID = currentDID.dup
                            extend_currentDID["log"] = extend_currentDID["full_log"]
                            # !!!TODO: check for delegates only at certain point in time
                            pubKeys, msg = Oydid.getDelegatedPubKeysFromFullDidDocument(extend_currentDID, "doc")
                            signature_verification = false
                            used_pubkey = ""
                            pubKeys.each do |key|
                                if Oydid.verify(message, signature, key).first
                                    signature_verification = true
                                    used_pubkey = key
                                    break
                                end
                            end
                            if signature_verification
                                if verification_output
                                    currentDID["verification"] += "found UPDATE log record:" + "\n"
                                    currentDID["verification"] += JSON.pretty_generate(log_el) + "\n"
                                    currentDID["verification"] += "✅ public key: " + used_pubkey.to_s + "\n"
                                    currentDID["verification"] += "verifies 'doc' reference of new DID Document: " + log_el["doc"].to_s + "\n"
                                    currentDID["verification"] += log_el["sig"].to_s + "\n"
                                    currentDID["verification"] += "of next DID Document (Details: https://ownyourdata.github.io/oydid/#verify_signature)" + "\n"

                                    next_doc_did = log_el["doc"].to_s
                                    next_doc_location = doc_location
                                    next_did_hash = next_doc_did.delete_prefix("did:oyd:")
                                    next_did_hash = next_did_hash.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
                                    next_did10 = next_did_hash[0,10]
                                    next_doc = retrieve_document_raw(next_doc_did, next_did10 + ".doc", next_doc_location, options)
                                    if next_doc.first.nil?
                                        currentDID["error"] = 2
                                        currentDID["message"] = next_doc.last
                                        return currentDID
                                    end
                                    next_doc = next_doc.first["doc"]
                                    if pubKeys.include?(next_doc["key"].split(":").first)
                                        currentDID["verification"] += "⚠️  no key rotation in updated DID Document" + "\n"
                                    end
                                    currentDID["verification"] += "\n"
                                end
                            else
                                currentDID["error"] = 1
                                currentDID["message"] = "Signature does not match"
                                if verification_output
                                    new_doc_did = log_el["doc"].to_s
                                    new_doc_location = doc_location
                                    new_did_hash = new_doc_did.delete_prefix("did:oyd:")
                                    new_did_hash = new_did_hash.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
                                    new_did10 = new_did_hash[0,10]
                                    new_doc = retrieve_document(new_doc_did, new_did10 + ".doc", new_doc_location, {}).first
                                    currentDID["verification"] += "found UPDATE log record:" + "\n"
                                    currentDID["verification"] += JSON.pretty_generate(log_el) + "\n"
                                    currentDID["verification"] += "⛔ none of available public keys (" + pubKeys.join(", ") + ")\n"
                                    currentDID["verification"] += "does not verify 'doc' reference of new DID Document: " + log_el["doc"].to_s + "\n"
                                    currentDID["verification"] += log_el["sig"].to_s + "\n"
                                    currentDID["verification"] += "next DID Document (Details: https://ownyourdata.github.io/oydid/#verify_signature)" + "\n"
                                    currentDID["verification"] += JSON.pretty_generate(new_doc) + "\n\n"
                                end
                                return currentDID
                            end
                            break
                        end
                    end
                end

                # the revocation record is published and no UPDATE record
                # builds on it: this DID is revoked. The verdict is only
                # acted upon after the log has been walked completely, so
                # that DID Rotation (handled in the REVOKE entry below) can
                # still take precedence.
                revoked = !update_term_found
            else
                if verification_output
                    currentDID["verification"] += "Revocation reference in log record: " + revoc_term.to_s + "\n"
                    currentDID["verification"] += "✅ cannot find revocation record searching at" + "\n"
                    currentDID["verification"] += "- " + log_location + "\n"
                    if !options.transform_keys(&:to_s)["log_location"].nil?
                        currentDID["verification"] += "- " + options.transform_keys(&:to_s)["log_location"].to_s + "\n"
                    end
                    currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#retrieve_log)" + "\n\n"
                end
                break
            end
        when 1 # revocation log entry
            # handle DID Rotation
            if (i == (currentDID["log"].length-1))
                if options[:followAlsoKnownAs]
                    current_doc = currentDID["doc"]
                    if current_doc["doc"].transform_keys(&:to_s).has_key?("alsoKnownAs")
                        rotate_DID = current_doc["doc"].transform_keys(&:to_s)["alsoKnownAs"]
                        if rotate_DID.start_with?("did:")
                            rotate_DID_method = rotate_DID.split(":").take(2).join(":")
                            did_orig = currentDID["did"]
                            if !did_orig.start_with?("did:oyd")
                                did_orig = "did:oyd:" + did_orig
                            end
                            case rotate_DID_method
                            when "did:ebsi", "did:cheqd"
                                public_resolver = DEFAULT_PUBLIC_RESOLVER
                                rotate_DID_Document = HTTParty.get(public_resolver + rotate_DID)
                                rotate_ddoc = JSON.parse(rotate_DID_Document.parsed_response)
                                rotate_ddoc = rotate_ddoc.except("didDocumentMetadata", "didResolutionMetadata")

                                # checks
                                # 1) is original DID revoked -> fulfilled, otherwise we would not be in this branch
                                # 2) das new DID reference back original DID
                                currentDID["did"] = rotate_DID
                                currentDID["doc"]["doc"] = rotate_ddoc
                                rotation_performed = true
                                if verification_output
                                    currentDID["verification"] += "DID rotation to: " + rotate_DID.to_s + "\n"
                                    currentDID["verification"] += "✅ original DID (" + did_orig + ") revoked and referenced in alsoKnownAs\n"
                                    currentDID["verification"] += "(Details: https://ownyourdata.github.io/oydid/#did_rotation)" + "\n\n"
                                end
                            when "did:oyd"
                                puts "try to resolve did:oyd with our own resolver"
                                puts "add verification text"
                            else
                                # do nothing: DID Rotation is not supported for this DID method yet
                            end
                        end
                    end
                end
            end
        when 5 # DELEGATE
            # do nothing
        else
            currentDID["error"] = 2
            currentDID["message"] = "FATAL ERROR: op code '" + el["op"].to_s + "' not implemented"
            return currentDID

        end
        i += 1
    end unless currentDID["log"].nil?

    # fail closed: a revoked DID has no resolvable DID Document unless the
    # controller rotated it to another DID via alsoKnownAs
    if revoked && !rotation_performed
        currentDID["error"] = REVOKED_ERROR_CODE
        currentDID["message"] = "revoked"
    end
    return currentDID
end

.dcpm(payload, options) ⇒ Object

DIDComm Plain Message ---------------------



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/oydid/didcomm.rb', line 7

def self.dcpm(payload, options)
    dcDoc = {}
    dcDoc["id"] = SecureRandom.random_number(10e14).to_i
    dcDoc["type"] = options[:didcomm_type]
    if !options[:didcomm_from_did].nil?
        dcDoc["from"] = options[:didcomm_from_did]
    end
    dcDoc["to"] = [options[:didcomm_to_did]]
    dcDoc["created_time"] = Time.now.utc.to_i
    dcDoc["body"] = payload
    return [dcDoc, ""]

end

.dcsm(payload, private_key_encoded, options) ⇒ Object

DIDComm Signed Message --------------------



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/oydid/didcomm.rb', line 22

def self.dcsm(payload, private_key_encoded, options)
    error = ""
    code, length, digest = multi_decode(private_key_encoded).first.unpack('SCa*')
    case Multicodecs[code].name
    when 'ed25519-priv'
        private_key = RbNaCl::Signatures::Ed25519::SigningKey.new(digest)
        token = JWT.encode payload, private_key, 'ED25519', { typ: 'JWM', kid: options[:sign_did].to_s, alg: 'ED25519' }
    else
        token = nil
        error = "unsupported key codec"
    end
    return [token, error]
end

.dcsm_verify(token, options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/oydid/didcomm.rb', line 36

def self.dcsm_verify(token, options)
    error = ""
    decoded_payload = JWT.decode token, nil, false
    pubkey_did = decoded_payload.last["kid"]
    result, msg = Oydid.read(pubkey_did, options)
    public_key_encoded = Oydid.w3c(result, options)["authentication"].first["publicKeyMultibase"]
    begin
        code, length, digest = multi_decode(public_key_encoded).first.unpack('CCa*')
        case Multicodecs[code].name
        when 'ed25519-pub'
            public_key = RbNaCl::Signatures::Ed25519::VerifyKey.new(digest)
            payload = JWT.decode token.to_s, public_key, true, { algorithm: 'ED25519' }
        else
            payload = nil
            error = "unsupported key codec"
        end
        return [payload, error]
    rescue
        return [nil, "verification failed"]
    end
end

.decode_private_key(key_encoded, options = {}) ⇒ Object



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/oydid/basic.rb', line 847

def self.decode_private_key(key_encoded, options = {})
    code, length, digest = multi_decode(key_encoded).first.unpack('SCa*')
    case Multicodecs[code].name
    when 'ed25519-priv'
        private_key = Ed25519::SigningKey.new(digest).to_bytes
    when 'p256-priv'
        group = OpenSSL::PKey::EC::Group.new('prime256v1')
        pub_key = group.generator.mul(OpenSSL::BN.new(digest, 2))
        pub_oct = pub_key.to_bn.to_s(2)

        parameters = OpenSSL::ASN1::ObjectId("prime256v1")
        parameters.tag = 0
        parameters.tagging = :EXPLICIT
        parameters.tag_class = :CONTEXT_SPECIFIC

        public_key_bitstring = OpenSSL::ASN1::BitString(pub_oct)
        public_key_bitstring.tag = 1
        public_key_bitstring.tagging = :EXPLICIT
        public_key_bitstring.tag_class = :CONTEXT_SPECIFIC

        ec_private_key_asn1 = OpenSSL::ASN1::Sequence([
            OpenSSL::ASN1::Integer(1),
            OpenSSL::ASN1::OctetString(digest),
            parameters,
            public_key_bitstring
        ])
        private_key = OpenSSL::PKey.read(ec_private_key_asn1.to_der)

    else
        return [nil, "unsupported key codec"]
    end
    return [private_key, nil]

end

.decode_public_key(key_encoded) ⇒ Object



882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'lib/oydid/basic.rb', line 882

def self.decode_public_key(key_encoded)
    begin
        pubkey = multi_decode(key_encoded).first
        if pubkey.bytes.length == 34
            code = pubkey.bytes.first
            digest = pubkey[-32..]
        else
            if pubkey.start_with?("\x80\x24".dup.force_encoding('ASCII-8BIT'))
                code = 4608 # Bytes 0x80 0x24 sind das Varint-Encoding des Multicodec-Codes 0x1200 (p256-pub)
                            # 4608 == Oydid.read_varint("\x80$") oder "\x80\x24".force_encoding('ASCII-8BIT')
            else
                code = pubkey.unpack('n').first
            end
            digest = pubkey[-1*(pubkey.bytes.length-2)..]
        end
        case Multicodecs[code].name
        when 'ed25519-pub'
            verify_key = Ed25519::VerifyKey.new(digest)
            return [verify_key, ""]
        when 'x25519-pub'
            pub_key = RbNaCl::PublicKey.new(digest)
            return [pub_key, ""]
        when 'p256-pub'
            asn1_public_key = OpenSSL::ASN1::Sequence.new([
              OpenSSL::ASN1::Sequence.new([
                OpenSSL::ASN1::ObjectId.new('id-ecPublicKey'),
                OpenSSL::ASN1::ObjectId.new('prime256v1')
              ]),
              OpenSSL::ASN1::BitString.new(digest)
            ])
            pub_key = OpenSSL::PKey::EC.new(asn1_public_key.to_der)

            return [pub_key, ""]
        else
            return [nil, "unsupported key codec"]
        end
    rescue
        return [nil, "unknown key codec"]
    end
end

.decrypt(message, private_key, options = {}) ⇒ Object



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/oydid/basic.rb', line 618

def self.decrypt(message, private_key, options = {})
    begin
        key_type = get_keytype(private_key)
        case key_type
        when 'ed25519-priv'
            cipher = [JSON.parse(message)["value"]].pack('H*')
            nonce = [JSON.parse(message)["nonce"]].pack('H*')
            code, length, digest = multi_decode(private_key).first.unpack('SCa*')
            if length != 32 # support only encoded keys
                digest = Multibases.unpack(private_key).decode.to_s('ASCII-8BIT')
                code = Multicodecs["ed25519-priv"].code
            end
            privKey = RbNaCl::PrivateKey.new(digest)
            authHash = RbNaCl::Hash.sha256('auth'.dup.force_encoding('ASCII-8BIT'))
            authKey = RbNaCl::PrivateKey.new(authHash).public_key
            box = RbNaCl::Box.new(authKey, privKey)
            retVal = box.decrypt(nonce, cipher)
            return [retVal, ""]
        when 'p256-priv'
            private_key = decode_private_key(private_key).first
            head_b64, _, iv_b64, cipher_b64, tag_b64 = message.split('.')
            decoded_header = JSON.parse(Base64.urlsafe_decode64(head_b64))
            epk_pub = Oydid.decode_public_key(Oydid.public_key_from_jwk(decoded_header['epk']).first).first

            shared_secret2 = private_key.dh_compute_key(epk_pub.public_key) # ECDH (Gegenseite)
            cek2 = OpenSSL::Digest::SHA256.digest(shared_secret2)

            decipher = OpenSSL::Cipher.new('aes-256-gcm')
            decipher.decrypt
            decipher.key = cek2
            decipher.iv = Base64.urlsafe_decode64(iv_b64)
            decipher.auth_tag = Base64.urlsafe_decode64(tag_b64)
            decipher.auth_data = ''
            plaintext = decipher.update(Base64.urlsafe_decode64(cipher_b64)) + decipher.final

            return [plaintext, nil]
        else
            return [nil, "unsupported key codec"]
        end
    rescue
        return [nil, "decryption failed"]
    end
end

.decryptJWE(message, private_key, options = {}) ⇒ Object



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/oydid/basic.rb', line 757

def self.decryptJWE(message, private_key, options = {})

    # JWE parsing
    jwe_full = JSON.parse(message)
    snd_pub_enc = jwe_full["recipients"].first["header"]["epk"]["x"]
    snd_key_enc = jwe_full["recipients"].first["encrypted_key"]
    snd_nnc_enc = jwe_full["recipients"].first["header"]["iv"]
    snd_tag_enc = jwe_full["recipients"].first["header"]["tag"]
    cnt_cip_enc = jwe_full["ciphertext"]
    cnt_tag_enc = jwe_full["tag"]
    cnt_nnc_enc = jwe_full["iv"]
    cnt_aad_enc = jwe_full["protected"]
    recipient_alg = jwe_full["recipients"].first["header"]["alg"]

    snd_pub = Base64.urlsafe_decode64(snd_pub_enc)
    snd_nnc = Base64.urlsafe_decode64(snd_nnc_enc)
    snd_key = Base64.urlsafe_decode64(snd_key_enc)
    snd_tag = Base64.urlsafe_decode64(snd_tag_enc)
    cnt_nnc = Base64.urlsafe_decode64(cnt_nnc_enc)
    cnt_cip = Base64.urlsafe_decode64(cnt_cip_enc)
    cnt_tag = Base64.urlsafe_decode64(cnt_tag_enc)
    cnt_aad = Base64.urlsafe_decode64(cnt_aad_enc)

    # Key Decryption
    code, length, digest = multi_decode(private_key).first.unpack('SCa*')
    buffer = RbNaCl::Util.zeros(RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey::BYTES)
    RbNaCl::Signatures::Ed25519::SigningKey.crypto_sign_ed25519_sk_to_curve25519(buffer, digest)
    shared_secret = RbNaCl::GroupElement.new(snd_pub).mult(buffer)
    jwe_const = [0, 0, 0, 1] + 
        shared_secret.to_bytes.unpack('C*') + 
        [0,0,0,15] + 
        recipient_alg.bytes + 
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
    kek = RbNaCl::Hash.sha256(jwe_const.pack('C*'))
    snd_aead = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(kek)
    cnt_key = snd_aead.decrypt(snd_nnc, snd_key+snd_tag, nil)

    # Content Decryption
    cnt_aead = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(cnt_key)
    cnt_dec = cnt_aead.decrypt(cnt_nnc, cnt_cip+cnt_tag, cnt_aad)

    return [cnt_dec, ""]
end

.deep_stringify_keys(object) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/oydid/basic.rb', line 155

def self.deep_stringify_keys(object)
    case object
    when Hash
        object.transform_keys(&:to_s).transform_values { |v| deep_stringify_keys(v) }
    when Array
        object.map { |e| deep_stringify_keys(e) }
    else
        object
    end
end

.delegate(did, options) ⇒ Object



1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
# File 'lib/oydid.rb', line 1338

def self.delegate(did, options)
    # check location
    location = options[:doc_location]
    if location.to_s == ""
        location = DEFAULT_LOCATION
    end
    if did.include?(LOCATION_PREFIX)
        tmp = did.split(LOCATION_PREFIX)
        did = tmp[0]
        location = tmp[1]
    end
    if did.include?(CGI.escape LOCATION_PREFIX)
        tmp = did.split(CGI.escape LOCATION_PREFIX)
        did = tmp[0] 
        location = tmp[1]
    end
    options[:doc_location] = location
    options[:log_location] = location

    if options[:ts].nil?
        ts = Time.now.utc.to_i
    else
        ts = options[:ts]
    end

    # build log record
    log = {}
    log["ts"] = ts
    log["op"] = 5 # DELEGATE
    pwd = false
    doc_privateKey, msg = getPrivateKey(options[:doc_enc], options[:doc_pwd], options[:doc_key], "", options)
    rev_privateKey, msg = getPrivateKey(options[:rev_enc], options[:rev_pwd], options[:rev_key], "", options)
    if !doc_privateKey.nil?
        pwd="doc"
        privateKey = doc_privateKey
    end
    if !rev_privateKey.nil?
        pwd="rev"
        privateKey = rev_privateKey
    end
    if !pwd || privateKey.to_s == ""
        return [nil, "missing or invalid delegate key"]
    end
    log["doc"] = pwd + ":" + public_key(privateKey, options).first.to_s
    log["sig"] = sign(privateKey, privateKey, options).first
    log["previous"] = [did] # DID in previous cannot be resolved in the DAG but guarantees unique log hash

    # revocation delegate keys need to specify a public key for encrypting the revocation record
    if pwd == "rev"
        publicEncryptionKey, msg = public_key(privateKey, {}, 'x25519-pub')
        log["encryption-key"] = publicEncryptionKey
    end
    log_hash, msg = write_log(did, log, options)
    if log_hash.nil?
        return [nil, msg]
    else
        return [{"log": log_hash}, ""]
    end
end

.document_id(did_info) ⇒ Object

The identifier a resolved DID document carries as id.

A did:oyd is the hash over its own document, so an update mints a new one and the resolution walks the log to the newest version. Answering with that newest identifier means a relying party never sees the DID it asked for - the one on the data carrier, in the credential, in the database. So the requested identifier is what goes into the document; which version it is remains readable from didDocumentMetadata canonicalId.

Callers that build a did_info by hand - the registrar endpoints do, for a DID that was just created or updated - carry no did_requested and keep getting the identifier they passed in.



1550
1551
1552
1553
1554
1555
1556
1557
1558
# File 'lib/oydid.rb', line 1550

def self.document_id(did_info)
    did = did_info["did_requested"].to_s
    did = did_info["did"].to_s if did == ""
    did = percent_encode(did)
    if !did.start_with?("did:oyd:")
        did = "did:oyd:" + did
    end
    did
end

.encrypt(message, public_key, options = {}) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/oydid/basic.rb', line 548

def self.encrypt(message, public_key, options = {})
    begin
        if options[:key_type].to_s == ''
            pk = multi_decode(public_key).first
            code = pk.bytes.first
            digest = pk[-32..]
            key_type = Multicodecs[code].name rescue ''
        else
            key_type = options[:key_type]
        end
        case key_type
        when 'x25519-pub'
            pubKey = RbNaCl::PublicKey.new(digest)
            authHash = RbNaCl::Hash.sha256('auth'.dup.force_encoding('ASCII-8BIT'))
            authKey = RbNaCl::PrivateKey.new(authHash)
            box = RbNaCl::Box.new(pubKey, authKey)
            nonce = RbNaCl::Random.random_bytes(box.nonce_bytes)
            msg = message.force_encoding('ASCII-8BIT')
            cipher = box.encrypt(nonce, msg)
            return [
                { 
                    value: cipher.unpack('H*')[0], 
                    nonce: nonce.unpack('H*')[0]
                }, ""
            ]
        when 'p256-pub'
            recipient_pub_key = decode_public_key(public_key).first

            # a) Ephemeren Sender-Key erzeugen + ECDH
            ephemeral_key = OpenSSL::PKey::EC.generate('prime256v1')
            shared_secret = ephemeral_key.dh_compute_key(recipient_pub_key.public_key)

            # b) Ableitung Content-Encryption-Key (CEK) – simple HKDF-Light
            cek = OpenSSL::Digest::SHA256.digest(shared_secret)

            # c) Symmetrische Verschlüsselung (AES-256-GCM)
            cipher = OpenSSL::Cipher.new('aes-256-gcm')
            cipher.encrypt
            cipher.key = cek
            iv = OpenSSL::Random.random_bytes(12) # 96-bit IV wie empfohlen
            cipher.iv = iv
            cipher.auth_data = '' # kein AAD
            ciphertext = cipher.update(message) + cipher.final
            tag = cipher.auth_tag

            # d) JWE-Header (nur die nötigen Felder)
            header = {
              alg: 'ECDH-ES',
              enc: 'A256GCM',
              kid: options[:kid],
              epk: JWT::JWK.new(ephemeral_key).export.slice(:kty, :crv, :x, :y)
            }

            # e) JWE-Compact-Serialisierung (EncryptedKey leer bei ECDH-ES)
            jwe_compact = [
                Base64.urlsafe_encode64(header.to_json).delete("="),
                '',
                Base64.urlsafe_encode64(iv).delete("="),
                Base64.urlsafe_encode64(ciphertext).delete("="),
                Base64.urlsafe_encode64(tag).delete("="),
            ].join('.')
            return [jwe_compact, nil]
        else
            return [nil, "unsupported key codec"]
        end
    rescue
        return [nil, "encryption failed"]
    end
end

.encryptJWE(message, public_key, options = {}) ⇒ Object



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/oydid/basic.rb', line 680

def self.encryptJWE(message, public_key, options = {})

    jwe_header = {"enc":"XC20P"}
    recipient_alg = 'ECDH-ES+XC20PKW'

    # Content Encryption ---
    # random nonce for XChaCha20-Poly1305: uses a 192-bit nonce (24 bytes)
    cnt_nnc = RbNaCl::Random.random_bytes(RbNaCl::AEAD::XChaCha20Poly1305IETF.nonce_bytes)
    # random key for XChaCha20-Poly1305: uses a 256-bit key (32 bytes)
    cnt_key = RbNaCl::Random.random_bytes(RbNaCl::AEAD::XChaCha20Poly1305IETF.key_bytes)
    # addtional data
    cnt_aad = jwe_header.to_json
    # setup XChaCha20-Poly1305 for Authenticated Encryption with Associated Data (AEAD)
    cnt_aead = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(cnt_key)
    # encrypt
    msg_enc = cnt_aead.encrypt(cnt_nnc, message, cnt_aad)
    cnt_enc = msg_enc[0...-cnt_aead.tag_bytes]
    cnt_tag = msg_enc[-cnt_aead.tag_bytes .. -1]

    # Key Encryption ---
    snd_prv = RbNaCl::PrivateKey.generate
    code, length, digest = multi_decode(public_key).first.unpack('CCa*')
    buffer = RbNaCl::Util.zeros(RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey::BYTES)
    RbNaCl::Signatures::Ed25519::VerifyKey.crypto_sign_ed25519_pk_to_curve25519(buffer, digest)
    shared_secret = RbNaCl::GroupElement.new(buffer).mult(snd_prv.to_bytes)
    jwe_const = [0, 0, 0, 1] + 
        shared_secret.to_bytes.unpack('C*') + 
        [0,0,0,15] + 
        recipient_alg.bytes + 
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
    kek = RbNaCl::Hash.sha256(jwe_const.pack('C*'))
    snd_nnc = RbNaCl::Random.random_bytes(RbNaCl::AEAD::XChaCha20Poly1305IETF.nonce_bytes)
    snd_aead = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(kek)
    snd_enc = snd_aead.encrypt(snd_nnc, cnt_key, nil)
    snd_key = snd_enc[0...-snd_aead.tag_bytes]
    snd_aut = snd_enc[-snd_aead.tag_bytes .. -1]

    # create JWE ---
    jwe_protected = Base64.urlsafe_encode64(jwe_header.to_json).delete("=")
    jwe_encrypted_key = Base64.urlsafe_encode64(snd_key).delete("=")
    jwe_init_vector = Base64.urlsafe_encode64(cnt_nnc).delete("=")
    jwe_cipher_text = Base64.urlsafe_encode64(cnt_enc).delete("=")
    jwe_auth_tag = Base64.urlsafe_encode64(cnt_tag).delete("=")
    rcp_nnc_enc = Base64.urlsafe_encode64(snd_nnc).delete("=")
    rcp_tag_enc = Base64.urlsafe_encode64(snd_aut).delete("=")

    jwe_full = {
        protected: jwe_protected,
        iv: jwe_init_vector,
        ciphertext: jwe_cipher_text,
        tag: jwe_auth_tag,
        recipients: [
            {
                encrypted_key: jwe_encrypted_key,
                header: {
                    alg: recipient_alg,
                    iv: rcp_nnc_enc,
                    tag: rcp_tag_enc,
                    epk: {
                        kty: "OKP",
                        crv: "X25519",
                        x: Base64.urlsafe_encode64(snd_prv.public_key.to_bytes).delete("=")
                    }
                }
            }
        ]
    }

    jwe = jwe_protected
    jwe += "." + jwe_encrypted_key
    jwe += "." + jwe_init_vector
    jwe += "." + jwe_cipher_text
    jwe += "." + jwe_auth_tag

    return [jwe_full, ""]
end

.expand_verification_methods(payload, wd, did) ⇒ Object

Verification relationships in the payload either name a key by fragment ("#key-doc") or embed a whole verification method. Both need the DID prefixed onto the identifier; an embedded method additionally needs a controller, which the client cannot supply - the DID is the hash of the document that would have to contain it, so it is not known while the document is being written.



1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
# File 'lib/oydid.rb', line 1404

def self.expand_verification_methods(payload, wd, did)
    return wd if !payload.is_a?(Hash)

    [ "authentication",
      "assertionMethod",
      "keyAgreement",
      "capabilityInvocation",
      "capabilityDelegation" ].each do |vm|
        next if payload[vm].to_s == ""
        entries = payload[vm]
        entries = [entries] if entries.is_a?(Hash) || entries.is_a?(String)

        new_entries = []
        entries.each do |el|
            if el.is_a?(String)
                new_entries << percent_encode(did) + el
            else
                new_el = el.transform_keys(&:to_s)
                new_el["id"] = percent_encode(did) + new_el["id"].to_s
                if new_el["controller"].to_s == ""
                    new_el["controller"] = percent_encode(did)
                end
                new_entries << new_el
            end
        end

        wd[vm] = new_entries.length > 0 ? new_entries : payload[vm]
        payload.delete(vm)
    end
    wd
end

.fromW3C(didDocument, options) ⇒ Object



1853
1854
1855
1856
1857
1858
1859
# File 'lib/oydid.rb', line 1853

def self.fromW3C(didDocument, options)
    didDocument = didDocument.transform_keys(&:to_s)
    if didDocument["@context"] == ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1"]
        didDocument.delete("@context")
    end
    didDocument
end

.generate_base(content, did, mode, options) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/oydid.rb', line 212

def self.generate_base(content, did, mode, options)
    # input validation
    did_doc = JSON.parse(content.to_json) rescue nil
    if did_doc.nil?
        if !content.nil?
            return [nil, nil, nil, "invalid payload"]
        end
    end

    did_old = nil
    log_old = nil
    prev_hash = []
    revoc_log = nil
    doc_location = options[:location]
    if options[:ts].nil?
        ts = Time.now.utc.to_i
    else
        ts = options[:ts]
    end

    # signatures collected from the client so far (CMSM only)
    cmsm_sig_rev = nil
    cmsm_sig_doc = nil
    cmsm_sig_create = nil
    cmsm_log_revoke_old = nil
    cmsm_resume = options[:cmsm] && options[:cmsm_session].to_s != ""

    if options[:cmsm]
        # A CMSM flow is identified by a session handle, not by the public
        # key: the same key may legitimately be reused across flows, and
        # keying the intermediate data on it made a second flow overwrite
        # the first one. Absence of a session means "start a new flow".
        if !cmsm_resume
            if did_doc["key"].nil?
                return [nil, nil, nil, "CMSM requires public key"]
            end
            cmsm_keys = did_doc["key"].split(':')
            did_doc.delete("key")
            if did_doc == {}
                did_doc = nil
            end
            case cmsm_keys.count
            when 2
                # both keys are managed by the client - the revocation record
                # of the new document has to be signed by the client as well
                publicKey = cmsm_keys[0]
                pubRevoKey = cmsm_keys[1]
                revocationKey = nil
            when 1
                # only the document key is client-managed; the revocation key
                # is generated here and stays with this process
                publicKey = cmsm_keys[0]
                revocationKey, msg = generate_private_key("", options[:key_type]+'-priv', options)
                if revocationKey.nil?
                    return [nil, nil, nil, "cannot generate revocation key: " + msg.to_s]
                end
                pubRevoKey = public_key(revocationKey, options).first
            else
                return [nil, nil, nil, "CMSM accepts at most two public keys"]
            end
        else
            # continue a persisted flow: the request carries the session and
            # exactly one new signature, which fills the next open slot
            incoming_sig = options[:sig]
            if incoming_sig.to_s == ""
                incoming_sig = did_doc["opt"]["sig"] rescue nil
            end
            if incoming_sig.to_s == ""
                return [nil, nil, nil, "missing signature in CMSM flow (sig)"]
            end

            payload, msg = check_cmsm(options[:cmsm_session], options)
            if payload.nil?
                if msg.to_s == ""
                    msg = "unknown or expired CMSM session"
                end
                return [nil, nil, nil, msg]
            end
            if payload.is_a?(String)
                payload = JSON.parse(payload) rescue nil
            end
            if payload.nil?
                return [nil, nil, nil, "invalid persisted data in CMSM flow"]
            end
            payload = payload.transform_keys(&:to_s)

            privateKey    = nil
            publicKey     = payload["publicKey"]
            pubRevoKey    = payload["pubRevoKey"]
            revocationKey = payload["revocationKey"]
            did_doc       = payload["did_doc"]
            cmsm_sig_rev    = payload["sig_rev"]
            cmsm_sig_doc    = payload["sig_doc"]
            cmsm_sig_create = payload["sig_create"]
            # the update context is only sent in phase 1
            if !payload["log_revoke_old"].nil?
                options[:log_revoke_old] = payload["log_revoke_old"]
            end
            # the timestamp and the location are part of every hash in the
            # flow and must not be recomputed per phase
            ts            = payload["ts"].to_i
            doc_location  = payload["doc_location"]

            if revocationKey.to_s == "" && cmsm_sig_rev.to_s == ""
                cmsm_sig_rev = incoming_sig
            elsif cmsm_sig_doc.to_s == ""
                cmsm_sig_doc = incoming_sig
            elsif cmsm_sig_create.to_s == ""
                cmsm_sig_create = incoming_sig
            else
                return [nil, nil, nil, "CMSM session is already complete"]
            end
        end
    else
        # key management
        tmp_did_hash = did.delete_prefix("did:oyd:") rescue ""
        tmp_did10 = tmp_did_hash[0,10] + "_private_key.enc" rescue ""
        privateKey, msg = getPrivateKey(options[:doc_enc], options[:doc_pwd], options[:doc_key], tmp_did10, options)
        if privateKey.nil?
            privateKey, msg = generate_private_key("", options[:key_type]+'-priv', options)
            if privateKey.nil?
                return [nil, nil, nil, "private document key not found"]
            end
        end
        tmp_did10 = tmp_did_hash[0,10] + "_revocation_key.enc" rescue ""
        revocationKey, msg = getPrivateKey(options[:rev_enc], options[:rev_pwd], options[:rev_key], tmp_did10, options)
        if revocationKey.nil?
            revocationKey, msg = generate_private_key("", options[:key_type]+'-priv', options)
            if revocationKey.nil?
                return [nil, nil, nil, "private revocation key not found"]
            end
        end
    end

    # mode-specific handling
    if mode == "create" || mode == "clone"
        operation_mode = 2 # CREATE

    else # mode == "update"  => read information first
        operation_mode = 3 # UPDATE

        did_info, msg = read(did, options)
        if did_info.nil?
            return [nil, nil, nil, "cannot resolve DID (on updating DID)"]
        end
        if did_info["error"] != 0
            return [nil, nil, nil, did_info["message"].to_s]
        end

        did = did_info["did"]
        did_hash = did.delete_prefix("did:oyd:")
        did10 = did_hash[0,10]
        if doc_location.to_s == ""
            if did_hash.include?(LOCATION_PREFIX)
                hash_split = did_hash.split(LOCATION_PREFIX)
                did_hash = hash_split[0]
                doc_location = hash_split[1]
            end
        end
        did_old = did.dup
        did10_old = did10.dup
        log_old = did_info["log"]

        # check if provided old keys are native DID keys or delegates ==================
        if options[:cmsm]
            # The old private keys are with the client, so nothing can be
            # derived from them here. The old public keys come from the DID
            # document just resolved, and the revocation record of the current
            # document is supplied by the client - it received that record when
            # the DID was created or last updated. Rebuilding it here is not
            # possible: it needs a signature by the old revocation key.
            old_privateKey = nil
            old_revocationKey = nil
            old_did_key = did_info["doc"]["key"].to_s
            old_publicDocKey = old_did_key.split(":").first.to_s
            old_publicRevKey = old_did_key.split(":").last.to_s

            verified, vmsg = verify_revocation_log(did_info, options[:log_revoke_old],
                                                   "log_revoke_old", options)
            if verified.nil?
                return [nil, nil, nil, vmsg]
            end
            revocationLog = verified.to_json
            cmsm_log_revoke_old = verified
        else
        tmp_old_doc_did10 = did10_old + "_private_key.enc" rescue ""
        old_privateKey, msg = getPrivateKey(options[:old_doc_enc], options[:old_doc_pwd], options[:old_doc_key], tmp_old_doc_did10, options)
        tmp_old_rev_did10 = did10_old + "_revocation_key.enc" rescue ""
        old_revocationKey, msg = getPrivateKey(options[:old_rev_enc], options[:old_rev_pwd], options[:old_rev_key], tmp_old_rev_did10, options)
        old_publicDocKey = public_key(old_privateKey, {}).first
        old_publicRevKey = public_key(old_revocationKey, {}).first

        old_did_key = old_publicDocKey + ":" + old_publicRevKey

        # compare old keys with existing DID Document & generate revocation record
        if old_did_key.to_s == did_info["doc"]["key"].to_s
            # provided keys are native DID keys ------------------

            # re-build revocation document
            old_did_doc = did_info["doc"]["doc"]
            old_ts = did_info["log"].last["ts"]
            old_subDid = {"doc": old_did_doc, "key": old_did_key}.to_json
            old_subDidHash = multi_hash(canonical(old_subDid), LOG_HASH_OPTIONS).first
            old_signedSubDidHash = sign(old_subDidHash, old_revocationKey, LOG_HASH_OPTIONS).first
            revocationLog = { 
                "ts": old_ts,
                "op": 1, # REVOKE
                "doc": old_subDidHash,
                "sig": old_signedSubDidHash }.transform_keys(&:to_s).to_json
        else
            # proviced keys are either delegates or invalid ------
            # * check validity of key-doc delegate
            pubKeys, msg = getDelegatedPubKeysFromDID(did, "doc")
            if !pubKeys.include?(old_publicDocKey)
                return [nil, nil, nil, "invalid or missing old private document key"]
            end

            # * check validity of key-rev delegate
            pubKeys, msg = getDelegatedPubKeysFromDID(did, "rev")
            if !pubKeys.include?(old_publicRevKey)
                return [nil, nil, nil, "invalid or missing old private revocation key"]
            end

            # retrieve revocationLog from previous in key-rev delegate
            revoc_log = nil
            log_old.each do |item|
                if !item["encrypted-revocation-log"].nil?
                    revoc_log = item["encrypted-revocation-log"]
                end
            end
            if revoc_log.nil?
                return [nil, nil, nil, "cannot retrieve revocation log"]
            end
            revocationLog, msg = decrypt(revoc_log.to_json, old_revocationKey.to_s)
            if revocationLog.nil?
                return [nil, nil, nil, "cannot decrypt revocation log entry: " + msg]
            end
        end # compare old keys with existing DID Document
        end # CMSM or self-managed

        revoc_log = JSON.parse(revocationLog)
        revoc_log["previous"] = [
            multi_hash(canonical(log_old[did_info["doc_log_id"].to_i]), LOG_HASH_OPTIONS).first, 
            multi_hash(canonical(log_old[did_info["termination_log_id"].to_i]), LOG_HASH_OPTIONS).first
        ]
        prev_hash = [multi_hash(canonical(revoc_log), LOG_HASH_OPTIONS).first]
    end
    if !options[:cmsm]
        publicKey = public_key(privateKey, options).first
        pubRevoKey = public_key(revocationKey, options).first
    end
    did_key = publicKey + ":" + pubRevoKey

    # the document is assembled once, in the first phase of a CMSM flow -
    # afterwards it comes back from the session unchanged
    if !cmsm_resume
        if options[:keyAgreement]
            if did_doc.nil?
                did_doc = {}
            end
            did_doc[:keyAgreement] = ["#key-doc"]
            did_doc = did_doc.transform_keys(&:to_s)
        end
        if options[:x25519_keyAgreement]
            if did_doc.nil?
                did_doc = {}
            end
            did_doc[:keyAgreement] = [{
                "id": "#key-doc-x25519",
                "type": "X25519KeyAgreementKey2019",
                "publicKeyMultibase": public_key(privateKey, options, 'x25519-pub').first
            }]
            did_doc = did_doc.transform_keys(&:to_s)
        end
        if options[:authentication]
            if did_doc.nil?
                did_doc = {}
            end
            did_doc[:authentication] = ["#key-doc"]
            did_doc = did_doc.transform_keys(&:to_s)
        end
    end

    # build new revocation document
    subDid = {"doc": did_doc, "key": did_key}.to_json
    retVal = multi_hash(canonical(subDid), LOG_HASH_OPTIONS)
    if retVal.first.nil?
        return [nil, nil, nil, retVal.last]
    end
    subDidHash = retVal.first

    if options[:cmsm] && revocationKey.to_s == "" && cmsm_sig_rev.to_s == ""
        # the revocation key is managed by the client, so the revocation
        # record of the new document has to be signed there. Its signature
        # feeds into r1 and thus into every later hash, which is why this
        # cannot be asked for together with the document-key signature.
        return cmsm_challenge(subDidHash, "key-rev", cmsm_state(publicKey,
                              pubRevoKey, revocationKey, did_doc, ts, doc_location,
                              cmsm_sig_rev, cmsm_sig_doc, cmsm_sig_create,
                              did_old, cmsm_log_revoke_old), options)
    end

    if revocationKey.to_s == ""
        signedSubDidHash = cmsm_sig_rev
    else
        signedSubDidHash = sign(subDidHash, revocationKey, LOG_HASH_OPTIONS).first
    end
    r1 = { "ts": ts,
           "op": 1, # REVOKE
           "doc": subDidHash,
           "sig": signedSubDidHash }.transform_keys(&:to_s)

    # build termination log entry
    l2_doc = multi_hash(canonical(r1), LOG_HASH_OPTIONS).first
    if !doc_location.nil?
        l2_doc += LOCATION_PREFIX + doc_location.to_s
    end

    if options[:cmsm]
        if cmsm_sig_doc.to_s == ""
            return cmsm_challenge(l2_doc, "key-doc", cmsm_state(publicKey,
                                  pubRevoKey, revocationKey, did_doc, ts, doc_location,
                                  cmsm_sig_rev, cmsm_sig_doc, cmsm_sig_create,
                              did_old, cmsm_log_revoke_old), options)
        end
        l2_sig = cmsm_sig_doc
    else
        l2_sig = sign(l2_doc, privateKey, options).first
    end

    if options[:confirm_logs].nil?
        previous_array = []
    else
        previous_array = options[:confirm_logs]
    end
    l2 = { "ts": ts,
           "op": 0, # TERMINATE
           "doc": l2_doc,
           "sig": l2_sig,
           "previous": previous_array }.transform_keys(&:to_s)

    # build actual DID document
    log_str = multi_hash(canonical(l2), LOG_HASH_OPTIONS).first
    if !doc_location.nil?
        log_str += LOCATION_PREFIX + doc_location.to_s
    end
    didDocument = { "doc": did_doc,
                    "key": did_key,
                    "log": log_str }.transform_keys(&:to_s)

    # create DID
    l1_doc = multi_hash(canonical(didDocument), options).first
    if !doc_location.nil?
        l1_doc += LOCATION_PREFIX + doc_location.to_s
    end    
    did = "did:oyd:" + l1_doc
    did10 = l1_doc[0,10]

    if mode == "clone"
        # create log entry for source DID
        new_log = {
            "ts": ts,
            "op": 4, # CLONE
            "doc": l1_doc,
            "sig": sign(l1_doc, privateKey, options).first,
            "previous": [options[:previous_clone].to_s]
        }
        retVal = HTTParty.post(options[:source_location] + "/log/" + options[:source_did],
            headers: { 'Content-Type' => 'application/json' },
            body: {"log": new_log}.to_json )
        prev_hash = [multi_hash(canonical(new_log), LOG_HASH_OPTIONS).first]
    end

    # build creation log entry
    log_revoke_encrypted_array = nil
    l1_sig = nil
    if operation_mode == 3 # UPDATE
        if options[:cmsm]
            if cmsm_sig_create.to_s == ""
                # the transition has to be authorised by the key of the
                # document being replaced, not by the new one
                return cmsm_challenge(l1_doc, "key-doc-old", cmsm_state(publicKey,
                                      pubRevoKey, revocationKey, did_doc, ts, doc_location,
                                      cmsm_sig_rev, cmsm_sig_doc, cmsm_sig_create,
                                      did_old, cmsm_log_revoke_old), options)
            end
            l1_sig = cmsm_sig_create
        else
            l1_sig = sign(l1_doc, old_privateKey, options).first
        end
        l1 = { "ts": ts,
               "op": operation_mode, # UPDATE
               "doc": l1_doc,
               "sig": l1_sig,
               "previous": prev_hash }.transform_keys(&:to_s)
        options[:confirm_logs].each do |el|
            # read each log entry to check if it is a revocation delegation
            log_item, msg = retrieve_log_item(el, doc_location, options)
            if log_item["doc"][0..3] == "rev:"
                cipher, msg = encrypt(r1.to_json, log_item["encryption-key"], {})
                cipher[:log] = el.to_s
                if log_revoke_encrypted_array.nil?
                    log_revoke_encrypted_array = [cipher]
                else
                    log_revoke_encrypted_array << cipher
                end
            end
        end unless options[:confirm_logs].nil?
    else
        if options[:cmsm]
            if cmsm_sig_create.to_s == ""
                # l1_doc is only known once the TERMINATE entry is signed, so
                # the CREATE signature cannot be collected any earlier. Without
                # this phase the CREATE log entry would stay unsigned.
                return cmsm_challenge(l1_doc, "key-doc", cmsm_state(publicKey,
                                      pubRevoKey, revocationKey, did_doc, ts, doc_location,
                                      cmsm_sig_rev, cmsm_sig_doc, cmsm_sig_create,
                              did_old, cmsm_log_revoke_old), options)
            end
            l1_sig = cmsm_sig_create
        else
            l1_sig = sign(l1_doc, privateKey, options).first
        end
        l1 = { "ts": ts,
               "op": operation_mode, # CREATE
               "doc": l1_doc,
               "sig": l1_sig,
               "previous": prev_hash }.transform_keys(&:to_s)
    end

    # did, didDocument, revoc_log, l1, l2, r1, privateKey, revocationKey, did_old, log_old, msg = Oydid.generate_base(content, "", "create", options)
    # did_doc = [did, didDocument, did_old]
    # did_log = [revoc_log, l1, l2, r1, log_old]
    # did_key = [privateKey, revocationKey]
    did_doc = {
        :did => did,
        :didDocument => didDocument,
        :did_old => did_old
    }
    did_log = {
        :revoc_log => revoc_log,
        :l1 => l1,
        :l2 => l2,
        :r1 => r1,
        :log_old => log_old
    }
    if !log_revoke_encrypted_array.nil?
        did_log[:r1_encrypted] = log_revoke_encrypted_array
    end

    did_key = {
        :privateKey => privateKey,
        :revocationKey => revocationKey
    }
    return [did_doc, did_key, did_log, ""]
    # return [did, didDocument, revoc_log, l1, l2, r1, privateKey, revocationKey, did_old, log_old, ""]
end

.generate_private_key(input, method = "ed25519-priv", options = {}) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/oydid/basic.rb', line 195

def self.generate_private_key(input, method = "ed25519-priv", options = {})
    begin
        omc = Multicodecs[method].code
    rescue
        return [nil, "unknown key codec"]
    end
    
    case Multicodecs[method].name 
    when 'ed25519-priv'
        if input == ""
            raw_key = Ed25519::SigningKey.generate
        else
            raw_key = Ed25519::SigningKey.new(RbNaCl::Hash.sha256(input))
        end
        raw_key = raw_key.to_bytes
    when 'p256-priv'
        if input == ""
            # no seed provided => generate a random key
            key = OpenSSL::PKey::EC.generate('prime256v1')
            raw_key = key.private_key.to_s(2)
        else
            # try to interpret input as a base64-encoded existing private key first;
            # otherwise derive the key deterministically from input (seed/password),
            # mirroring the ed25519-priv behaviour above
            key = nil
            begin
                candidate = OpenSSL::PKey.read(Base64.decode64(input))
                key = candidate if candidate.is_a?(OpenSSL::PKey::EC)
            rescue
                key = nil
            end
            if key.nil?
                # deterministic derivation: hash seed to a scalar in [1, n-1]
                order = OpenSSL::PKey::EC::Group.new('prime256v1').order
                scalar = (OpenSSL::BN.new(RbNaCl::Hash.sha256(input), 2) % (order - OpenSSL::BN.new(1))) + OpenSSL::BN.new(1)
                raw_key = scalar.to_s(2)
                raw_key = ("\x00".b * (32 - raw_key.bytesize)) + raw_key if raw_key.bytesize < 32
            else
                raw_key = key.private_key.to_s(2)
            end
        end
    else
        return [nil, "unsupported key codec"]
    end

    # only encoding without specifying key-type
    # encoded = multi_encode(raw_key, options)

    # encoding with specyfying key-type
    length = raw_key.bytesize
    encoded = multi_encode([omc, length, raw_key].pack("SCa#{length}"), options)
    if encoded.first.nil?
        return [nil, encoded.last]
    else
        return [encoded.first, ""]
    end
end

.get_digest(message) ⇒ Object



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
# File 'lib/oydid/basic.rb', line 73

def self.get_digest(message)
    decoded_message, error = multi_decode(message)
    if decoded_message.nil?
        return [nil, error]
    end
    # retVal = Multihashes.decode decoded_message
    # if retVal[:hash_function].to_s != ""
    #     return [retVal[:hash_function].to_s, ""]
    # end
    case decoded_message[0..1].to_s
    when "\x02\x10"
        return ["blake2b-16", ""]
    when "\x04 "
        return ["blake2b-32", ""]
    when "\b@"
        return ["blake2b-64", ""]
    else
        code, length, digest = decoded_message.unpack('CCa*')
        # intermediate BLAKE2b sizes carry a code outside the multicodec
        # registry; require the length byte to match so a stray code
        # cannot be mistaken for one of them
        blake2b_size = BLAKE2B_EXTRA_CODES.key(code)
        if !blake2b_size.nil? && length == blake2b_size
            return ["blake2b-" + blake2b_size.to_s, ""]
        end
        retVal = Multicodecs[code].name rescue nil
        if !retVal.nil?
            return [retVal, ""]
        else
            return [nil, "unknown digest"]
        end
    end
end

.get_encoding(message) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/oydid/basic.rb', line 107

def self.get_encoding(message)
    # from https://github.com/multiformats/multibase/blob/master/multibase.csv 
    begin
        [Multibases.unpack(message).encoding, ""]
    rescue => error
        [nil, error.message] 
    end
end

.get_keytype(input) ⇒ Object

key management ----------------------------



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/oydid/basic.rb', line 167

def self.get_keytype(input)
    code, length, digest = multi_decode(input).first.unpack('SCa*')
    case Multicodecs[code]&.name
    when 'ed25519-priv', 'p256-priv'
        return Multicodecs[code].name
    else
        pubkey = multi_decode(input).first
        if pubkey.bytes.length == 34
            code = pubkey.bytes.first
            digest = pubkey[-32..]
        else
            if pubkey.start_with?("\x80\x24".dup.force_encoding('ASCII-8BIT'))
                code = 4608 # Bytes 0x80 0x24 sind das Varint-Encoding des Multicodec-Codes 0x1200 (p256-pub)
                            # 4608 == Oydid.read_varint("\x80$") oder "\x80\x24".force_encoding('ASCII-8BIT')
            else
                code = pubkey.unpack('n').first
            end
            digest = pubkey[-1*(pubkey.bytes.length-2)..]
        end
        case Multicodecs[code]&.name
        when 'ed25519-pub', 'p256-pub'
            return Multicodecs[code].name
        else
            return nil
        end
    end
end

.get_location(id) ⇒ Object



1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/oydid/basic.rb', line 1237

def self.get_location(id)
    if id.include?(LOCATION_PREFIX)
        id_split = id.split(LOCATION_PREFIX)
        return id_split[1]
    else
        if id.include?(CGI.escape(LOCATION_PREFIX))
            id_split = id.split(CGI.escape(LOCATION_PREFIX))
            return id_split[1]
        else
            return DEFAULT_LOCATION
        end
    end
end

.getDelegatedPubKeysFromDID(did, key_type = "doc") ⇒ Object

available key_types

  • doc - document key
  • rev - revocation key


355
356
357
358
359
360
361
362
363
364
# File 'lib/oydid/basic.rb', line 355

def self.getDelegatedPubKeysFromDID(did, key_type = "doc")
    # retrieve DID
    did_document, msg = read(did, {})
    keys, msg = getDelegatedPubKeysFromFullDidDocument(did_document, key_type)
    if keys.nil?
        return [nil, msg]
    else
        return [keys, ""]
    end
end

.getDelegatedPubKeysFromFullDidDocument(did_document, key_type = "doc") ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/oydid/basic.rb', line 366

def self.getDelegatedPubKeysFromFullDidDocument(did_document, key_type = "doc")
    # get current public key
    case key_type
    when "doc"
        keys = [did_document["doc"]["key"].split(":").first] rescue nil
    when "rev"
        keys = [did_document["doc"]["key"].split(":").last] rescue nil
    else
        return [nil, "invalid key type: " + key_type]
    end
    if keys.nil?
        return [nil, "cannot retrieve current key"]
    end

    # travers through log and get active delegation public keys
    log = did_document["log"]
    log.each do |item|
        if item["op"] == 5 # DELEGATE
            # !!!OPEN: check if log entry is confirmed / referenced in a termination entry
            item_keys = item["doc"]
            if key_type == "doc" && item_keys[0..3] == "doc:"
                keys << item_keys[4-item_keys.length..]
            elsif key_type == "rev" && item_keys[0..3] == "rev:"
                keys << item_keys[4-item_keys.length..]
            end
        end
    end unless log.nil?

    # return array
    return [keys.uniq, ""]
end

.getPrivateKey(enc, pwd, dsk, dfl, options) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/oydid/basic.rb', line 302

def self.getPrivateKey(enc, pwd, dsk, dfl, options)
    if enc.to_s == "" # usually read from options[:doc_enc]
        if pwd.to_s == "" # usually read from options[:doc_pwd]
            if dsk.to_s == "" # usually read from options[:doc_key]
                if dfl.to_s == "" # default file name for key
                    return [nil, "no reference"]
                else
                    privateKey, msg = read_private_key(dfl.to_s, options)
                end
            else
                privateKey, msg = read_private_key(dsk.to_s, options)
            end
        else
            kt = options[:key_type].to_s
            kt = 'ed25519' if kt == ''
            privateKey, msg = generate_private_key(pwd, kt + '-priv', options)
        end
    else
        privateKey, msg = decode_private_key(enc.to_s, options)
        if msg.nil?
            privateKey = enc.to_s
        end
    end
    return [privateKey, msg]
end

.getPubKeyFromDID(did) ⇒ Object

if the identifier is already the public key there is no validation if it is a valid key (this is a privacy-preserving feature)



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/oydid/basic.rb', line 330

def self.getPubKeyFromDID(did)
    identifier = did.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first rescue did
    identifier = identifier.delete_prefix("did:oyd:")

    # check if identifier is already PubKey
    if decode_public_key(identifier).first.nil?
        did_document, msg = read(did, {})
        if did_document.nil?
            return [nil, msg]
            exit
        end
        pubKey = did_document["doc"]["key"].split(":").first rescue nil
        if pubKey.nil?
            return [nil, "cannot resolve " + did.to_s]
        else
            return [pubKey, ""]
        end
    else
        return [identifier, ""]
    end
end

.hash(message) ⇒ Object



32
33
34
# File 'lib/oydid/basic.rb', line 32

def self.hash(message)
    return multi_hash(message, {:digest => DEFAULT_DIGEST}).first
end

.hex_to_multibase(hex, options = {}) ⇒ Object

convert raw hex data (no multicodec prefix, e.g. a signature) to Multibase



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'lib/oydid/basic.rb', line 1122

def self.hex_to_multibase(hex, options = {})
    hex, msg = normalize_hex(hex)
    return [nil, msg] if hex.nil?
    # the multibases gem cannot represent an all-zero byte string: packing
    # raises NoMethodError and unpacking the correct base58btc form ("z1111")
    # raises RangeError. Leading zero bytes in otherwise non-zero input are
    # handled correctly, so only the all-zero case has to be rejected.
    if hex.delete("0") == ""
        return [nil, "all-zero input is not supported by the multibase encoder"]
    end
    return multi_encode([hex].pack("H*"), options)
end

.jwt_from_vc(vc, options) ⇒ Object



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/oydid/vc.rb', line 584

def self.jwt_from_vc(vc, options)
    if options[:issuer].to_s == ''
        return [nil, 'missing issuer DID']
    end
    header = {
        alg: 'ES256',
        typ: 'JWT',
        kid: options[:issuer] + '#key-doc'
    }

    if options[:issuer_privateKey].to_s == ''
        return [nil, 'missing issuer private key']
    end
    private_key = decode_private_key(options[:issuer_privateKey]).first

    encoded_header = Base64.urlsafe_encode64(header.to_json, padding: false)
    encoded_payload = Base64.urlsafe_encode64(vc.to_json_c14n, padding: false)
    data_to_sign = "#{encoded_header}.#{encoded_payload}"
# puts 'data_to_sign: ' + data_to_sign.to_s
# puts 'privateKey: ' + options[:issuer_privateKey].to_s

    jwt_digest = OpenSSL::Digest::SHA256.new
    asn1_signature = OpenSSL::ASN1.decode(private_key.dsa_sign_asn1(jwt_digest.digest(data_to_sign)))
    raw_signature = asn1_signature.value.map { |i| i.value.to_s(2).rjust(32, "\x00") }.join()
    encoded_signature = Base64.urlsafe_encode64(raw_signature, padding: false)
# puts 'encoded_signature: ' + encoded_signature.to_s

    jwt = "#{encoded_header}.#{encoded_payload}.#{encoded_signature}"

    return [jwt, nil]
end

.key_to_hex(key_encoded, options = {}) ⇒ Object

reverse of private_key_from_hex / public_key encoding: decode an OYDID Multibase-encoded key (private or public) back to raw hex.



1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
# File 'lib/oydid/basic.rb', line 1137

def self.key_to_hex(key_encoded, options = {})
    key_encoded = key_encoded.to_s.strip
    key_type = get_keytype(key_encoded) rescue nil
    if key_type.nil?
        return [nil, "unsupported or invalid key"]
    end
    begin
        raw = multi_decode(key_encoded).first
        case key_type
        when 'ed25519-priv', 'p256-priv'
            # custom layout: [uint16 codec][uint8 length][raw key bytes]
            _code, _length, digest = raw.unpack('SCa*')
            hex = digest.unpack1('H*')
        when 'ed25519-pub', 'p256-pub'
            # multicodec varint prefix (2 bytes) followed by raw key material
            hex = raw[2..].unpack1('H*')
        else
            return [nil, "unsupported key codec"]
        end
    rescue
        return [nil, "invalid key"]
    end
    return [hex, ""]
end

link a verified revocation record into the log of the DID it revokes



766
767
768
769
770
771
772
773
774
# File 'lib/oydid.rb', line 766

def self.link_revocation_log(record, did_info)
    log_old = did_info["log"]
    record = record.dup
    record["previous"] = [
        multi_hash(canonical(log_old[did_info["doc_log_id"].to_i]), LOG_HASH_OPTIONS).first,
        multi_hash(canonical(log_old[did_info["termination_log_id"].to_i]), LOG_HASH_OPTIONS).first
    ]
    record
end

.match_log_did?(log, doc) ⇒ Boolean

check if signature matches current document check if signature in log is correct

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/oydid/log.rb', line 18

def self.match_log_did?(log, doc)
    message = log["doc"].to_s
    signature = log["sig"].to_s
    public_keys = doc["key"].to_s
    public_key = public_keys.split(":")[0] rescue ""
    return verify(message, signature, public_key).first
end

.msg_decrypt(token, public_key_encoded, options) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/oydid/didcomm.rb', line 96

def self.msg_decrypt(token, public_key_encoded, options)
    error = ""
    code, length, digest = Oydid.multi_decode(public_key_encoded).first.unpack('CCa*')
    case Multicodecs[code].name
    when 'ed25519-pub'
        public_key = RbNaCl::Signatures::Ed25519::VerifyKey.new(digest)
        payload = JWT.decode token.to_s, public_key, true, { algorithm: 'ED25519' }
    else
        payload = nil
        error = "unsupported key codec"
    end
    return [payload, error]
end

.msg_encrypt(payload, private_key_encoded, did, options) ⇒ Object

encryption -----------------------------------



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
# File 'lib/oydid/didcomm.rb', line 59

def self.msg_encrypt(payload, private_key_encoded, did, options)
    error = ""
    code, length, digest = multi_decode(private_key_encoded).first.unpack('SCa*')
    case Multicodecs[code].name
    when 'ed25519-priv'
        private_key = RbNaCl::Signatures::Ed25519::SigningKey.new(digest)
        token = JWT.encode payload, private_key, 'ED25519'
    when 'p256-priv'
        group = OpenSSL::PKey::EC::Group.new('prime256v1')
        pub_key = group.generator.mul(OpenSSL::BN.new(digest, 2))
        pub_oct = pub_key.to_bn.to_s(2)

        parameters = OpenSSL::ASN1::ObjectId("prime256v1")
        parameters.tag = 0
        parameters.tagging = :EXPLICIT
        parameters.tag_class = :CONTEXT_SPECIFIC

        public_key_bitstring = OpenSSL::ASN1::BitString(pub_oct)
        public_key_bitstring.tag = 1
        public_key_bitstring.tagging = :EXPLICIT
        public_key_bitstring.tag_class = :CONTEXT_SPECIFIC

        ec_private_key_asn1 = OpenSSL::ASN1::Sequence([
            OpenSSL::ASN1::Integer(1),
            OpenSSL::ASN1::OctetString(digest),
            parameters,
            public_key_bitstring
        ])
        ec_key = OpenSSL::PKey.read(ec_private_key_asn1.to_der) 
        token = JWT.encode(payload, ec_key, 'ES256')         
    else
        token = nil
        error = "unsupported key codec"
    end
    return [token, error]
end

.msg_sign(payload, hmac_secret) ⇒ Object

signing for JWS ---------------------------



111
112
113
114
# File 'lib/oydid/didcomm.rb', line 111

def self.msg_sign(payload, hmac_secret)
    token = JWT.encode payload, hmac_secret, 'HS256'
    return [token, ""]
end

.msg_verify_jws(token, hmac_secret) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/oydid/didcomm.rb', line 116

def self.msg_verify_jws(token, hmac_secret)
    begin
        decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
        return [decoded_token, ""]
    rescue
        return [nil, "verification failed"]
    end
end

.multi_decode(message) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/oydid/basic.rb', line 24

def self.multi_decode(message)
    begin
        [Multibases.unpack(message).decode.to_s('ASCII-8BIT'), ""]
    rescue => error
        [nil, error.message] 
    end
end

.multi_encode(message, options) ⇒ Object

basic functions --------------------------- %w[multibases multihashes rbnacl json multicodecs].each { |f| require f }



14
15
16
17
18
19
20
21
22
# File 'lib/oydid/basic.rb', line 14

def self.multi_encode(message, options)
    method = options[:encode] || DEFAULT_ENCODING rescue DEFAULT_ENCODING
    case method
    when *SUPPORTED_ENCODINGS
        return [Multibases.pack(method, message).to_s, ""]
    else
        return [nil, "unsupported encoding: '" + method + "'"]
    end
end

.multi_hash(message, options) ⇒ Object



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
# File 'lib/oydid/basic.rb', line 36

def self.multi_hash(message, options)
    method = options[:digest] || DEFAULT_DIGEST
    # only set within the case statement for digests that are not looked up
    # in the multicodec registry (see BLAKE2B_EXTRA_CODES)
    code = nil
    case method.to_s
    when "sha2-256"
        digest = RbNaCl::Hash.sha256(message)
    when "sha2-512"
        digest = RbNaCl::Hash.sha512(message)
    when "sha3-224", "sha3-256", "sha3-384", "sha3-512"
        digest = OpenSSL::Digest.digest(method, message)
    when "blake2b-16"
        digest = RbNaCl::Hash.blake2b(message, {digest_size: 16})
    when "blake2b-17", "blake2b-18", "blake2b-19", "blake2b-20",
         "blake2b-21", "blake2b-22", "blake2b-23"
        digest_size = method.to_s.split("-").last.to_i
        digest = RbNaCl::Hash.blake2b(message, {digest_size: digest_size})
        code = BLAKE2B_EXTRA_CODES[digest_size]
    when "blake2b-32"
        digest = RbNaCl::Hash.blake2b(message, {digest_size: 32})
    when "blake2b-64"
        digest = RbNaCl::Hash.blake2b(message)
    else
        return [nil, "unsupported digest: '" + method.to_s + "'"]
    end
    code = Multicodecs[method].code if code.nil?
    length = digest.bytesize
    encoded = multi_encode([code, length, digest].pack("CCa#{length}"), options)
    # encoded = multi_encode(Multihashes.encode(digest, method.to_s), options)
    if encoded.first.nil?
        return [nil, encoded.last]
    else
        return [encoded.first, ""]
    end
end

.normalize_hex(hex) ⇒ Object

normalize and validate hex input shared by the hex-based converters below: strips whitespace and an optional '0x' prefix and rejects anything that is not an even number of hex digits. Returns [normalized_hex, ""] or [nil, msg].



1037
1038
1039
1040
1041
1042
1043
# File 'lib/oydid/basic.rb', line 1037

def self.normalize_hex(hex)
    hex = hex.to_s.strip.delete_prefix("0x").delete_prefix("0X")
    unless hex =~ /\A[0-9a-fA-F]+\z/ && hex.length.even?
        return [nil, "invalid hex input"]
    end
    return [hex, ""]
end

.percent_encode(did) ⇒ Object



125
126
127
128
# File 'lib/oydid/basic.rb', line 125

def self.percent_encode(did)
    # remove "https://" from string as it is default
    did = did.sub("https://","").sub("@", "%40").sub("http://","http%3A%2F%2F").gsub(":","%3A").sub("did%3Aoyd%3A", "did:oyd:")
end

.persist_cmsm(session, payload, options) ⇒ Object

persist the intermediate data of a CMSM flow and return the session handle it is stored under. A blank session starts a new flow; the handle is generated here so that the local and the remote store behave identically.



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'lib/oydid.rb', line 821

def self.persist_cmsm(session, payload, options)
    if session.to_s == ""
        session = "cmsm-" + SecureRandom.hex(8)
    end

    # in-process callers can supply a store object (responding to
    # #set(session, payload)) to persist CMSM data in a local database
    if options[:cmsm_store]
        options[:cmsm_store].set(session, payload)
        return [session, ""]
    end

    doc_location = options[:doc_location]
    if doc_location.to_s == ""
        doc_location = DEFAULT_LOCATION
    end
    doc_location = doc_location.sub("%3A%2F%2F","://").sub("%3A", ":")

    my_body = {
        session: session,
        pubkey: payload[:publicKey] || payload["publicKey"],
        payload: payload.to_json
    }
    case doc_location.to_s
    when /^http/
        persist_url = doc_location.to_s + "/cmsm"
        retVal = HTTParty.post(persist_url,
            headers: { 'Content-Type' => 'application/json' },
            body: my_body.to_json )
        if retVal.code != 200
            err_msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/cmsm"
            return [nil, err_msg]
        end
    else
        return [nil, "location not supported for persisting data in cmsm-flow"]
    end
    return [session, ""]

end

.private_key_from_hex(hex, options = {}) ⇒ Object

build an OYDID multibase-encoded private key from a raw hex-encoded key (e.g. an externally generated P-256 or Ed25519 key). The key type is taken from options (default 'ed25519').



1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/oydid/basic.rb', line 1048

def self.private_key_from_hex(hex, options = {})
    hex, msg = normalize_hex(hex)
    return [nil, msg] if hex.nil?
    raw = [hex].pack("H*")
    key_type = options[:key_type].to_s
    key_type = "ed25519" if key_type == ""
    case key_type
    when "p256"
        unless raw.bytesize == 32
            return [nil, "p256 private key must be 32 bytes (64 hex characters)"]
        end
        # scalar must be a valid private key in [1, n-1]
        order = OpenSSL::PKey::EC::Group.new("prime256v1").order
        scalar = OpenSSL::BN.new(raw, 2)
        if scalar < OpenSSL::BN.new(1) || scalar >= order
            return [nil, "p256 private key out of range"]
        end
        code = Multicodecs["p256-priv"].code
    when "ed25519"
        unless raw.bytesize == 32
            return [nil, "ed25519 private key must be 32 bytes (64 hex characters)"]
        end
        code = Multicodecs["ed25519-priv"].code
    else
        return [nil, "unsupported key type"]
    end
    length = raw.bytesize
    return multi_encode([code, length, raw].pack("SCa#{length}"), options)
end

.private_key_from_jwk(jwk, options = {}) ⇒ Object



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
# File 'lib/oydid/basic.rb', line 1008

def self.private_key_from_jwk(jwk, options = {})
    begin
        if jwk.is_a?(String)
            jwk = JSON.parse(jwk)
        end
    rescue
        return [nil, "invalid input"]
    end
    jwk = jwk.transform_keys(&:to_s)
    if jwk["kty"] == "EC" && jwk["crv"] == "P-256"
        digest = base64_url_decode(jwk["d"])
        group = OpenSSL::PKey::EC::Group.new("prime256v1")
        public_key = group.generator.mul(OpenSSL::BN.new(digest, 2))
        point = public_key.to_bn.to_s(2)
        x_calc = Base64.urlsafe_encode64(point[1, 32], padding: false)
        y_calc = Base64.urlsafe_encode64(point[33, 32], padding: false)
        return [nil, "x/y do not match d"] unless x_calc == jwk["x"] && y_calc == jwk["y"]

        code = Multicodecs["p256-priv"].code
        length = digest.bytesize
        return multi_encode([code, length, digest].pack("SCa#{length}"), options)
    else
        return [nil, "unsupported key codec"]
    end
end

.private_key_to_jwk(private_key) ⇒ Object



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/oydid/basic.rb', line 923

def self.private_key_to_jwk(private_key)
    code, length, digest = multi_decode(private_key).first.unpack('SCa*')
    case Multicodecs[code].name
    when 'ed25519-priv'
        return [nil, "not supported yet"]
    when 'p256-priv'
        group = OpenSSL::PKey::EC::Group.new('prime256v1')
        public_key = group.generator.mul(OpenSSL::BN.new(digest, 2))
        point = public_key.to_bn.to_s(2) 

        x_bin = point[1, 32]
        y_bin = point[33, 32]
        x = Base64.urlsafe_encode64(x_bin, padding: false)
        y = Base64.urlsafe_encode64(y_bin, padding: false)
        d = Base64.urlsafe_encode64(digest, padding: false)

        jwk = {
          kty: "EC",
          crv: "P-256",
          x: x,
          y: y,
          d: d
        }
        return [jwk, ""]
    else
        return [nil, "unsupported key codec"]
    end
end

.public_key(private_key, options = {}, method = nil) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/oydid/basic.rb', line 253

def self.public_key(private_key, options = {}, method = nil)
    code, length, digest = multi_decode(private_key).first.unpack('SCa*')
    case Multicodecs[code].name
    when 'ed25519-priv'
        method = 'ed25519-pub' if method.nil?
        case method
        when 'ed25519-pub'
            public_key = Ed25519::SigningKey.new(digest).verify_key
        when 'x25519-pub'
            public_key = RbNaCl::PrivateKey.new(digest).public_key
        else
            return [nil, "unsupported key codec"]
        end

        # encoding according to https://www.w3.org/TR/vc-di-eddsa/#ed25519verificationkey2020
        encoded = multi_encode(
            Multibases::DecodedByteArray.new(
                ([Multicodecs[method].code, 1] << 
                    public_key.to_bytes.bytes).flatten)
                .to_s(Encoding::BINARY),
            options)

        # previous (up until oydid 0.5.6) wrong encoding (length should not be set!):
        # length = public_key.to_bytes.bytesize
        # encoded = multi_encode([Multicodecs[method].code, length, public_key].pack("CCa#{length}"), options)
        if encoded.first.nil?
            return [nil, encoded.last]
        else
            return [encoded.first, ""]
        end
    when 'p256-priv'
        method = 'p256-pub' if method.nil?
        group = OpenSSL::PKey::EC::Group.new('prime256v1')
        public_key = group.generator.mul(OpenSSL::BN.new(digest, 2))
        encoded = multi_encode(
            Multibases::DecodedByteArray.new(
                (to_varint(0x1200) << public_key.to_bn.to_s(2).bytes)
                .flatten).to_s(Encoding::BINARY),
            options)
        if encoded.first.nil?
            return [nil, encoded.last]
        else
            return [encoded.first, ""]
        end
    else
        return [nil, "unsupported key codec"]
    end
end

.public_key_from_hex(hex, options = {}) ⇒ Object

counterpart to private_key_from_hex for public keys.

NOTE on the p256 length check: multicodec 0x1200 ('p256-pub') is specified for compressed points, but Oydid.public_key() emits uncompressed ones (OpenSSL's default point_conversion_form), so both forms are accepted here to stay compatible with keys this library produces itself.



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
# File 'lib/oydid/basic.rb', line 1084

def self.public_key_from_hex(hex, options = {})
    hex, msg = normalize_hex(hex)
    return [nil, msg] if hex.nil?
    raw = [hex].pack("H*")
    key_type = options[:key_type].to_s
    key_type = "ed25519" if key_type == ""
    case key_type
    when "p256"
        unless [33, 65].include?(raw.bytesize)
            return [nil, "p256 public key must be 33 (compressed) or 65 (uncompressed) bytes"]
        end
        # the length alone says nothing: verify that the input actually
        # decodes to a point on prime256v1 (this also rejects a wrong
        # leading byte), otherwise an unusable key would be encoded happily
        begin
            OpenSSL::PKey::EC::Point.new(
                OpenSSL::PKey::EC::Group.new("prime256v1"),
                OpenSSL::BN.new(hex, 16))
        rescue StandardError
            return [nil, "p256 public key is not a valid point on the curve"]
        end
        code = Multicodecs["p256-pub"].code
    when "ed25519"
        unless raw.bytesize == 32
            return [nil, "ed25519 public key must be 32 bytes (64 hex characters)"]
        end
        # there is no cheap point validation for ed25519 (Ed25519::VerifyKey
        # accepts any 32 bytes), and a private key has the same length - so
        # passing a *private* key with --public silently yields a public key
        # multibase. Callers have to know what they hand in.
        code = Multicodecs["ed25519-pub"].code
    else
        return [nil, "unsupported key type"]
    end
    return multi_encode(Multibases::DecodedByteArray.new((to_varint(code) << raw.bytes).flatten).to_s(Encoding::BINARY), options)
end

.public_key_from_jwk(jwk, options = {}) ⇒ Object



1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
# File 'lib/oydid/basic.rb', line 1162

def self.public_key_from_jwk(jwk, options = {})
    begin
        if jwk.is_a?(String)
            jwk = JSON.parse(jwk)
        end
    rescue
        return [nil, "invalid input"]
    end
    jwk = jwk.transform_keys(&:to_s)
    if jwk["kty"] == "EC" && jwk["crv"] == "P-256"
        x = base64_url_decode(jwk["x"])
        y = base64_url_decode(jwk["y"])
        digest = OpenSSL::ASN1::BitString.new(OpenSSL::BN.new("\x04" + x + y, 2).to_s(2))
        encoded = multi_encode(
            Multibases::DecodedByteArray.new(
                (to_varint(0x1200) << digest.value.bytes)
                .flatten).to_s(Encoding::BINARY),
            options)

        # asn1_public_key = OpenSSL::ASN1::Sequence.new([
        #   OpenSSL::ASN1::Sequence.new([
        #     OpenSSL::ASN1::ObjectId.new('id-ecPublicKey'),
        #     OpenSSL::ASN1::ObjectId.new('prime256v1')
        #   ]),
        #   OpenSSL::ASN1::BitString.new(OpenSSL::BN.new("\x04" + x + y, 2).to_s(2))
        # ])
        # pub_key = OpenSSL::PKey::EC.new(asn1_public_key.to_der)
        # encoded = multi_encode(
        #     Multibases::DecodedByteArray.new(
        #         (to_varint(0x1200) << pub_key.to_bn.to_s(2).bytes)
        #         .flatten).to_s(Encoding::BINARY),
        #     options)


        if encoded.first.nil?
            return [nil, encoded.last]
        else
            return [encoded.first, ""]
        end
    else
        return [nil, "unsupported key codec"]
    end
end

.public_key_to_jwk(public_key) ⇒ Object



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/oydid/basic.rb', line 952

def self.public_key_to_jwk(public_key)
    begin
        pubkey = multi_decode(public_key).first
        if pubkey.bytes.length == 34
            code = pubkey.bytes.first
            digest = pubkey[-32..]
        else
            if pubkey.start_with?("\x80\x24".dup.force_encoding('ASCII-8BIT'))
                code = 4608 # Bytes 0x80 0x24 sind das Varint-Encoding des Multicodec-Codes 0x1200 (p256-pub)
                            # 4608 == Oydid.read_varint("\x80$") oder "\x80\x24".force_encoding('ASCII-8BIT')
            else
                code = pubkey.unpack('n').first
            end
            digest = pubkey[-1*(pubkey.bytes.length-2)..]
        end
        case Multicodecs[code].name
        when 'ed25519-pub'
            return [nil, "not supported yet"]
        when 'p256-pub'
            if digest.bytes.first == 4
                # Unkomprimiertes Format: X (32 Bytes) || Y (32 Bytes)
                x_coord = digest[1..32]
                y_coord = digest[33..64]
                x_base64 = Base64.urlsafe_encode64(x_coord, padding: false)
                y_base64 = Base64.urlsafe_encode64(y_coord, padding: false)
            else 
                asn1_public_key = OpenSSL::ASN1::Sequence.new([
                  OpenSSL::ASN1::Sequence.new([
                    OpenSSL::ASN1::ObjectId.new('id-ecPublicKey'),
                    OpenSSL::ASN1::ObjectId.new('prime256v1')
                  ]),
                  OpenSSL::ASN1::BitString.new(digest)
                ])
                key = OpenSSL::PKey::EC.new(asn1_public_key.to_der)
                x, y = key.public_key.to_octet_string(:uncompressed)[1..].unpack1('H*').scan(/.{64}/)
                x_base64 = Base64.urlsafe_encode64([x].pack('H*'), padding: false)
                y_base64 = Base64.urlsafe_encode64([y].pack('H*'), padding: false)
            end
            jwk = {
                "kty" => "EC",
                "crv" => "P-256",
                "x" => x_base64,
                "y" => y_base64 }
            return [jwk, ""]
        else
            return [nil, "unsupported key codec"]
        end
    rescue
        return [nil, "unknown key codec"]
    end
end

.publish(did, didDocument, logs, options) ⇒ Object



670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/oydid.rb', line 670

def self.publish(did, didDocument, logs, options)
    # in-process callers (e.g. a repository storing DIDs in its own database)
    # can skip remote/file publishing and persist the returned logs themselves
    if options[:skip_publish]
        return [true, ""]
    end

    did_hash = did.delete_prefix("did:oyd:")
    did10 = did_hash[0,10]

    doc_location = options[:doc_location]
    if doc_location.to_s == ""
        if did_hash.include?(LOCATION_PREFIX)
            hash_split = did_hash.split(LOCATION_PREFIX)
            did_hash = hash_split[0]
            doc_location = hash_split[1]
        else
            doc_location = DEFAULT_LOCATION
        end
    end

    # wirte data based on location
    case doc_location.to_s
    when /^http/
        # build object to post
        did_data = {
            "did": did,
            "did-document": didDocument,
            "logs": logs
        }
        oydid_url = doc_location.to_s + "/doc"
        retVal = HTTParty.post(oydid_url,
            headers: { 'Content-Type' => 'application/json' },
            body: did_data.to_json )
        if retVal.code != 200
            err_msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/doc"
            return [false, err_msg]
        end
    else
        # write files to disk
        write_private_storage(logs.to_json, did10 + ".log")
        write_private_storage(didDocument.to_json, did10 + ".doc")
        write_private_storage(did, did10 + ".did")
    end

    return [true, ""]

end

.publish_vc(vc, options) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/oydid/vc.rb', line 298

def self.publish_vc(vc, options)
    vc = vc.transform_keys(&:to_s)
    identifier = vc["identifier"] rescue nil
    if identifier.nil?
        identifier = vc["id"] rescue nil
    end
    if identifier.nil?
        return [nil, "invalid format (missing identifier)"]
        exit
    end
    if vc["credentialSubject"].is_a?(Array)
        cs = vc["credentialSubject"].last.transform_keys(&:to_s) rescue nil
    else
        cs = vc["credentialSubject"].transform_keys(&:to_s) rescue nil
    end
    holder = cs["id"] rescue nil
    if holder.nil?
        return [nil, "invalid format (missing holder)"]
        exit
    end

    vc_location = ""
    if !options[:location].nil?
        vc_location = options[:location]
    end
    if vc_location.to_s == ""
        vc_location = DEFAULT_LOCATION
    end
    if !identifier.start_with?('http')
        identifier = vc_location.sub(/(\/)+$/,'') + "/credentials/" + identifier
    end

    # build object to post
    vc_data = {
        "identifier": identifier,
        "vc": vc,
        "holder": holder
    }
    vc_url = vc_location.sub(/(\/)+$/,'') + "/credentials"
    retVal = HTTParty.post(vc_url,
        headers: { 'Content-Type' => 'application/json' },
        body: vc_data.to_json )
    if retVal.code != 200
        err_msg = retVal.parsed_response("error").to_s rescue "invalid response from " + vc_url.to_s
        return [nil, err_msg]
    end
    return [retVal["identifier"], ""]
end

.publish_vp(vp, options) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/oydid/vc.rb', line 419

def self.publish_vp(vp, options)
    vp = vp.transform_keys(&:to_s)
    identifier = vp["identifier"] rescue nil
    if identifier.nil?
        return [nil, "invalid format (missing identifier)"]
        exit
    end

    proof = vp["proof"].transform_keys(&:to_s) rescue nil
    holder = vp["holder"] rescue nil
    if holder.nil?
        return [nil, "invalid format (missing holder)"]
        exit
    end

    vp_location = ""
    if !options[:location].nil?
        vp_location = options[:location]
    end
    if vp_location.to_s == ""
        vp_location = DEFAULT_LOCATION
    end
    vp["identifier"] = vp_location.sub(/(\/)+$/,'') + "/presentations/" + identifier

    # build object to post
    vp_data = {
        "identifier": identifier,
        "vp": vp,
        "holder": holder
    }
    vp_url = vp_location.sub(/(\/)+$/,'') + "/presentations"
    retVal = HTTParty.post(vp_url,
        headers: { 'Content-Type' => 'application/json' },
        body: vp_data.to_json )
    if retVal.code != 200
        err_msg = retVal.parsed_response("error").to_s rescue "invalid response from " + vp_url.to_s
        return [nil, err_msg]
    end
    return [vp["identifier"], ""]
end

.read(did, options) ⇒ Object

expected DID format: did:oyd:123



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
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/oydid.rb', line 65

def self.read(did, options)
    if did.to_s == ""
        return [nil, "missing DID"]
    end

    # setup
    #
    # did_requested is the identifier the caller asked for. dag_update
    # overwrites "did" with every version it walks through, so by the time a
    # DID document is rendered the requested identifier would be lost - and
    # after an update that is exactly the one a relying party holds, e.g.
    # printed on a product's data carrier.
    currentDID = {
        "did": did,
        "did_requested": did,
        "doc": "",
        "log": [],
        "doc_log_id": nil,
        "termination_log_id": nil,
        "error": 0,
        "message": "",
        "verification": ""
    }.transform_keys(&:to_s)

    # get did location
    did_location = ""
    if !options[:doc_location].nil?
        did_location = options[:doc_location]
    end
    if did_location.to_s == ""
        if !options[:location].nil?
            did_location = options[:location]
        end
    end
    if did_location.to_s == ""
        if did.include?(LOCATION_PREFIX)
            tmp = did.split(LOCATION_PREFIX)
            did = tmp[0] 
            did_location = tmp[1]
        end
        if did.include?(CGI.escape LOCATION_PREFIX)
            tmp = did.split(CGI.escape LOCATION_PREFIX)
            did = tmp[0] 
            did_location = tmp[1]
        end
    end
    if did_location == ""
        did_location = DEFAULT_LOCATION
    end
    did_hash = did.delete_prefix("did:oyd:")
    did10 = did_hash[0,10]

    # retrieve DID document
    did_document = retrieve_document(did_hash, did10 +  ".doc", did_location, options)
    if did_document.first.nil?
        return [nil, did_document.last]
    end
    did_document = did_document.first
    currentDID["doc"] = did_document
    if options[:trace]
        puts " .. DID document retrieved"
    end

    # get log location
    log_hash = did_document["log"]
    log_location = ""
    if !options[:log_location].nil?
        log_location = options[:log_location]
    end
    if log_location.to_s == ""
        if !options[:location].nil?
            log_location = options[:location]
        end
    end
    if log_location.to_s == ""
        if log_hash.include?(LOCATION_PREFIX)
            hash_split = log_hash.split(LOCATION_PREFIX)
            log_hash = hash_split[0]
            log_location = hash_split[1]
        end
    end
    if log_location == ""
        log_location = DEFAULT_LOCATION
    end

    # retrieve and traverse log to get current DID state
    log_array, msg = retrieve_log(log_hash, did10 + ".log", log_location, options)
    if log_array.nil?
        return [nil, msg]
    else
        if options[:trace]
            puts " .. Log retrieved"
        end
        dag, create_index, terminate_index, msg = dag_did(log_array, options)
        if dag.nil?
            return [nil, msg]
        end
        if options[:trace]
            puts " .. DAG with " + dag.vertices.length.to_s + " vertices and " + dag.edges.length.to_s + " edges, CREATE index: " + create_index.to_s
        end

        result = dag2array(dag, log_array, create_index, [], options)
        ordered_log_array = dag2array_terminate(dag, log_array, terminate_index, result, options)
        currentDID["log"] = ordered_log_array
        # !!! ugly hack to get access to all delegation keys required in dag_update
        currentDID["full_log"] = log_array
        if options[:trace]
            if options[:silent].nil? || !options[:silent]
                dag.edges.each do |e|
                    puts "    edge " + e.origin[:id].to_s + " <- " + e.destination[:id].to_s
                end
            end
        end

        # identify if DID Rotation was performed
        rotated_DID = (currentDID.transform_keys(&:to_s)["doc"]["doc"].has_key?("@context") &&
            currentDID.transform_keys(&:to_s)["doc"]["doc"].has_key?("id") &&
            currentDID.transform_keys(&:to_s)["doc"]["doc"]["id"].split(":").first == "did") rescue false

        if rotated_DID
            doc = currentDID["doc"].dup
            currentDID = dag_update(currentDID, options)
            currentDID["doc"] = doc
        else
            currentDID = dag_update(currentDID, options)
        end
        if options[:log_complete]
            currentDID["log"] = log_array
        end
        return [currentDID, ""]
    end
end

.read_private_key(filename, options) ⇒ Object



801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/oydid/basic.rb', line 801

def self.read_private_key(filename, options)
    begin
        f = File.open(filename)
        key_encoded = f.read.strip
        f.close
    rescue
        return [nil, "cannot read file"]
    end
    # get_keytype raises for anything that is not a valid key, and the
    # inline rescue below only guards the assignment - so key_type can end up
    # nil when the caller passes no :key_type either. Coerce to a string
    # instead of calling String methods on nil (raised NoMethodError before).
    key_type = (get_keytype(key_encoded) || options[:key_type] rescue options[:key_type]).to_s
    if key_type.include?('-')
        key_type = (key_type.split('-').first || options[:key_type]).to_s
    end
    if key_type == 'p256'
        begin
            key = decode_private_key(key_encoded).first
            private_key = key.private_key.to_s(2)
            code = Multicodecs["p256-priv"].code
        rescue
            return [nil, "invalid base64 encoded p256-priv key"]
        end
    else
        begin
            code, length, digest = multi_decode(key_encoded).first.unpack('SCa*')
            case Multicodecs[code].name
            when 'ed25519-priv'
                private_key = Ed25519::SigningKey.new(digest).to_bytes
            # when 'p256-priv'
            #     key = OpenSSL::PKey::EC.new('prime256v1')
            #     key.private_key = OpenSSL::BN.new(digest, 2)
            #     private_key = key.private_key.to_s(2)
            else
                return [nil, "unsupported key codec"]
            end
        rescue
            return [nil, "invalid key"]
        end
    end
    length = private_key.bytesize
    return multi_encode([code, length, private_key].pack("SCa#{length}"), options)

end

.read_private_storage(filename) ⇒ Object



1211
1212
1213
1214
1215
1216
1217
# File 'lib/oydid/basic.rb', line 1211

def self.read_private_storage(filename)
    begin
        File.open(filename, 'r') { |f| f.read }
    rescue
        nil
    end
end

.read_varint(str) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/oydid/basic.rb', line 145

def self.read_varint(str)
    n = shift = 0
    str.each_byte do |byte|
        n |= (byte & 0x7f) << shift
        break unless (byte & 0x80) == 0x80
        shift += 7
    end
    return n
end

.read_vc(identifier, options) ⇒ Object



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
# File 'lib/oydid/vc.rb', line 5

def self.read_vc(identifier, options)
    vc_location = ""
    if !options[:location].nil?
        vc_location = options[:location]
    end
    if vc_location.to_s == ""
        vc_location = DEFAULT_LOCATION
    end
    vc_url = vc_location.sub(/(\/)+$/,'') + "/credentials/" + identifier

    holder = options[:holder].to_s rescue nil
    if holder.to_s == ""
        msg = "missing holder information"
        return [nil, msg]
        exit
    end

    private_key = options[:holder_privateKey].to_s rescue nil
    if private_key.to_s == ""
        msg = "missing private document key information"
        return [nil, msg]
        exit
    end

    # authenticate against repository
    init_url = vc_location + "/oydid/init"
    sid = SecureRandom.hex(20).to_s
    response = HTTParty.post(init_url,
        headers: { 'Content-Type' => 'application/json' },
        body: { "session_id": sid, 
                "public_key": Oydid.public_key(private_key, options).first }.to_json ).parsed_response rescue {}
    if response["challenge"].nil?
        msg = "missing challenge for repository authentication"
        return [nil, msg]
        exit
    end
    challenge = response["challenge"].to_s

    # sign challenge and request token
    token_url = vc_location + "/oydid/token"
    response = HTTParty.post(token_url,
        headers: { 'Content-Type' => 'application/json' },
        body: { "session_id": sid, 
                "signed_challenge": Oydid.sign(challenge, private_key, options).first }.to_json).parsed_response rescue {}
    access_token = response["access_token"].to_s rescue nil
    if access_token.nil?
        msg = "invalid repository authentication (access_token)"
        return [nil, msg]
        exit
    end
    retVal = HTTParty.get(vc_url,
        headers: {'Authorization' => 'Bearer ' + access_token})
    if retVal.code != 200
        if retVal.code == 401
            msg = "unauthorized (valid Bearer token required)"
        else
            msg = retVal.parsed_response("error").to_s rescue "invalid response from " + vc_url.to_s
        end
        return [nil, msg]
    end
    return [retVal.parsed_response, ""]
end

.read_vp(identifier, options) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/oydid/vc.rb', line 347

def self.read_vp(identifier, options)
    vp_location = ""
    if !options[:location].nil?
        vp_location = options[:location]
    end
    if vp_location.to_s == ""
        vp_location = DEFAULT_LOCATION
    end
    vp_url = vp_location.sub(/(\/)+$/,'') + "/presentations/" + identifier
    retVal = HTTParty.get(vp_url)
    if retVal.code != 200
        msg = retVal.parsed_response("error").to_s rescue "invalid response from " + vp_url.to_s
        return [nil, msg]
    end
    return [retVal.parsed_response, ""]
end

.retrieve_document(doc_identifier, doc_file, doc_location, options) ⇒ Object



1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'lib/oydid/basic.rb', line 1251

def self.retrieve_document(doc_identifier, doc_file, doc_location, options)
    # in-process callers can supply the DID document directly (e.g. read from
    # a local database) to avoid any HTTP/file lookup
    if options[:local_doc].is_a?(Hash) && !options[:local_doc].empty?
        return [options[:local_doc], ""]
    end
    if doc_location == ""
        doc_location = DEFAULT_LOCATION
    end
    if !(doc_location == "" || doc_location == "local")
        if !doc_location.start_with?("http")
            doc_location = "https://" + doc_location
        end
    end
    case doc_location
    when /^http/
        doc_location = doc_location.sub("%3A%2F%2F","://").sub("%3A", ":")
        option_str = ""
        if options[:followAlsoKnownAs]
            option_str = "?followAlsoKnownAs=true"
        end
        retVal = HTTParty.get(doc_location + "/doc/" + doc_identifier + option_str)
        if retVal.code != 200
            msg = retVal.parsed_response["error"].to_s rescue ""
            if msg.to_s == ""
                msg = "invalid response from " + doc_location.to_s + "/doc/" + doc_identifier.to_s
            end
            return [nil, msg]
        end
        if options.transform_keys(&:to_s)["trace"]
            if options[:silent].nil? || !options[:silent]
                puts "GET " + doc_identifier + " from " + doc_location
            end
        end
        return [retVal.parsed_response, ""]
    when "", "local"
        doc = JSON.parse(read_private_storage(doc_file)) rescue {}
        if doc == {}
            return [nil, "cannot read file"]
        else
            return [doc, ""]
        end
    end
end

.retrieve_document_raw(doc_hash, doc_file, doc_location, options) ⇒ Object



1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
# File 'lib/oydid/basic.rb', line 1296

def self.retrieve_document_raw(doc_hash, doc_file, doc_location, options)
    doc_hash = doc_hash.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first rescue doc_hash
    doc_hash = doc_hash.delete_prefix("did:oyd:")

    # in-process callers (e.g. a repository holding DIDs in its own database)
    # can supply a store object responding to #get(doc_hash) that returns
    # {"doc" => <did document>, "log" => [<log entries>]} or nil. Unlike
    # options[:local_doc] this resolves *per hash*: dag_update walks several
    # different documents along an update chain, so a single document must
    # not be handed back for every hash it asks for.
    if options[:local_store]
        local_obj = options[:local_store].get(doc_hash)
        if !local_obj.nil?
            return [local_obj, ""]
        end
    end

    if doc_location == ""
        doc_location = DEFAULT_LOCATION
    end
    if !(doc_location == "" || doc_location == "local")
        if !doc_location.start_with?("http")
            doc_location = "https://" + doc_location
        end
    end

    case doc_location
    when /^http/
        doc_location = doc_location.sub("%3A%2F%2F","://").sub("%3A", ":")
        retVal = HTTParty.get(doc_location + "/doc_raw/" + doc_hash)
        if retVal.code != 200
            msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/doc/" + doc_hash.to_s
            return [nil, msg]
        end
        if options.transform_keys(&:to_s)["trace"]
            if options[:silent].nil? || !options[:silent]
                puts "GET " + doc_hash + " from " + doc_location
            end
        end
        return [retVal.parsed_response, ""]
    when "", "local"
        doc = JSON.parse(read_private_storage(doc_file)) rescue {}
        log = JSON.parse(read_private_storage(doc_file.sub(".doc", ".log"))) rescue {}
        if doc == {}
            return [nil, "cannot read file"]
        else
            obj = {"doc" => doc, "log" => log}
            return [obj, ""]
        end
    end
end

.retrieve_log(did_hash, log_file, log_location, options) ⇒ Object



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
# File 'lib/oydid/log.rb', line 26

def self.retrieve_log(did_hash, log_file, log_location, options)
    # in-process callers can supply the raw log array directly (e.g. read from
    # a local database) to avoid any HTTP/file lookup
    if options[:local_log].is_a?(Array)
        return [options[:local_log], ""]
    end
    if log_location == ""
        log_location = DEFAULT_LOCATION
    end
    if !(log_location == "" || log_location == "local")
        if !log_location.start_with?("http")
            log_location = "https://" + log_location
        end
    end

    case log_location
    when /^http/
        log_location = log_location.gsub("%3A",":")
        log_location = log_location.gsub("%2F%2F","//")
        retVal = HTTParty.get(log_location + "/log/" + did_hash)
        if retVal.code != 200
            msg = retVal.parsed_response("error").to_s rescue 
                    "invalid response from " + log_location.to_s + "/log/" + did_hash.to_s

            return [nil, msg]
        end
        if options.transform_keys(&:to_s)["trace"]
            if options[:silent].nil? || !options[:silent]
                puts "GET log for " + did_hash + " from " + log_location
            end
        end
        retVal = JSON.parse(retVal.to_s) rescue nil
        return [retVal, ""]
    when "", "local"
        doc = JSON.parse(read_private_storage(log_file)) rescue {}
        if doc == {}
            return [nil, "cannot read file '" + log_file + "'"]
        end
        return [doc, ""]
    end
end

.retrieve_log_item(log_hash, log_location, options) ⇒ Object



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
# File 'lib/oydid/log.rb', line 68

def self.retrieve_log_item(log_hash, log_location, options)
    if log_location.to_s == ""
        log_location = DEFAULT_LOCATION
    end
    if !log_location.start_with?("http")
        log_location = "https://" + log_location
    end

    case log_location
    when /^http/
        log_location = log_location.gsub("%3A",":")
        log_location = log_location.gsub("%2F%2F","//")
        retVal = HTTParty.get(log_location + "/log/" + log_hash + "/item")
        if retVal.code != 200
            msg = retVal.parsed_response("error").to_s rescue 
                    "invalid response from " + log_location.to_s + "/log/" + log_hash.to_s + "/item"
            return [nil, msg]
        end
        if options.transform_keys(&:to_s)["trace"]
            if options[:silent].nil? || !options[:silent]
                puts "GET log entry for " + log_hash + " from " + log_location
            end
        end
        retVal = JSON.parse(retVal.to_s) rescue nil
        return [retVal, ""]
    else
        return [nil, "cannot read from " + log_location]
    end
end

.revoke(did, options) ⇒ Object



1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
# File 'lib/oydid.rb', line 1276

def self.revoke(did, options)
    revoc_log, msg = revoke_base(did, options)
    if revoc_log.nil?
        return [nil, msg]
    end
    # in-process callers persist the revocation log themselves; return it
    # instead of publishing to a remote location / file
    if options[:skip_publish]
        return [revoc_log, ""]
    end
    success, msg = revoke_publish(did, revoc_log, options)
end

.revoke_base(did, options) ⇒ Object



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
# File 'lib/oydid.rb', line 1052

def self.revoke_base(did, options)
    did_orig = did.dup
    doc_location = options[:doc_location]
    if options[:ts].nil?
        ts = Time.now.utc.to_i
    else
        ts = options[:ts]
    end
    did_info, msg = read(did, options)
    if did_info.nil?
        return [nil, "cannot resolve DID (on revoking DID)"]
    end
    if did_info["error"] != 0
        return [nil, did_info["message"].to_s]
    end

    # A client that holds no private revocation key - a secure element does
    # not export one - submits the record it kept from creating or updating
    # the DID. Rebuilding it here is impossible, and re-signing it would not
    # help: the hash committed in the TERMINATE entry would no longer match.
    if !options[:log_revoke].nil?
        verified, vmsg = verify_revocation_log(did_info, options[:log_revoke], "log_revoke", options)
        if verified.nil?
            return [nil, vmsg]
        end
        return [link_revocation_log(verified, did_info), ""]
    end

    did = did_info["did"]
    did_hash = did.delete_prefix("did:oyd:")
    did10 = did_hash[0,10]
    if doc_location.to_s == ""
        if did_hash.include?(LOCATION_PREFIX)
            hash_split = did_hash.split(LOCATION_PREFIX)
            did_hash = hash_split[0]
            doc_location = hash_split[1]
        end
    end

    # collect relevant information from previous did
    did_old = did.dup
    did10_old = did10.dup
    log_old = did_info["log"]

    msg = ""
    if options[:old_doc_key].nil?
        if options[:old_doc_enc].nil?
            if options[:old_doc_pwd].nil?
                privateKey_old = read_private_storage(did10_old + "_private_key.enc")
            else
                privateKey_old, msg = generate_private_key(options[:old_doc_pwd].to_s, options[:key_type]+'-priv', options)
            end
        else
            privateKey_old = options[:old_doc_enc].to_s
        end
    else
        privateKey_old, msg = read_private_key(options[:old_doc_key].to_s, options)
    end
    if privateKey_old.nil?
        return [nil, "invalid or missing old private document key"]
    end
    if options[:old_rev_key].nil?
        if options[:old_rev_enc].nil?
            if options[:old_rev_pwd].nil?
                revocationKey_old = read_private_storage(did10_old + "_revocation_key.enc")
            else
                revocationKey_old, msg = generate_private_key(options[:old_rev_pwd].to_s, options[:key_type]+'-priv', options)
            end
        else
            revocationKey_old = options[:old_rev_enc].to_s
        end
    else
        revocationKey_old, msg = read_private_key(options[:old_rev_key].to_s, options)
    end
    if revocationKey_old.nil?
        return [nil, "invalid or missing old private revocation key"]
    end

    if options[:rev_key].nil? && options[:rev_pwd].nil? && options[:rev_enc].nil?
        # revocationKey, msg = read_private_key(did10 + "_revocation_key.enc", options)
        revocationLog = read_private_storage(did10 + "_revocation.json")
    else

        # check if provided old keys are native DID keys or delegates ==================
        msg = ""
        if options[:doc_key].nil?
            if options[:doc_enc].nil?
                old_privateKey, msg = generate_private_key(options[:old_doc_pwd].to_s, options[:key_type]+'-priv', options)
            else
                old_privateKey = options[:old_doc_enc].to_s
            end
        else
            old_privateKey, msg = read_private_key(options[:old_doc_key].to_s, options)
        end
        if options[:rev_key].nil?
            if options[:rev_enc].nil?
                old_revocationKey, msg = generate_private_key(options[:old_rev_pwd].to_s, options[:key_type]+'-priv', options)
            else
                old_revocationKey = options[:old_rev_enc].to_s
            end
        else
            old_revocationKey, msg = read_private_key(options[:old_rev_key].to_s, options)
        end
        old_publicDocKey = public_key(old_privateKey, {}).first
        old_publicRevKey = public_key(old_revocationKey, {}).first
        old_did_key = old_publicDocKey + ":" + old_publicRevKey

        # compare old keys with existing DID Document & generate revocation record
        if old_did_key.to_s == did_info["doc"]["key"].to_s
            # provided keys are native DID keys ------------------

            # re-build revocation document
            old_did_doc = did_info["doc"]["doc"]
            old_ts = did_info["log"].last["ts"]
            old_subDid = {"doc": old_did_doc, "key": old_did_key}.to_json
            old_subDidHash = multi_hash(canonical(old_subDid), LOG_HASH_OPTIONS).first
            old_signedSubDidHash = sign(old_subDidHash, old_revocationKey, LOG_HASH_OPTIONS).first
            revocationLog = { 
                "ts": old_ts,
                "op": 1, # REVOKE
                "doc": old_subDidHash,
                "sig": old_signedSubDidHash }.transform_keys(&:to_s).to_json
        else
            # proviced keys are either delegates or invalid ------
            # * check validity of key-doc delegate
            pubKeys, msg = getDelegatedPubKeysFromDID(did, "doc")
            if !pubKeys.include?(old_publicDocKey)
                return [nil, "invalid or missing private document key"]
            end

            # * check validity of key-rev delegate
            pubKeys, msg = getDelegatedPubKeysFromDID(did, "rev")
            if !pubKeys.include?(old_publicRevKey)
                return [nil, "invalid or missing private revocation key"]
            end

            # retrieve revocationLog from previous in key-rev delegate
            revoc_log = nil
            log_old.each do |item|
                if !item["encrypted-revocation-log"].nil?
                    revoc_log = item["encrypted-revocation-log"]
                end
            end
            if revoc_log.nil?
                return [nil, "cannot retrieve revocation log"]
            end
            revocationLog, msg = decrypt(revoc_log.to_json, old_revocationKey.to_s)
            if revocationLog.nil?
                return [nil, "cannot decrypt revocation log entry: " + msg]
            end
        end # compare old keys with existing DID Document
    end

    if revocationLog.nil?
        return [nil, "private revocation key not found"]
    end

    # check if REVOCATION hash matches hash in TERMINATION
    # the "doc" field of the TERMINATE log entry may carry a location suffix
    # (e.g. "<hash>@http://localhost:3000") when the DID was created with -l,
    # so strip the location prefix before comparing with the bare revocation hash
    termination_doc = did_info["log"][did_info["termination_log_id"]]["doc"]
    termination_doc = termination_doc.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
    if termination_doc != multi_hash(canonical(revocationLog), LOG_HASH_OPTIONS).first
        return [nil, "invalid revocation information"]
    end
    revoc_log = JSON.parse(revocationLog)
    revoc_log["previous"] = [
        multi_hash(canonical(log_old[did_info["doc_log_id"].to_i]), LOG_HASH_OPTIONS).first, 
        multi_hash(canonical(log_old[did_info["termination_log_id"].to_i]), LOG_HASH_OPTIONS).first,
    ]
    return [revoc_log, ""]
end

.revoke_publish(did, revoc_log, options) ⇒ Object



1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
# File 'lib/oydid.rb', line 1226

def self.revoke_publish(did, revoc_log, options)
    did_hash = did.delete_prefix("did:oyd:")
    did10 = did_hash[0,10]
    doc_location = options[:doc_location]
    if did_hash.include?(LOCATION_PREFIX)
        hash_split = did_hash.split(LOCATION_PREFIX)
        did_hash = hash_split[0]
        doc_location = hash_split[1]
    end
    if did_hash.include?(CGI.escape LOCATION_PREFIX)
        hash_split = did_hash.split(CGI.escape LOCATION_PREFIX)
        did_hash = hash_split[0]
        doc_location = hash_split[1]
    end
    if doc_location.to_s == ""
        doc_location = DEFAULT_LOCATION
    end
    # the location extracted from the DID may be percent-encoded
    # (e.g. "http%3A%2F%2Flocalhost%3A3000"); decode before using as URL
    doc_location = doc_location.to_s.gsub("%3A", ":").gsub("%2F", "/")
    # percent_encode strips the default scheme, so a DID published to an
    # https location carries a bare host ("oydid.ownyourdata.eu"). Without
    # putting the scheme back this is not recognised as a remote location
    # and the revocation is written to a local file instead - which is how
    # the branch below used to be reached at all.
    if !(doc_location == "" || doc_location == "local") &&
       !doc_location.start_with?("http")
        doc_location = "https://" + doc_location
    end

    # publish revocation log based on location
    case doc_location.to_s
    when /^http/
        retVal = HTTParty.post(doc_location.to_s + "/log/" + did_hash.to_s,
            headers: { 'Content-Type' => 'application/json' },
            body: {"log": revoc_log}.to_json )
        if retVal.code != 200
            msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/log/" + did_hash.to_s
            return [nil, msg]
        end
    else
        # did_old / did10_old were never defined here - they are locals of
        # generate_base. Writing the revocation next to the previous DID is
        # the job of the caller that knows about it.
        write_private_storage(revoc_log.to_json, did10 + ".log")
    end

    return [did, ""]
end

.sign(message, private_key, options = {}) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/oydid/basic.rb', line 398

def self.sign(message, private_key, options = {})
    key_type = get_keytype(private_key)
    case key_type
    when 'ed25519-priv'
        code, length, digest = multi_decode(private_key).first.unpack('SCa*')
        encoded = multi_encode(Ed25519::SigningKey.new(digest).sign(message), options)
        if encoded.first.nil?
            return [nil, encoded.last]
        else
            return [encoded.first, ""]
        end
    when 'p256-priv'
        # non-deterministic signing
        # ec_key = decode_private_key(private_key).first
        # dgst_bin = OpenSSL::Digest::SHA256.digest(message)
        # der = ec_key.dsa_sign_asn1(dgst_bin)
        # asn1 = OpenSSL::ASN1.decode(der)
        # r_hex = asn1.value[0].value.to_s(16).rjust(64, '0')
        # s_hex = asn1.value[1].value.to_s(16).rjust(64, '0')
        # sig_bin = [r_hex + s_hex].pack('H*')
        # encoded_signature = Base64.strict_encode64(sig_bin).tr('+/', '-_').delete('=')

        # === deterministic signing with P-256 =====
        # key & constants
        ec_key = decode_private_key(private_key).first
        if (ec_key.respond_to?(:private_key) ? ec_key.private_key : nil).nil?
            return [nil, "invalild private key"]
        end

        group = OpenSSL::PKey::EC::Group.new('prime256v1')
        n = group.order
        qlen = n.num_bits
        holen = 256
        bx = ec_key.private_key

        # hash message
        h1_bin = OpenSSL::Digest::SHA256.digest(message)
        h1_int = h1_bin.unpack1('H*').to_i(16)

        # helper (RFC 6979 Section 2.3)
        int2octets = lambda do |int|
            bin = int.to_s(16).rjust((qlen + 7) / 8 * 2, '0')
            [bin].pack('H*')
        end
        bits2octets = lambda do |bits|
            z1 = bits % n
            int2octets.call(z1)
        end

        # initialize HMAC-DRBG
        v = "\x01" * (holen / 8)
        k = "\x00" * (holen / 8)
        key_oct = int2octets.call(bx)
        hash_oct = bits2octets.call(h1_int)

        k = OpenSSL::HMAC.digest('SHA256', k, v + "\x00" + key_oct + hash_oct)
        v = OpenSSL::HMAC.digest('SHA256', k, v)
        k = OpenSSL::HMAC.digest('SHA256', k, v + "\x01" + key_oct + hash_oct)
        v = OpenSSL::HMAC.digest('SHA256', k, v)

        # identify k (step H in RFC 6979)
        loop do
            v = OpenSSL::HMAC.digest('SHA256', k, v)
            t = v.unpack1('H*').to_i(16)

            k_candidate = t % n
            if k_candidate.positive? && k_candidate < n
                k_bn = OpenSSL::BN.new(k_candidate)
                # calculate signature (r,s)
                r_bn = group.generator.mul(k_bn).to_bn                     # Point → BN (uncompressed)
                # extract x
                r_int = r_bn.to_s(2)[1, 32].unpack1('H*').to_i(16) % n
                next if r_int.zero?

                kinv = k_bn.mod_inverse(n)
                s_int = (kinv * (h1_int + bx.to_i * r_int)) % n
                next if s_int.zero?
                s_int = n.to_i - s_int if s_int > n.to_i / 2

                # encode r||s -> URL-safe Base64
                r_hex = r_int.to_s(16).rjust(64, '0')
                s_hex = s_int.to_s(16).rjust(64, '0')
                sig   = [r_hex + s_hex].pack('H*')
                return [Base64.urlsafe_encode64(sig, padding: false), ""]
            end

            k = OpenSSL::HMAC.digest('SHA256', k, v + "\x00")
            v = OpenSSL::HMAC.digest('SHA256', k, v)
        end            

        return [encoded_signature, ""]
    else
        return [nil, "unsupported key codec"]
    end
end

.simulate_did(content, did, mode, options) ⇒ Object



206
207
208
209
210
# File 'lib/oydid.rb', line 206

def self.simulate_did(content, did, mode, options)
    did_doc, did_key, did_log, msg = generate_base(content, did, mode, options)
    user_did = did_doc[:did]
    return [user_did, msg]
end

.strip_location(id) ⇒ Object

The identifier without its location suffix.

A did:oyd can carry "@" (raw or percent-encoded) to say where the document is hosted. That is a resolution hint, not part of the identity: the same document can be mirrored at any number of locations, so the set of location-bound variants is open and cannot be enumerated. Everything that states identity - canonicalId and equivalentId in the didDocumentMetadata - therefore uses this location-free form. The location-bound variant stays in alsoKnownAs.

Note this must NOT be applied to the id of the DID document itself: DID Core requires that "the value of the id property in the retrieved DID document must always match the DID being resolved", so a location-bound DID that was requested is echoed as requested (see Oydid.document_id).



1233
1234
1235
# File 'lib/oydid/basic.rb', line 1233

def self.strip_location(id)
    id.to_s.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first rescue id.to_s
end

.to_varint(n) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/oydid/basic.rb', line 130

def self.to_varint(n)
    bytes = []
    loop do
        byte = n & 0x7F
        n >>= 7
        if n == 0
            bytes << byte
            break
        else
            bytes << (byte | 0x80)
        end
    end
    bytes
end

.token_from_challenge(host, pwd, options = {}) ⇒ Object

DID Auth for data container with challenge ---



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/oydid/didcomm.rb', line 126

def self.token_from_challenge(host, pwd, options = {})
    sid = SecureRandom.hex(20).to_s
    public_key = public_key(generate_private_key(pwd, options).first, options).first
    retVal = HTTParty.post(host + "/oydid/init",
                headers: { 'Content-Type' => 'application/json' },
                body: { "session_id": sid, "public_key": public_key }.to_json )
    challenge = retVal.parsed_response["challenge"]
    signed_challenge = sign(challenge, Oydid.generate_private_key(pwd, options).first, options).first
    retVal = HTTParty.post(host + "/oydid/token",
                headers: { 'Content-Type' => 'application/json' },
                body: {
                    "session_id": sid,
                    "signed_challenge": signed_challenge,
                    "public_key": public_key
                }.to_json)
    return retVal.parsed_response["access_token"]
end

.update(content, did, options) ⇒ Object



202
203
204
# File 'lib/oydid.rb', line 202

def self.update(content, did, options)
    return write(content, did, "update", options)
end

.vc_proof(vc, proof, private_key_encoded, options) ⇒ Object

Verifiable Credential hash vc = "type", "issuer", "issuanceDate", "credentialSubject" but no "proof"! proof = "verificationMethod", "proofPurpose", "created" but no "proofValue" private_key_encoded (string) "z..." https://www.w3.org/TR/vc-di-eddsa/#representation-ed25519signature2020



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/oydid/vc.rb', line 117

def self.vc_proof(vc, proof, private_key_encoded, options)
    vc, vc_hash, errmsg = vc_proof_prep(vc, proof)
    if vc.nil?
        return [nil, errmsg]
    end
    code, length, digest = multi_decode(private_key_encoded).first.unpack('SCa*')
    case Multicodecs[code].name
    when 'ed25519-priv'
        signing_key = Ed25519::SigningKey.new(digest)
        vc["proof"]["proofValue"] = multi_encode(signing_key.sign([vc_hash].pack('H*')).bytes, options).first
    when 'p256-priv'
        vc["proof"]["proofValue"] = sign("message", private_key_encoded, options)
    else
        return [nil, "unsupported key codec"]
    end
    return [vc, nil]

end

.vc_proof_prep(vc, proof) ⇒ Object



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
# File 'lib/oydid/vc.rb', line 68

def self.vc_proof_prep(vc, proof)
    cntxt = vc["@context"].dup
    if !cntxt.is_a?(Array)
        cntxt = [cntxt]
    end
    cntxt << ED25519_SECURITY_SUITE unless cntxt.include?(ED25519_SECURITY_SUITE)
    vc["@context"] = cntxt.dup
    vc.delete("proof")
    vc = JSON::LD::API.compact(JSON.parse(vc.to_json), JSON.parse(cntxt.to_json))
    graph = RDF::Graph.new << JSON::LD::Reader.new(vc.to_json)
    norm_graph = graph.dump(:normalize).to_s
    if norm_graph.strip == ""
        return [nil, nil, "empty VC"]
    end
    hash1 = Multibases.pack("base16", RbNaCl::Hash.sha256(norm_graph)).to_s[1..]

    remove_context = false
    if proof["@context"].nil?
        proof["@context"] = cntxt.dup
        remove_context = true
    else
        cntxt = proof["@context"]
    end
    if proof["created"].nil?
        proof["created"] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
    end        
    proof.delete("proofValue")
    proof = JSON::LD::API.compact(JSON.parse(proof.to_json), JSON.parse(cntxt.to_json))
    graph = RDF::Graph.new << JSON::LD::Reader.new(proof.to_json)
    norm_graph = graph.dump(:normalize).to_s
    if norm_graph.strip == ""
        return [nil, nil, "empty proof"]
    end
    hash2 = Multibases.pack("base16", RbNaCl::Hash.sha256(norm_graph)).to_s[1..]
    if remove_context
        proof.delete("@context")
    end
    vc["proof"] = proof

    return [vc, hash2+hash1, nil]
end

.verify(message, signature, public_key) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/oydid/basic.rb', line 494

def self.verify(message, signature, public_key)
    begin
        pubkey = multi_decode(public_key).first
        if pubkey.bytes.length == 34
            code = pubkey.bytes.first
            digest = pubkey[-32..]
        else
            if pubkey.start_with?("\x80\x24".dup.force_encoding('ASCII-8BIT'))
                code = 4608 # Bytes 0x80 0x24 sind das Varint-Encoding des Multicodec-Codes 0x1200 (p256-pub)
                            # 4608 == Oydid.read_varint("\x80$") oder "\x80\x24".force_encoding('ASCII-8BIT')
            else
                code = pubkey.unpack('n').first
            end
            digest = pubkey[-1*(pubkey.bytes.length-2)..]
        end
        case Multicodecs[code].name
        when 'ed25519-pub'
            verify_key = Ed25519::VerifyKey.new(digest)
            signature_verification = false
            begin
                verify_key.verify(multi_decode(signature).first, message)
                signature_verification = true
            rescue Ed25519::VerifyError
                signature_verification = false
            end
            return [signature_verification, ""]
        when 'p256-pub'
            asn1_public_key = OpenSSL::ASN1::Sequence.new([
              OpenSSL::ASN1::Sequence.new([
                OpenSSL::ASN1::ObjectId.new('id-ecPublicKey'),
                OpenSSL::ASN1::ObjectId.new('prime256v1')
              ]),
              OpenSSL::ASN1::BitString.new(digest)
            ])
            key = OpenSSL::PKey::EC.new(asn1_public_key.to_der)

            sig_raw = Base64.urlsafe_decode64(signature + "=" * ((4 - signature.size % 4) % 4))
            r_hex   = sig_raw[0, 32].unpack1("H*")
            s_hex   = sig_raw[32, 32].unpack1("H*")
            asn_r   = OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(r_hex, 16))
            asn_s   = OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(s_hex, 16))
            sig_der = OpenSSL::ASN1::Sequence.new([asn_r, asn_s]).to_der

            message_digest = OpenSSL::Digest::SHA256.new
            valid = key.dsa_verify_asn1(message_digest.digest(message), sig_der)
            return [valid, ""]
        else
            return [nil, "unsupported key codec"]
        end
    rescue
        return [nil, "unknown key codec"]
    end
end

.verify_revocation_log(did_info, log_revoke, field, options = {}) ⇒ Object

Verify a revocation record supplied by a client against the DID it claims to revoke. Used wherever the private revocation key is not available here: a CMSM update (revoking the document being replaced) and a client-managed deactivation.

Three things have to line up, and the third is the strict one: the hash of the record is committed in the TERMINATE entry of the current document, so a record from any other DID - or a re-signed one that happens to differ - cannot pass.

Returns the normalised record (without "previous") or [nil, message].



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/oydid.rb', line 730

def self.verify_revocation_log(did_info, log_revoke, field, options = {})
    record = log_revoke
    record = (JSON.parse(record) rescue nil) if record.is_a?(String)
    record = record.transform_keys(&:to_s) if record.is_a?(Hash)
    if !record.is_a?(Hash)
        return [nil, "missing revocation record (" + field + ")"]
    end

    did_key = did_info["doc"]["key"].to_s
    pubRevKey = did_key.split(":").last.to_s
    subDid = {"doc": did_info["doc"]["doc"], "key": did_key}.to_json
    subDidHash = multi_hash(canonical(subDid), LOG_HASH_OPTIONS).first

    if record["doc"].to_s != subDidHash.to_s
        return [nil, field + " does not match the current DID document"]
    end
    sig_ok, _msg = verify(subDidHash, record["sig"].to_s, pubRevKey)
    if !sig_ok
        return [nil, "invalid signature in " + field]
    end

    normalised = { "ts" => record["ts"],
                   "op" => 1, # REVOKE
                   "doc" => subDidHash,
                   "sig" => record["sig"] }

    termination_doc = did_info["log"][did_info["termination_log_id"].to_i]["doc"].to_s
    termination_doc = termination_doc.split(LOCATION_PREFIX).first.split(CGI.escape LOCATION_PREFIX).first
    if termination_doc != multi_hash(canonical(normalised.to_json), LOG_HASH_OPTIONS).first
        return [nil, field + " does not match the TERMINATE entry of the current document"]
    end

    [normalised, ""]
end

.verify_vc(content, options) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/oydid/vc.rb', line 460

def self.verify_vc(content, options)
    retVal = {}
    vercred = content.to_json_c14n rescue nil
    if vercred.nil?
        return [nil, "invalid verifiableCredential input"]
    end
    retVal[:id] = content["id"] rescue nil
    if retVal[:id].nil?
        retVal[:id] = content["identifier"] rescue nil
        if retVal[:id].nil?
            return [nil, "invalid VC (missing id)"]
        end
    end
    issuer = content["issuer"].to_s rescue nil
    if issuer.nil?
        return [nil, "invalid VC (unknown issuer)"]
        exit
    end
    publicKey, msg = getPubKeyFromDID(issuer)
    if publicKey.nil?
        return [nil, "cannot verify public key"]
        exit
    end
    vc, vc_hash, errmsg = vc_proof_prep(JSON.parse(content.to_json), JSON.parse(content["proof"].to_json))
    begin
        pubkey = Oydid.multi_decode(publicKey).first
        code = pubkey.bytes.first
        digest = pubkey[-32..]
        case Multicodecs[code].name
        when 'ed25519-pub'
            verify_key = Ed25519::VerifyKey.new(digest)
            signature_verification = false
            begin
                verify_key.verify(multi_decode(content["proof"]["proofValue"]).first, [vc_hash].pack('H*'))
                signature_verification = true
            rescue Ed25519::VerifyError
                signature_verification = false
            end
            if signature_verification
                return [retVal, nil]
            else
                return [nil, "proof signature does not match VC"]
            end
        else
            return [nil, "unsupported key codec"]
        end
    rescue
        return [nil, "unknown key codec"]
    end
end

.verify_vp(content, options) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/oydid/vc.rb', line 511

def self.verify_vp(content, options)
    retVal = {}
    verpres = content.to_json_c14n rescue nil
    if verpres.nil?
        return [nil, "invalid verifiablePresetation input"]
    end
    retVal[:id] = content["id"] rescue nil
    if retVal[:id].nil?
        retVal[:id] = content["identifier"] rescue nil
        if retVal[:id].nil?
            return [nil, "invalid VP (missing id)"]
        end
    end
    holder = content["proof"]["verificationMethod"].to_s rescue nil
    if holder.nil?
        return [nil, "invalid VP (unknown holder"]
    end
    publicKey, msg = getPubKeyFromDID(holder)
    if publicKey.nil?
        return [nil, "cannot verify public key"]
    end
    # begin
        key_type = get_keytype(publicKey)
        case key_type
        # pubkey = Oydid.multi_decode(publicKey).first
        # code = pubkey.bytes.first
        # digest = pubkey[-32..]
        # case Multicodecs[code].name
        when 'ed25519-pub'
            pubkey = Oydid.multi_decode(publicKey).first
            code = pubkey.bytes.first
            digest = pubkey[-32..]
            verify_key = Ed25519::VerifyKey.new(digest)
            vp, vp_hash, errmsg = vc_proof_prep(JSON.parse(content.to_json), JSON.parse(content["proof"].to_json))

            signature_verification = false
            begin
                verify_key.verify(multi_decode(content["proof"]["proofValue"]).first, [vp_hash].pack('H*'))
                signature_verification = true
            rescue Ed25519::VerifyError
                signature_verification = false
            end
            if signature_verification
                return [retVal, nil]
            else
                return [nil, "proof signature does not match VP"]
            end
        when 'p256-pub'
            jws = content["proof"]["jws"]
            head_b64, _, sig_b64 = jws.split('.')
            verpres = JSON.parse(verpres)
            verpres["proof"].delete("jws")
            verpres.delete("identifier")
            encoded_payload = Base64.urlsafe_encode64(verpres.to_json_c14n, padding: false)
            data_to_sign = "#{head_b64}.#{encoded_payload}"
# puts 'data_to_sign: ' + data_to_sign.to_s
# puts 'encoded_signature: ' + sig_b64.to_s
# puts 'publicKey: ' + publicKey.to_s

            valid = verify(data_to_sign, sig_b64, publicKey).first
            if valid
                return [retVal, nil]
            else
                return [nil, "proof signature does not match VP"]
            end
        else
            return [nil, "unsupported key codec"]
        end
    # rescue
    #     return [nil, "unknown key codec"]
    # end
end

.version_ids(did_info, keep_location = false) ⇒ Object

Identifiers of every published version of a DID, oldest first.

A did:oyd is the hash over its own DID document, so every update mints a new identifier while the earlier ones stay resolvable through the log. The version reached by traversing the log is the canonical identifier, every other version identifier is logically equivalent to it - which is what DID Core expresses as didDocumentMetadata canonicalId/equivalentId.

This lives in the gem rather than in the resolving controllers because there are three of them (repository resolve and resolve_full, plus the uniresolver plugin) and they had drifted into computing this list with different rules.

Both values are location-free: the "@" suffix says where a document is hosted, not who it is, and the same document can be mirrored at any number of locations - so the set of location-bound variants is open and equivalentId could not state it correctly anyway. The raw values these are built from do carry a location (dag_update sets did_info from the log entry, and log["doc"] is written with one), which is why strip_location is applied here rather than assumed. The location-bound variant stays in alsoKnownAs; document_id is deliberately left alone (see strip_location).

Returns [canonicalId, equivalentIds]; equivalentIds never contains the identifier the DID document itself carries as id (see document_id) and is empty - not a set with the DID itself in it - while there is only one version. keep_location = true reproduces the pre-0.9.1 values, location suffix and all. Only w3c uses it, to keep alsoKnownAs listing the location-bound variant of a DID - dropping that would take the location out of the document altogether, and alsoKnownAs is where it belongs.

Deliberately a positional argument: callers pass did_info as a braceless hash literal (version_ids("did" => ..., "log" => ...)), and a method with a keyword parameter swallows that hash as keywords instead - every such call site would raise ArgumentError.



1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
# File 'lib/oydid.rb', line 1471

def self.version_ids(did_info, keep_location = false)
    normalize = lambda do |id|
        id = keep_location ? id.to_s : strip_location(id.to_s)
        id = percent_encode(id)
        id = "did:oyd:" + id if !id.start_with?("did:oyd:")
        id
    end
    canonical = normalize.call(did_info["did"])
    own = document_id(did_info)
    equivalentIds = []
    did_info["log"].each do |log|
        if log["op"] == 2 || log["op"] == 3
            eid = normalize.call(log["doc"])
            if eid != own && !equivalentIds.include?(eid)
                equivalentIds << eid
            end
        end
    end unless did_info["log"].nil?
    [canonical, equivalentIds]
end

.version_metadata(did_info) ⇒ Object

created / updated / versionId of the resolved document version, as DID Core 7.1.3 defines them. Returned as a hash with string keys, ready to be merged into didDocumentMetadata; a property the log cannot answer is absent rather than null - in particular updated, which the spec requires to be "omitted if an Update operation has never been performed on the DID document".

The resolved version is did_info - dag_update walks the log to the newest document and leaves its identifier there. Deriving both versionId and the updated entry from it avoids guessing which log entry is the newest, which timestamps alone cannot decide (they are client-supplied and two entries can share a second).

versionId is the bare document hash, without the "did:oyd:" prefix and without a location: it is the method-specific identifier of that version, so "did:oyd:" + versionId is the versioned DID.



1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
# File 'lib/oydid.rb', line 1507

def self.(did_info)
    as_datetime = lambda do |ts|
        # XML Datetime normalised to UTC, no sub-second precision (7.1.3)
        Time.at(ts.to_i).utc.strftime("%Y-%m-%dT%H:%M:%SZ") rescue nil
    end
    version_of = lambda do |id|
        strip_location(id.to_s).delete_prefix("did:oyd:")
    end

    meta = {}
    resolved = version_of.call(did_info["did"])
    meta["versionId"] = resolved if resolved != ""
    return meta if did_info["log"].nil?

    created_entry = did_info["log"].find { |el| el["op"].to_i == 2 }
    if !created_entry.nil? && !created_entry["ts"].nil?
        created = as_datetime.call(created_entry["ts"])
        meta["created"] = created if !created.nil?
    end

    updated_entry = did_info["log"].find do |el|
        el["op"].to_i == 3 && version_of.call(el["doc"]) == resolved
    end
    if !updated_entry.nil? && !updated_entry["ts"].nil?
        updated = as_datetime.call(updated_entry["ts"])
        meta["updated"] = updated if !updated.nil?
    end

    meta
end

.w3c(did_info, options) ⇒ Object



1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
# File 'lib/oydid.rb', line 1560

def self.w3c(did_info, options)
    # check if doc is already W3C DID
    is_already_w3c_did = (did_info.transform_keys(&:to_s)["doc"]["doc"].has_key?("@context") &&
        did_info.transform_keys(&:to_s)["doc"]["doc"].has_key?("id") &&
        did_info.transform_keys(&:to_s)["doc"]["doc"]["id"].split(":").first == "did") rescue false
    if is_already_w3c_did
        return did_info.transform_keys(&:to_s)["doc"]["doc"]
    end
    did = document_id(did_info)

    didDoc = did_info.dup.transform_keys(&:to_s)["doc"]
    pubDocKey = didDoc["key"].split(":")[0] rescue ""
    pubRevKey = didDoc["key"].split(":")[1] rescue ""
    delegateDocKeys = getDelegatedPubKeysFromDID(did, "doc").first - [pubDocKey] rescue []
    if delegateDocKeys.is_a?(String)
        if delegateDocKeys == pubDocKey
            delegateDocKeys = nil
        else
            delegateDocKeys = [delegateDocKeys]
        end
    end
    delegateRevKeys = getDelegatedPubKeysFromDID(did, "rev").first - [pubRevKey] rescue []
    if delegateRevKeys.is_a?(String)
        if delegateRevKeys == pubRevKey
            delegateRevKeys = nil
        else
            delegateRevKeys = [delegateRevKeys]
        end
    end

    oyd_context = ["https://www.w3.org/ns/did/v1"]
    pubkey = multi_decode(pubDocKey).first
    if pubkey.bytes.length == 34
        code = pubkey.bytes.first
        digest = pubkey[-32..]
    else
        if pubkey.start_with?("\x80\x24".dup.force_encoding('ASCII-8BIT'))
            code = 4608 # Bytes 0x80 0x24 sind das Varint-Encoding des Multicodec-Codes 0x1200 (p256-pub)
                        # 4608 == Oydid.read_varint("\x80$") oder "\x80\x24".force_encoding('ASCII-8BIT')
        else
            code = pubkey.unpack('n').first
        end
        digest = pubkey[-1*(pubkey.bytes.length-2)..]
    end
    case Multicodecs[code].name
    when 'ed25519-pub'
        oyd_context << "https://w3id.org/security/suites/ed25519-2020/v1"
    when 'p256-pub'
        oyd_context << "https://w3id.org/security/suites/jws-2020/v1"
    else
        return {"error": "unsupported key codec (" + Multicodecs[code].name.to_s + ")"}
    end

    wd = {}
    if didDoc["doc"].is_a?(Hash) 
        if didDoc["doc"]["@context"].nil?
            wd["@context"] = oyd_context
        else
            if didDoc["doc"]["@context"].is_a?(Array)
                wd["@context"] = (oyd_context + didDoc["doc"]["@context"]).uniq
            else
                oyd_context << didDoc["doc"]["@context"]
                wd["@context"] = oyd_context.uniq
            end
            didDoc["doc"].delete("@context")
        end
    else
        wd["@context"] = oyd_context
    end
    wd["id"] = percent_encode(did)
    case Multicodecs[code].name
    when 'ed25519-pub'
        wd["verificationMethod"] = [{
            "id": did + "#key-doc",
            "type": "Ed25519VerificationKey2020",
            "controller": did,
            "publicKeyMultibase": pubDocKey
        },{
            "id": did + "#key-rev",
            "type": "Ed25519VerificationKey2020",
            "controller": did,
            "publicKeyMultibase": pubRevKey
        }]
    when 'p256-pub'
        pubDocKey_jwk, msg = public_key_to_jwk(pubDocKey)
        if pubDocKey_jwk.nil?
            return {"error": "document key: " + msg.to_s}
        end
        pubRevKey_jwk, msg = public_key_to_jwk(pubRevKey)
        if pubRevKey_jwk.nil?
            return {"error": "revocation key: " + msg.to_s}
        end
        wd["verificationMethod"] = [{
            "id": did + "#key-doc",
            "type": "JsonWebKey2020",
            "controller": did,
            "publicKeyJwk": pubDocKey_jwk
        },{
            "id": did + "#key-rev",
            "type": "JsonWebKey2020",
            "controller": did,
            "publicKeyJwk": pubRevKey_jwk
        }]
    else
        return {"error": "unsupported key codec (" + Multicodecs[code].name.to_s + ")"}
    end

    if !delegateDocKeys.nil? && delegateDocKeys.count > 0
        i = 0
        wd["capabilityDelegation"] = []
        delegateDocKeys.each do |key|
            i += 1

            delegaton_object = {
                "id": did + "#key-delegate-doc-" + i.to_s,
                "type": "Ed25519VerificationKey2020",
                "controller": did,
                "publicKeyMultibase": key
            }
            wd["capabilityDelegation"] << delegaton_object
        end
    end
    if !delegateRevKeys.nil? && delegateRevKeys.count > 0
        i = 0
        if wd["capabilityDelegation"].nil?
            wd["capabilityDelegation"] = []
        end
        delegateRevKeys.each do |key|
            i += 1
            delegaton_object = {
                "id": did + "#key-delegate-rev-" + i.to_s,
                "type": "Ed25519VerificationKey2020",
                "controller": did,
                "publicKeyMultibase": key
            }
            wd["capabilityDelegation"] << delegaton_object
        end
    end

    # alsoKnownAs is a statement about the DID *subject*, and DID Core calls
    # it best practice not to read it as equivalence unless the relationship
    # is reciprocated - which it cannot be here, because all versions resolve
    # to the same document. It stays for backwards compatibility; the
    # authoritative statement is didDocumentMetadata canonicalId/equivalentId,
    # built from the same list - except that this one keeps the location
    # suffix. alsoKnownAs therefore carries two kinds of statement: other
    # versions of the DID, and the location-bound variant of one. It must not
    # be read as a list of locations; the method specification says so
    # explicitly.
    equivalentIds = version_ids(did_info, true).last
    if equivalentIds.length > 0
        wd["alsoKnownAs"] = equivalentIds
    end

    if didDoc["doc"].is_a?(Hash) && !didDoc["doc"]["service"].nil?
        location = options[:location]
        if location.nil?
            location = get_location(did_info["did"].to_s)
        end
        # a payload carrying a service used to skip the handling below
        # entirely and get merged verbatim, which left fragment identifiers
        # unresolved and embedded methods without a controller
        wd = expand_verification_methods(didDoc["doc"], wd, did)
        wd = wd.merge(didDoc["doc"])
        if wd["service"] != []
            if wd["service"].is_a?(Array)
                wdf = wd["service"].first
            else
                wdf = wd["service"]
            end
            wdf = { "id": did + "#payload",
                    "type": "Custom",
                    "serviceEndpoint": location }.transform_keys(&:to_s).merge(wdf)
            if wdf["id"][0] == '#'
                wdf["id"] = did + wdf["id"]
            end
            wd["service"] = [wdf] + wd["service"].drop(1) 
        end
    else
        payload = nil
        if didDoc["doc"].is_a?(Hash)
            if didDoc["doc"] != {}
                didDoc = didDoc["doc"]
                wd = expand_verification_methods(didDoc, wd, did)
                if didDoc["alsoKnownAs"].to_s != ""
                    if didDoc["alsoKnownAs"].is_a?(Array)
                        dda = didDoc["alsoKnownAs"]
                    else
                        dda = [didDoc["alsoKnownAs"]]
                    end
                    if wd["alsoKnownAs"].nil?
                        wd["alsoKnownAs"] = dda
                    else
                        wd["alsoKnownAs"] += dda
                    end
                    didDoc.delete("alsoKnownAs")
                end
                payload = didDoc
                if payload == {}
                    payload = nil
                end
            end
        else
            payload = didDoc["doc"]
        end
        if !payload.nil?
            location = options[:location]
            if location.nil?
                location = get_location(did_info["did"].to_s)
            end
            if payload.is_a?(Array) &&
                    payload.length == 1 &&
                    payload.first.is_a?(Hash) &&
                    !payload.first["id"].nil? &&
                    !payload.first["type"].nil? &&
                    !payload.first["serviceEndpoint"].nil?
                wd["service"] = payload
            else
                wd["service"] = [{
                    "id": did + "#payload",
                    "type": "Custom",
                    "serviceEndpoint": location,
                    "payload": payload
                }]
            end
        end
    end
    return wd
end

.w3c_legacy(did_info, options) ⇒ Object



1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
# File 'lib/oydid.rb', line 1791

def self.w3c_legacy(did_info, options)
    did = did_info["did"]
    if !did.start_with?("did:oyd:")
        did = "did:oyd:" + did
    end

    didDoc = did_info.transform_keys(&:to_s)["doc"]
    pubDocKey = didDoc["key"].split(":")[0] rescue ""
    pubRevKey = didDoc["key"].split(":")[1] rescue ""

    wd = {}
    wd["@context"] = ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1"]
    wd["id"] = percent_encode(did)
    wd["verificationMethod"] = [{
        "id": did + "#key-doc",
        "type": "Ed25519VerificationKey2020",
        "controller": did,
        "publicKeyMultibase": pubDocKey
    },{
        "id": did + "#key-rev",
        "type": "Ed25519VerificationKey2020",
        "controller": did,
        "publicKeyMultibase": pubRevKey
    }]

    if didDoc["@context"] == ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/suites/ed25519-2020/v1"]
        didDoc.delete("@context")
    end
    if !didDoc["doc"].nil?
        newDidDoc = []
        if didDoc.is_a?(Hash)
            if didDoc["authentication"].to_s != ""
                wd["authentication"] = didDoc["authentication"]
                didDoc.delete("authentication")
            end
            if didDoc["service"].to_s != ""
                if didDoc["service"].is_a?(Array)
                    newDidDoc = didDoc.dup
                    newDidDoc.delete("service")
                    if newDidDoc == {}
                        newDidDoc = []
                    else
                        if !newDidDoc.is_a?(Array)
                            newDidDoc=[newDidDoc]
                        end
                    end
                    newDidDoc << didDoc["service"]
                    newDidDoc = newDidDoc.flatten
                else
                    newDidDoc = didDoc["service"]
                end
            else
                newDidDoc = didDoc["doc"]
            end
        else
            newDidDoc = didDoc["doc"]
        end
        wd["service"] = newDidDoc
    end
    return wd
end

.write(content, did, mode, options) ⇒ Object



896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'lib/oydid.rb', line 896

def self.write(content, did, mode, options)
    did_doc, did_key, did_log, msg = generate_base(content, did, mode, options)
    if msg != ""
        if msg == "cmsm"
            return [did_doc, 'cmsm']
        end
        return [nil, msg]
    end
    did = did_doc[:did]
    didDocument = did_doc[:didDocument]
    did_old = did_doc[:did_old]
    revoc_log = did_log[:revoc_log]
    l1 = did_log[:l1]
    l2 = did_log[:l2]
    r1 = did_log[:r1]
    r1_encrypted = did_log[:r1_encrypted]
    log_old = did_log[:log_old]
    privateKey = did_key[:privateKey]
    revocationKey = did_key[:revocationKey]
    # did, didDocument, revoc_log, l1, l2, r1, privateKey, revocationKey, did_old, log_old, msg = generate_base(content, did, mode, options)

    did_hash = did.delete_prefix("did:oyd:")
    did10 = did_hash[0,10]
    did_old_hash = did_old.delete_prefix("did:oyd:") rescue nil
    did10_old = did_old_hash[0,10] rescue nil

    doc_location = options[:doc_location]
    if doc_location.to_s == ""
        if did_hash.include?(LOCATION_PREFIX)
            hash_split = did_hash.split(LOCATION_PREFIX)
            did_hash = hash_split[0]
            doc_location = hash_split[1]
        else
            doc_location = DEFAULT_LOCATION
        end
    end

    case doc_location.to_s
    when /^http/
        logs = [revoc_log, l1, l2, r1_encrypted].flatten.compact
    else
        logs = [log_old, revoc_log, l1, l2].flatten.compact
        if !did_old.nil?
            write_private_storage([log_old, revoc_log, l1, l2].flatten.compact.to_json, did10_old + ".log")
        end
    end

    success, msg = publish(did, didDocument, logs, options)

    if success
        didDocumentBackup = Marshal.load(Marshal.dump(didDocument))
        w3c_input = {
            "did" => did.clone,
            "doc" => didDocument.clone
        }
        doc_w3c = w3c(w3c_input, options)
        didDocument = didDocumentBackup
        retVal = {
            "did" => did,
            "doc" => didDocument,
            "doc_w3c" => doc_w3c,
            "log" => logs
        }
        if options[:return_secrets]
            retVal["private_key"] = privateKey
            retVal["revocation_key"] = revocationKey
            retVal["revocation_log"] = r1
        else
            write_private_storage(privateKey, did10 + "_private_key.enc")
            write_private_storage(revocationKey, did10 + "_revocation_key.enc")
            write_private_storage(r1.to_json, did10 + "_revocation.json")
        end

        return [retVal, ""]
    else
        return [nil, msg]
    end
end

.write_log(did, log, options = {}) ⇒ Object



975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/oydid.rb', line 975

def self.write_log(did, log, options = {})
    # validate log
    if !log.is_a?(Hash)
        return [nil, "invalid log input"]
    end
    log = log.transform_keys(&:to_s)
    if log["ts"].nil?
        return [nil, "missing timestamp in log"]
    end
    if log["op"].nil?
       return [nil, "missing operation in log"]
    end 
    if log["doc"].nil?
       return [nil, "missing doc entry in log"]
    end 
    if log["sig"].nil?
       return [nil, "missing signature in log"]
    end

    # validate did
    if did.include?(LOCATION_PREFIX)
        tmp = did.split(LOCATION_PREFIX)
        did = tmp[0]
        source_location = tmp[1]
        log_location = tmp[1]
    end
    if did.include?(CGI.escape LOCATION_PREFIX)
        tmp = did.split(CGI.escape LOCATION_PREFIX)
        did = tmp[0] 
        source_location = tmp[1]
        log_location = tmp[1]
    end

    if source_location.to_s == ""
        if options[:doc_location].nil?
            source_location = DEFAULT_LOCATION
        else
            source_location = options[:doc_location]
        end
        if options[:log_location].nil?
            log_location = DEFAULT_LOCATION
        else
            log_location = options[:log_location]
        end
    end
    options[:doc_location] = source_location
    options[:log_location] = log_location
    source_did, msg = read(did, options)
    if source_did.nil?
        return [nil, "cannot resolve DID (on writing logs)"]
    end
    if source_did["error"] != 0
        return [nil, source_did["message"].to_s]
    end
    if source_did["doc_log_id"].nil?
        return [nil, "cannot parse DID log"]
    end

    # write log
    source_location = source_location.gsub("%3A",":")
    source_location = source_location.gsub("%2F%2F","//")
    retVal = HTTParty.post(source_location + "/log/" + did,
        headers: { 'Content-Type' => 'application/json' },
        body: {"log": log}.to_json )
    code = retVal.code rescue 500
    if code != 200
        err_msg = retVal.parsed_response["error"].to_s rescue "invalid response from " + source_location.to_s + "/log"
        return ["", err_msg]
    end
    log_hash = retVal.parsed_response["log"] rescue ""
    if log_hash == ""
        err_msg = "missing log hash from " + source_location.to_s + "/log"
        return ["", err_msg]
    end
    return [log_hash, nil]
end

.write_private_storage(payload, filename) ⇒ Object

storage functions -----------------------------



1207
1208
1209
# File 'lib/oydid/basic.rb', line 1207

def self.write_private_storage(payload, filename)
    File.open(filename, 'w') {|f| f.write(payload)}
end

Instance Method Details

#jweh(key) ⇒ Object



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/oydid/basic.rb', line 662

def jweh(key)
    pub_key=key[-64..-1]
    prv_key=key[0..-65]
    
    hex_pub=pub_key
    bin_pub=[hex_pub].pack('H*')
    int_pub=RbNaCl::PublicKey.new(bin_pub)
    len_pub=int_pub.to_bytes.bytesize
    enc_pub=multi_encode([Multicodecs["x25519-pub"].code,len_pub,int_pub].pack("CCa#{len_pub}"),{}).first

    hex_prv=prv_key
    bin_prv=[hex_prv].pack('H*')
    int_prv=RbNaCl::PrivateKey.new(bin_prv)
    len_prv=int_prv.to_bytes.bytesize
    enc_prv=multi_encode([Multicodecs["ed25519-priv"].code,len_prv,int_prv].pack("SCa#{len_prv}"),{}).first
    return [enc_pub, enc_prv]
end