76
76
77
77
CHIP_ERROR CHIPOperationalCredentialsDelegate::GenerateNOC (
78
78
NodeId nodeId, FabricId fabricId, const chip::CATValues & cats, const Crypto::P256PublicKey & pubkey, MutableByteSpan & noc)
79
+ {
80
+ return GenerateNOC (
81
+ *mIssuerKey , (mIntermediateCert != nil ) ? mIntermediateCert : mRootCert , nodeId, fabricId, cats, pubkey, noc);
82
+ }
83
+
84
+ CHIP_ERROR CHIPOperationalCredentialsDelegate::GenerateNOC (P256Keypair & signingKeypair, NSData * signingCertificate, NodeId nodeId,
85
+ FabricId fabricId, const CATValues & cats, const P256PublicKey & pubkey, MutableByteSpan & noc)
79
86
{
80
87
uint32_t validityStart, validityEnd;
81
88
90
97
}
91
98
92
99
ChipDN signerSubject;
93
- NSData * signer = (mIntermediateCert != nil ) ? mIntermediateCert : mRootCert ;
94
- ReturnErrorOnFailure (ExtractSubjectDNFromX509Cert (AsByteSpan (signer), signerSubject));
100
+ ReturnErrorOnFailure (ExtractSubjectDNFromX509Cert (AsByteSpan (signingCertificate), signerSubject));
95
101
96
102
ChipDN noc_dn;
97
103
ReturnErrorOnFailure (noc_dn.AddAttribute_MatterFabricId (fabricId));
98
104
ReturnErrorOnFailure (noc_dn.AddAttribute_MatterNodeId (nodeId));
99
105
ReturnErrorOnFailure (noc_dn.AddCATs (cats));
100
106
101
107
X509CertRequestParams noc_request = { 1 , validityStart, validityEnd, noc_dn, signerSubject };
102
- return NewNodeOperationalX509Cert (noc_request, pubkey, * mIssuerKey , noc);
108
+ return NewNodeOperationalX509Cert (noc_request, pubkey, signingKeypair , noc);
103
109
}
104
110
105
111
CHIP_ERROR CHIPOperationalCredentialsDelegate::GenerateNOCChain (const chip::ByteSpan & csrElements, const chip::ByteSpan & csrNonce,
@@ -199,7 +205,9 @@ uint64_t GetIssuerId(NSNumber * _Nullable providedIssuerId)
199
205
ReturnErrorOnFailure (rcac_dn.AddAttribute_MatterRCACId (GetIssuerId (issuerId)));
200
206
201
207
if (fabricId != nil ) {
202
- ReturnErrorOnFailure (rcac_dn.AddAttribute_MatterFabricId ([fabricId unsignedLongLongValue ]));
208
+ FabricId fabric = [fabricId unsignedLongLongValue ];
209
+ VerifyOrReturnError (fabric != kUndefinedFabricId , CHIP_ERROR_INVALID_ARGUMENT);
210
+ ReturnErrorOnFailure (rcac_dn.AddAttribute_MatterFabricId (fabric));
203
211
}
204
212
205
213
uint32_t validityStart, validityEnd;
@@ -228,7 +236,7 @@ uint64_t GetIssuerId(NSNumber * _Nullable providedIssuerId)
228
236
{
229
237
*intermediateCert = nil ;
230
238
231
- // Verify that the provided certificate public key matches the root keypair.
239
+ // Verify that the provided root certificate public key matches the root keypair.
232
240
if ([MTRCertificates keypair: rootKeypair matchesCertificate: rootCertificate] == NO ) {
233
241
return CHIP_ERROR_INVALID_ARGUMENT;
234
242
}
@@ -248,7 +256,9 @@ uint64_t GetIssuerId(NSNumber * _Nullable providedIssuerId)
248
256
ChipDN icac_dn;
249
257
ReturnErrorOnFailure (icac_dn.AddAttribute_MatterICACId (GetIssuerId (issuerId)));
250
258
if (fabricId != nil ) {
251
- ReturnErrorOnFailure (icac_dn.AddAttribute_MatterFabricId ([fabricId unsignedLongLongValue ]));
259
+ FabricId fabric = [fabricId unsignedLongLongValue ];
260
+ VerifyOrReturnError (fabric != kUndefinedFabricId , CHIP_ERROR_INVALID_ARGUMENT);
261
+ ReturnErrorOnFailure (icac_dn.AddAttribute_MatterFabricId (fabric));
252
262
}
253
263
254
264
uint32_t validityStart, validityEnd;
@@ -270,3 +280,47 @@ uint64_t GetIssuerId(NSNumber * _Nullable providedIssuerId)
270
280
*intermediateCert = AsData (icac);
271
281
return CHIP_NO_ERROR;
272
282
}
283
+
284
+ CHIP_ERROR CHIPOperationalCredentialsDelegate::GenerateOperationalCertificate (id <CHIPKeypair> signingKeypair,
285
+ NSData * signingCertificate, SecKeyRef operationalPublicKey, NSNumber * fabricId, NSNumber * nodeId,
286
+ NSArray <NSNumber *> * _Nullable caseAuthenticatedTags, NSData * _Nullable __autoreleasing * _Nonnull operationalCert)
287
+ {
288
+ *operationalCert = nil ;
289
+
290
+ // Verify that the provided signing certificate public key matches the signing keypair.
291
+ if ([MTRCertificates keypair: signingKeypair matchesCertificate: signingCertificate] == NO ) {
292
+ return CHIP_ERROR_INVALID_ARGUMENT;
293
+ }
294
+
295
+ if ([caseAuthenticatedTags count ] > kMaxSubjectCATAttributeCount ) {
296
+ return CHIP_ERROR_INVALID_ARGUMENT;
297
+ }
298
+
299
+ FabricId fabric = [fabricId unsignedLongLongValue ];
300
+ VerifyOrReturnError (fabric != kUndefinedFabricId , CHIP_ERROR_INVALID_ARGUMENT);
301
+
302
+ NodeId node = [nodeId unsignedLongLongValue ];
303
+ VerifyOrReturnError (IsOperationalNodeId (node), CHIP_ERROR_INVALID_ARGUMENT);
304
+
305
+ CHIPP256KeypairBridge keypairBridge;
306
+ ReturnErrorOnFailure (keypairBridge.Init (signingKeypair));
307
+ CHIPP256KeypairNativeBridge nativeSigningKeypair (keypairBridge);
308
+
309
+ P256PublicKey pubKey;
310
+ ReturnErrorOnFailure (CHIPP256KeypairBridge::MatterPubKeyFromSecKeyRef (operationalPublicKey, &pubKey));
311
+
312
+ CATValues cats;
313
+ if (caseAuthenticatedTags != nil ) {
314
+ size_t idx = 0 ;
315
+ for (NSNumber * cat in caseAuthenticatedTags) {
316
+ cats.values [idx++] = [cat unsignedIntValue ];
317
+ }
318
+ }
319
+
320
+ uint8_t nocBuffer[Controller::kMaxCHIPDERCertLength ];
321
+ MutableByteSpan noc (nocBuffer);
322
+ ReturnErrorOnFailure (GenerateNOC (nativeSigningKeypair, signingCertificate, node, fabric, cats, pubKey, noc));
323
+
324
+ *operationalCert = AsData (noc);
325
+ return CHIP_NO_ERROR;
326
+ }
0 commit comments