1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include <plarena.h>
6 #include <prerror.h>
7 #include <prio.h>
8 #include <prprf.h>
9 #include <seccomon.h>
10 #include <secmod.h>
11 #include <jar.h>
12 #include <secutil.h>
13 
14 /* These are installation functions that make calls to the security library.
15  * We don't want to include security include files in the C++ code too much.
16  */
17 
18 static char *PR_fgets(char *buf, int size, PRFileDesc *file);
19 
20 /***************************************************************************
21  *
22  * P k 1 1 I n s t a l l _ A d d N e w M o d u l e
23  */
24 int
Pk11Install_AddNewModule(char * moduleName,char * dllPath,unsigned long defaultMechanismFlags,unsigned long cipherEnableFlags)25 Pk11Install_AddNewModule(char *moduleName, char *dllPath,
26                          unsigned long defaultMechanismFlags,
27                          unsigned long cipherEnableFlags)
28 {
29     return (SECMOD_AddNewModule(moduleName, dllPath,
30                                 SECMOD_PubMechFlagstoInternal(defaultMechanismFlags),
31                                 SECMOD_PubCipherFlagstoInternal(cipherEnableFlags)) == SECSuccess)
32                ? 0
33                : -1;
34 }
35 
36 /*************************************************************************
37  *
38  * P k 1 1 I n s t a l l _ U s e r V e r i f y J a r
39  *
40  * Gives the user feedback on the signatures of a JAR files, asks them
41  * whether they actually want to continue.
42  * Assumes the jar structure has already been created and is valid.
43  * Returns 0 if the user wants to continue the installation, nonzero
44  * if the user wishes to abort.
45  */
46 short
Pk11Install_UserVerifyJar(JAR * jar,PRFileDesc * out,PRBool query)47 Pk11Install_UserVerifyJar(JAR *jar, PRFileDesc *out, PRBool query)
48 {
49     JAR_Context *ctx;
50     JAR_Cert *fing;
51     JAR_Item *item;
52     char stdinbuf[80];
53     int count = 0;
54 
55     CERTCertificate *cert, *prev = NULL;
56 
57     PR_fprintf(out, "\nThis installation JAR file was signed by:\n");
58 
59     ctx = JAR_find(jar, NULL, jarTypeSign);
60 
61     while (JAR_find_next(ctx, &item) >= 0) {
62         fing = (JAR_Cert *)item->data;
63         cert = fing->cert;
64         if (cert == prev) {
65             continue;
66         }
67 
68         count++;
69         PR_fprintf(out, "----------------------------------------------\n");
70         if (cert) {
71             if (cert->nickname) {
72                 PR_fprintf(out, "**NICKNAME**\n%s\n", cert->nickname);
73             }
74             if (cert->subjectName) {
75                 PR_fprintf(out, "**SUBJECT NAME**\n%s\n", cert->subjectName);
76             }
77             if (cert->issuerName) {
78                 PR_fprintf(out, "**ISSUER NAME**\n%s\n", cert->issuerName);
79             }
80         } else {
81             PR_fprintf(out, "No matching certificate could be found.\n");
82         }
83         PR_fprintf(out, "----------------------------------------------\n\n");
84 
85         prev = cert;
86     }
87 
88     JAR_find_end(ctx);
89 
90     if (count == 0) {
91         PR_fprintf(out, "No signatures found: JAR FILE IS UNSIGNED.\n");
92     }
93 
94     if (query) {
95         PR_fprintf(out,
96                    "Do you wish to continue this installation? (y/n) ");
97 
98         if (PR_fgets(stdinbuf, 80, PR_STDIN) != NULL) {
99             char *response;
100 
101             if ((response = strtok(stdinbuf, " \t\n\r"))) {
102                 if (!PL_strcasecmp(response, "y") ||
103                     !PL_strcasecmp(response, "yes")) {
104                     return 0;
105                 }
106             }
107         }
108     }
109 
110     return 1;
111 }
112 
113 /**************************************************************************
114  *
115  * P R _ f g e t s
116  *
117  * fgets implemented with NSPR.
118  */
119 static char *
PR_fgets(char * buf,int size,PRFileDesc * file)120 PR_fgets(char *buf, int size, PRFileDesc *file)
121 {
122     int i;
123     int status;
124     char c;
125 
126     i = 0;
127     while (i < size - 1) {
128         status = PR_Read(file, (void *)&c, 1);
129         if (status == -1) {
130             return NULL;
131         } else if (status == 0) {
132             break;
133         }
134         buf[i++] = c;
135         if (c == '\n') {
136             break;
137         }
138     }
139     buf[i] = '\0';
140 
141     return buf;
142 }
143 
144 /**************************************************************************
145  *
146  * m y S E C U _ E r r o r S t r i n g
147  *
148  */
149 const char *
mySECU_ErrorString(PRErrorCode errnum)150 mySECU_ErrorString(PRErrorCode errnum)
151 {
152     return SECU_Strerror(errnum);
153 }
154