Skip to content

Commit 7c8684b

Browse files
committed
Add returning error when unknown attribute type
1 parent 2314922 commit 7c8684b

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

coder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ func EncodeAttribute(attribute C.CK_ATTRIBUTE, forceValueNil bool) []byte {
455455
buffer.Write(EncodeByte(ConvertBooleanToByte(hasLength)))
456456

457457
valueType := GetAttributeValueType(attribute._type, attribute.ulValueLen)
458+
if valueType == nil {
459+
return nil
460+
}
458461

459462
if hasLength && valueType.Kind() == reflect.Slice {
460463
if valueType.Elem() == reflect.TypeOf(*new(C.CK_ATTRIBUTE)) {

main.go

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,11 @@ func C_CopyObject(hSession C.CK_SESSION_HANDLE, hObject C.CK_OBJECT_HANDLE, pTem
372372
inBuffer.Write(EncodeUnsignedLong(hObject))
373373
inBuffer.Write(EncodeUnsignedLongAsLength(ulCount)) // Moved up
374374
for _, attribute := range unsafe.Slice(pTemplate, ulCount) {
375-
inBuffer.Write(EncodeAttribute(attribute, false))
375+
attr := EncodeAttribute(attribute, false)
376+
if attr == nil {
377+
return C.CKR_ATTRIBUTE_TYPE_INVALID
378+
}
379+
inBuffer.Write(attr)
376380
}
377381
// (See: Moved up)
378382
inputParameters := inBuffer.Bytes()
@@ -401,7 +405,11 @@ func C_CreateObject(hSession C.CK_SESSION_HANDLE, pTemplate C.CK_ATTRIBUTE_PTR,
401405
inBuffer.Write(EncodeUnsignedLong(hSession))
402406
inBuffer.Write(EncodeUnsignedLongAsLength(ulCount)) // Moved up
403407
for _, attribute := range unsafe.Slice(pTemplate, ulCount) {
404-
inBuffer.Write(EncodeAttribute(attribute, false))
408+
attr := EncodeAttribute(attribute, false)
409+
if attr == nil {
410+
return C.CKR_ATTRIBUTE_TYPE_INVALID
411+
}
412+
inBuffer.Write(attr)
405413
}
406414
// (See: Moved up)
407415
inputParameters := inBuffer.Bytes()
@@ -723,7 +731,11 @@ func C_DeriveKey(hSession C.CK_SESSION_HANDLE, pMechanism C.CK_MECHANISM_PTR, hB
723731
inBuffer.Write(EncodeUnsignedLong(hBaseKey))
724732
inBuffer.Write(EncodeUnsignedLongAsLength(ulAttributeCount)) // Moved up
725733
for _, attribute := range unsafe.Slice(pTemplate, ulAttributeCount) {
726-
inBuffer.Write(EncodeAttribute(attribute, false))
734+
attr := EncodeAttribute(attribute, false)
735+
if attr == nil {
736+
return C.CKR_ATTRIBUTE_TYPE_INVALID
737+
}
738+
inBuffer.Write(attr)
727739
}
728740
// (See: Moved up)
729741
inputParameters := inBuffer.Bytes()
@@ -1186,7 +1198,11 @@ func C_FindObjectsInit(hSession C.CK_SESSION_HANDLE, pTemplate C.CK_ATTRIBUTE_PT
11861198
inBuffer.Write(EncodeUnsignedLong(hSession))
11871199
inBuffer.Write(EncodeUnsignedLongAsLength(ulCount)) // Moved up
11881200
for _, attribute := range unsafe.Slice(pTemplate, ulCount) {
1189-
inBuffer.Write(EncodeAttribute(attribute, false))
1201+
attr := EncodeAttribute(attribute, false)
1202+
if attr == nil {
1203+
return C.CKR_ATTRIBUTE_TYPE_INVALID
1204+
}
1205+
inBuffer.Write(attr)
11901206
}
11911207
// (See: Moved up)
11921208
inputParameters := inBuffer.Bytes()
@@ -1205,7 +1221,11 @@ func C_GenerateKey(hSession C.CK_SESSION_HANDLE, pMechanism C.CK_MECHANISM_PTR,
12051221
inBuffer.Write(EncodeMechanism(*pMechanism))
12061222
inBuffer.Write(EncodeUnsignedLongAsLength(ulCount)) // Moved up
12071223
for _, attribute := range unsafe.Slice(pTemplate, ulCount) {
1208-
inBuffer.Write(EncodeAttribute(attribute, false))
1224+
attr := EncodeAttribute(attribute, false)
1225+
if attr == nil {
1226+
return C.CKR_ATTRIBUTE_TYPE_INVALID
1227+
}
1228+
inBuffer.Write(attr)
12091229
}
12101230
// (See: Moved up)
12111231
inputParameters := inBuffer.Bytes()
@@ -1235,12 +1255,20 @@ func C_GenerateKeyPair(hSession C.CK_SESSION_HANDLE, pMechanism C.CK_MECHANISM_P
12351255
inBuffer.Write(EncodeMechanism(*pMechanism))
12361256
inBuffer.Write(EncodeUnsignedLongAsLength(ulPublicKeyAttributeCount)) // Moved up
12371257
for _, attribute := range unsafe.Slice(pPublicKeyTemplate, ulPublicKeyAttributeCount) {
1238-
inBuffer.Write(EncodeAttribute(attribute, false))
1258+
attr := EncodeAttribute(attribute, false)
1259+
if attr == nil {
1260+
return C.CKR_ATTRIBUTE_TYPE_INVALID
1261+
}
1262+
inBuffer.Write(attr)
12391263
}
12401264
// (See: Moved up)
12411265
inBuffer.Write(EncodeUnsignedLongAsLength(ulPrivateKeyAttributeCount)) // Moved up
12421266
for _, attribute := range unsafe.Slice(pPrivateKeyTemplate, ulPrivateKeyAttributeCount) {
1243-
inBuffer.Write(EncodeAttribute(attribute, false))
1267+
attr := EncodeAttribute(attribute, false)
1268+
if attr == nil {
1269+
return C.CKR_ATTRIBUTE_TYPE_INVALID
1270+
}
1271+
inBuffer.Write(attr)
12441272
}
12451273
// (See: Moved up)
12461274
inputParameters := inBuffer.Bytes()
@@ -1300,7 +1328,11 @@ func C_GetAttributeValue(hSession C.CK_SESSION_HANDLE, hObject C.CK_OBJECT_HANDL
13001328
inBuffer.Write(EncodeUnsignedLong(hObject))
13011329
inBuffer.Write(EncodeUnsignedLongAsLength(ulCount)) // Moved up
13021330
for _, attribute := range unsafe.Slice(pTemplate, ulCount) {
1303-
inBuffer.Write(EncodeAttribute(attribute, true))
1331+
attr := EncodeAttribute(attribute, true)
1332+
if attr == nil {
1333+
return C.CKR_ATTRIBUTE_TYPE_INVALID
1334+
}
1335+
inBuffer.Write(attr)
13041336
}
13051337
// (See: Moved up)
13061338
inputParameters := inBuffer.Bytes()
@@ -1963,7 +1995,11 @@ func C_SetAttributeValue(hSession C.CK_SESSION_HANDLE, hObject C.CK_OBJECT_HANDL
19631995
inBuffer.Write(EncodeUnsignedLong(hObject))
19641996
inBuffer.Write(EncodeUnsignedLongAsLength(ulCount)) // Moved up
19651997
for _, attribute := range unsafe.Slice(pTemplate, ulCount) {
1966-
inBuffer.Write(EncodeAttribute(attribute, false))
1998+
attr := EncodeAttribute(attribute, false)
1999+
if attr == nil {
2000+
return C.CKR_ATTRIBUTE_TYPE_INVALID
2001+
}
2002+
inBuffer.Write(attr)
19672003
}
19682004
// (See: Moved up)
19692005
inputParameters := inBuffer.Bytes()
@@ -2300,7 +2336,11 @@ func C_UnwrapKey(hSession C.CK_SESSION_HANDLE, pMechanism C.CK_MECHANISM_PTR, hU
23002336
inBuffer.Write(EncodeBytePointer(pWrappedKey, ulWrappedKeyLen))
23012337
inBuffer.Write(EncodeUnsignedLongAsLength(ulAttributeCount)) // Moved up
23022338
for _, attribute := range unsafe.Slice(pTemplate, ulAttributeCount) {
2303-
inBuffer.Write(EncodeAttribute(attribute, false))
2339+
attr := EncodeAttribute(attribute, false)
2340+
if attr == nil {
2341+
return C.CKR_ATTRIBUTE_TYPE_INVALID
2342+
}
2343+
inBuffer.Write(attr)
23042344
}
23052345
// (See: Moved up)
23062346
inputParameters := inBuffer.Bytes()

0 commit comments

Comments
 (0)