Debian Codegraph @

src/rsawrapr.c

raw
1 /*
2 * PKCS#1 encoding and decoding functions.
3 * This file is believed to contain no code licensed from other parties.
4 *
5 * ***** BEGIN LICENSE BLOCK *****
6 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 *
8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
12 *
13 * Software distributed under the License is distributed on an "AS IS" basis,
14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15 * for the specific language governing rights and limitations under the
16 * License.
17 *
18 * The Original Code is the Netscape security libraries.
19 *
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1994-2000
23 * the Initial Developer. All Rights Reserved.
24 *
25 * Contributor(s):
26 * Rob Crittenden ([email protected])
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
39 *
40 * ***** END LICENSE BLOCK ***** */
41 /* $Id: $ */
42
43 #include "ckpem.h"
44
45 #include <blapi.h>
46 #include <nssbase.h>
47 #include <secerr.h>
48 #include <sechash.h>
49
50 /* XXX Doesn't set error code */
51 SECStatus
52 pem_RSA_Sign(NSSLOWKEYPrivateKey * key,
53 unsigned char *output,
54 unsigned int *output_len,
55 unsigned int maxOutputLen,
56 unsigned char *input,
57 unsigned int input_len)
58 {
59 if (maxOutputLen < pem_PrivateModulusLen(key))
60 return SECFailure;
61
62 PORT_Assert(key->keyType == NSSLOWKEYRSAKey);
63 if (key->keyType != NSSLOWKEYRSAKey)
64 return SECFailure;
65
66 return RSA_Sign(&key->u.rsa, output, output_len, maxOutputLen,
67 input, input_len);
68 }
69
70 /* XXX Doesn't set error code */
71 SECStatus
72 pem_RSA_DecryptBlock(NSSLOWKEYPrivateKey * key,
73 unsigned char *output,
74 unsigned int *output_len,
75 unsigned int max_output_len,
76 unsigned char *input, unsigned int input_len)
77 {
78 SECStatus rv;
79 unsigned int modulus_len = pem_PrivateModulusLen(key);
80 unsigned int i;
81 unsigned char *buffer;
82
83 PORT_Assert(key->keyType == NSSLOWKEYRSAKey);
84 if (key->keyType != NSSLOWKEYRSAKey)
85 goto failure;
86 if (input_len != modulus_len)
87 goto failure;
88
89 buffer = (unsigned char *) NSS_ZAlloc(NULL, modulus_len + 1);
90 if (!buffer)
91 goto failure;
92
93 rv = RSA_PrivateKeyOp(&key->u.rsa, buffer, input);
94 if (rv != SECSuccess) {
95 goto loser;
96 }
97
98 if (buffer[0] != 0 || buffer[1] != 2)
99 goto loser;
100 *output_len = 0;
101 for (i = 2; i < modulus_len; i++) {
102 if (buffer[i] == 0) {
103 *output_len = modulus_len - i - 1;
104 break;
105 }
106 }
107 if (*output_len == 0)
108 goto loser;
109 if (*output_len > max_output_len)
110 goto loser;
111
112 memcpy(output, buffer + modulus_len - *output_len, *output_len);
113
114 NSS_ZFreeIf(buffer);
115 return SECSuccess;
116
117 loser:
118 NSS_ZFreeIf(buffer);
119 failure:
120 return SECFailure;
121 }