LibreOffice Module onlineupdate (master) 1
Functions
mar_sign.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <onlineupdate/mar_private.h>
#include <onlineupdate/mar_cmdline.h>
#include <onlineupdate/mar.h>
#include "cryptox.h"
#include <unistd.h>
#include "nss_secutil.h"
#include "base64.h"
Include dependency graph for mar_sign.c:

Go to the source code of this file.

Functions

int NSSInitCryptoContext (const char *NSSConfigDir)
 Initializes the NSS context. More...
 
int NSSSignBegin (const char *certName, SGNContext **ctx, SECKEYPrivateKey **privKey, CERTCertificate **cert, uint32_t *signatureLength)
 Obtains a signing context. More...
 
int WriteAndUpdateSignatures (FILE *fpDest, void *buffer, uint32_t size, SGNContext **ctxs, uint32_t ctxCount, const char *err)
 Writes the passed buffer to the file fp and updates the signature contexts. More...
 
void AdjustIndexContentOffsets (char *indexBuf, uint32_t indexLength, uint32_t offsetAmount)
 Adjusts each entry's content offset in the passed in index by the specified amount. More...
 
int ReadWriteAndUpdateSignatures (FILE *fpSrc, FILE *fpDest, void *buffer, uint32_t size, SGNContext **ctxs, uint32_t ctxCount, const char *err)
 Reads from fpSrc, writes it to fpDest, and updates the signature contexts. More...
 
int ReadAndWrite (FILE *fpSrc, FILE *fpDest, void *buffer, uint32_t size, const char *err)
 Reads from fpSrc, writes it to fpDest. More...
 
int strip_signature_block (const char *src, const char *dest)
 Writes out a copy of the MAR at src but with the signature block stripped. More...
 
int extract_signature (const char *src, uint32_t sigIndex, const char *dest)
 Extracts a signature from a MAR file, base64 encodes it, and writes it out. More...
 
int import_signature (const char *src, uint32_t sigIndex, const char *base64SigFile, const char *dest)
 Imports a base64 encoded signature into a MAR file. More...
 
int mar_repackage_and_sign (const char *NSSConfigDir, const char *const *certNames, uint32_t certCount, const char *src, const char *dest)
 Writes out a copy of the MAR at src but with embedded signatures. More...
 

Function Documentation

◆ AdjustIndexContentOffsets()

void AdjustIndexContentOffsets ( char *  indexBuf,
uint32_t  indexLength,
uint32_t  offsetAmount 
)

Adjusts each entry's content offset in the passed in index by the specified amount.

Parameters
indexBufA buffer containing the MAR index
indexLengthThe length of the MAR index
offsetAmountThe amount to adjust each index entry by

Definition at line 160 of file mar_sign.c.

References htonl(), and ntohl().

Referenced by mar_repackage_and_sign(), and strip_signature_block().

◆ extract_signature()

int extract_signature ( const char *  src,
uint32_t  sigIndex,
const char *  dest 
)

Extracts a signature from a MAR file, base64 encodes it, and writes it out.

Parameters
srcThe path of the source MAR file
sigIndexThe index of the signature to extract
destThe path of file to write the signature to
Returns
0 on success -1 on error

Definition at line 489 of file mar_sign.c.

References FILE, i, ntohl(), NULL, remove(), and SIGNATURE_BLOCK_OFFSET.

Referenced by main().

◆ import_signature()

int import_signature ( const char *  src,
uint32_t  sigIndex,
const char *  base64SigFile,
const char *  dest 
)

Imports a base64 encoded signature into a MAR file.

Parameters
srcThe path of the source MAR file
sigIndexThe index of the signature to import
base64SigFileA file which contains the signature to import
destThe path of the destination MAR file with replaced signature
Returns
0 on success -1 on error

Definition at line 605 of file mar_sign.c.

References BLOCKSIZE, FILE, i, MAX_SIGNATURES, ntohl(), NULL, ReadAndWrite(), remove(), and SIGNATURE_BLOCK_OFFSET.

Referenced by main().

◆ mar_repackage_and_sign()

int mar_repackage_and_sign ( const char *  NSSConfigDir,
const char *const *  certNames,
uint32_t  certCount,
const char *  src,
const char *  dest 
)

Writes out a copy of the MAR at src but with embedded signatures.

The passed in MAR file must not already be signed or an error will be returned.

Parameters
NSSConfigDirThe NSS directory containing the private key for signing
certNamesThe nicknames of the certificate to use for signing
certCountThe number of certificate names contained in certNames. One signature will be produced for each certificate.
srcThe path of the source MAR file to sign
destThe path of the MAR file to write out that is signed
Returns
0 on success -1 on error

Definition at line 818 of file mar_sign.c.

References AdjustIndexContentOffsets(), BLOCKSIZE, FILE, get_mar_file_info(), HOST_TO_NETWORK64, htonl(), i, MAR_ID_SIZE, MAX_SIGNATURES, MAX_SIZE_OF_MAR_FILE, NETWORK_TO_HOST64, NSSInitCryptoContext(), NSSSignBegin(), ntohl(), NULL, ReadWriteAndUpdateSignatures(), remove(), SECU_GetModulePassword(), and WriteAndUpdateSignatures().

Referenced by main().

◆ NSSInitCryptoContext()

int NSSInitCryptoContext ( const char *  NSSConfigDir)

Initializes the NSS context.

Parameters
NSSConfigDirThe config dir containing the private key to use
Returns
0 on success -1 on error

Definition at line 35 of file mar_sign.c.

Referenced by main(), and mar_repackage_and_sign().

◆ NSSSignBegin()

int NSSSignBegin ( const char *  certName,
SGNContext **  ctx,
SECKEYPrivateKey **  privKey,
CERTCertificate **  cert,
uint32_t *  signatureLength 
)

Obtains a signing context.

Parameters
ctxA pointer to the signing context to fill
Returns
0 on success -1 on error

Definition at line 55 of file mar_sign.c.

References BLOCKSIZE, ctx, and XP_MIN_SIGNATURE_LEN_IN_BYTES.

Referenced by mar_repackage_and_sign().

◆ ReadAndWrite()

int ReadAndWrite ( FILE *  fpSrc,
FILE *  fpDest,
void *  buffer,
uint32_t  size,
const char *  err 
)

Reads from fpSrc, writes it to fpDest.

Parameters
fpSrcThe file pointer to read from.
fpDestThe file pointer to write to.
bufferThe buffer to write.
sizeThe size of the buffer to write.
errThe name of what is being written to in case of error.
Returns
0 on success -1 on read error -2 on write error

Definition at line 225 of file mar_sign.c.

References err, and size.

Referenced by import_signature(), and strip_signature_block().

◆ ReadWriteAndUpdateSignatures()

int ReadWriteAndUpdateSignatures ( FILE *  fpSrc,
FILE *  fpDest,
void *  buffer,
uint32_t  size,
SGNContext **  ctxs,
uint32_t  ctxCount,
const char *  err 
)

Reads from fpSrc, writes it to fpDest, and updates the signature contexts.

Parameters
fpSrcThe file pointer to read from.
fpDestThe file pointer to write to.
bufferThe buffer to write.
sizeThe size of the buffer to write.
ctxsPointer to the first element in an array of signature contexts to update.
ctxCountThe number of signature contexts pointed to by ctxs
errThe name of what is being written to in case of error.
Returns
0 on success -1 on read error -2 on write error -3 on signature update error

Definition at line 194 of file mar_sign.c.

References err, size, and WriteAndUpdateSignatures().

Referenced by mar_repackage_and_sign().

◆ strip_signature_block()

int strip_signature_block ( const char *  src,
const char *  dest 
)

Writes out a copy of the MAR at src but with the signature block stripped.

Parameters
srcThe path of the source MAR file
destThe path of the MAR file to write out that has no signature block
Returns
0 on success -1 on error

Definition at line 255 of file mar_sign.c.

References AdjustIndexContentOffsets(), BLOCKSIZE, CryptoX_Error, FILE, get_mar_file_info(), HOST_TO_NETWORK64, htonl(), i, MAR_ID_SIZE, NETWORK_TO_HOST64, ntohl(), NULL, ReadAndWrite(), and remove().

Referenced by main().

◆ WriteAndUpdateSignatures()

int WriteAndUpdateSignatures ( FILE *  fpDest,
void *  buffer,
uint32_t  size,
SGNContext **  ctxs,
uint32_t  ctxCount,
const char *  err 
)

Writes the passed buffer to the file fp and updates the signature contexts.

Parameters
fpDestThe file pointer to write to.
bufferThe buffer to write.
sizeThe size of the buffer to write.
ctxsPointer to the first element in an array of signature contexts to update.
ctxCountThe number of signature contexts pointed to by ctxs
errThe name of what is being written to in case of error.
Returns
0 on success -2 on write error -3 on signature update error

Definition at line 127 of file mar_sign.c.

References err, and size.

Referenced by mar_repackage_and_sign(), and ReadWriteAndUpdateSignatures().