Skip to content

opendkim.sig_getcanonlen()

Christopher Mooney edited this page Feb 12, 2018 · 3 revisions

DESCRIPTION

This call retrieves the number of bytes of the canonicalized message and the number of bytes that were included in the signed message. This can be called after opendkim.eoh(). This function uses the signature handle found by opendkim.get_signature() to pull the selector from. Internally, this function calls opendkim.get_signature(), so there is no need to explicitly call it yourself.

ARGUMENTS

Type: undefined

RETURN VALUES

  • throws an error if
    • The function was called before opendkim.eoh() and thus opendkim.verify()
  • returns the canonicalized message length in the following format:
{
  "msglen": 512, // Number of bytes in the canonicalized form of the message. Note: for simple this should be about the size of the message and for relaxed this could be significantly less.
  "canonlen": 256, // Number of bytes in the canonicalized form of the message that were included in the signed messaged. 
  "signlen": -1 // The signature length limit for the signature. If none was provided then -1 is returned.
}

NOTES

  • throws an error if there is simply no signature on the message.

EXAMPLE (async/await)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
async function verify(message) {                                                                         
  var opendkim = new OpenDKIM();                                                                         
                                                                                                         
  try {                                                                                                  
    await opendkim.verify({id: undefined});                                                              
    await opendkim.chunk({                                                                               
      message: message,                                                                                  
      length: message.length                                                                             
    });                                                                                                  
    await opendkim.chunk_end();                                                                          
    var canonlen = opendkim.sig_getcanonlen();                                                           
    console.log('message length: ' + canonlen.msglen);                                                   
    console.log('canonicalized length: ' + canonlen.canonlen);                                           
    console.log('signature length limit: ' + canonlen.signlen);                                          
  } catch (err) {                                                                                        
    console.log(opendkim.sig_geterrorstr(opendkim.sig_geterror()));                                     
    console.log(err);                                                                                    
  }                                                                                                      
}                                                                                                        

EXAMPLE (sync)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
function verify_sync(message) {                                                                          
  var opendkim = new OpenDKIM();                                                                         
                                                                                                         
  try {                                                                                                  
    opendkim.verify_sync({id: undefined});                                                               
    opendkim.chunk_sync({                                                                                
      message: message,                                                                                  
      length: message.length                                                                             
    });                                                                                                  
    opendkim.chunk_end_sync();                                                                           
    var canonlen = opendkim.sig_getcanonlen();                                                           
    console.log('message length: ' + canonlen.msglen);                                                   
    console.log('canonicalized length: ' + canonlen.canonlen);                                           
    console.log('signature length limit: ' + canonlen.signlen);                                          
  } catch (err) {                                                                                        
    console.log(opendkim.sig_geterrorstr(opendkim.sig_geterror()));                                     
    console.log(err);                                                                                    
  }                                                                                                      
}                                                                                                        

EXAMPLE (errback)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
function verify(opendkim, message, callback) {                                                                                                      
  opendkim.verify({id: undefined}, function (err, result) {                                              
    if (err) {                                                                                           
      return callback(err, result);                                                                      
    }                                                                                                    
                                                                                                         
    var options = {                                                                                      
      message: message,                                                                                  
      length: message.length                                                                             
    };                                                                                                   
                                                                                                         
    opendkim.chunk(options, function (err, result) {                                                     
      if (err) {                                                                                         
        return callback(err, result);                                                                    
      }                                                                                                  
                                                                                                         
      opendkim.chunk_end(function (err, result) {                                                        
        if (err) {                                                                                       
          return callback(err, result);                                                                  
        }                                                                                                
                                                                                                         
        var canonlen = opendkim.sig_getcanonlen();                                                       
                                                                                                         
        return callback(err, canonlen);                                                                  
      });                                                                                                
    });                                                                                                  
  });                                                                                                    
}
                                                                                                        
var opendkim = new OpenDKIM();

verify(opendkim, message, function (err, canonlen) {                                                               
  if (err) {                                                                                             
    return console.log(opendkim.sig_geterrorstr(opendkim.sig_geterror()));                                                                                              
  }                                                                                                      
                                                                                                         
  // success                                                                                             
  console.log('message length: ' + canonlen.msglen);                                                     
  console.log('canonicalized length: ' + canonlen.canonlen);                                             
  console.log('signature length limit: ' + canonlen.signlen);                                            
});