Skip to content

Commit

Permalink
standardize returns on <= 0 (or 0 >=)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunixbochs committed Nov 17, 2014
1 parent e1857f7 commit 3207418
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 67 deletions.
18 changes: 9 additions & 9 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (n *Name) AddTextEntry(field, value string) error {
defer C.free(unsafe.Pointer(cvalue))
ret := C.X509_NAME_add_entry_by_txt(
n.name, cfield, C.MBSTRING_ASC, cvalue, -1, -1, 0)
if ret == 0 {
if ret <= 0 {
return errors.New("failed to add x509 name text entry")
}
return nil
Expand Down Expand Up @@ -162,7 +162,7 @@ func (c *Certificate) GetIssuerName() (*Name, error) {
}

func (c *Certificate) SetSubjectName(name *Name) error {
if C.X509_set_subject_name(c.x, name.name) == 0 {
if C.X509_set_subject_name(c.x, name.name) <= 0 {
return errors.New("failed to set subject name")
}
return nil
Expand All @@ -186,15 +186,15 @@ func (c *Certificate) SetIssuer(issuer *Certificate) error {
// SetIssuerName populates the issuer name of a certificate.
// Use SetIssuer instead, if possible.
func (c *Certificate) SetIssuerName(name *Name) error {
if C.X509_set_issuer_name(c.x, name.name) == 0 {
if C.X509_set_issuer_name(c.x, name.name) <= 0 {
return errors.New("failed to set subject name")
}
return nil
}

// SetSerial sets the serial of a certificate.
func (c *Certificate) SetSerial(serial int) error {
if C.ASN1_INTEGER_set(C.X509_get_serialNumber(c.x), C.long(serial)) == 0 {
if C.ASN1_INTEGER_set(C.X509_get_serialNumber(c.x), C.long(serial)) <= 0 {
return errors.New("failed to set serial")
}
return nil
Expand Down Expand Up @@ -223,7 +223,7 @@ func (c *Certificate) SetExpireDate(when time.Duration) error {
// SetPubKey assigns a new public key to a certificate.
func (c *Certificate) SetPubKey(pubKey PublicKey) error {
c.pubKey = pubKey
if C.X509_set_pubkey(c.x, pubKey.evpPKey()) == 0 {
if C.X509_set_pubkey(c.x, pubKey.evpPKey()) <= 0 {
return errors.New("failed to set public key")
}
return nil
Expand Down Expand Up @@ -271,7 +271,7 @@ func (c *Certificate) insecureSign(privKey PrivateKey, digest EVP_MD) error {
case EVP_SHA512:
md = C.EVP_sha512()
}
if C.X509_sign(c.x, privKey.evpPKey(), md) == 0 {
if C.X509_sign(c.x, privKey.evpPKey(), md) <= 0 {
return errors.New("failed to sign certificate")
}
return nil
Expand All @@ -291,7 +291,7 @@ func (c *Certificate) AddExtension(nid NID, value string) error {
return errors.New("failed to create x509v3 extension")
}
defer C.X509_EXTENSION_free(ex)
if C.X509_add_ext(c.x, ex, -1) == 0 {
if C.X509_add_ext(c.x, ex, -1) <= 0 {
return errors.New("failed to add x509v3 extension")
}
return nil
Expand All @@ -310,7 +310,7 @@ func (c *Certificate) AddExtensions(extensions map[NID]string) error {

// LoadCertificateFromPEM loads an X509 certificate from a PEM-encoded block.
func LoadCertificateFromPEM(pem_block []byte) (*Certificate, error) {
if len(pem_block) == 0 {
if len(pem_block) <= 0 {
return nil, errors.New("empty pem block")
}
runtime.LockOSThread()
Expand All @@ -336,7 +336,7 @@ func (c *Certificate) MarshalPEM() (pem_block []byte, err error) {
return nil, errors.New("failed to allocate memory BIO")
}
defer C.BIO_free(bio)
if int(C.PEM_write_bio_X509(bio, c.x)) != 1 {
if int(C.PEM_write_bio_X509(bio, c.x)) <= 0 {
return nil, errors.New("failed dumping certificate")
}
return ioutil.ReadAll(asAnyBio(bio))
Expand Down
39 changes: 18 additions & 21 deletions ciphers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (ctx *cipherCtx) applyKeyAndIV(key, iv []byte) error {
iptr = (*C.uchar)(&iv[0])
}
if kptr != nil || iptr != nil {
if 1 != C.EVP_EncryptInit_ex(ctx.ctx, nil, nil, kptr, iptr) {
if 0 >= C.EVP_EncryptInit_ex(ctx.ctx, nil, nil, kptr, iptr) {
return errors.New("failed to apply key/IV")
}
}
Expand All @@ -178,19 +178,18 @@ func (ctx *cipherCtx) IVSize() int {

func (ctx *cipherCtx) setCtrl(code, arg int) error {
res := C.EVP_CIPHER_CTX_ctrl(ctx.ctx, C.int(code), C.int(arg), nil)
if res != 1 {
return fmt.Errorf("failed to set code %d to %d [result %d]",
code, arg, res)
if res <= 0 {
return fmt.Errorf("failed to set code %d to %d", code, arg)
}
return nil
}

func (ctx *cipherCtx) setCtrlBytes(code, arg int, value []byte) error {
res := C.EVP_CIPHER_CTX_ctrl(ctx.ctx, C.int(code), C.int(arg),
unsafe.Pointer(&value[0]))
if res != 1 {
return fmt.Errorf("failed to set code %d with arg %d to %x [result %d]",
code, arg, value, res)
if res <= 0 {
return fmt.Errorf("failed to set code %d with arg %d to %x",
code, arg, value)
}
return nil
}
Expand All @@ -199,9 +198,8 @@ func (ctx *cipherCtx) getCtrlInt(code, arg int) (int, error) {
var returnVal C.int
res := C.EVP_CIPHER_CTX_ctrl(ctx.ctx, C.int(code), C.int(arg),
unsafe.Pointer(&returnVal))
if res != 1 {
return 0, fmt.Errorf("failed to get code %d with arg %d [result %d]",
code, arg, res)
if res <= 0 {
return 0, fmt.Errorf("failed to get code %d with arg %d", code, arg)
}
return int(returnVal), nil
}
Expand All @@ -210,9 +208,8 @@ func (ctx *cipherCtx) getCtrlBytes(code, arg, expectsize int) ([]byte, error) {
returnVal := make([]byte, expectsize)
res := C.EVP_CIPHER_CTX_ctrl(ctx.ctx, C.int(code), C.int(arg),
unsafe.Pointer(&returnVal[0]))
if res != 1 {
return nil, fmt.Errorf("failed to get code %d with arg %d [result %d]",
code, arg, res)
if res <= 0 {
return nil, fmt.Errorf("failed to get code %d with arg %d", code, arg)
}
return returnVal, nil
}
Expand Down Expand Up @@ -263,7 +260,7 @@ func newEncryptionCipherCtx(c *Cipher, e *Engine, key, iv []byte) (
if e != nil {
eptr = e.e
}
if 1 != C.EVP_EncryptInit_ex(ctx.ctx, c.ptr, eptr, nil, nil) {
if 0 >= C.EVP_EncryptInit_ex(ctx.ctx, c.ptr, eptr, nil, nil) {
return nil, errors.New("failed to initialize cipher context")
}
err = ctx.applyKeyAndIV(key, iv)
Expand All @@ -286,7 +283,7 @@ func newDecryptionCipherCtx(c *Cipher, e *Engine, key, iv []byte) (
if e != nil {
eptr = e.e
}
if 1 != C.EVP_DecryptInit_ex(ctx.ctx, c.ptr, eptr, nil, nil) {
if 0 >= C.EVP_DecryptInit_ex(ctx.ctx, c.ptr, eptr, nil, nil) {
return nil, errors.New("failed to initialize cipher context")
}
err = ctx.applyKeyAndIV(key, iv)
Expand All @@ -311,8 +308,8 @@ func (ctx *encryptionCipherCtx) EncryptUpdate(input []byte) ([]byte, error) {
outlen := C.int(len(outbuf))
res := C.EVP_EncryptUpdate(ctx.ctx, (*C.uchar)(&outbuf[0]), &outlen,
(*C.uchar)(&input[0]), C.int(len(input)))
if res != 1 {
return nil, fmt.Errorf("failed to encrypt [result %d]", res)
if res <= 0 {
return nil, fmt.Errorf("failed to encrypt")
}
return outbuf[:outlen], nil
}
Expand All @@ -322,16 +319,16 @@ func (ctx *decryptionCipherCtx) DecryptUpdate(input []byte) ([]byte, error) {
outlen := C.int(len(outbuf))
res := C.EVP_DecryptUpdate(ctx.ctx, (*C.uchar)(&outbuf[0]), &outlen,
(*C.uchar)(&input[0]), C.int(len(input)))
if res != 1 {
return nil, fmt.Errorf("failed to decrypt [result %d]", res)
if res <= 0 {
return nil, fmt.Errorf("failed to decrypt")
}
return outbuf[:outlen], nil
}

func (ctx *encryptionCipherCtx) EncryptFinal() ([]byte, error) {
outbuf := make([]byte, ctx.BlockSize())
var outlen C.int
if 1 != C.EVP_EncryptFinal_ex(ctx.ctx, (*C.uchar)(&outbuf[0]), &outlen) {
if 0 >= C.EVP_EncryptFinal_ex(ctx.ctx, (*C.uchar)(&outbuf[0]), &outlen) {
return nil, errors.New("encryption failed")
}
return outbuf[:outlen], nil
Expand All @@ -340,7 +337,7 @@ func (ctx *encryptionCipherCtx) EncryptFinal() ([]byte, error) {
func (ctx *decryptionCipherCtx) DecryptFinal() ([]byte, error) {
outbuf := make([]byte, ctx.BlockSize())
var outlen C.int
if 1 != C.EVP_DecryptFinal_ex(ctx.ctx, (*C.uchar)(&outbuf[0]), &outlen) {
if 0 >= C.EVP_DecryptFinal_ex(ctx.ctx, (*C.uchar)(&outbuf[0]), &outlen) {
// this may mean the tag failed to verify- all previous plaintext
// returned must be considered faked and invalid
return nil, errors.New("decryption failed")
Expand Down
8 changes: 4 additions & 4 deletions ciphers_gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewGCMEncryptionCipherCtx(blocksize int, e *Engine, key, iv []byte) (
return nil, fmt.Errorf("could not set IV len to %d: %s",
len(iv), err)
}
if 1 != C.EVP_EncryptInit_ex(ctx.ctx, nil, nil, nil,
if 0 >= C.EVP_EncryptInit_ex(ctx.ctx, nil, nil, nil,
(*C.uchar)(&iv[0])) {
return nil, errors.New("failed to apply IV")
}
Expand All @@ -112,7 +112,7 @@ func NewGCMDecryptionCipherCtx(blocksize int, e *Engine, key, iv []byte) (
return nil, fmt.Errorf("could not set IV len to %d: %s",
len(iv), err)
}
if 1 != C.EVP_DecryptInit_ex(ctx.ctx, nil, nil, nil,
if 0 >= C.EVP_DecryptInit_ex(ctx.ctx, nil, nil, nil,
(*C.uchar)(&iv[0])) {
return nil, errors.New("failed to apply IV")
}
Expand All @@ -125,7 +125,7 @@ func (ctx *authEncryptionCipherCtx) ExtraData(aad []byte) error {
return nil
}
var outlen C.int
if 1 != C.EVP_EncryptUpdate(ctx.ctx, nil, &outlen, (*C.uchar)(&aad[0]),
if 0 >= C.EVP_EncryptUpdate(ctx.ctx, nil, &outlen, (*C.uchar)(&aad[0]),
C.int(len(aad))) {
return errors.New("failed to add additional authenticated data")
}
Expand All @@ -137,7 +137,7 @@ func (ctx *authDecryptionCipherCtx) ExtraData(aad []byte) error {
return nil
}
var outlen C.int
if 1 != C.EVP_DecryptUpdate(ctx.ctx, nil, &outlen, (*C.uchar)(&aad[0]),
if 0 >= C.EVP_DecryptUpdate(ctx.ctx, nil, &outlen, (*C.uchar)(&aad[0]),
C.int(len(aad))) {
return errors.New("failed to add additional authenticated data")
}
Expand Down
16 changes: 8 additions & 8 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (c *Ctx) SetEllipticCurve(curve EllipticCurve) error {
}
defer C.EC_KEY_free(k)

if int(C.SSL_CTX_set_tmp_ecdh_not_a_macro(c.ctx, k)) != 1 {
if int(C.SSL_CTX_set_tmp_ecdh_not_a_macro(c.ctx, k)) <= 0 {
return errorFromErrorQueue()
}

Expand All @@ -248,7 +248,7 @@ func (c *Ctx) UseCertificate(cert *Certificate) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
c.cert = cert
if int(C.SSL_CTX_use_certificate(c.ctx, cert.x)) != 1 {
if int(C.SSL_CTX_use_certificate(c.ctx, cert.x)) <= 0 {
return errorFromErrorQueue()
}
return nil
Expand All @@ -260,7 +260,7 @@ func (c *Ctx) AddChainCertificate(cert *Certificate) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
c.chain = append(c.chain, cert)
if int(C.SSL_CTX_add_extra_chain_cert_not_a_macro(c.ctx, cert.x)) != 1 {
if int(C.SSL_CTX_add_extra_chain_cert_not_a_macro(c.ctx, cert.x)) <= 0 {
return errorFromErrorQueue()
}
return nil
Expand All @@ -272,7 +272,7 @@ func (c *Ctx) UsePrivateKey(key PrivateKey) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
c.key = key
if int(C.SSL_CTX_use_PrivateKey(c.ctx, key.evpPKey())) != 1 {
if int(C.SSL_CTX_use_PrivateKey(c.ctx, key.evpPKey())) <= 0 {
return errorFromErrorQueue()
}
return nil
Expand Down Expand Up @@ -301,7 +301,7 @@ func (s *CertificateStore) AddCertificate(cert *Certificate) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
s.certs = append(s.certs, cert)
if int(C.X509_STORE_add_cert(s.store, cert.x)) != 1 {
if int(C.X509_STORE_add_cert(s.store, cert.x)) <= 0 {
return errorFromErrorQueue()
}
return nil
Expand Down Expand Up @@ -359,7 +359,7 @@ func (c *Ctx) LoadVerifyLocations(ca_file string, ca_path string) error {
c_ca_path = C.CString(ca_path)
defer C.free(unsafe.Pointer(c_ca_path))
}
if C.SSL_CTX_load_verify_locations(c.ctx, c_ca_file, c_ca_path) != 1 {
if C.SSL_CTX_load_verify_locations(c.ctx, c_ca_file, c_ca_path) <= 0 {
return errorFromErrorQueue()
}
return nil
Expand Down Expand Up @@ -479,7 +479,7 @@ func (c *Ctx) SetSessionId(session_id []byte) error {
ptr = (*C.uchar)(unsafe.Pointer(&session_id[0]))
}
if int(C.SSL_CTX_set_session_id_context(c.ctx, ptr,
C.uint(len(session_id)))) == 0 {
C.uint(len(session_id)))) <= 0 {
return errorFromErrorQueue()
}
return nil
Expand All @@ -493,7 +493,7 @@ func (c *Ctx) SetCipherList(list string) error {
defer runtime.UnlockOSThread()
clist := C.CString(list)
defer C.free(unsafe.Pointer(clist))
if int(C.SSL_CTX_set_cipher_list(c.ctx, clist)) == 0 {
if int(C.SSL_CTX_set_cipher_list(c.ctx, clist)) <= 0 {
return errorFromErrorQueue()
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func EngineById(name string) (*Engine, error) {
if e.e == nil {
return nil, fmt.Errorf("engine %s missing", name)
}
if C.ENGINE_init(e.e) == 0 {
if C.ENGINE_init(e.e) <= 0 {
C.ENGINE_free(e.e)
return nil, fmt.Errorf("engine %s not initialized", name)
}
Expand Down
6 changes: 3 additions & 3 deletions hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Certificate) CheckHost(host string, flags CheckFlags) error {
if rv > 0 {
return nil
}
if rv == 0 {
if rv <= 0 {
return ValidationError
}
return errors.New("hostname validation had an internal failure")
Expand All @@ -84,7 +84,7 @@ func (c *Certificate) CheckEmail(email string, flags CheckFlags) error {
if rv > 0 {
return nil
}
if rv == 0 {
if rv <= 0 {
return ValidationError
}
return errors.New("email validation had an internal failure")
Expand All @@ -102,7 +102,7 @@ func (c *Certificate) CheckIP(ip net.IP, flags CheckFlags) error {
if rv > 0 {
return nil
}
if rv == 0 {
if rv <= 0 {
return ValidationError
}
return errors.New("ip validation had an internal failure")
Expand Down
Loading

0 comments on commit 3207418

Please sign in to comment.