-1

From the meigk/pkcs11 package. Is there any way to assign a negative value in the Login function as userType? Because the HSM model that I'm working with, supports one more additional role except for the following standard roles and I want to login with that role.

Standard roles:

CKU_SO               uint = 0
CKU_USER             uint = 1
CKU_CONTEXT_SPECIFIC uint = 2

Login function source code from https://github.com/miekg/pkcs11/

func (c *Ctx) Login(sh SessionHandle, userType uint, pin string) error {
    p := C.CString(pin)
    defer C.free(unsafe.Pointer(p))
    e := C.Login(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_USER_TYPE(userType), p, C.CK_ULONG(len(pin)))
    return toError(e)
}

In the C#. I have using the PKCS11Interop package and its login function also accepts only for the uint parameter as userType. But it can be done by using the unchecked keyword. So I can put the negative value in the Login function without an error.

Here is my sample code in C# using https://github.com/Pkcs11Interop

public enum SpecialRole
{
   CU = -12345
}
session.Login(unchecked((CKU)SpecialRole.CU), Settings.CryptoUserPin);

Then, how can I do this in Go? Kindly suggest.

1 Answer 1

2

If type of the parameter is uint, you can only pass values that are assignable to the type uint (as stated in Spec: Calls). And the valid range of uint does not include negative numbers.

What you may do is convert your negative value to uint. Converting a negative int to uint is roughly equivalent to subtracting the absolute value from the max value of int plus 1. So if your other constants are small, this won't cause a collision.

For example:

func main() {
    f(1)
    
    f(math.MaxUint64+1 -10)

    x := -10
    f(uint(x))
}

func f(i uint) {
    fmt.Println(i)
}

This will output (try it on the Go Playground):

1
18446744073709551606
18446744073709551606
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.