Exemple #1
0
func (key *pKey) SignPKCS1v15(method Method, data []byte) ([]byte, error) {
	var ctx C.EVP_MD_CTX
	C.EVP_MD_CTX_init(&ctx)
	defer C.EVP_MD_CTX_cleanup(&ctx)

	if 1 != C.EVP_SignInit_not_a_macro(&ctx, method) {
		return nil, errors.New("signpkcs1v15: failed to init signature")
	}
	if len(data) > 0 {
		if 1 != C.EVP_SignUpdate_not_a_macro(
			&ctx, unsafe.Pointer(&data[0]), C.uint(len(data))) {
			return nil, errors.New("signpkcs1v15: failed to update signature")
		}
	}
	sig := make([]byte, C.EVP_PKEY_size(key.key))
	var sigblen C.uint
	if 1 != C.EVP_SignFinal(&ctx,
		((*C.uchar)(unsafe.Pointer(&sig[0]))), &sigblen, key.key) {
		return nil, errors.New("signpkcs1v15: failed to finalize signature")
	}
	return sig[:sigblen], nil
}
Exemple #2
0
func (key *pKey) SignPKCS1v15(data []byte) ([]byte, error) {
	//
	// Initialize the context using sha1 as method, as we
	// need to generate RSA-SHA1 signature
	//
	var ctx C.EVP_MD_CTX
	C.EVP_MD_CTX_init(&ctx)
	defer C.EVP_MD_CTX_cleanup(&ctx)

	if 1 != C.EVP_SignInit_not_a_macro(&ctx, C.EVP_sha1()) {
		return nil, errors.New("rsasha1signature: failed to init signature")
	}
	if len(data) > 0 {
		if 1 != C.EVP_SignUpdate_not_a_macro(
			&ctx, unsafe.Pointer(&data[0]), C.uint(len(data))) {
			return nil, errors.New("rsasha1signature: failed to update signature")
		}
	}

	//
	// Sign
	//
	sig := make([]byte, C.EVP_PKEY_size(key.key))
	var sigblen C.uint

	// prevent data race when multiple threads are spawned
	// by the go runtime, mostly because you are performing the signing
	// inside a concurrent http request.
	sslMutex.Lock()
	defer sslMutex.Unlock()

	if 1 != C.EVP_SignFinal(&ctx,
		((*C.uchar)(unsafe.Pointer(&sig[0]))), &sigblen, key.key) {
		return nil, errors.New("rsasha1signature: failed to finalize signature")
	}
	return sig[:sigblen], nil
}