2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
22 Contains: BER routines
24 Written by: Doug Mitchell
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
31 #include "sslMemory.h"
34 #include "appleCdsa.h"
37 #include <Security/cssmdata.h>
38 #include <SecurityNssAsn1/SecNssCoder.h>
39 #include <SecurityNssAsn1/keyTemplates.h>
42 * Given a PKCS-1 encoded RSA public key, extract the
43 * modulus and public exponent.
45 * RSAPublicKey ::= SEQUENCE {
46 * modulus INTEGER, -- n
47 * publicExponent INTEGER -- e }
50 OSStatus
sslDecodeRsaBlob(
51 const SSLBuffer
*blob
, /* PKCS-1 encoded */
52 SSLBuffer
*modulus
, /* data mallocd and RETURNED */
53 SSLBuffer
*exponent
) /* data mallocd and RETURNED */
58 assert(modulus
!= NULL
);
59 assert(exponent
!= NULL
);
61 /* DER-decode the blob */
62 NSS_RSAPublicKeyPKCS1 nssPubKey
;
65 memset(&nssPubKey
, 0, sizeof(nssPubKey
));
66 PRErrorCode perr
= coder
.decode(blob
->data
, blob
->length
,
67 NSS_RSAPublicKeyPKCS1Template
, &nssPubKey
);
72 /* malloc & copy components */
73 srtn
= SSLCopyBufferFromData(nssPubKey
.modulus
.Data
,
74 nssPubKey
.modulus
.Length
, *modulus
);
78 return SSLCopyBufferFromData(nssPubKey
.publicExponent
.Data
,
79 nssPubKey
.publicExponent
.Length
, *exponent
);
83 * Given a raw modulus and exponent, cook up a
84 * BER-encoded RSA public key blob.
86 OSStatus
sslEncodeRsaBlob(
87 const SSLBuffer
*modulus
,
88 const SSLBuffer
*exponent
,
89 SSLBuffer
*blob
) /* data mallocd and RETURNED */
91 assert((modulus
!= NULL
) && (exponent
!= NULL
));
95 /* convert to NSS_RSAPublicKeyPKCS1 */
96 NSS_RSAPublicKeyPKCS1 nssPubKey
;
97 SSLBUF_TO_CSSM(modulus
, &nssPubKey
.modulus
);
98 SSLBUF_TO_CSSM(exponent
, &nssPubKey
.publicExponent
);
104 perr
= coder
.encodeItem(&nssPubKey
, NSS_RSAPublicKeyPKCS1Template
, encBlob
);
109 /* copy out to caller */
110 return SSLCopyBufferFromData(encBlob
.Data
, encBlob
.Length
, *blob
);
114 * Given a DER encoded DHParameterBlock, extract the prime and generator.
115 * modulus and public exponent.
116 * This will work with either PKCS-1 encoded DHParameterBlock or
117 * openssl-style DHParameter.
119 OSStatus
sslDecodeDhParams(
120 const SSLBuffer
*blob
, /* PKCS-1 encoded */
121 SSLBuffer
*prime
, /* data mallocd and RETURNED */
122 SSLBuffer
*generator
) /* data mallocd and RETURNED */
124 assert(blob
!= NULL
);
125 assert(prime
!= NULL
);
126 assert(generator
!= NULL
);
129 NSS_DHParameterBlock paramBlock
;
133 memset(¶mBlock
, 0, sizeof(paramBlock
));
134 SSLBUF_TO_CSSM(blob
, &cblob
);
137 * Since the common case here is to decode a parameter block coming
138 * over the wire, which is in openssl format, let's try that format first.
140 perr
= coder
.decodeItem(cblob
, NSS_DHParameterTemplate
,
144 * OK, that failed when trying as a CDSA_formatted parameter
145 * block DHParameterBlock). Openssl uses a subset of that,
146 * a DHParameter. Try that instead.
148 memset(¶mBlock
, 0, sizeof(paramBlock
));
149 perr
= coder
.decodeItem(cblob
, NSS_DHParameterBlockTemplate
,
152 /* Ah well, we tried. */
153 sslErrorLog("sslDecodeDhParams: both CDSA and openssl format"
159 /* copy out components */
160 NSS_DHParameter
¶m
= paramBlock
.params
;
161 OSStatus ortn
= SSLCopyBufferFromData(param
.prime
.Data
,
162 param
.prime
.Length
, *prime
);
166 return SSLCopyBufferFromData(param
.base
.Data
,
167 param
.base
.Length
, *generator
);
171 * Given a prime and generator, cook up a BER-encoded DHParameter blob.
173 OSStatus
sslEncodeDhParams(
174 const SSLBuffer
*prime
,
175 const SSLBuffer
*generator
,
176 SSLBuffer
*blob
) /* data mallocd and RETURNED */
178 assert((prime
!= NULL
) && (generator
!= NULL
));
182 /* convert to NSS_DHParameter */
183 NSS_DHParameter dhParams
;
184 SSLBUF_TO_CSSM(prime
, &dhParams
.prime
);
185 SSLBUF_TO_CSSM(generator
, &dhParams
.base
);
186 dhParams
.privateValueLength
.Data
= NULL
;
187 dhParams
.privateValueLength
.Length
= 0;
193 perr
= coder
.encodeItem(&dhParams
, NSS_DHParameterTemplate
, encBlob
);
198 /* copy out to caller */
199 return SSLCopyBufferFromData(encBlob
.Data
, encBlob
.Length
, *blob
);