public
Description: command-line tool to sign update manifests of Mozilla extensions (update.rdf)
Homepage: http://hyperstruct.net/projects/spock
Clone URL: git://github.com/bard/spock.git
bard (author)
Sat Aug 16 15:18:38 -0700 2008
spock / nss_sign_data.c
100644 173 lines (142 sloc) 4.626 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (C) 2007 by Massimiliano Mirra
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Massimiliano Mirra, <bard [at] hyperstruct [dot] net>
*/
 
 
#include "pk11pub.h"
#include "nspr.h"
#include "nss.h"
#include "keyhi.h"
#include "plbase64.h"
 
#define MAXSIZE 32768
 
 
DERTemplate SECAlgorithmIDTemplate[] = {
    { DER_SEQUENCE,
      0, NULL, sizeof(SECAlgorithmID) },
    { DER_OBJECT_ID,
      offsetof(SECAlgorithmID,algorithm), },
    { DER_OPTIONAL | DER_ANY,
      offsetof(SECAlgorithmID,parameters), },
    { 0, }
};
 
DERTemplate CERTSignatureDataTemplate[] =
{
    { DER_SEQUENCE,
          0, NULL, sizeof(CERTSignedData) },
    { DER_INLINE,
          offsetof(CERTSignedData,signatureAlgorithm),
          SECAlgorithmIDTemplate, },
    { DER_BIT_STRING,
          offsetof(CERTSignedData,signature), },
    { 0, }
};
 
int main(int argc, char *argv[])
{
    PRBool readOnly = PR_FALSE;
    PK11SlotInfo *slot = NULL;
    SECOidTag alg = SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION;
    SECStatus rv;
    char *progName;
    SECKEYPrivateKey *key;
    char data[MAXSIZE];
    int dataLen;
 
    progName = argv[0];
    if(argc != 2) {
        fprintf(stderr, "Usage: %s <mccoy profile dir>\n", progName);
        goto shutdown;
    }
 
    char *configDirectory = argv[1];
 
    // Initialize NSS
 
    rv = NSS_Initialize(configDirectory, "", "", "secmod.db", 0);
    if(rv != SECSuccess) {
SECU_PrintPRandOSError(progName);
rv = SECFailure;
goto shutdown;
    }
    SECU_RegisterDynamicOids();
 
    // Exit if keys are protected.
    
    slot = PK11_GetInternalKeySlot();
    if(PK11_NeedUserInit(slot)) {
        fprintf(stderr, "Error: needs user init.\n");
        rv = SECFailure;
        goto shutdown;
    }
 
    if(PK11_NeedLogin(slot)) {
        fprintf(stderr, "Error: password-protected key databases not supported.\n");
        rv = SECFailure;
        goto shutdown;
    }
    
    // Retrieve first key in database
 
    SECKEYPrivateKeyList *list;
    SECKEYPrivateKeyListNode *node;
    list = PK11_ListPrivKeysInSlot(slot, NULL, NULL);
    if(!list) {
        fprintf(stderr, "Error: no keys found.\n");
        rv = SECFailure;
        goto shutdown;
    }
    node = PRIVKEY_LIST_HEAD(list);
    key = SECKEY_CopyPrivateKey(node->key);
    SECKEY_DestroyPrivateKeyList(list);
 
    // Read data
 
    dataLen = fread(data, 1, MAXSIZE, stdin);
 
    // Create signature
 
    SECItem signature;
    PORT_Memset(&signature, 0, sizeof(SECItem));
    
    CERTSignedData sd;
    PORT_Memset(&sd, 0, sizeof(CERTSignedData));
    
    rv = SEC_SignData(&(sd.signature), data, dataLen, key, alg);
    if(rv != SECSuccess) {
        fprintf(stderr, "Error: could not sign data.\n");
        goto shutdown;
    }
    sd.signature.len = sd.signature.len << 3;
 
    PRArenaPool *arena;
    arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
    if(!arena) {
        fprintf(stderr, "Error: couldn't get an arena (whatever that is).\n");
        rv = SECFailure;
        goto shutdown;
    }
 
    rv = SECOID_SetAlgorithmID(arena, &sd.signatureAlgorithm, alg, 0);
    if(rv != SECSuccess) {
        fprintf(stderr, "Error: couldn't set algorithm id.\n");
        SECITEM_FreeItem(&(sd.signature), PR_FALSE);
        PORT_FreeArena(arena, PR_FALSE);
        goto shutdown;
    }
 
    // Encode results
 
    SECItem result;
    rv = DER_Encode(arena, &result, CERTSignatureDataTemplate, &sd);
    SECITEM_FreeItem(&(sd.signature), PR_FALSE);
    if(rv != SECSuccess) {
        fprintf(stderr, "Error: couldn't encode result.\n");
        PORT_FreeArena(arena, PR_FALSE);
        goto shutdown;
    }
 
    char *sign = PL_Base64Encode((const char*)result.data, result.len, NULL);
    PORT_FreeArena(arena, PR_FALSE);
    
    printf("%s\n", sign);
 
 
  shutdown:
    if(slot) {
        PK11_FreeSlot(slot);
    }
    if(rv == SECSuccess) {
        return 0;
    } else {
        return 255;
    }
}